描述
对于给定的算术表达式,按规则输出计算结果,仅包含加法和大小判断。
输入
一行字符串,为加号、大于、小于( + < > ) 连接的两个不限大小的非负整数。
输出
当符号为 + 时, 计算两个数相加的和, 并以字符串格式返回; 当符号为 < 时, 如果左数小于右数, 返回大写字母字符 Y, 否则返回大写字母字符 N; 当符号为 > 时, 如果左数大于右数, 返回大写字母字符 Y, 否则返回大写字母字符 N。
!!!请同学们尽量使用算法来解决这个问题
输入样例
972919822976663297>74058 875098336507333719633571722631534917759993913379786689>53558270653237768027942884431075534537929401567824882097903948774409200 7625022925148127196027859399571498914361+790786706794530
输出样例
Y N 7625022925148127196027860190358205708891
AC代码:
#include <bits/stdc++.h> #include<vector> #include<stack> #include<sstream> using namespace std; string func1(string s1,string s2) //大于号 { if(s1.length()>s2.length()) { return "Y"; } else if(s1.length()<s2.length()) { return "N"; } else if(s1 == s2) { return "N"; } else { int i = 0; while(i!=s1.length()) { if((s1[i]-'0') > (s2[i]-'0')) { return "Y"; } else if((s1[i]-'0') < (s2[i]-'0')) { return "N"; } i++; } } } string func2(string s1,string s2) //小于号 { if(s1.length()>s2.length()) { return "N"; } else if(s1.length()<s2.length()) { return "Y"; } else if(s1 == s2) { return "N"; } else { int i = 0; while(i!=s1.length()) { if((s1[i]-'0') > (s2[i]-'0')) { return "N"; } else if((s1[i]-'0') < (s2[i]-'0')) { return "Y"; } i++; } } } string func3(string s1,string s2) //加号 { char temp; string result =""; bool flag = false;//判断是否有进位 //已经默认s1>=s2 while(s1.length()!=s2.length()){ s2 = "0" + s2; } int i = s1.length()-1; while(i>=0) { stringstream ss; ss.clear(); string temp = ""; if(flag == false) { if((s1[i]-'0') + (s2[i]-'0') > 9) { flag = true; ss.clear(); ss<<(((s1[i]-'0') + (s2[i]-'0'))-10); ss>>temp; result =temp + result; } else { flag = false; ss.clear(); ss<<((s1[i]-'0') + (s2[i]-'0')); ss>>temp; result = temp + result; } } else { if((s1[i]-'0') + (s2[i]-'0')+1 > 9) { flag = true; ss.clear(); ss<<(((s1[i]-'0') + (s2[i]-'0')+1)-10); ss>>temp; result = temp+ result; } else { flag = false; ss.clear(); ss<<((s1[i]-'0') + (s2[i]-'0')+1); ss>>temp; result = temp + result; } } i--; } if(flag == true) { result = "1" + result; } return result; } int main() { string s=""; while(cin>>s) { int mode = 0; string s1 = ""; string s2 = ""; for(int i = 0; i<s.length(); i++) { if(s[i] == '>' || s[i] =='<' ||s[i] =='+') { s1 = s.substr(0,i); s2 = s.substr(i+1,s.length()-i-1); if(s[i] == '>') { mode = 1; } else if(s[i] == '<') { mode = 2; } else if(s[i] == '+') { mode = 3; } break; } } if(mode == 1) { cout<<func1(s1,s2)<<endl; } else if(mode == 2) { cout<<func2(s1,s2)<<endl; } else if(mode == 3) { if(func1(s1,s2) == "Y") { cout<<func3(s1,s2)<<endl; } else { cout<<func3(s2,s1)<<endl; } } } }