Register | Login | |||||
Main
| Memberlist
| Active users
| ACS
| Commons
| Calendar
| Online users Ranks | FAQ | Color Chart | Photo album | IRC Chat |
| |
0 user currently in Programming. | 3 guests |
Acmlm's Board - I2 Archive - Programming - C++ question | | | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread |
User | Post | ||
Weasel Missionary in Peru Level: 34 Posts: 248/454 EXP: 236444 For next: 17207 Since: 03-15-04 From: Washington Since last post: 467 days Last activity: 339 days |
| ||
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? (edited by Weasel on 04-28-04 10:31 AM) |
|||
Euclid Cheep-cheep Level: 23 Posts: 50/193 EXP: 65528 For next: 2195 Since: 03-15-04 From: Australia Since last post: 24 days Last activity: 7 days |
| ||
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. |
|||
MathOnNapkins Math n' Hacks Level: 67 Posts: 138/2189 EXP: 2495887 For next: 96985 Since: 03-18-04 From: Base Tourian Since last post: 1 hour Last activity: 32 min. |
| ||
^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. | |||
Weasel Missionary in Peru Level: 34 Posts: 249/454 EXP: 236444 For next: 17207 Since: 03-15-04 From: Washington Since last post: 467 days Last activity: 339 days |
| ||
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. |
|||
neotransotaku Baby Mario 戻れたら、 誰も気が付く Level: 87 Posts: 510/4016 EXP: 6220548 For next: 172226 Since: 03-15-04 From: Outside of Time/Space Since last post: 11 hours Last activity: 1 hour |
| ||
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. |
|||
Dish Spiny Level: 38 Posts: 51/596 EXP: 355646 For next: 14801 Since: 03-15-04 From: Disch Since last post: 18 days Last activity: 18 days |
| ||
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. |
|||
Weasel Missionary in Peru Level: 34 Posts: 250/454 EXP: 236444 For next: 17207 Since: 03-15-04 From: Washington Since last post: 467 days Last activity: 339 days |
| ||
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 Spiny Level: 38 Posts: 52/596 EXP: 355646 For next: 14801 Since: 03-15-04 From: Disch Since last post: 18 days Last activity: 18 days |
| ||
well then how the hell are you supposed to take user input with commas? | |||
neotransotaku Baby Mario 戻れたら、 誰も気が付く Level: 87 Posts: 511/4016 EXP: 6220548 For next: 172226 Since: 03-15-04 From: Outside of Time/Space Since last post: 11 hours Last activity: 1 hour |
| ||
Originally posted by Weasel 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 |
|||
Weasel Missionary in Peru Level: 34 Posts: 251/454 EXP: 236444 For next: 17207 Since: 03-15-04 From: Washington Since last post: 467 days Last activity: 339 days |
| ||
Originally posted by neotransotakuPlease (edited by Weasel on 04-28-04 09:42 PM) |
|||
Euclid Cheep-cheep Level: 23 Posts: 52/193 EXP: 65528 For next: 2195 Since: 03-15-04 From: Australia Since last post: 24 days Last activity: 7 days |
| ||
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) (edited by Euclid on 04-28-04 10:24 PM) (edited by Euclid on 04-28-04 10:27 PM) |
|||
neotransotaku Baby Mario 戻れたら、 誰も気が付く Level: 87 Posts: 513/4016 EXP: 6220548 For next: 172226 Since: 03-15-04 From: Outside of Time/Space Since last post: 11 hours Last activity: 1 hour |
| ||
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); |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread |
Acmlm's Board - I2 Archive - Programming - C++ question | | | |