1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#include <iostream>
#include <string>
using namespace std;
class Node
{
public:
int val;
Node * next;
Node(){val=0;next=NULL;}
Node(int v){val=v;next=NULL;}
};
void AddHead(Node*& h, int v)
{
Node *a = new Node(v);
a->next = h;
h=a;
}
void Print(Node *h)
{
while(h)
{
cout<<h->val;
h=h->next;
}
}
int Length(Node *h)
{
int i=0;
while(h)
{
i++;
h=h->next;
}
return i;
}
void DeleteHead(Node*&h)
{
if(!h) return;
Node *t = h;
h=h->next;
delete t;
}
void Plus(Node* a, Node* b, Node* c)
{
Node*head=NULL;
int ans=0;
while(a && b)
{
ans = a->val+b->val;
if(!a->next)
{
AddHead(head, ans);
a=a->next;
b=b->next;
break;
}
if(ans>=10)
{
ans=ans-10;
if(a->next) a->next->val++;
}
AddHead(head, ans);
a=a->next;
b=b->next;
}
while(a)
{
AddHead(head, a->val);
a=a->next;
}
while(b)
{
AddHead(head, b->val);
b=b->next;
}
Print(head);
delete a,b,c;
}
void Minus(Node* a, Node* b, Node* c)
{
Node*head=NULL;
int ans=0;
while(a && b)
{
if(a->val-b->val<0)
{
a->val=a->val+10;
a->next->val--;
ans = a->val-b->val;
}
else
{
ans = a->val-b->val;
}
AddHead(head, ans);
a=a->next;
b=b->next;
}
while(a)
{
AddHead(head, a->val);
a=a->next;
}
while(b)
{
AddHead(head, b->val);
b=b->next;
}
Print(head);
delete a,b,c;
}
bool CheckBigger(Node*a, Node* b)
{
if (a->val > b->val)
return true;
else if( a->val < b->val)
return false;
a=a->next;
b=b->next;
}
int main()
{
Node* answer = NULL;
string num1="", num2="";
char ansdo, ansop;
do{
cout<<"Enter the first number:";
cin>>num1;
const char *in = num1.c_str();
int i=0;
Node * head1 = NULL;
while(in[i]!='\0')
{
int value = in[i]-'0';
AddHead(head1, value);
i++;
}
cout<<"Enter the second number:";
cin>>num2;
const char *in2 = num2.c_str();
int j=0;
Node * head2 = NULL;
while(in2[j]!='\0')
{
int value = in2[j]-'0';
AddHead(head2, value);
j++;
}
cout<<"Enter + to add or - to subtract: ";
cin>>ansop;
if ( ansop == '+')
{
Plus(head1,head2,answer);
Print(answer);
cout<<endl;
}
else if (ansop == '-')
{
if (Length(head1)>Length(head2))
{
Minus(head1,head2,answer);
Print(answer);
}
else if (Length(head1)<Length(head2))
{
Minus(head2,head1, answer);
Print(answer);
}
else
if (CheckBigger(head1, head2))
{
Minus(head1,head2,answer);
Print(answer);
}
else
{
Minus(head2,head1, answer);
Print(answer);
}
cout<<endl;
}
else cout<<"Wrong Input!"<<endl;
cout<<"Would you like to repeat the calculation? <Y to repeat>:";
cin>>ansdo;
} while(ansdo == 'y' || ansdo == 'Y');
return 0;
}
|