加减乘除



#include<bits/stdc++.h>
 
using namespace std;
typedef long long int ll;
const int maxn=400+10;
const int char_long=400;
typedef pair<int,int> pii;
int num[maxn];


string add(string x,string y);
string minuse(string x,string y);
string int_string(int s){   //int->string
	if(s==0)return "0";
    int x=s;
    string res;
    s=abs(s);
    while(s){
        res=char((s%10)+'0')+res;
        s/=10;
    }
    if(x<0)return "-"+res;
    return res;
 
}
string max(string s,string s1){
    if(s.size()!=s1.size()){
        if(s.size()>s1.size())return s;
        return s1;
    }
    for(int i=0;i<=s.size()-1;i++){
        if(s[i]!=s1[i]){
            if(s[i]>s1[i])return s;
            return s1;
        }
    }
    return s;
}
string add(string s1,string s2){   //加法 
    memset(num,0,sizeof(num));
    if(s1[0]=='-'&&s2[0]=='-'){
        return "-"+add(string(s1.begin()+1,s1.end()),string(s2.begin()+1,s2.end()));
    }else if(s2[0]=='-')return minuse(s1,string(s2.begin()+1,s2.end()));
    else if(s1[0]=='-')return minuse(s2,string(s1.begin()+1,s1.end()));
    int len1=s1.size(),len2=s2.size();
    for(int i=len1-1,j=0;i>=0;i--,j++){
        num[j]=s1[i]-'0';
    }
    for(int i=len2-1,j=0;i>=0;i--,j++){
        num[j]=num[j]+(s2[i]-'0');
    }
    for(int i=0;i<char_long;i++){
         
        if(num[i]>=10){
            num[i]=num[i]%10;
            num[i+1]++;
        }
    }
    string ans;
    for(int i=char_long-1;i>=0;i--){
        ans+=char(num[i]+'0');
    }
    int tail=0;while(tail<char_long&&ans[tail]=='0')tail++;
    if(tail==char_long)return "0";
    return string(ans.begin()+tail,ans.end());
}
string minuse(string s1,string s2){   //减法 
	if(s1==s2)return "0";
    memset(num,0,sizeof(num));
    int len1=s1.size(),len2=s2.size();
    if(s1[0]=='-'&&s2[0]=='-')
        return minuse(string(s2.begin()+1,s2.end()),string(s1.begin()+1,s1.end()));
    if(s2[0]=='-')return add(s1,string(s2.begin()+1,s2.end()));
    if(s1[0]=='-')return "-"+add(s2,string(s1.begin()+1,s1.end()));
    if(len1<len2||s1<s2&&len1==len2)return "-"+minuse(s2,s1);
    for(int i=len1-1,j=0;i>=0;i--,j++){
        num[j]=s1[i]-'0';
    }
    for(int i=len2-1,j=0;i>=0;i--,j++){
        num[j]=num[j]-(s2[i]-'0');
    }
    for(int i=0;i<char_long;i++){
         
        if(num[i]<0){
            num[i]=10+num[i];
            num[i+1]--;
        }
    }
    string ans;
    for(int i=char_long-1;i>=0;i--){
        ans+=char(num[i]+'0');
    }
    int tail=0;while(tail<char_long&&ans[tail]=='0')tail++;
    if(tail==char_long)return "0";
    return string(ans.begin()+tail,ans.end());
}
int b[maxn],a[maxn];
string mult(string s1,string s2){  //乘法
	if(s1=="0"||s2=="0")return "0";
    if(s1[0]=='-'&&s2[0]=='-')
        return mult(string(s1.begin()+1,s1.end()),string(s2.begin()+1,s2.end()));
    if(s1[0]=='-')
        return "-"+mult(string(s1.begin()+1,s1.end()),s2);
    if(s2[0]=='-')
        return "-"+mult(s1,string(s2.begin()+1,s2.end()));
    memset(b,0,sizeof(b));
    memset(a,0,sizeof(a));
    int len1=s1.size(),len2=s2.size();
    for(int i=len1-1,j=0;i>=0;i--,j++){
        a[j]=s1[i]-'0';
    }
    int k=0;
    for(int w=len2-1;w>=0;w--){
        int x = s2[w]-'0';
        int jin=0;
        for(int i=0,j=k;i<char_long;i++,j++){
            b[j]+=(jin+a[i]*x);
            jin=(jin+a[i]*x)/10;
            b[j]-=10*jin;
        }
        k++;
    }
    int jin=0;
    for(int i=0;i<char_long;i++){
        a[i]=(jin+b[i])%10;
        jin=(jin+b[i])/10;
    }
    string ans;
    for(int i=char_long-1;i>=0;i--){
        ans+=char(a[i]+'0');
    }
    int tail=0;while(tail<char_long&&ans[tail]=='0')tail++;
    if(tail==char_long)return "0";
    return string(ans.begin()+tail,ans.end());
     
}
string pri[10];
pair<int,string> swol(string s){
	for(int i=9;i>=1;i--){
		if(s.size()==pri[i].size()&&s>=pri[i]||s.size()>pri[i].size())return {i,pri[i]};
	}
	return {0,"0"};
}
pair<string,string> chu(string s1,string s2){//除法 (无符号)
	if(s1.size()==s2.size()&&s1<s2||s1.size()<s2.size())return {"0",s1};
	int len = s1.size();
	
	for(int i=1;i<=9;i++){
		pri[i]=mult(s2,int_string(i));
	}
	string lin="0";
	string ans;
	int tail=0;
	while(tail<=len){
		pair<int,string> x = swol(lin);
			
		lin = minuse(lin,x.second);
			
		ans+=int_string(x.first);
			
		if(tail==len)break;
		string sw="";sw+=s1[tail++];
		lin = add(mult(lin,"10"),sw);
		
	}
	tail=0;while(tail<ans.size()&&ans[tail]=='0')tail++;
	if(tail==ans.size())return {"0",lin};
    ans = string(ans.begin()+tail,ans.end());
	return {ans,lin};
}

int main(){
    ios::sync_with_stdio;
	int t;cin>>t;
	while(t--){
		string s1,s2;cin>>s1>>s2;
		cout<<add(s1,s2)<<endl;
		cout<<minuse(s1,s2)<<endl;
		cout<<mult(s1,s2)<<endl;
	}
    
	return 0;
}


全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务