36进制加法-c++

114 阅读1分钟

36进制加法实现-c++

#include<iostream>
#include<algorithm>
using namespace std;
int getInt(char a){
	if(a>='0' && a<='9'){
		return a-'0';
	}else{
		return a-'a'+10;
	}
}
char getChar(int a){
	if(a>=10){
		return a+'a'-10;
		
	}else{
		return a+'0';
	}
}
int main(){
	string s1,s2;
	cin >> s1;
	cin >> s2;
	
	int i = s1.size()-1,j = s2.size()-1;
	int jinwei = 0;
	string res = "";
	int zhjinwei = 0;
	while(i>=0 && j>=0){
		int a1 = getInt(s1[i]);
		int a2 = getInt(s2[j]);
		int t = a1+a2+jinwei;
		//jinwei = 0;
		if(t>=36){
			jinwei = t/36;
			t = t%36;
		}
		
		char tt = getChar(t);
		res += tt;
		
		i--;
		j--;
		zhjinwei = jinwei;
		jinwei = 0;
	}
	while(i>=0){
		int a1 = getInt(s1[i]);
		int t = a1+zhjinwei;
		if(t>=36){
			zhjinwei = t/36;
			t = t%36;
		}
		
		char tt = getChar(t);
		res += tt;
		
		i--;
		zhjinwei = 0;
	}
	while(j>=0){
		int a2 = getInt(s2[j]);
		int t = a2+zhjinwei;
		if(t>=36){
			zhjinwei = t/36;
			t = t%36;
		}
		
		char tt = getChar(t);
		res += tt;
		
		j--;
		zhjinwei = 0;
	}
	reverse(res.begin(),res.end());
	cout << res <<endl;
	
	return 0;
}