Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5. class BigInt
  6. {
  7. private:
  8. int *digits = new int[10];
  9. int amount;
  10. string zn;
  11. public:
  12. void input(){
  13. string num, zn;
  14. cout << "Введите знак числа" << endl;
  15. cin >> zn;
  16. cout << "Введите число" << endl;
  17. cin >> num;
  18. input(num, zn);;
  19. }
  20. string getZn(){
  21. return zn;
  22. }
  23. void input(string s, string zn1)
  24. {
  25. string str = s;
  26. zn = zn1;
  27. int pos = 0;
  28. for (int i = str.size() - 1; i >= 0; i--)
  29. digits[pos++] = str[i] - '0';
  30. amount = str.size();
  31. }
  32. BigInt(int num1) {
  33. string num = "";
  34. if (num1 < 0) zn = "-";
  35. else zn = "+";
  36. num = num1;
  37. input(num, zn);
  38. }
  39. BigInt() {
  40. string num = "57";
  41. input(num, "+");
  42. }
  43. BigInt(string s, string zn1) {
  44. input(s, zn1);
  45. }
  46. void output()
  47. {
  48. cout << zn;
  49. for (int i = amount - 1; i >= 0; i--)
  50. cout << digits[i];
  51. }
  52. friend bool operator > (BigInt &a, BigInt &b) {
  53. if(a.zn=="-" && b.zn=="-"){
  54. if (a.amount != b.amount)
  55. return b.amount>b.amount;
  56. for (int i = a.amount - 1; i >= 0; i--)
  57. {
  58. if (a.digits[i] != b.digits[i])
  59. return b.digits[i]>a.digits[i];
  60. }
  61. return false;
  62. }
  63. else{
  64. if (a.amount != b.amount)
  65. return a.amount>b.amount;
  66. for (int i = a.amount - 1; i >= 0; i--)
  67. {
  68. if (a.digits[i] != b.digits[i])
  69. return a.digits[i]>b.digits[i];
  70. }
  71. return false;
  72. }
  73. }
  74. friend bool operator < (BigInt &a, BigInt &b) {
  75. if(a.zn=="-" && b.zn=="-"){
  76. if (a.amount != b.amount) return b.amount<a.amount;
  77. for (int i = a.amount - 1; i >= 0; i--)
  78. {
  79. if (a.digits[i] != b.digits[i])
  80. return b.digits[i]<a.digits[i];
  81. }
  82. return false;
  83. }
  84. else{
  85. if (a.amount != b.amount) return a.amount<b.amount;
  86. for (int i = a.amount - 1; i >= 0; i--)
  87. {
  88. if (a.digits[i] != b.digits[i])
  89. return a.digits[i]<b.digits[i];
  90. }
  91. return false;
  92. }
  93. }
  94.  
  95.  
  96. BigInt plus(BigInt &a, BigInt &b)
  97. {
  98. BigInt res;
  99. res.amount = max(a.amount, b.amount);
  100. int r = 0;
  101. for (int i = 0; i < res.amount; i++)
  102. {
  103. if (i >= min(a.amount, b.amount)) {
  104. if (res.amount == a.amount) b.digits[i] = 0;
  105. else a.digits[i] = 0;
  106. }
  107. res.digits[i] = a.digits[i] + b.digits[i] + r;
  108. if (res.digits[i] >= 10)
  109. {
  110. res.digits[i] -= 10;
  111. r = 1;
  112. }
  113. else
  114. r = 0;
  115. if (i == res.amount - 1)
  116. {
  117. i++;
  118. res.digits[i] = r;
  119. }
  120. }
  121. if (res.digits[res.amount])
  122. res.amount++;
  123. if (a.zn == b.zn && (a.zn == "+")) res.zn = "+";
  124. else if (a.zn == b.zn && (a.zn == "-")) res.zn = "-";
  125. else res.zn = "+";
  126. return res;
  127. }
  128. BigInt minus(BigInt &a, BigInt &b)
  129. {
  130. BigInt res;
  131. bool isMinus = false;
  132. if (a < b) {
  133. isMinus = true;
  134. }
  135. if (max(a.amount, b.amount) == a.amount && b < a) {
  136. res = a;
  137. }
  138. else {
  139. res = b;
  140. swap(a, b);
  141. }
  142. if (a.zn == "-" && b.zn == "-" && b < a) isMinus = false;
  143. if (isMinus) res.zn = "-";
  144. else res.zn = "+";
  145. int r = 0;
  146. for (int i = 0; i < res.amount; i++)
  147. {
  148. if (i >= min(a.amount, b.amount)) {
  149. if (res.amount == a.amount) b.digits[i] = 0;
  150. else a.digits[i] = 0;
  151. }
  152. res.digits[i] -= b.digits[i] + r;
  153. if (res.digits[i] < 0)
  154. {
  155. res.digits[i] += 10;
  156. res.digits[i + 1]--;
  157. }
  158. }
  159. int pos = res.amount;
  160. while (pos && !res.digits[pos])
  161. pos--;
  162. res.amount = pos + 1;
  163. res.amount--;
  164. return res;
  165. }
  166. friend void operator++(BigInt &a){
  167. if (a.zn == "+") a.zn = "-";
  168. else a.zn = "+";
  169. }
  170.  
  171. friend BigInt operator+(BigInt &a, BigInt &b){
  172. if (a.zn == b.zn && (a.zn == "+" || a.zn == "-")) {
  173. return a.plus(a, b);
  174. }
  175. else if (a.zn == "+" && b.zn == "-") {
  176. b.zn = "+";
  177. return a.minus(a, b);
  178. }
  179. else if (a.zn == "-" && b.zn == "+") {
  180. a.zn="+";
  181. return a.minus(b, a);
  182. }
  183. }
  184. friend BigInt operator-(BigInt &a, BigInt &b)
  185. {
  186. if (a.zn == b.zn && a.zn == "+") {
  187. return a.minus(a, b);
  188. }
  189. else if (a.zn == "+" && b.zn == "-") {
  190. return a.plus(a, b);
  191. }
  192. else if (a.zn == "-" && b.zn == "+") {
  193. return a.minus(a, b);
  194. }
  195. else if (a.zn == "-" && b.zn == "-") {
  196. b.zn = "+";
  197. return a.minus(b, a);
  198. }
  199. }
  200. friend BigInt operator*(BigInt &a, BigInt &b)
  201. {
  202. BigInt res;
  203. int temp1 = 10;
  204. if (a < b) swap(a, b);
  205.  
  206. BigInt temp = a;
  207. for (int j = 0; j < b.amount; j++){
  208. for (int i = 0; i < b.digits[j] - 1; i++){
  209. res = a.plus(a, temp);
  210. temp = res;
  211. }
  212. b.digits[j + 1] *= temp1;
  213. temp1 *= 10;
  214. }
  215.  
  216. if (a.zn == b.zn) res.zn = "+";
  217. else res.zn = "-";
  218. return res;
  219. }
  220. friend BigInt operator/(BigInt &a, BigInt &b)
  221. {
  222. string zn1 = "+";
  223. if (a.zn != b.zn) zn1 = "-";
  224. BigInt res("25", "+"), res1("1", "+");
  225. int temp1 = 0;
  226. int k = 0;
  227. if (a < b) swap(a, b);
  228. BigInt temp = a;
  229. while (res > res1){
  230. int f = 0;
  231. res = a.minus(temp, b);
  232. temp = res;
  233. temp1++;
  234. for (int i = 0; i < res.amount; i++){
  235. if (res.digits[i] == 0) f++;
  236. }
  237. if (f == res.amount) break;
  238. }
  239. if (a.zn == b.zn) cout << zn1 << temp1 << endl;
  240. else cout << zn1 << temp1 << endl;
  241. return res;
  242. }
  243.  
  244. };
  245.  
  246. int main()
  247. {
  248. setlocale(LC_ALL, "Russian");
  249. BigInt bg("50", "+"), bg1("5", "+"), bg2;
  250. cout << "Введите 1 для суммы, 2-разности, 3-умножения, 4-деления, 5-сравнения, 6-смены знака, 0-выход" << endl;
  251. int i;
  252. cin >> i;
  253. while (i != 0){
  254. if (i == 1){
  255. BigInt bg;
  256. BigInt bg1;
  257. BigInt bg2;
  258. bg.input();
  259. bg1.input();
  260. cout << "Сумма: ";
  261. bg2 = bg + bg1;
  262. bg2.output();
  263. cout << endl;
  264. }
  265. if (i == 2){
  266. BigInt bg;
  267. BigInt bg1;
  268. BigInt bg2;
  269. bg.input();
  270. bg1.input();
  271. cout << "Разность: ";
  272. bg2 = bg - bg1;
  273. bg2.output();
  274. cout << endl;
  275. }
  276. if (i == 3){
  277. BigInt bg;
  278. BigInt bg1;
  279. BigInt bg2;
  280. bg.input();
  281. bg1.input();
  282. cout << "Умножение: ";
  283. bg2 = bg*bg1;
  284. bg2.output();
  285. cout << endl;
  286. }
  287. if (i == 4){
  288. BigInt bg;
  289. BigInt bg1;
  290. BigInt bg2;
  291. bg.input();
  292. bg1.input();
  293. cout << "Деление: ";
  294. bg2 = bg / bg1;
  295. }
  296. if (i == 5){
  297. BigInt bg;
  298. BigInt bg1;
  299. bg.input();
  300. bg1.input();
  301. cout << "Сравнение: ";
  302. ++bg;
  303. if ((bg < bg1) == true) cout << "Первое число больше";
  304. else cout << "Второе число больше";
  305. }
  306. if (i == 6){
  307. BigInt bg;
  308. bg.input();
  309. cout << "Смена знака: ";
  310. ++bg;
  311. bg.output();
  312. }
  313. cin >> i;
  314. }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement