Advertisement
Guest User

C Code

a guest
Mar 21st, 2012
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>   /* gets */
  2. #include <stdlib.h>  /* atoi, malloc */
  3. #include <string.h>  /* strcpy */
  4. #include "uthash.h"
  5.  
  6. struct my_struct
  7. {
  8.     int id;            /* we'll use this field as the key, it's also a power */
  9.     int data1;         /* and this one is coefficient */
  10.     UT_hash_handle hh; /* makes this structure hashable */
  11. };
  12. // basically speaking, just a structure, quite similat to linked list. UT_hash_handle is necessary for uthash library.
  13.  
  14.  
  15. struct my_struct *pow = NULL;
  16. // pointer to next one should always start as NULL. Otherwise it might lead to some random places in memory.
  17. void add_power(int id1, int data1) // this one is used to add new power to database. It should be used only once per power appearing. So for 6^7 and 8^7 it should be run only once for 6^7, 8 should be added to it using add_coefficient function.
  18. {
  19.     struct my_struct *s;
  20.  
  21.     s = malloc(sizeof(struct my_struct));
  22.     s->id = id1;
  23.     s->data1 = data1;
  24.  
  25.     HASH_ADD_INT( pow, id, s );  /* id: name of key field */
  26. }
  27.  
  28. struct my_struct *find_power(int id1)  //uses uthash library to find pointer to structure when it's power is known
  29. {
  30.     struct my_struct *s;
  31.  
  32.     HASH_FIND_INT( pow, &id1, s );  /* s: output pointer */
  33.     return s;
  34. }
  35.  
  36. struct my_struct *s;
  37.  
  38. void add_coefficient(int id, int value) // used to add value to coefficient in already existing power. It's not bug
  39. //resistant functiom (eg. it allows u to add value to power that didnt appear at all) so existence_test to check whether given power really exists
  40. //in database before using add_coefficient is necessary.
  41. {
  42.     s = find_power(id); // searching for structure that holds given power
  43.     s->data1+=value; // and addition of value to its coefficient
  44. }
  45.  
  46. int existence_test(int id) // we use this one to check if given power already exists in database. Returns 1 if it exists, -1 otherwise.
  47. {
  48.     s = find_power(id);
  49.  
  50.     if(s)
  51.     {
  52.         return 1;
  53.     }
  54.     else
  55.     {
  56.         return -1;
  57.     }
  58.  
  59. }
  60.  
  61. int id_sort(struct my_struct *a, struct my_struct *b) {
  62.     return (a->id - b->id);
  63. }
  64.  
  65. void sort_by_id() {
  66.     HASH_SORT(pow, id_sort);
  67. }
  68. void print_all() // used to print all columns from databese in same sequence they appeared in input. It excludes 0 and below powers from appearing as well.
  69. {
  70.     int zero;
  71.     int coeff; // this varable holds power of each structure. in fact, it doesnt need be used since u can manually
  72.     // get power from every structure and use it in if but i like having something like this.
  73.     struct my_struct *s;
  74.  
  75.     for(s=pow; s != NULL; s=s->hh.next) { // hh is apparently unique id created by uthash
  76.         zero=s->id;
  77.         coeff=s->data1;
  78.         if (zero<=0 || coeff==0){continue;}
  79.         else{
  80.         printf("%d,%d;", s->id, s->data1);}
  81. }
  82. }
  83. int main()
  84. {
  85.     int power=0, coefficient=0; // starting at 0
  86.     int swap=0; // swap is used to once modify power value, next time coefficient, next is power, coefficient etc.
  87.     char column[1024000]; // used to store one column of data. 102400 characters is a lot i think but u can make it bigger if necessary.
  88.     char * pch; // used for strtok
  89.     for(; ;){fgets (column, sizeof(column), stdin);
  90.     coefficient=-3; // necessary for debugging reasons if by some wild chance coefficient or power were not created in a loop to avoid bugs
  91.     power=-3;
  92.  
  93.         swap=0;
  94.         if (column[0]==';') // ; at the very beginning of column is used as indicator that it's the end of the program.
  95.         {
  96.             break;
  97.         }
  98.         pch = strtok (column," ,;\n");
  99.      while (pch != NULL)
  100.   {
  101.     if (swap==2) // swap=2 means that we have both power and coefficient ready to add to database
  102.     {
  103.         if (existence_test(power)==1){add_coefficient(power, coefficient);} //existence test to check whether or not such power exists in our database already
  104.         else {add_power(power, coefficient);} // if not, it will be created in here so we can use it later on.
  105.         swap=0; // resetting swap so we can create new pair of power + coefficient.
  106.     }
  107.  
  108.     if(swap==0){power=atoi(pch);}
  109.     if (swap==1){coefficient=atoi(pch);}
  110.     swap++;
  111.      pch = strtok (NULL," ,;\n");
  112.   }
  113.         if (power==-3 || coefficient==-3){break;} // fact that power or coefficient pair wasnt modified means we reached end of program or encountered serious bug. It breaks the loop instantly in such a case.
  114.         if (existence_test(power)==1){add_coefficient(power, coefficient);} //theoretically, it's already in loop above. But this one is necessary for the very last iteration of the loop...i think at least.
  115.         else {add_power(power, coefficient);} // it was written at 3 am so this line may or may be necessary :D Seems to be working fine though.
  116.         swap=0; // resetting swap cuz we entered new loop. In fact, it's more like "safeguard" then necessity and it's only necessary if some1 wrote something like 1,3;1,4;2, without last number so swap wouldnt reset itself correctly in the loop above.
  117.  
  118.  
  119.     }
  120.     sort_by_id();
  121.     print_all(); // used to print all data
  122.     printf("\n;"); // ; at the end of code
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement