abc_mx @ 2022-08-03 17:42:04
#include<bits/stdc++.h>
using namespace std;
int n,aa,bb,cc;
char a[1001],ans[1001],las;
string s;
int main(){
cin>>n;
while(n--){
fgets(s,sizeof(s),stdin);
if(s[0]=='0'||s[0]=='1'||s[0]=='2') las=s[0],s[0]=' ';
sscanf(s,"%d,%d",&aa,&bb);
switch(las){
case 'a': cc=aa+bb; sprintf(ans,"%d+%d=%d",aa,bb,cc);
case 'b': cc=aa-bb; sprintf(ans,"%d-%d=%d",aa,bb,cc);
case 'c': cc=aa*bb; sprintf(ans,"%d*%d=%d",aa,bb,cc);
}
printf("%s\n%d\n",ans,strlen(ans));
}
return 0;
}
by abc_mx @ 2022-08-03 17:42:33
就fgets函数老是报错
by Hisaishi_Kanade @ 2022-08-03 17:46:02
@abc_mx fgets
第二个参数要的是最大长度
by xyf007 @ 2022-08-03 17:57:40
@abc_mx 你不应该用 fgets
来读入 std::string
,可以考虑 std::getline
。
by xyf007 @ 2022-08-03 17:57:54
@bye_wjx ……?
by abc_mx @ 2022-08-03 18:01:20
@bye_wjx sizeof函数不就是获得了长度吗,要用strlen?
by abc_mx @ 2022-08-03 18:02:18
@xyf007 能讲一下具体的代码实现吗
by Hisaishi_Kanade @ 2022-08-03 18:02:59
哪怕你用 char
,你这个也不是很合理
by Hisaishi_Kanade @ 2022-08-03 18:03:24
fgets(s,1000,stdin);
by abc_mx @ 2022-08-03 18:11:28
改了之后输出都是0。。。。能帮我看一下问题吗
by xyf007 @ 2022-08-03 20:04:41
@abc_mx std::getline(std::cin, s)