[C++] syntaxhighlighter_viewsource syntaxhighlighter_copycode
#include<iostream>
#include<string>
#include<stdlib.h>
#include <memory.h>
using namespace std;
#define N 3
class add{
public:
add(string y="N",string x="0",int z=0);
int add_fun();
int stradd_fun(string );
int strmul_fun();
string A_stradd_fun(string ,string);
int abc_fun(int );
int judgement();
string retrun_b(){return b;};
void display();
private:
string a;
string b;
string a1;
int c;
};
add::add(string x,string y,int z){
b=x;
a=x;
c=z;
a1=b;
}
int add::strmul_fun()
{
int v1,a2,length_a,length_b;
int c=0,i,d,d1,time;
string x1,x2="";
length_b=b.length();
length_a=a.length();
for(d=0;d<length_b;d++)
{
d1=d;
x1=a;
v1=b[length_b-1-d]-48; //get the last element
time=length_a-1;
for(i=0;i<length_a;i++)
{
x1[time]=a[time]-'0'; //conversion the date
if(c>0)
{
x1[time]=v1*x1[time]+c;
c=0;
}
else
{
x1[time]=v1*x1[time];
}
c=x1[time]/10; //get the carried date c
x1[time]=x1[time]-c*10;
x1[time]=x1[time]+48; //conversion the date to char
time--;
}
if(c>0){x1.insert(0,c+48);c=0;}
for(d1--;d1>=0;d1--)
{
a2=x1.length();
x1.insert(a2,"0");
}
if(d>0){x2=A_stradd_fun(x2,x1);}
else{x2=x1;}
}
a=x2;
return 0;
}
string add::A_stradd_fun(string x,string y)
{
int i,x_last,y_last,length;
x_last=x.length()-1;
y_last=y.length()-1;
c=0;
for(i=0;i<x.length();i++)
{
if(c==1){y[y_last]=y[y_last]+x[x_last]+1-48;} //有进位的情况
else{y[y_last]=y[y_last]+x[x_last]-48;}
c=0;
if(y[y_last]>57)
{
y[y_last]-=10;
c=1;
}
x_last--;
y_last--;
}
if(x_last==-1&&c==1)
{
length=y.length()-x.length(); //判断加数最高位给被加数进位的影响
if(length==0){y.insert(0,"1");c=0;} //如果加数和被加数位数相等 则执行
else
{
length--;
for(;c==1&&length>=0;length--)
{
c=0; //进位标志设0
y[length]++;
if(y[length]>57)
{
y[length]-=10;
c=1; //有进位
}
}
if(length<0&&c==1){y.insert(0,"1");c=0;}
}
}
return y;
}
void add::display(){
cout<<"结果为: "<<a<<endl;
}
int main()
{
int n=0,i,u;
int n1=0,n2=0;
string c,b;
char p[255];
cout<<"基数"<<endl;
cin >> c;
cout<<"指数"<<endl;
cin>>b;
strcpy(p,b.c_str());
u=atoi(p);
add a(c);
for(i=0;i<u-1;i++)
{
a.strmul_fun();
}
a.display();
return 0;
}