fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class polynome {
  7. public:
  8. polynome(){
  9. n=2;
  10. ind=new int[n];
  11. pt=0;
  12. }
  13. polynome(int *, int);
  14. ~polynome();
  15. static int quantity() {
  16. return q;
  17. }
  18. void print();
  19. void printGorts();
  20. polynome operator+(polynome pol);
  21. polynome& operator=(polynome s){
  22. n=s.n;
  23. pt=s.pt;
  24. for(int i=0;i<n;i++){
  25. ind[i]=s.ind[i];
  26. }
  27. return (*this);
  28. }
  29. void read () {
  30. string s;
  31. cout << "Intup a polynome: ";
  32. cin >> s;
  33. ssize=s.size();
  34. int nshan = 1;
  35. int pos = 0;
  36. while (pos < s.size()) {
  37. string tmp;
  38. while (pos < s.size() && s[pos] != '-' && s[pos] != '+') {
  39. tmp.push_back(s[pos]);
  40. pos++;
  41. }
  42. string tmp1, tmp2;
  43. int posInTmp = 0;
  44. while (tmp[posInTmp] >= '0' && tmp[posInTmp] <= '9' && posInTmp < (int)tmp.size()) {
  45. tmp1.push_back(tmp[posInTmp]);
  46. posInTmp++;
  47. }
  48. if (posInTmp == tmp.size()) {
  49. int gortsakic = strToInt(tmp1);
  50. ind[0] += nshan*gortsakic;
  51. if (pos < s.size()) {
  52. if (s[pos] == '+')
  53. nshan = 1;
  54. else
  55. nshan = -1;
  56. pos++;
  57. }
  58. continue;
  59. }
  60. if (posInTmp == tmp.size()-1) {
  61. int gortsakic = strToInt(tmp1);
  62. ind[1] += nshan*gortsakic;
  63. if (pos < s.size()) {
  64. if (s[pos] == '+')
  65. nshan = 1;
  66. else
  67. nshan = -1;
  68. pos++;
  69. }
  70. continue;
  71. }
  72. posInTmp += 2; // trnum em x^ - i vrov
  73. while (posInTmp < (int)tmp.size()) {
  74. tmp2.push_back(tmp[posInTmp]);
  75. posInTmp++;
  76. }
  77. int gortsakic = nshan*strToInt(tmp1),
  78. cucich = strToInt(tmp2);
  79. if (!tmp2.empty())
  80. ind[cucich] += gortsakic;
  81. else
  82. ind[0] += gortsakic;
  83. if (pos != (int)s.size()) {
  84. if (s[pos] == '-')
  85. nshan = -1;
  86. else
  87. nshan = 1;
  88. pos++;
  89. }
  90. }
  91. int d=0;;
  92. for (int i = pt; i >= 0; i--){
  93. if(ind[i]==0){
  94. continue;
  95. }
  96. d=i;
  97. break;
  98. }
  99. pt=d-1;
  100. n=d;
  101. }
  102. private:
  103. int *ind;
  104. int pt,ssize,n;
  105. static int q;
  106. int power(int x, int n) {
  107. int k=1;
  108. for (int i = 1; i < n; i++)
  109. k *= x;
  110. return k;
  111. }
  112. int strToInt (string s) {
  113. if (s.empty())
  114. return 1;
  115. int tmp = 0;
  116. for (int i = 0; i < (int)s.size(); ++i) {
  117. tmp = tmp*10 + (s[i]-'0');
  118. }
  119. return tmp;
  120. }
  121. };
  122.  
  123. polynome polynome::operator+(polynome pol){
  124.  
  125. int size = max(pol.n,n);
  126. cout<<size<<endl;
  127. polynome polyn;
  128. polyn.ind= new int[size];
  129. polyn.n = size;
  130. for(int i=0;i<=size;i++){
  131. polyn.ind[i]=ind[i]+pol.ind[i];
  132. }
  133. return polyn;
  134. }
  135. int polynome::q = 0;
  136. polynome::polynome(int *ind, int pt) {
  137. this->ind = new int[pt+1];
  138. for (int i = 0; i < pt+1; i++)
  139. this->ind[i] = ind[i];
  140. this->pt = pt;
  141. ++q;
  142. }
  143. polynome::~polynome() {
  144. delete[] ind;
  145. //--q; /////////////////// es pahin ushadir
  146. }
  147. void polynome::print() {
  148. // es funkcian asum a sirun tpenq, es el ushadir
  149. bool flag = false;
  150. for (int i = ssize; i > 1; --i) {
  151. if (ind[i] != 0) {
  152. if (ind[i] == 1) {
  153. if (flag) {
  154. cout << "+x^" << i;
  155. }
  156. else
  157. cout << "x^" << i;
  158. flag = true;
  159. continue;
  160. }
  161. if (ind[i] == -1) {
  162. cout << "-x^" << i;
  163. flag = true;
  164. continue;
  165. }
  166. if (ind[i] > 0 && flag)
  167. cout << '+';
  168. cout << ind[i] << "x^" << i;
  169. flag = true;
  170. }
  171. }
  172. if (ind[1] != 0) {
  173. if (ind[1] > 0 && flag)
  174. cout << '+';
  175. if (ind[1] != 1 && ind[1] != -1)
  176. cout << ind[1];
  177. else if (ind[1] == -1)
  178. cout << '-';
  179. cout << 'x';
  180. flag = true;
  181. }
  182. if (ind[0] != 0) {
  183. if (ind[0] > 0 && flag)
  184. cout << '+';
  185. cout << ind[0];
  186. }
  187. cout << endl;
  188. }
  189. void polynome::printGorts() { // esi vat a, erevi arji dzes
  190. for(int i=0;i<=n;i++){
  191. if(ind[i]==0)
  192. continue;
  193. cout<<ind[i]<<endl;
  194. }
  195. }
  196.  
  197. int main() {
  198. int *a = new int[101];
  199. for (int i = 0; i < 101; ++i)
  200. a[i] = 0;
  201. polynome p(a, 100),c(a,100),*sum;
  202. p.read();
  203. c.read();
  204. sum=new polynome();
  205. (*sum)=p+c;
  206. sum->printGorts();
  207. return 0;
  208. }
  209.  
