Advertisement
Guest User

Untitled

a guest
May 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. typedef struct mono
  5. {
  6. int degre;
  7. float coeff;
  8. struct mono *pg;
  9. struct mono *pd;
  10. }monome;
  11. typedef struct poly
  12. {
  13. monome *first;
  14. monome *last;
  15. }polycreux;
  16. polycreux init(polycreux p)
  17. {
  18. p.first=NULL;
  19. p.last=NULL;
  20. return p;
  21. }
  22. polycreux supprimer(polycreux p, int deg)
  23. {
  24. monome *m1;
  25. if(p.first==NULL)
  26. {
  27. printf("erreur");
  28. }
  29. else
  30. {
  31. if(p.first->degre==deg)
  32. {
  33. p.first=p.first->pd;
  34. free(p.first->pg);
  35. p.first->pg=NULL;
  36. }
  37. else if(p.last->degre==deg)
  38. {
  39. p.last=p.last->pg;
  40. free(p.last->pd);
  41. p.last->pd=NULL;
  42. }
  43. else
  44. {
  45. m1=p.first;
  46. while(m1!=NULL && m1->degre!=deg)
  47. {
  48. m1=m1->pd;
  49. }
  50. if(m1==NULL)
  51. {
  52. printf("element n existe pas!!");
  53. }
  54. else
  55. {
  56. m1->pg->pd=m1->pd;
  57. m1->pd->pg=m1->pg;
  58. free(m1);
  59. }
  60. }
  61. }
  62. return p;
  63.  
  64. }
  65. polycreux inserer(polycreux p , float coef,int deg)
  66. {
  67. monome *m1,*m2;
  68. m1=p.first;
  69. while(m1!=NULL && m1->degre!=deg)
  70. {
  71. m1=m1->pd;
  72. }
  73. if(m1!=NULL)
  74. {
  75. m1->coeff=m1->coeff+coef;
  76. if(m1->coeff==0)
  77. {
  78. p=supprimer(p,deg);
  79. }
  80. }
  81. else
  82. {
  83. m1=(monome*)malloc(sizeof(monome));
  84. m1->coeff=coef;
  85. m1->degre=deg;
  86. if(p.first==NULL)
  87. {
  88. m1->pd=NULL;
  89. m1->pg=NULL;
  90. p.first=m1;
  91. p.last=m1;
  92. }
  93. else
  94. {
  95. if(p.first->degre<deg)
  96. {
  97. m1->pg=NULL;
  98. m1->pd=p.first;
  99. p.first->pg=m1;
  100. p.first=m1;
  101. }
  102. else if (p.last->degre>deg)
  103. {
  104. m1->pd=NULL;
  105. m1->pg=p.last;
  106. p.last->pd=m1;
  107. p.last=m1;
  108. }
  109. else
  110. {
  111. m2=p.first->pd;
  112. while(m2->pd!=NULL && deg<m2->degre)
  113. {
  114. m2=m2->pd;
  115. }
  116. m1->pd=m2;
  117. m1->pg=m2->pg;
  118. m2->pg->pd=m1;
  119. m2->pg=m1;
  120. }
  121. }
  122. }
  123. return p;
  124.  
  125. }
  126. void affichage1(polycreux p)
  127. {
  128. monome* m;
  129. m=p.first;
  130. while(m!=NULL)
  131. {
  132. printf("%.1fX%d ",m->coeff,m->degre);
  133. printf("\n");
  134. m=m->pd;
  135. }
  136. }
  137. void affichage2(polycreux p)
  138. {
  139. monome* m;
  140. m=p.last;
  141. while(m!=NULL)
  142. {
  143. printf("%.fX%d ",m->coeff,m->degre);
  144. printf("\n");
  145. m=m->pg;
  146. }
  147. }
  148. polycreux destruction(polycreux p)
  149. {
  150. monome *m;
  151. while(p.first==NULL)
  152. {
  153. m=p.first;
  154. p.first=p.first->pd;
  155. free(m);
  156. }
  157. init(p);
  158. return p;
  159. }
  160. polycreux multipscal(polycreux p,float s)
  161. {
  162. monome *m;
  163. m=p.first;
  164. while(m!=NULL)
  165. {
  166. m->coeff=m->coeff*s;
  167. m=m->pd;
  168. }
  169. return p;
  170. }
  171. polycreux multipmono(polycreux p,float c,int d)
  172. {
  173. monome *m;
  174. m=p.first;
  175. while(m!=NULL)
  176. {
  177. m->coeff=m->coeff*c;
  178. m->degre=m->degre+d;
  179. m=m->pd;
  180. }
  181. return p;
  182. }
  183.  
  184. polycreux somme(polycreux p1,polycreux p2,polycreux p3)
  185. {
  186. monome *m;
  187. m=p1.first;
  188. while(m!=NULL)
  189. {
  190. p3=inserer(p3,m->coeff,m->degre);
  191. m=m->pd;
  192. }
  193. m=p2.first;
  194. while(m!=NULL)
  195. {
  196. p3=inserer(p3,m->coeff,m->degre);
  197. m=m->pd;
  198. }
  199. return p3;
  200. }
  201.  
  202. polycreux soustraction(polycreux p1,polycreux p2,polycreux p3)
  203. {
  204. monome *m;
  205. m=p1.first;
  206. while(m!=NULL)
  207. {
  208. p3=inserer(p3,m->coeff,m->degre);
  209. m=m->pd;
  210. }
  211. m=p2.first;
  212. while(m!=NULL)
  213. {
  214. p3=inserer(p3,-m->coeff,m->degre);
  215. m=m->pd;
  216. }
  217. return p3;
  218. }
  219.  
  220. int main()
  221. {
  222. int choix,d;
  223. float c,s;
  224. polycreux p1,p2,p3;
  225. do
  226. {
  227. printf("entrer votre choix");
  228. printf("\n");
  229. printf("entrer 1 pour initialiser le polynome");
  230. printf("\n");
  231. printf("entrer 21 pour l insertion dans p1 ");
  232. printf("\n");
  233. printf("entrer 22 pour l insertion dans p2");
  234. printf("\n");
  235. printf("entrer 31 pour la suppression dans p1");
  236. printf("\n");
  237. printf("entrer 32 pour la suppression dans p2");
  238. printf("\n");
  239. printf("entrer 41 pour affichage p1 par ordre decroissant");
  240. printf("\n");
  241. printf("entrer 42cpour affichage p2 par ordre decroissant");
  242. printf("\n");
  243. printf("entrer 51 pour affichage p1 par ordre croissant");
  244. printf("\n");
  245. printf("entrer 52 pour affichage p2 par ordre croissant");
  246. printf("\n");
  247. printf("entrer 61 pour la destruction p1");
  248. printf("\n");
  249. printf("entrer 62 pour la destruction p1");
  250. printf("\n");
  251. printf("entrer 71 pour la multiplication par scalaire *p1");
  252. printf("\n");
  253. printf("entrer 72 pour la multiplication par scalaire*p2");
  254. printf("\n");
  255. printf("entrer 81 pour la multiplication par un monome *p1");
  256. printf("\n");
  257. printf("entrer 82 pour la multiplication par un monome *p2");
  258. printf("\n");
  259. printf("entrer 9 pour la somme de p1 et p2 ");
  260. printf("\n");
  261. printf("entrer 10 pour la soustraction de p1 et p2");
  262. printf("\n");
  263. printf("enter 0 pour sortir");
  264. printf("\n");
  265. scanf("%d",&choix);
  266. clrscr();
  267. switch(choix)
  268. {
  269. case 1 :
  270. init(p1);
  271. init(p2);
  272. init(p3);
  273. break;
  274. case 21 :
  275. printf("enter le degre est le coeff");
  276. scanf("%d",&d);
  277. scanf("%f",&c);
  278. p1=inserer(p1,c,d);
  279. break;
  280. case 22 :
  281. printf("enter le degre est le coeff");
  282. scanf("%d",&d);
  283. scanf("%f",&c);
  284. inserer(p2,c,d);
  285. break;
  286. case 31 :
  287. printf("enter le degre");
  288. scanf("%d",&d);
  289. p1=supprimer(p1,d);
  290. break;
  291. case 32 :
  292. printf("enter le degre");
  293. scanf("%d",&d);
  294. p2=supprimer(p2,d);
  295. break;
  296. case 41 :
  297. affichage1(p1);
  298. break;
  299. case 42 :
  300. affichage1(p2);
  301. break;
  302. case 51 :
  303. affichage2(p1);
  304. break;
  305. case 52 :
  306. affichage2(p2);
  307. break;
  308. case 61 :
  309. p1=destruction(p1);
  310. break;
  311. case 62 :
  312. p2=destruction(p2);
  313. break;
  314. case 71 :
  315. printf("entrer le scalaire");
  316. scanf("%f",&s);
  317. p1=multipscal(p1,s);
  318. break;
  319. case 72 :
  320. printf("entrer le scalaire");
  321. scanf("%f",&s);
  322. p2=multipscal(p2,s);
  323. break;
  324. case 81 :
  325. printf("entrer le degre du monome et le coeff");
  326. scanf("%d",&d);
  327. scanf("%f",&c);
  328. p1=multipmono(p1,c,d);
  329. break;
  330. case 9 :
  331. p3=somme(p1,p2,p3);
  332. break;
  333. case 10 :
  334. p3=soustraction(p1,p2,p3);
  335. break;
  336. }
  337. clrscr();
  338. }while(choix!=0);
  339. return 0;
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement