Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> /* gets */
- #include <stdlib.h> /* atoi, malloc */
- #include <string.h> /* strcpy */
- #include "uthash.h"
- struct my_struct
- {
- int id; /* we'll use this field as the key, it's also a power */
- int data1; /* and this one is coefficient */
- UT_hash_handle hh; /* makes this structure hashable */
- };
- // basically speaking, just a structure, quite similat to linked list. UT_hash_handle is necessary for uthash library.
- struct my_struct *pow = NULL;
- // pointer to next one should always start as NULL. Otherwise it might lead to some random places in memory.
- 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.
- {
- struct my_struct *s;
- s = malloc(sizeof(struct my_struct));
- s->id = id1;
- s->data1 = data1;
- HASH_ADD_INT( pow, id, s ); /* id: name of key field */
- }
- struct my_struct *find_power(int id1) //uses uthash library to find pointer to structure when it's power is known
- {
- struct my_struct *s;
- HASH_FIND_INT( pow, &id1, s ); /* s: output pointer */
- return s;
- }
- struct my_struct *s;
- void add_coefficient(int id, int value) // used to add value to coefficient in already existing power. It's not bug
- //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
- //in database before using add_coefficient is necessary.
- {
- s = find_power(id); // searching for structure that holds given power
- s->data1+=value; // and addition of value to its coefficient
- }
- 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.
- {
- s = find_power(id);
- if(s)
- {
- return 1;
- }
- else
- {
- return -1;
- }
- }
- int id_sort(struct my_struct *a, struct my_struct *b) {
- return (a->id - b->id);
- }
- void sort_by_id() {
- HASH_SORT(pow, id_sort);
- }
- 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.
- {
- int zero;
- int coeff; // this varable holds power of each structure. in fact, it doesnt need be used since u can manually
- // get power from every structure and use it in if but i like having something like this.
- struct my_struct *s;
- for(s=pow; s != NULL; s=s->hh.next) { // hh is apparently unique id created by uthash
- zero=s->id;
- coeff=s->data1;
- if (zero<=0 || coeff==0){continue;}
- else{
- printf("%d,%d;", s->id, s->data1);}
- }
- }
- int main()
- {
- int power=0, coefficient=0; // starting at 0
- int swap=0; // swap is used to once modify power value, next time coefficient, next is power, coefficient etc.
- 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.
- char * pch; // used for strtok
- for(; ;){fgets (column, sizeof(column), stdin);
- coefficient=-3; // necessary for debugging reasons if by some wild chance coefficient or power were not created in a loop to avoid bugs
- power=-3;
- swap=0;
- if (column[0]==';') // ; at the very beginning of column is used as indicator that it's the end of the program.
- {
- break;
- }
- pch = strtok (column," ,;\n");
- while (pch != NULL)
- {
- if (swap==2) // swap=2 means that we have both power and coefficient ready to add to database
- {
- if (existence_test(power)==1){add_coefficient(power, coefficient);} //existence test to check whether or not such power exists in our database already
- else {add_power(power, coefficient);} // if not, it will be created in here so we can use it later on.
- swap=0; // resetting swap so we can create new pair of power + coefficient.
- }
- if(swap==0){power=atoi(pch);}
- if (swap==1){coefficient=atoi(pch);}
- swap++;
- pch = strtok (NULL," ,;\n");
- }
- 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.
- 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.
- 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.
- 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.
- }
- sort_by_id();
- print_all(); // used to print all data
- printf("\n;"); // ; at the end of code
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement