天梯赛——正整数A+B

题目链接:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<sstream>//头文件
using namespace std;
bool num(string s)
{
    for(int i=0;i<s.length();i++)
        if(s[i]<'0'||s[i]>'9')
            return 0;
        return 1;
}
int main()
{
    string s1,s2,s11,s22;
    int a,b;
    stringstream ss1,ss2;
    cin>>s1;
    getchar();
    getline(cin,s2);
    s11=s1;
    s22=s2;
    if(num(s1)==1&&num(s2)==1)
    {
        ss1<<s1;
        ss1>>a;
        ss2<<s2;
        ss2>>b;//字符串转化为整数;
        if(a>1000||a<1)
            s11="?";
        if(b>1000||b<1)
            s22="?";
        cout<<s11<<" + "<<s22;
        if(s11=="?"||s22=="?")
            cout<<" = "<<'?'<<endl;
        else
            cout<<" = "<<a+b;
    }
    if(num(s1)==0)
        s1="?";
    if(num(s2)==0)
        s2="?";
    if(s1=="?"||s2=="?")
            cout<<s1<<" + "<<s2<<" = "<<'?'<<endl;
    return 0;
}

学到一个新的东西

sstream

stringstream把字符串转为整数

猜你喜欢

转载自blog.csdn.net/qq_42232118/article/details/81751184
今日推荐