关于sscanf的提问

P1957 口算练习题

ww4445 @ 2020-12-18 18:51:24

emmmm是我的用法不对吗。。。一直处于编译失败的状态

#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<cstdio>
using namespace std;
int main()
{
    int i1,s2,s3;
    string str1,str2,str3,len,str0;
    char ch1;
    vector<string> ans;
    cin>>i1;
    for(int i=1;i<=i1;i++)
    {
        cin>>str1;
        if(isalpha(str1[0]))
        {
            ch1=str1[0];
            cin>>str2>>str3; 
        }
        else
    {
        str2=str1;
        cin>>str3;
    }
        sscanf(str2,"%d",&s2);
        sscanf(str3,"%d",&s3);
        if(ch1=='a')
            sprintf(str0,"%d+%d=%d",s2,s3,s2+s3);
        if(ch1=='b')
            sprintf(str0,"%d-%d=%d",s2,s3,s2-s3);
        if(ch1=='c')
            sprintf(str0,"%d*%d=%d",s2,s3,s2*s3);
        sprintf(len,"%d",strlen(str0));
        ans.push_back(str0);
        ans.push_back(len);
    }
    return 0;
}

by _caiji_ @ 2020-12-18 18:54:27

sscanf不认识c++的string,它只认识c的char[]


by Baiwhiter @ 2020-12-18 19:28:58


by ww4445 @ 2020-12-18 19:56:38

@caijianhong 啊!谢谢,终于解决了!反反复复改了好久,没想到问题出在这


by ww4445 @ 2020-12-18 20:01:03

@Baiwhiter 谢谢!已经解决了,使用vector是想要练习一下,但是输入一行后直接输出该行结果而不是统一输出,不会影响吗?


by Baiwhiter @ 2020-12-18 20:05:02

@ww4445 不会的QuQ


|