EkSulfur @ 2023-12-14 08:44:06
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char s[10], c;
int calculate(int x, int y, char *a) {
switch (*a) {
case 'a': {
*a = '+';
return x + y;
break;
}
case 'b': {
*a = '-';
return x - y;
break;
}
case 'c': {
*a = '*';
return x * y;
break;
}
}
printf("ERR\n");
return 0;
}
int main(void) {
int n;
scanf("%d", &n);
char m[n];
int a[n][3];
for (int i = 0; i < n; i++) {
m[i] = s[0];
scanf("%s", &s);
if ((int)strlen(s) > 1) {
a[i][0] = atoi(s);
scanf("%d", &a[i][1]);
a[i][2] = calculate(a[i][0], a[i][1], &m[i]);
} else {
m[i] = s[0];
scanf("%d%d", &a[i][0], &a[i][1]);
a[i][2] = calculate(a[i][0], a[i][1], &m[i]);
}
}
for (int i = 0; i < n; i++) {
char ans[50];
sprintf(ans, "%d%c%d=%d", a[i][0], m[i], a[i][1], a[i][2]);
printf("%s\n%d\n", ans, (int)(strlen(ans)));
}
return 0;
}
by conancat @ 2023-12-14 11:24:14
#include<bits/stdc++.h>
using namespace std;
int main (){
int n;
cin>>n;//输入n
char ch;//要输入的字母
string s[n];//存放最后的表达式
for(int i=0;i<n;i++){
string st;
stringstream io;
int a,b,ans=0;//ans是答案
if(!(cin>>a))//利用了cin的性质
{
cin.clear();
cin>>ch>>a;
}
cin>>b;
io<<a;io>>s[i];io.clear();//接下来对ch进行判断
if(ch=='a')
{
ans=a+b;
s[i]+='+';
io<<b;io>>st;io.clear();//io每次用完后要记得清空哦
s[i]+=st;
s[i]+='=';
io<<ans;io>>st;io.clear();
s[i]+=st;
}//以此类推
if(ch=='b')
{
ans=a-b;
s[i]+='-';
io<<b;io>>st;io.clear();
s[i]+=st;
s[i]+='=';
io<<ans;io>>st;io.clear();
s[i]+=st;
}
if(ch=='c')
{
ans=a*b;
s[i]+='*';
io<<b;io>>st;io.clear();
s[i]+=st;
s[i]+='=';
io<<ans;io>>st;io.clear();
s[i]+=st;
}
}
for(int i=0;i<n;i++)//循环输出结果
{
cout<<s[i]<<endl;
cout<<s[i].size();
if(i!=n-1)cout<<endl;
}
}