多项式的加减乘

举报
悦来客栈的老板 发表于 2020/12/30 01:56:47 2020/12/30
【摘要】 这是我学数据结构写的第一个比较"大型"的程序,唉,还是有不足之处啊。。。 # include <stdio.h># include <iostream.h># include <stdlib.h># include <malloc.h> # define OK 1# define ERROR 0# define OVERFLO...

这是我学数据结构写的第一个比较"大型"的程序,唉,还是有不足之处啊。。。


  
  1. # include <stdio.h>
  2. # include <iostream.h>
  3. # include <stdlib.h>
  4. # include <malloc.h>
  5. # define OK 1
  6. # define ERROR 0
  7. # define OVERFLOW -1
  8. typedef struct Polynomial
  9. {
  10. float coef;
  11. int expn;
  12. struct Polynomial *next;
  13. }Polynomial,*LinkPolyn;
  14. void Create_Polyn(LinkPolyn &L)
  15. {
  16. int i,n;
  17. LinkPolyn p,s;
  18. L = (LinkPolyn) malloc (sizeof (Polynomial));
  19. if (!L)
  20. exit(OVERFLOW);
  21. L->next = NULL;
  22. p = L;
  23. cout<<" 请输入您要创建多项式的项数:";
  24. cin>>n;
  25. for (i=0; i<n; i++)
  26. {
  27. s = (LinkPolyn) malloc (sizeof (Polynomial));
  28. p->next = s;
  29. cout<<" 请输入第"<<i+1<<"项的系数:";
  30. cin>>s->coef;
  31. cout<<" 请输入第"<<i+1<<"项的指数:";
  32. cin>>s->expn;
  33. p = s;
  34. }
  35. p->next = NULL;
  36. }
  37. void Display(LinkPolyn L)
  38. {
  39. LinkPolyn p;
  40. if (!L->next )
  41. cout<<" 多项式的项数为0!无法显示"<<endl;
  42. p = L->next ;
  43. while (p)
  44. {
  45. if (p->coef == 0.0)
  46. cout<<"";
  47. else if (p->expn == 0 )
  48. cout<<p->coef ;
  49. else if (p->coef == 1 && p->expn == 1)
  50. cout<<"X";
  51. else if (p->coef == 1 && p->expn != 1)
  52. cout<<"X^"<<p->expn;
  53. else if (p->coef == -1 && p->expn == 1)
  54. cout<<"-X";
  55. else if (p->coef != 1 && p->expn == 1)
  56. cout<<p->coef<<"X";
  57. else if (p->coef == -1 && p->expn != 1)
  58. cout<<"-X^"<<p->expn ;
  59. else
  60. cout<<p->coef <<"X^"<<p->expn ;
  61. if (p->next!=NULL && p->coef !=0 && p->next->coef >=0)
  62. cout<<"+";
  63. p = p->next ;
  64. }
  65. cout<<endl;
  66. }
  67. void SortPolyn(LinkPolyn &L)
  68. {
  69. LinkPolyn p,q;
  70. int temp;
  71. float tmp;
  72. if (!L->next )
  73. cout<<" 多项式的项数为0,无法排序!"<<endl;
  74. for(p=L->next;p; p=p->next )
  75. for(q=L->next ;q; q=q->next)
  76. {
  77. if(p->expn < q->expn )
  78. {
  79. temp = p->expn ;
  80. p->expn = q->expn ;
  81. q->expn = temp;
  82. tmp = p->coef ;
  83. p->coef = q->coef ;
  84. q->coef = tmp;
  85. }
  86. else if (p != q && p->expn == q->expn)
  87. {
  88. p->coef +=q->coef ;
  89. q->coef = 0;
  90. q->expn = 0;
  91. }
  92. }
  93. }
  94. void Diff_Polyn(LinkPolyn &L)
  95. {
  96. LinkPolyn p;
  97. if (!L->next )
  98. cout<<" 多项式的项数为0,无法求导!"<<endl;
  99. p = L->next ;
  100. while (p)
  101. {
  102. p->coef *= p->expn ;
  103. p->expn -=1;
  104. p = p->next ;
  105. }
  106. }
  107. void Int_Polyn(LinkPolyn &L)
  108. {
  109. LinkPolyn p;
  110. if (!L->next )
  111. cout<<" 多项式的项数为0,无法求积分!"<<endl;
  112. p = L->next ;
  113. while (p)
  114. {
  115. p->coef = p->coef/(p->expn+1);
  116. p->expn +=1;
  117. p = p->next ;
  118. }
  119. }
  120. int cmp (int a, int b)
  121. {
  122. if (a>b)
  123. return 1;
  124. else if (a == b)
  125. return 0;
  126. return -1;
  127. }
  128. int Add_Polyn(LinkPolyn &PA, LinkPolyn &PB)
  129. {
  130. LinkPolyn qc,qa,qb,temp;
  131. float sum;
  132. qc = PA;
  133. qa = PA->next ;
  134. qb = PB->next ;
  135. while (qa && qb)
  136. {
  137. switch(cmp(qa->expn ,qb->expn ))
  138. {
  139. case -1:
  140. qc->next = qa;
  141. qc = qa;
  142. qa = qa->next;
  143. break;
  144. case 0:
  145. sum = qa->coef + qb->coef ;
  146. if(sum !=0.0)
  147. {
  148. qa->coef = sum;
  149. qc->next = qa;
  150. qc = qa;
  151. qa = qa->next;
  152. }
  153. else
  154. {
  155. temp = qa;
  156. qa = qa->next ;
  157. free(temp);
  158. }
  159. temp = qb;
  160. qb = qb->next ;
  161. free(temp);
  162. break;
  163. case 1:
  164. qc->next = qb;
  165. qc = qb;
  166. qb = qb->next;
  167. break;
  168. }
  169. }
  170. while(qa)
  171. {
  172. qc->next = qa;
  173. qc = qa;
  174. qa = qa->next;
  175. }
  176. while(qb)
  177. {
  178. qc->next = qb;
  179. qc = qb;
  180. qb = qb->next;
  181. }
  182. free(qb);
  183. return OK;
  184. }
  185. int Sub_Polyn(LinkPolyn &PA, LinkPolyn &PB)
  186. {
  187. LinkPolyn qc,qa,qb,temp;
  188. float sum;
  189. qc = PA;
  190. qa = PA->next ;
  191. qb = PB->next ;
  192. while (qa && qb)
  193. {
  194. switch(cmp(qa->expn ,qb->expn ))
  195. {
  196. case -1:
  197. qc->next = qa;
  198. qc = qa;
  199. qa = qa->next;
  200. break;
  201. case 0:
  202. sum = qa->coef - qb->coef ;
  203. if(sum !=0.0)
  204. {
  205. qa->coef = sum;
  206. qc->next = qa;
  207. qc = qa;
  208. qa = qa->next;
  209. }
  210. else
  211. {
  212. temp = qa;
  213. qa = qa->next ;
  214. free(temp);
  215. }
  216. temp = qb;
  217. qb = qb->next ;
  218. free(temp);
  219. break;
  220. case 1:
  221. qb->coef =-qb->coef ;
  222. qc->next = qb;
  223. qc = qb;
  224. qb = qb->next;
  225. break;
  226. }
  227. }
  228. while(qa)
  229. {
  230. qc->next = qa;
  231. qc = qa;
  232. qa = qa->next;
  233. }
  234. while(qb)
  235. {
  236. qb->coef =-qb->coef ;
  237. qc->next = qb;
  238. qc = qb;
  239. qb = qb->next;
  240. }
  241. free(qb);
  242. return OK;
  243. }
  244. int Mul_Polyn(LinkPolyn pa,LinkPolyn pb,LinkPolyn &pc)
  245. {
  246. LinkPolyn qa,qb,qc,s;
  247. pc = (LinkPolyn) malloc (sizeof (Polynomial));
  248. pc->next = NULL;
  249. qc = pc;
  250. for(qa=pa->next;qa;qa=qa->next)
  251. {
  252. for(qb=pb->next;qb;qb=qb->next)
  253. {
  254. s = (LinkPolyn) malloc (sizeof (Polynomial));
  255. qc->next = s;
  256. s->coef=qa->coef*qb->coef;
  257. s->expn=qa->expn+qb->expn;
  258. qc = s;
  259. }
  260. }
  261. qc->next = NULL;
  262. SortPolyn(pc);
  263. return OK;
  264. }
  265. int main (void)
  266. {
  267. char i;
  268. LinkPolyn L;
  269. LinkPolyn PA,PB,PC;
  270. cout<<endl;
  271. cout<<" ********************************************************"<<endl;
  272. cout<<" | 欢迎使用本系统! |"<<endl;
  273. cout<<" | 制作人:风尘三侠 |"<<endl;
  274. cout<<" | 1:创建一个多项式 2:对一个多项式求导 |"<<endl;
  275. cout<<" | 3:对一个多项式求积分 4:两个多项式相加 |"<<endl;
  276. cout<<" | 5:两个多项式相减 6:两个多项式相乘 |"<<endl;
  277. cout<<" | 7:帮助 8:退出 |"<<endl;
  278. cout<<" ********************************************************"<<endl;
  279. cout<<endl;
  280. do
  281. {
  282. cout<<" 请选择您要执行的操作:";
  283. cin>>i;
  284. switch (i)
  285. {
  286. case '1':
  287. Create_Polyn(L);
  288. SortPolyn(L);
  289. cout<<" 您创建的多项式是: F(X) = ";
  290. Display(L);
  291. break;
  292. case '2':
  293. cout<<" 对一个多项式求导前请先创建一个多项式!"<<endl;
  294. Create_Polyn(L);
  295. SortPolyn(L);
  296. cout<<" 您创建的多项式是: F(X) = ";
  297. Display(L);
  298. Diff_Polyn(L);
  299. cout<<" 求导后的多项式是: F'(X) = ";
  300. Display(L);
  301. break;
  302. case '3':
  303. cout<<" 对一个多项式求积分前请先创建一个多项式!"<<endl;
  304. Create_Polyn(L);
  305. SortPolyn(L);
  306. cout<<" 您创建的多项式是: F(X) = ";
  307. Display(L);
  308. Int_Polyn(L);
  309. cout<<" 积分后的多项式是: F'(X) = ";
  310. Display(L);
  311. break;
  312. case '4':
  313. cout<<" 请先创建两个多项式!"<<endl;
  314. cout<<" 创建第一个多项式!"<<endl;
  315. Create_Polyn(PA);
  316. SortPolyn(PA);
  317. cout<<" 您创建的第一个多项式是: F(X) = ";
  318. Display(PA);
  319. cout<<" 创建第二个多项式!"<<endl;
  320. Create_Polyn(PB);
  321. SortPolyn(PB);
  322. cout<<" 您创建的第二个多项式是: G(X) = ";
  323. Display(PB);
  324. cout<<" 两个多项式相加后:F(X) + G(X) = ";
  325. Add_Polyn(PA, PB);
  326. Display(PA);
  327. break;
  328. case '5':
  329. cout<<" 请先创建两个多项式!"<<endl;
  330. cout<<" 创建第一个多项式!"<<endl;
  331. Create_Polyn(PA);
  332. SortPolyn(PA);
  333. cout<<" 您创建的第一个多项式是: F(X) = ";
  334. Display(PA);
  335. cout<<" 创建第二个多项式!"<<endl;
  336. Create_Polyn(PB);
  337. SortPolyn(PB);
  338. cout<<" 您创建的第二个多项式是: G(X) = ";
  339. Display(PB);
  340. cout<<" 两个多项式相减后:F(X) - G(X) = ";
  341. Sub_Polyn(PA, PB);
  342. Display(PA);
  343. break;
  344. case '6':
  345. cout<<" 请先创建两个多项式!"<<endl;
  346. cout<<" 创建第一个多项式!"<<endl;
  347. Create_Polyn(PA);
  348. SortPolyn(PA);
  349. cout<<" 您创建的第一个多项式是: F(X) = ";
  350. Display(PA);
  351. cout<<" 创建第二个多项式!"<<endl;
  352. Create_Polyn(PB);
  353. SortPolyn(PB);
  354. cout<<" 您创建的第二个多项式是: G(X) = ";
  355. Display(PB);
  356. cout<<" 两个多项式相乘后:F(X) * G(X) = ";
  357. Mul_Polyn(PA, PB,PC);
  358. Display(PC);
  359. break;
  360. case '7':
  361. cout<<" 创建多项式! "<<endl;
  362. cout<<" 先输入多项式的项数,要输入自然数!"<<endl;
  363. cout<<" 可以输入重复的项,先输入系数,再输入指数!"<<endl;
  364. cout<<" 系数可以是任意的数字,指数要输入自然数!"<<endl;
  365. break;
  366. case '8':
  367. cout<<" 谢谢使用,再见!"<<endl;
  368. exit(OVERFLOW);
  369. break;
  370. default:
  371. {
  372. cout<<" 您的输入有误,请重新输入!"<<endl;
  373. break;
  374. }
  375. }
  376. }while (i != 8);
  377. return 0;
  378. }


 

文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/6818975

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。