Tuesday, July 14, 2009

How do i save big integer numbers in "C" Language?

How do i save big integer numbers in "C" Language?


hi all,


i want to save student ID "10 Digits each" on vars


but the problem if i use integer vars , its limited to 32768 and can't hold a number from 10 digits !!





what can i do ?


any help would be great.


thanks all at least for trying =)





all i want to do is to read an id from the user with scanf function and to put it in a var... thats all





* 5 hours ago


* - 3 days left to answer.





Additional Details





5 hours ago


thanks for the idea,,,


but can you please explain more about the LONG INT ?..





how exactly to use it,,,


whats the limits of long int...


and if u can type simple small program to read IDS from users and put them in long integers...


thanks again





5 hours ago


Using a numerical type will make it impossible to effectively hold student IDs that have leading 0s.





Yes thats right,, then how i can read 10 different ids and to sort them from small to big,,, or something like that,,





thats exactly wt makes me crazy

How do i save big integer numbers in "C" Language?
The previous recommendations to use a char array for your IDs, and sort the IDs after you've collected them, are good suggestions. I wrote the code below to illustrate a way to do this, with a few extra features:





IDs can be entered 1 or more per line, separated by commas, tabs, or spaces.


IDs shorter than 10 charcters will be padded with leading zeros.


IDs longer than 10 characters will be truncated.


Entered IDs are inserted into a linked list in alphabetical order.





A feature you could add: check for duplicate entries, reject them and display an error message.





Enjoy!





#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;stdlib.h%26gt;





#define MAX_LINE 80


#define ID_LEN 10


#define WS " ,\t"





struct IdListNode_t {


int len;


char *id;


struct IdListNode_t *next;


};





typedef enum { false = 0, true } Bool;





typedef struct IdListNode_t *IdList_t;


IdList_t newIdList();


void idListInsert(IdList_t, char *);


void printIdList(IdList_t);


void deleteIdList(IdList_t);





int main(int argc, char *argv[]) {


char line[MAX_LINE],*s;


char id[ID_LEN];


IdList_t idList = newIdList();


Bool done = false;





printf("\nEnter ids (one or more per line, empty line to quit):\n");


while (done == false) {


fgets(line,MAX_LINE,stdin);


*strchr(line,'\n') = '\0';


if ((done = (strlen(line) == 0)) == false) {


for (s = strtok(line,WS); s != NULL; s = strtok(NULL,WS)) {


if (strlen(s) %26lt;= ID_LEN) {


memset(id,'0',ID_LEN);


strcpy(id + ID_LEN - strlen(s),s);


} else {


strncpy(id,s,ID_LEN);


}


idListInsert(idList,id);


}


}


}





printIdList(idList);


deleteIdList(idList);





return 0;


}





IdList_t newIdList() {


return calloc(1,sizeof(struct IdListNode_t));


}





void idListInsert(IdList_t l, char *s) {


struct IdListNode_t *p = l;


struct IdListNode_t *n = calloc(1,sizeof(struct IdListNode_t));





n-%26gt;len = strlen(s);


n-%26gt;id = malloc(n-%26gt;len);


strcpy(n-%26gt;id,s);


while ((p-%26gt;next != NULL) %26amp;%26amp;


(strcmp(n-%26gt;id,p-%26gt;next-%26gt;id) %26gt; 0)) {


p = p-%26gt;next;


}


n-%26gt;next = p-%26gt;next;


p-%26gt;next = n;


}





void printIdList(IdList_t l) {


struct IdListNode_t *p = l-%26gt;next;


while (p != NULL) {


printf("|%s|\n",p-%26gt;id);


p = p-%26gt;next;


}


}





void deleteIdList(IdList_t l) {


struct IdListNode_t *p = l;


while (p != NULL) {


struct IdListNode_t *next = p-%26gt;next;


free(p-%26gt;id);


free(p);


p = next;


}


}
Reply:the primitive type 'long' will happily store any 10 digit number, however you will have problems storing ids starting with 0.





My recommendation is to just use a string (character array)





rough example:





char student_id[10];


scanf("%s", student_id);





Then use any regular sorting algorithm to sort the results.


No comments:

Post a Comment