Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #define N 10
- using namespace std;
- struct Number{
- int num[N];
- int length;
- };
- int init(Number* a) {
- for (int i = 0; i < N; i++)
- a->num[i] = 0;
- }
- void output(Number* a) {
- int i = 0;
- while (i < N && a->num[i] == 0)
- i++;
- if (i == N)
- cout << "0";
- for (i = 0; i < N; i++) {
- cout << a->num[i];
- }
- cout << endl;
- }
- void enter(Number* a, Number* b) {
- string str;
- int length;
- for (int i = 0; i < N; i++) {
- a->num[i] = 0;
- b->num[i] = 0;
- }
- cout << "Enter the sign: " << endl;
- cin >> str;
- a->length = str.length();
- for (int i = 0; i < str.length(); i++) {
- a->num[N - str.length() + i] = str[i] - '0';
- }
- cout << "Enter the sign: " << endl;
- cin >> str;
- b->length = str.length();
- for (int i = 0; i < str.length(); i++) {
- b->num[N - str.length() + i] = str[i] - '0';
- }
- }
- int sum(Number* a, Number* b, Number* c) {
- for (int i = N - 1; i >= 0; i--) {
- c->num[i] = a->num[i] + b->num[i] + c->num[i];
- if (c->num[i] > 9) {
- c->num[i] = c->num[i] - 10;
- c->num[i - 1]++;
- }
- }
- output(c);
- }
- char compare(Number* a, Number* b) {
- int i = N - a->length;
- int j = N - b->length;
- if (i > j)
- return '<';
- else if (i < j)
- return '>';
- else {
- if (a->num[i] > b->num[j])
- return '>';
- else if (a->num[i] < b->num[j])
- return '<';
- else {
- while (true) {
- if (a->num[i] > b->num[j])
- return '>';
- else if (a->num[i] < b->num[j])
- return '<';
- i++;
- j++;
- if (i == N || j == N)
- return '=';
- }
- }
- }
- }
- int minus(Number* a, Number* b, Number* c) {
- for (int i = N - 1; i >= 0; i--) {
- if (a->num[i] < b->num[i]) {
- a->num[i] += 10;
- a->num[i - 1] -= 1;
- }
- if (a->num[i] == b->num[i]) {
- c->num[i] = 0;
- }
- c->num[i] = a->num[i] - b->num[i];
- }
- output(c);
- }
- int main() {
- int i, j;
- Number* a = new Number[N];
- Number* b = new Number[N];
- Number* c = new Number[N];
- init(c);
- enter(a, b);
- char comp = compare(a, b);
- cout << endl << "The 1st number " << comp << endl << endl;
- cout << "The sum = ";
- sum(a, b, c);
- cout << "The subtraction is ";
- if (comp == '>' || comp == '=')
- minus(a, b, c);
- else {
- cout << " - ";
- minus(b, a, c);
- }
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement