fork download
  1. #pragma once
  2. using namespace std;
  3. #include <iostream>
  4. #include <string.h>
  5.  
  6. class BigNum
  7. {
  8. private:
  9. char *x = new char[40001];
  10. //char x[40001];
  11. public:
  12. BigNum()
  13. {
  14. x[0] = '+';
  15. for (int i = 1; i < 40001; i++)x[i] = 0;
  16. }
  17. BigNum(const BigNum &B)
  18. {
  19. int count = 0;
  20. int str = strlen(B.x);
  21. cout << "copy\n";
  22. while (B.x[1 + count] == 48 && str != count + 2/*||(str!=count+3&&B.x[count+1]!=48)*/)
  23. {
  24. count++;
  25. }
  26. x[0] = B.x[0];
  27. //if (str == count + 3 && B.x[count + 1] == 48)this->x[0]='+';
  28. strncpy(x + 1, B.x + count + 1, 40000);
  29. }
  30. ~BigNum()
  31. {
  32. delete[] x;
  33. }
  34. friend int compare(BigNum & A, BigNum &B);
  35. friend istream & operator >> (istream &, BigNum &);
  36. friend ostream & operator << (ostream &, const BigNum &);
  37. friend BigNum operator +(BigNum A2, BigNum B2);
  38. friend BigNum operator -(BigNum A2, BigNum B2);
  39. friend BigNum operator *(BigNum A2, BigNum B2);
  40. friend BigNum operator /(BigNum A2, BigNum B2);
  41. const BigNum &operator =(const BigNum & B)
  42. {
  43. int count = 0;
  44. int str = strlen(B.x);
  45. while (B.x[1 + count] == 48 && str != count + 2)//||(str!=count+3&&B.x[count+1]!=48))
  46. {
  47. count++;
  48. }
  49. x[0] = B.x[0];
  50. //if (str == count + 3 && B.x[count + 1] == 48)this->x[0]='+';
  51. strncpy(x + 1, B.x + count + 1, 40000);
  52. return *this;
  53. };
  54. };
  55. int compare(BigNum & A, BigNum &B)
  56. {
  57.  
  58. int count = 1;
  59. while (A.x[count] == 48)count++;
  60. count = 1;
  61. while (B.x[0] == 48)count++;
  62. int s = strlen(A.x + count), t = strlen(B.x + count);
  63.  
  64. if (s - t < 0)return 1;
  65. if (s - t == 0)for (int i = 1; i < s + 1; i++)
  66. {
  67. if (A.x[i] > B.x[i])return 0;
  68. if (A.x[i] < B.x[i])return 1;
  69.  
  70. }
  71. return 0;
  72.  
  73. }
  74. istream & operator >> (istream & str, BigNum & obj)
  75. {
  76.  
  77. str >> obj.x + 1;
  78. //scanf("%s", obj.x+1);
  79. //fgets(obj.x, 4097, stdin);
  80. // for (int i = 1; i < 4100; i++)//scanf("%c", &obj.x[i]);
  81. //str.get(obj.x[i]);
  82. int len = strlen(obj.x + 1);
  83. if (obj.x[1] == '-' || obj.x[1] == '+')
  84. {
  85. for (int i = 0; i < len; i++)
  86. {
  87. obj.x[i] = obj.x[i + 1];
  88. }
  89. obj.x[len] = 0;
  90. }
  91. return str;
  92. }
  93. ostream & operator << (ostream & str, const BigNum & obj)
  94. {
  95. if ((obj.x[1] == '0'&&obj.x[0] == '-') || obj.x[0] == '+')str << obj.x + 1;
  96. else str << obj.x;
  97. return str;
  98. }
  99. BigNum operator +(BigNum A2, BigNum B2)
  100. {
  101. BigNum temp;
  102. if (A2.x[0] == '+'&&B2.x[0] == '-')//+-
  103. {
  104. B2.x[0] = '+';
  105. return A2 - B2;
  106. }
  107. else if (A2.x[0] == '-'&&B2.x[0] == '+')//-+
  108. {
  109. A2.x[0] = '+';
  110. return B2 - A2;
  111. }
  112.  
  113.  
  114. int str1 = strlen(A2.x), str2 = strlen(B2.x);
  115. int gap = str1 - str2;
  116.  
  117. if (str2 > str1)
  118. {
  119. int temp2;
  120. temp = A2;
  121. A2 = B2;
  122. B2 = temp;
  123. temp2 = str1;
  124. str1 = str2;
  125. str2 = temp2;
  126. gap = gap*(-1);
  127. }
  128.  
  129. if (A2.x[0] == '-'&&B2.x[0] == '-')temp.x[0] = '-';
  130. temp.x[1] = 48;
  131. strncpy(temp.x + 2, A2.x + 1, str1 + 1);
  132. for (int i = str1 - 1; i > gap; i--)
  133. temp.x[i + 1] = temp.x[i + 1] + B2.x[i - gap] - 48;
  134. for (int i = str1 - 1; i >= 1; i--)
  135. if (temp.x[i + 1] > 57)
  136. {
  137. temp.x[i + 1] = temp.x[i + 1] - 10;
  138. temp.x[i]++;
  139. }
  140. return temp;
  141.  
  142.  
  143.  
  144. }
  145. BigNum operator -(BigNum A2, BigNum B2)
  146. {
  147. BigNum temp;
  148. if (A2.x[0] == '+'&&B2.x[0] == '-')
  149. {
  150. B2.x[0] = '+';
  151. return A2 + B2;
  152. }
  153. else if (A2.x[0] == '-'&&B2.x[0] == '+')
  154. {
  155. B2.x[0] = '-';
  156. return A2 + B2;
  157. }
  158. int str1 = strlen(A2.x), str2 = strlen(B2.x);
  159. int gap = str1 - str2;
  160. if (str2 > str1 || compare(A2, B2))
  161. {
  162. int temp2;
  163. temp = A2;
  164. A2 = B2;
  165. B2 = temp;
  166. temp2 = str1;
  167. str1 = str2;
  168. str2 = temp2;
  169. gap = gap*(-1);
  170. if (A2.x[0] == '+'&&B2.x[0] == '+')temp.x[0] = '-';
  171. else temp.x[0] = '+';
  172. }
  173. else if (A2.x[0] == '-'&&B2.x[0] == '-')temp.x[0] = '-';
  174. else temp.x[0] = '+';
  175. strncpy(temp.x + 1, A2.x + 1, str1);
  176.  
  177. for (int i = gap + 1; i < str1; i++)
  178. {
  179. if (temp.x[i] >= B2.x[i - gap])temp.x[i] = temp.x[i] - B2.x[i - gap] + 48;
  180. else
  181. {
  182. temp.x[i] = temp.x[i] - B2.x[i - gap] + 58;
  183. for (int j = i - 1; j >= 0; j--)
  184. if (temp.x[j] > 48)
  185. {
  186. temp.x[j]--;
  187. for (int k = j + 1; k < i; k++)
  188. {
  189. temp.x[k] = temp.x[k] + 9;
  190. }
  191. break;
  192. }
  193. }
  194. }
  195. return temp;
  196. }
  197. BigNum operator *(BigNum A2, BigNum B2)
  198. {
  199. int str1 = strlen(A2.x), str2 = strlen(B2.x);
  200. int gap = str1 - str2;
  201. static int mul[40001];
  202. BigNum temp;
  203. if (str2 > str1)
  204. {
  205. int temp2;
  206. temp = A2;
  207. A2 = B2;
  208. B2 = temp;
  209. temp2 = str1;
  210. str1 = str2;
  211. str2 = temp2;
  212. gap = gap*(-1);
  213. }
  214. if ((A2.x[0] == '-') ^ (B2.x[0] == '-'))temp.x[0] = '-';
  215. else temp.x[0] = '+';
  216. for (int i = 0; i < 40001; i++)mul[i] = 48;
  217. mul[str1 + str2] = 0;
  218. temp.x[str1 + str2] = 0;
  219. for (int i = 1; i < str1; i++)
  220. for (int j = 1; j < str2; j++)//函數乘法
  221. mul[i + j + 1] = mul[i + j + 1] + (A2.x[i] - 48) * (B2.x[j] - 48);
  222.  
  223. for (int i = str1 + str2 - 1; i >= 1; i--)
  224. if (mul[i + 1] > 57)
  225. {
  226. mul[i] = mul[i] + (mul[i + 1] - 48) / 10;
  227. mul[i + 1] = (mul[i + 1] - 48) % 10 + 48;
  228. }
  229.  
  230.  
  231. for (int i = 1; i < str1 + str2; i++)temp.x[i] = (char)mul[i];
  232.  
  233. return temp;
  234.  
  235. }
  236. BigNum operator /(BigNum A2, BigNum B2)
  237. {
  238. int str1 = strlen(A2.x), str2 = strlen(B2.x);
  239. int gap = str1 - str2;
  240. BigNum temp;
  241. temp.x[1] = '0';
  242. if (str2 > str1 || compare(A2, B2))return temp;
  243. if ((A2.x[0] == '-') ^ (B2.x[0] == '-'))temp.x[0] = '-';
  244. else temp.x[0] = '+';
  245. A2.x[0] = B2.x[0] = '+';
  246. BigNum temp2 = A2;
  247. for (int i = 1; i <= gap; i++)
  248. {
  249. B2.x[str2 + i - 1] = 48;
  250. B2.x[str2 + i] = 0;
  251. }
  252. for (int j = 1; j < gap + 2; j++)
  253. {
  254. if (compare(temp2, B2) == 1)
  255. {
  256. temp.x[j] = 48;
  257. }
  258. else for (int i = 1; i < 10; i++)
  259. {
  260. temp2 = temp2 - B2;
  261. if (compare(temp2, B2) != 1)continue;//1小0大
  262. else
  263. {
  264. temp.x[j] = i + 48;
  265. break;
  266. }
  267. }
  268. B2.x[str1 - j] = 0;
  269. }
  270. return temp;
  271.  
  272. }
  273.  
  274.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:9: warning: #pragma once in main file
 #pragma once
         ^~~~
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty