输入一元多项式的参数,然后根据一元多项式加法减法乘法的计算法则,求解最终结果。用到了结构体,指针,操作的时候要注意。
不多说,上代码:
1 #include <stdio.h> 2 #include <malloc.h> 3 4 typedef struct node{ 5 float coef; 6 int expn; 7 struct node *next; 8 }PLOY; 9 10 void start(){ 11 printf("*******************************\n"); 12 printf("两个一元多项式相加/相减,相乘:\n"); 13 printf("*******************************\n"); 14 printf("请选择操作:\n"); 15 printf("0.退出\n"); 16 printf("1.两个一元多项式相加\n"); 17 printf("2.两个一元多项式相乘\n"); 18 printf("3.两个一元多项式相减\n"); 19 } 20 21 void insert(PLOY *head,PLOY *inpt){ 22 PLOY *pre,*now; 23 int signal=0; 24 pre=head;//pre定义为现在的前一个链接 25 if(pre->next==NULL){ 26 pre->next=inpt; 27 } 28 else{ 29 now=pre->next; 30 while(signal==0){ 31 if(inpt->expn<now->expn){ 32 if(now->next==NULL){ 33 now->next=inpt; 34 signal=1; 35 } 36 else{ 37 pre=now; 38 now=pre->next; 39 } 40 } 41 else{ 42 if(inpt->expn > now->expn){ 43 inpt->next=now; 44 pre->next=inpt; 45 signal=1; 46 } 47 else{ 48 now->coef=now->coef+inpt->coef; 49 signal=1; 50 free(inpt); 51 if(now->coef==0){ 52 pre->next=now->next; 53 free(now); 54 } 55 } 56 } 57 } 58 } 59 } 60 61 PLOY *creat(char ch){ 62 PLOY *head,*inpt; 63 float x; 64 int y; 65 head=(PLOY *)malloc(sizeof(PLOY)); 66 head->next=NULL; 67 printf("请输入一元多项式%c:(格式是系数 指数;以0 0结束!)\n",ch); 68 scanf("%f %d",&x,&y); 69 while(x!=0){ 70 inpt=(PLOY *)malloc(sizeof(PLOY)); 71 inpt->coef=x; 72 inpt->expn=y; 73 inpt->next=NULL; 74 insert(head,inpt); 75 printf("请输入一元多项式%c的下一项:(以0 0结束!)\n",ch); 76 scanf("%f %d",&x,&y); 77 } 78 return head; 79 } 80 81 PLOY *addPLOY(PLOY *head,PLOY *pre){ 82 PLOY *inpt; 83 int flag=0; 84 while(flag==0){ 85 if(pre->next==NULL) 86 flag=1; 87 else{ 88 pre=pre->next; 89 inpt=(PLOY *)malloc(sizeof(PLOY)); 90 inpt->coef=pre->coef; 91 inpt->expn=pre->expn; 92 inpt->next=NULL; 93 insert(head,inpt); 94 } 95 } 96 return head; 97 } 98 99 PLOY *minusPLOY(PLOY *head,PLOY *pre){ 100 PLOY *inpt; 101 int flag=0; 102 while(flag==0){ 103 if(pre->next==NULL){ 104 flag=1; 105 } 106 else{ 107 pre=pre->next; 108 inpt=(PLOY *)malloc(sizeof(PLOY)); 109 inpt->coef=0-pre->coef; 110 inpt->expn=pre->expn; 111 inpt->next=NULL; 112 insert(head,inpt); 113 } 114 } 115 return head; 116 } 117 118 PLOY *byPLOY(PLOY *headl,PLOY *head2){ 119 PLOY *inpt,*res,*pre; 120 int flag=0; 121 res=(PLOY *)malloc(sizeof(PLOY)); 122 res->next=NULL; 123 headl=headl->next; 124 pre=head2; 125 while(flag==0){ 126 if(pre->next==NULL){ 127 pre=head2; 128 headl=headl->next; 129 continue; 130 } 131 if(headl==NULL){ 132 flag=1; 133 continue; 134 } 135 pre=pre->next; 136 inpt=(PLOY *)malloc(sizeof(PLOY)); 137 inpt->coef=pre->coef * headl->coef; 138 inpt->expn=pre->expn + headl->expn; 139 inpt->next=NULL; 140 insert(res,inpt); 141 } 142 return res; 143 } 144 145 void print(PLOY *fun){ 146 PLOY *printing; 147 int flag=0; 148 printing=fun->next; 149 if(fun->next==NULL){ 150 printf("0\n"); 151 return ; 152 } 153 while(flag==0){ 154 if(printing->coef>0&&fun->next!=printing) 155 printf("+"); 156 if(printing->coef==1); 157 else if(printing->coef==-1) 158 printf("-"); 159 else 160 printf("%f",printing->coef); 161 if(printing->expn!=0) 162 printf("x^%d",printing->expn); 163 else if((printing->coef==1)||(printing->coef==-1)) 164 printf("1"); 165 if(printing->next==NULL) 166 flag=1; 167 else 168 printing=printing->next; 169 } 170 printf("\n"); 171 } 172 173 int main(){ 174 PLOY *f,*g; 175 int sign=-1; 176 start(); 177 while(sign!=0){ 178 scanf("%d",&sign); 179 switch(sign){ 180 case 0: 181 break; 182 case 1: 183 { 184 printf("你选择的操作是多项式相加:\n"); 185 f=creat(‘f‘); //输入多项式f(x) 186 printf("f(x)="); 187 print(f); 188 g=creat(‘g‘); //输入多项式g(x) 189 printf("g(x)="); 190 print(g); 191 printf("F(x)=f(x)+g(x)="); 192 f=addPLOY(f,g); 193 print(f); 194 sign=-1; 195 start(); 196 break; 197 } 198 case 2: 199 { 200 printf("你选择的操作是多项式相乘:\n"); 201 f=creat(‘f‘); 202 printf("f(x)="); 203 print(f); 204 g=creat(‘g‘); 205 printf("g(x)="); 206 print(g); 207 printf("F(x)=f(x)*g(x)="); 208 f=byPLOY(f,g); 209 print(f); 210 sign=-1; 211 start(); 212 break; 213 } 214 case 3: 215 { 216 printf("你选择的操作是多项式相减:\n"); 217 f=creat(‘f‘); 218 printf("f(x)="); 219 print(f); 220 g=creat(‘g‘); 221 printf("g(x)="); 222 print(g); 223 printf("F(x)=f(x)-g(x)="); 224 f=minusPLOY(f,g); 225 print(f); 226 sign=-1; 227 start(); 228 break; 229 } 230 default: 231 { 232 printf("输入有误!请重新选择操作!\n"); 233 start(); 234 break; 235 } 236 } 237 238 } 239 } 240 241 242 243 244
时间: 2024-10-06 00:07:20