Runtime error #stdin #stdout #stderr 0s 80768KB
stdin
4x^4+4x-7
2x^2+5
stdout
Intup a polynome: Intup a polynome: 4
-2
4
2
stderr
*** Error in `./prog': double free or corruption (!prev): 0x0000000000734f60 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bcb)[0x2b1001927bcb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76f96)[0x2b100192df96]
/lib/x86_64-linux-gnu/libc.so.6(+0x7778e)[0x2b100192e78e]
./prog[0x401603]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x2b10018d72b1]
./prog[0x400dfa]
======= Memory map: ========
00400000-00403000 r-xp 00000000 fd:00 16392159                           /home/zUPL5z/prog
00602000-00603000 r--p 00002000 fd:00 16392159                           /home/zUPL5z/prog
00603000-00604000 rw-p 00003000 fd:00 16392159                           /home/zUPL5z/prog
00723000-00755000 rw-p 00000000 00:00 0                                  [heap]
2b1000bd7000-2b1000bfa000 r-xp 00000000 fd:00 7484103                    /lib/x86_64-linux-gnu/ld-2.24.so
2b1000bfa000-2b1000bfe000 rw-p 00000000 00:00 0 
2b1000c07000-2b1000c0c000 rw-p 00000000 00:00 0 
2b1000dfa000-2b1000dfb000 r--p 00023000 fd:00 7484103                    /lib/x86_64-linux-gnu/ld-2.24.so
2b1000dfb000-2b1000dfc000 rw-p 00024000 fd:00 7484103                    /lib/x86_64-linux-gnu/ld-2.24.so
2b1000dfc000-2b1000dfd000 rw-p 00000000 00:00 0 
2b1000dfd000-2b1000f00000 r-xp 00000000 fd:00 7484132                    /lib/x86_64-linux-gnu/libm-2.24.so
2b1000f00000-2b10010ff000 ---p 00103000 fd:00 7484132                    /lib/x86_64-linux-gnu/libm-2.24.so
2b10010ff000-2b1001100000 r--p 00102000 fd:00 7484132                    /lib/x86_64-linux-gnu/libm-2.24.so
2b1001100000-2b1001101000 rw-p 00103000 fd:00 7484132                    /lib/x86_64-linux-gnu/libm-2.24.so
2b1001101000-2b1001273000 r-xp 00000000 fd:00 7487266                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b1001273000-2b1001473000 ---p 00172000 fd:00 7487266                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b1001473000-2b100147d000 r--p 00172000 fd:00 7487266                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b100147d000-2b100147f000 rw-p 0017c000 fd:00 7487266                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
2b100147f000-2b1001483000 rw-p 00000000 00:00 0 
2b1001483000-2b1001499000 r-xp 00000000 fd:00 7484067                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b1001499000-2b1001698000 ---p 00016000 fd:00 7484067                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b1001698000-2b1001699000 r--p 00015000 fd:00 7484067                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b1001699000-2b100169a000 rw-p 00016000 fd:00 7484067                    /lib/x86_64-linux-gnu/libgcc_s.so.1
2b100169a000-2b10016b2000 r-xp 00000000 fd:00 7484088                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b10016b2000-2b10018b1000 ---p 00018000 fd:00 7484088                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b10018b1000-2b10018b2000 r--p 00017000 fd:00 7484088                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b10018b2000-2b10018b3000 rw-p 00018000 fd:00 7484088                    /lib/x86_64-linux-gnu/libpthread-2.24.so
2b10018b3000-2b10018b7000 rw-p 00000000 00:00 0 
2b10018b7000-2b1001a4c000 r-xp 00000000 fd:00 7484231                    /lib/x86_64-linux-gnu/libc-2.24.so
2b1001a4c000-2b1001c4b000 ---p 00195000 fd:00 7484231                    /lib/x86_64-linux-gnu/libc-2.24.so
2b1001c4b000-2b1001c4f000 r--p 00194000 fd:00 7484231                    /lib/x86_64-linux-gnu/libc-2.24.so
2b1001c4f000-2b1001c51000 rw-p 00198000 fd:00 7484231                    /lib/x86_64-linux-gnu/libc-2.24.so
2b1001c51000-2b1001c55000 rw-p 00000000 00:00 0 
2b1004000000-2b1004021000 rw-p 00000000 00:00 0 
2b1004021000-2b1008000000 ---p 00000000 00:00 0 
7ffcb91ed000-7ffcb920e000 rw-p 00000000 00:00 0                          [stack]
7ffcb93ba000-7ffcb93bc000 r-xp 00000000 00:00 0                          [vdso]
7ffcb93bc000-7ffcb93be000 r--p 00000000 00:00 0                          [vvar]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]