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
|
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<math.h>
#include<string.h>
class natural{
public :
char x[10000];
friend natural operator+(natural,natural);
friend natural operator+(natural,unsigned int);
friend natural operator-(natural,natural);
friend natural operator-(natural,unsigned int);
friend natural operator*(natural,natural);
friend natural operator/(natural,natural);
friend int operator==(natural,natural);
natural operator=(natural);
friend ostream& operator << (ostream& os,natural z);
friend istream& operator >> (istream& is,natural z); };
natural operator+(natural a,natural b){
natural c;
int o[1000],m[1000],n[1000],l,l1,lt,d,r,i;
for(i=0;i<strlen(a.x);i++)
m[i]=a.x[i]-48;
for(i=0;i<strlen(b.x);i++)
n[i]=b.x[i]-48; l=strlen(a.x); l1=strlen(b.x);
if(l>l1) {lt=l; d=l-l1;
for(i=l1-1;i>=0;i--)
n[i+d]=n[i];
for(i=0;i<d;i++)
n[i]=0; }
else {lt=l1; d=l1-l;
for(i=l-1;i>=0;i--)
m[i+d]=m[i];
for(i=0;i<d;i++)
m[i]=0; }
r=0;
for(i=lt-1;i>-1;i--)
{o[i]=(m[i]+n[i]+r)%10; r=(m[i]+n[i])/10;
}
for(i=0;i<lt;i++)
c.x[i]=o[i]+48;
return c;}
natural operator+(natural a,unsigned int b){
natural c;
int o[1000],m[1000],n[1000],l1,l2,lt,d,r,i;
for(i=0;i<strlen(a.x);i++)
m[i]=a.x[i]-48;
lt=l2=strlen(a.x);
l2--;
while(b!=0)
{n[l2]=b%10;
b=b/10;l2--; }
for(i=0;i<=l2;i++)
n[i]=0;
for(i=0;i<lt;i++)
cout<<n[i];
cout<<endl;
r=0;
for(i=lt-1;i>-1;i--)
{o[i]=(m[i]+n[i]+r)%10; r=(m[i]+n[i])/10;
}
for(i=0;i<lt;i++)
c.x[i]=o[i]+48;
return c;}
natural operator-(natural a,natural b){
natural c;
int o[1000],m[1000],n[1000],l,l1,lt,d,r,i;
for(i=0;i<strlen(a.x);i++)
m[i]=a.x[i]-48;
for(i=0;i<strlen(b.x);i++)
n[i]=b.x[i]-48; l=strlen(a.x); l1=strlen(b.x);
if(l>l1) {lt=l; d=l-l1;
for(i=l1-1;i>=0;i--)
n[i+d]=n[i];
for(i=0;i<d;i++)
n[i]=0; }
else {lt=l1; d=l1-l;
for(i=l-1;i>=0;i--)
m[i+d]=m[i];
for(i=0;i<d;i++)
m[i]=0; }
r=0;
for(i=lt-1;i>-1;i--)
{if((m[i]>n[i]) ||(m[i]==n[i])) o[i]=m[i]-n[i];
else {o[i]=m[i]+10-n[i]; r=i; m[r-1]--; }
}
for(i=0;i<lt;i++)
c.x[i]=o[i]+48;
return c;}
natural operator-(natural a,int b){
natural c;
int o[1000],m[1000],n[1000],l,l1,lt,d,r,i;
for(i=0;i<strlen(a.x);i++)
m[i]=a.x[i]-48;
lt=l1=strlen(a.x);
l1--;
while(b!=0)
{n[l1]=b%10;
b=b/10;l1--; }
for(i=0;i<=l1;i++)
n[i]=0;
for(i=0;i<lt;i++)
cout<<n[i];cout<<endl;
r=0;
for(i=lt-1;i>-1;i--)
{if((m[i]>n[i]) ||(m[i]==n[i])) o[i]=m[i]-n[i];
else {o[i]=m[i]+10-n[i]; r=i; m[r-1]--; }
}
for(i=0;i<lt;i++)
c.x[i]=o[i]+48;
return c;}
int operator==(natural a,natural b) { int ok;ok=1;
if(strlen(a.x)!=strlen(b.x)) ok=0;
else {for(int i=0;i<strlen(a.x);i++)
if(a.x[i]!=b.x[i])
ok=0; }
if (ok!=1) return 0;
else return 1;
}
ostream& operator << (ostream& os,natural z){
for(int i=0;i<strlen(z.x);i++)
os<<z.x[i];
return os;}
istream& operator >> (istream& is,natural& z){
is>>z.x;
return is;}
main ()
{clrscr();
natural w,y;
unsigned int t;
char f;
do{
cout<<"1-natural+natural"<<endl;
cout<<"2-natural+int"<<endl;
cout<<"3-natural-natural"<<endl;
cout<<"4-natural-int"<<endl;
cout<<"5-natural*natural"<<endl;
cout<<"6-natural/natural"<<endl;
cout<<"0-exit"<<endl;
cin>>f;
switch(f)
{
case '1': cout<<"---------natural+natural--------"<<endl;
cout<<"introdu valoarea numarului natural"<<"\n";
cin>>w.x;
cin>>y.x;
cout<<"valorile introduse:"<<"\n";
cout<<"w="<<w<<"\n";
cout<<"y="<<y<<"\n";
cout<<w+y<<endl;
if(w==y)
cout<<"numerele sint egale";
else cout<<"numerele sint diferite"; cout<<endl;
break;
case '2': cout<<"--------natural + int------"<<endl;
cout<<"dati un nr natural:";
cin>>w.x;
cout<<"dati un nr int:";
cin>>t;
cout<<w+t<<endl; break;
case '3':cout<<"-----------natural-natural------------"<<endl;
cout<<w-y<<endl; break;
default : cout<<"operatie necunoscuta:";cout<<endl;}}
while(f!='0');
}
|