Guest User

Untitled

a guest
Oct 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. #include "header.h"
  2.  
  3. void pascalTab(int ** tab, int n) // tworzenie tablicy pascala
  4. {
  5. for(int i=0;i<n;i++)
  6. {
  7. tab[i][0] = 0;
  8. tab[0][i] = 1;
  9. }
  10. for(int i=1;i<n;i++)
  11. {
  12. for(int j=1;j<n;j++)
  13. {
  14. tab[j][i] = tab[j-1][i-1] + tab[j][i-1];
  15. }
  16. }
  17. };
  18.  
  19. bool getPolynomial(string line, int * c, int * e, int &word, int &d)
  20. {
  21. int size = strlen(line.c_str());
  22. int i = 1;
  23. bool cprog = true;
  24. bool eprog = false;
  25. word = -1;
  26. d = 0;
  27.  
  28. while(i<size)
  29. {
  30. if (line[i] == '.') // jesli natrafimy na kropke to wyswietlamy komunikat i wychodzimy z funkcji
  31. {
  32. if(cprog == true)
  33. {
  34. cout << "C is not an integer.\n";
  35. return false;
  36. }
  37. if(eprog == true)
  38. {
  39. cout << "E is not an integer.\n";
  40. return false;
  41. }
  42.  
  43. }
  44. if (i==1 && line[i] == 'n') // jesli pierwszy wyraz w linii to odrazu n to domyslnie c ustawiamy na 1
  45. {
  46. word++;
  47. c[word] = 1;
  48. cprog = false;
  49. eprog = true;
  50. }
  51. if (line[i] == '-') // jesli mamy znak to wiemy ze dostaniemy kolejna wartosc w wielomianie
  52. {
  53. word++;
  54. cprog = true;
  55. eprog = false;
  56.  
  57. if (line[i+1] == 'n')
  58. {
  59. c[word] = -1;
  60. }
  61. else
  62. {
  63. c[word] = 0;
  64. }
  65. }
  66. if (line[i] == '+')
  67. {
  68. word++;
  69. cprog = true;
  70. eprog = false;
  71.  
  72. if (line[i+1] == 'n')
  73. {
  74. c[word] = 1;
  75. }
  76. else
  77. {
  78. c[word] = 0;
  79. }
  80. }
  81.  
  82. if (line[i]>='0' && line[i] <='9')
  83. {
  84. if (i==1)
  85. {
  86. word++;
  87. }
  88. if (cprog == true)
  89. {
  90. c[word]*=10;
  91.  
  92. if (c[word] >= 0)
  93. c[word]+=line[i] - 48;
  94. else
  95. c[word]-=line[i] - 48;
  96. if (line[i-1] == '-')
  97. c[word] *= -1;
  98. }
  99.  
  100. if (eprog == true)
  101. {
  102. e[word]*=10;
  103. e[word]+=line[i] - 48;
  104.  
  105. if (e[word] > 100)
  106. {
  107. cout << "Potega nie miesci sie w przedziale <0,100>\n";
  108. return false;
  109. }
  110. }
  111. }
  112. if (line[i] == 'n')
  113. {
  114. if (line[i+1] != '^')
  115. e[word] = 1;
  116. cprog = false;
  117. eprog = true;
  118. }
  119.  
  120. if (line[i] == ')')
  121. {
  122. if(line[i]-1=='n')
  123. {
  124. e[word] = 1;
  125. }
  126. break;
  127. }
  128.  
  129. i++;
  130. }
  131.  
  132. i+=2;
  133.  
  134. if (line[i] == '-')
  135. {
  136. cout << "D is negative integer.\n";
  137. return false;
  138. }
  139. while(i<size)
  140. {
  141. if (line[i] == '.')
  142. {
  143. cout << "D is not an integer.\n";
  144. return false;
  145. }
  146. d *= 10;
  147. d += line[i] - 48;
  148. i++;
  149. }
  150.  
  151. return true;
  152.  
  153. };
  154. void cleanPolynomial(int *c, int *e, int n)
  155. {
  156. for(int i=0;i<n;i++)
  157. {
  158. c[i] = 0;
  159. e[i] = 0;
  160. }
  161. };
  162. void calcPolynomial(int *c, int *e, int word, int ** tab, int d)
  163. {
  164. if (d==0)
  165. {
  166. cout << "You can not divide by zero." << endl;
  167. return;
  168. }
  169. int sum = 0;
  170. for(int i=0;i<=word;i++)
  171. {
  172. sum+=c[i];
  173. }
  174.  
  175. if (sum%d != 0)
  176. {
  177. cout << "Not always an integer." << endl;
  178. return;
  179. }
  180.  
  181. int st = e[0];
  182. int * temp = new int[st+1];
  183. int * p2 = new int[st+1];
  184.  
  185. for(int i=0;i<=st;i++)
  186. {
  187. p2[i] = 0;
  188. }
  189. for(int w=0;w<=word;w++)
  190. {
  191. int stopien = e[w];
  192. int wsp = c[w];
  193. for(int i=0;i<=stopien;i++)
  194. {
  195. temp[i] = tab[i][stopien];
  196. temp[i] *= wsp;
  197. }
  198.  
  199. for(int i=0;i<=stopien;i++)
  200. {
  201. p2[i] += temp[i];
  202. }
  203. };
  204.  
  205. for(int i=0;i<=st;i++)
  206. {
  207. for(int j=0;j<=word;j++)
  208. {
  209. if (e[j]==i)
  210. {
  211. p2[i] -= c[j];
  212. }
  213. }
  214. };
  215.  
  216. for(int i=0;i<=st;i++)
  217. {
  218. if (p2[i]%d!=0)
  219. {
  220. cout << "Not always an integer." << endl;
  221. return;
  222. }
  223. }
  224. cout << "Always an integer." << endl;
  225. };
Add Comment
Please, Sign In to add comment