Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - C++ question
  
User name:
Password:
Reply:
 

UserPost
neotransotaku
Posts: 513/4016


anyways...to make you do somework, this code won't compile...but it lays out the framework of what you must do

listnode* head = (listnode*)malloc(listnode);
listnode* tail = head;


sscanf("%d", &value);


for(int i = 10; i > 0; )
{
int place = pow(10, i);
head->character = int2char(value / place);
listnode* buffer = (listnode*)malloc(listnode);
head->tail = buffer;
tail = buffer;
value = value % place;
i--;
if(i % 3 == 0 && i != 0)
{
head->character = ',';
}
}

Euclid
Posts: 52/193
something like this Weasel:

this is in c, not strings involved, just characters.

#include (all those libraries)

typedef struct linkListNode{
char letter;
linkListNode* next;
}linkList;

int main()
{
char temp;
linkList* list = (linkList*) malloc(sizeof(linkList));
linkList* currentList = list;
//check if there's enough memory, do that yourself
while((temp = getchar()) != '\n') //put whatever end condition you need here
{
//check if it's valid character, do that yourself, just make sure when you see ',' in the right pos ignore the following saving code in link list.
currentList->next = (linkList*)malloc(sizeof(linkList));
//check if there's memory, again, you can do that
currentList->letter = temp;
//move to next pos for next char
currentList = currentList->next;
}
//list is now stored in the linkList
return 0;
}

EDITS: (damn why don't [code] work)
Weasel
Posts: 251/454
Originally posted by neotransotaku
if you still don't get it, i can write it all out for you
Please
neotransotaku
Posts: 511/4016
Originally posted by Weasel
I'm not allowed to use arrays (or strings) in this program. My teacher just informed me of this...


didn't you read my idea?

all you need is a counter (variable 1), the value read (variable 2), the constant 10, a function that will give you power to calculate exponentials, remainder (variable 3), a pointer to the beginning of the list (variable 4), a pointer to the end of the list (variable 5)

if you still don't get it, i can write it all out for you
Dish
Posts: 52/596
well then how the hell are you supposed to take user input with commas?
Weasel
Posts: 250/454
I'm not allowed to use arrays (or strings) in this program. My teacher just informed me of this...


Got any ideas that don't require arrays?
Dish
Posts: 51/596
since you need the commas... take the input from the user in the form of a string (char array). Then run through the *last* character in that array first, put that digit in a linked list... then do the next... then the next... then make sure there's a comma (every 3 places).. then do the next... etc.. etc.

converting a single digit number from char to int is a snap:

char ch = '5';
int val = ch - '0'; // val will now equal 5.
neotransotaku
Posts: 510/4016
use the change of base algorithm...in this case, your base remains the same. since you want to convert a decimal number into each of its decimal counter parts, you divde your number by each of the components.

for example:

1,000 / 1,000 = 1 r 0
0 / 100 = 0 r 0
0 / 10 = 0 r 0
0 / 1 = 0 r 0

34853 / 10,000 = 3 r 4853
4853 / 1,000 = 4 r 853
853 / 100 = 8 r 53
53 / 10 = 5 r 3
3 / 1 = 3 r 0

as you can see, this is the general base conversion algorithm. try it by converting decimal to hexadecimal or decimal to binary.
Weasel
Posts: 249/454
No no, neither of these answer my question XD

How do I put "1,000" into a linked list? Without using an array.

Euclid, you just told me how linked lists work. How do I get each digit into the list?

MON: i have no idea what you're even talking about.



Ok, i'll try to reexplain it.

User enters the number 1. That's easy to store into a linked list.

User enters 12. Somehow (this is the problem I need help with) I need to store that as 1 --> 2 . In the list. How do I break down a number and store each digit separately into the linked list?

Next, the user enters 1,234. This needs to be stored something like 1 --> 2 --> 3 --> 4. How do I break THAT down? I have to avoid using that comma too.
MathOnNapkins
Posts: 138/2189
^probably makes good sense because numbers are represented in positive integers 0-9. I assume that a negative sign will also be in a separate node.
Euclid
Posts: 50/193
if you don't mind c, then i can give it a shot (there's not much difference anyway)

first have your linklist struct contain a char (yes, do it in characters) and a pointer to the next list.

in that case you'll be storing '1' instead of 1, but you can always do some conversions to convert it backwards and forwards.

when you're storing it in chars, ',' shouldn't be a problem at all.
Weasel
Posts: 248/454
Ok, this is kinda hard to explain....


I have to write a program that gets an integer from the user and puts this integer into a linked list. But it's weird, because:

1) The integer has to have commas, if necessary. For example 1000 is invalid, but 1,000 is valid.

2) That 1,000 has to be stored in a linked list like this:
1 --> 0 --> 0 --> 0
Not 1000 in a single node.
Each digit has to be in a separate node.


I have no idea how to do this Any ideas?
Acmlm's Board - I2 Archive - Programming - C++ question


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.003 seconds.