#include #include #include #include #include typedef struct node *ptr ; typedef ptr list ; typedef ptr position ; typedef struct node // struct of the polynomial ,,,, << { int power ; int co ; ptr next ; }; void calculate(list [],int); void multi(list , list , list ); void printValueOnFile(list [],int , int ); void printValueOnScreen(list [],int , int); int ValueOfPoly(list, int); void readData(FILE *,list [],int); void add(list ,list,list ); int numberOfPolynomails(FILE *); int isAllNum(char *); void seprate(char *,list ); void insert(int ,int ,list ); void operation(list ,list ); int menu(void); void print(list [],int); void readDataForSecond(); int main() { int k; FILE *in ; in = fopen("C:\\Users\\Wajed\\Desktop\\dataProjects\\oa.txt","r"); //k = numberOfPolynomails(in); k=8;//plz notice that number of poly = 8 list o[k] ; int j = 0; for (j =0;j < k;j++){ o[j] = (list)(malloc(sizeof(struct node))); o[j] -> next = NULL; } int i ; for(;;){ // system("cls"); i = menu(); switch (i) { case 1 :readData(in,o,k); break ; case 2 : printf(" Calculate"); calculate(o,k); break ; case 3 : readDataForSecond(); printf(" second polynomial"); break ; case 4 : printf(" print"); print(o,k); break ; } } return 0; } int menu(){ int i ; do{ printf("\t\t\tMenu\n\n"); printf("\t\t1- Read Data\n"); printf("\t\t2- Calculate\n"); printf("\t\t3- second polynomial\n"); printf("\t\t4 - print \n"); printf("\t\tselect a number"); scanf("%d",&i); }while((i != 1)&&(i != 2)&&(i != 3)&&(i != 4)); return (i); } void calculate(list l[],int size){ int x , y; printf("calculate the value of the function\n"); printf("please enter the value of x \n"); scanf("%d",&x); printf("please , chose from the folowing\n"); printf("1 - print on file\n"); printf("2 - print on screen\n"); scanf("%d",&y); while ( (y != 1) && (y!=2)){ printf("please select correct number\n"); scanf("%d",&y); } if ( y == 1){ printValueOnFile(l,x,size) ; } else { printValueOnScreen(l,x,size); } } void printValueOnFile(list o[],int x,int size)//correct { FILE *out; char name[10]; printf("please enter the name of the file\n"); scanf("%s",name); out = fopen(name,"w"); while ( out == NULL){ printf("file does not exist\n"); printf("renter the name of the file\n"); scanf("%s",&name); out = fopen(name,"w"); } int i = 0 ; for (i=0;inext; fprintf(out,"polynomial #%d ",i+1); while(p!=NULL){ fprintf(out,"%dX^%d",p->co,p->power); if(p->next!=NULL){ fprintf(out," + "); } p = p->next ; } fprintf(out,"the value of polynomial #%d = %d ",i+1,ValueOfPoly(o[i],x)); fprintf(out,"\n"); } fclose(out); } int ValueOfPoly(list o , int x) //correct { int value = 0 ; position p; p=o ->next; while(p!=NULL){ value = value + (p->co)* pow(x,p->power); p= p->next ; } return value ; } void printValueOnScreen(list o[],int x,int size)//correct { int i = 0 ; for (i=0;inext; printf("polynomial #%d : ",i+1); while(p!=NULL){ printf("%dX^%d",p->co,p->power); if(p->next!=NULL){ printf(" + "); } p= p->next ; } printf("the value of polynomial #%d = %d ",i+1,ValueOfPoly(o[i],x)); printf("\n"); } } int numberOfPolynomails(FILE *data)//correct { char line[100]; int counter = 0; while(fgets(line,sizeof line,data )!= NULL){ counter++; } return counter; } void readData(FILE *in,list l[],int i)// plz check this function ,, { printf(" \nread data\n"); int j = 0; // int flag = 1; char tempo[100]; char line[100]; while(fgets(line,sizeof line,in)!= NULL){ char delims[] = "+-"; char *result = NULL; printf("%s",line); result = strtok(line, delims ); while( result != NULL ) { // printf("result ,,%s\n",result); strcpy(tempo,result); seprate(tempo,l[j]); result = strtok(NULL,delims); } // pz notice that it reads the first token from the line then it's stop j++; } } void seprate( char *temp,list l) // correct but plz check it ,, { int co ,pow ; char t[20]; strcpy(t,temp); char t_1[10] ; char t_2[10]; char *t_3[2]; strcpy(t,temp); t_3[0] = strtok(t,"x^"); t_3[1] = strtok(NULL,"x^"); if(t[0] == 'x'){ // printf("enter in 1\n"); co = 1; pow = atoi(t_3[0]); } else if(isAllNum(temp)==0){ // printf("enter in 2\n"); pow = 0; co = atoi ( t ); } else if ((t_3[0] != NULL)&& (t_3[1] == NULL)){ // printf("enter in 3\n"); pow = 1; co = atoi(t_3[0]); } else { // printf("enter in 4\n"); pow = atoi(t_3[1]); co = atoi(t_3[0]); } // printf("co --> %d",co); // printf("power ---> %d",pow); insert(co ,pow,l); } void insert(int co , int pow ,list l )//this function will add new node to the array ,, { position p = (position)malloc (sizeof (struct node)); p-> co = co; p->power = pow; if(l->next == NULL){ p->next = l->next ; l->next = p ; // printf("enter in 1\n"); } else { position f ; f = l ; while((f -> next != NULL)&&(f->next->power > pow)){ f = f->next; } p->next = f->next ; f->next = p ; } return ; } void print(list o[],int size) { int i = 0 ; for (i=0;inext; while(p!=NULL){ printf("%dX^%d",p->co,p->power); if(p->next!=NULL){ printf(" + "); } p= p->next ; } printf("\n"); } } int isAllNum(char *temp) { int i = 0; int flag = 0; for (i=0;i next = NULL; char delims[] = "+-"; char *result = NULL; result = strtok(line, delims ); while( result != NULL ) { strcpy(tempo,result); seprate(tempo,l1); result = strtok(NULL,delims); } //print(l1); printf("enter second poly\n"); scanf("%s",&line); char *r2; list l2 = (list)(malloc(sizeof(struct node))); l2 -> next = NULL; r2 = strtok(line, delims ); char te[20]; while( r2 != NULL ) { strcpy(te,r2); seprate(te,l1); r2 = strtok(NULL,delims); } //print(l2); operation(l1,l2); } void operation(list l1,list l2) { int i; list l3 = (list)(malloc(sizeof(struct node))); l3 -> next = NULL; list l4 = (list)(malloc(sizeof(struct node))); l4 -> next = NULL; printf("chose the op\n"); printf("1 - add\n"); printf("2 - multi\n"); printf("3 - sub \n"); printf("4 - exit\n"); scanf("%d",&i); switch ( i){ case 1 : add(l1,l2,l3); break ; case 2 : multi(l1,l2,l4); break ; case 3 : printf("xlos"); break ; case 4 :printf("exit"); exit(0); } } void add(list l1,list l2,list l) // there is error here << { int i =0; list p1,p2; int g = 0; p1=l1->next ; p2=l2; while(p1 != NULL){ g=0; while(p2!=NULL)// it doesn't get in here ,, { printf("p2 null\n"); if(p1->power == p2->power) { g=1; break; } p2 = p2->next; i++; } if(g==1) { insert((p1 -> co + p2->co ),p1->power,l); } else if (g ==0){ insert(p1->co,p1->power,l); } p1=p1->next; p2=l2->next; } while(p2!=NULL){ g=0; while(p1!=NULL){ if((p1->power)==(p2->power)) { g=1; break; } p1=p1->next; } if(g == 1){ insert(p2->co,p2->power,l); p2=p2->next; p1=l1->next; } } // print(l,1); } void multi(list l1 , list l2, list l) // there is error here ,, { position p1,p2; p2 = l1->next ; p1 = l2->next; // printf("%d\n",p1->power); //printf("%d\n",p1->co); int cp,co; // printf("%d\n",p2->power); // printf("%d\n",p2->co); while(p1 != NULL){ while(p2!=NULL) // it doesn't get in here { cp =(p1->co) * (p2->co); // printf("%d\n",cp); co = (p1->power)* (p2->power); insert( cp,co,l); p2=p2->next ; } p1=p1->next; p2=l2; } }