huoyi7895123654 @ 2023-09-01 17:26:22
#include <stdio.h>
#include <malloc.h>
#include <string.h>
int howManySpace(const char *);
void compute(int position,char** question,int * mode);
const char plus [50]="%d+%d=%d";
const char multiply[50]="%d*%d=%d";
const char minus[50]="%d-%d=%d";
enum{Plus,Multiply,Minus};
int main()
{
int num;
scanf("%d",&num);
getchar();
char ** question = (char **)malloc(num*sizeof(char *));
for(int i=0;i<num;i++)
{
question[i]=(char *)malloc(50*sizeof(char));
fgets(question[i], 50, stdin);
}
int mode;
for(int i=0;i<num-1;i++)
{
compute(i, question, &mode);
printf("\n");
}
compute(num-1, question, &mode);
for(int i=0;i<num;i++)
{
free(question[i]);
}
free(question);
return 0;
}
int howManySpace(const char * source)
{
int ans=0;
for(int i=0;source[i]!='\0';i++)
{
if(source[i]==' ')
{
ans++;
}
}
return ans;
}
void compute(int position,char** question,int * mode)
{
if(howManySpace(question[position])==2)
{
char m;int a;int b;
sscanf(question[position], "%c%d%d",&m,&a,&b);
if(m=='a')
{
*mode=Plus;
char answer[50];
sprintf(answer, plus,a,b,a+b);
printf("%s\n%d",answer,(int)strlen(answer));
}
else if (m=='b') {
*mode=Minus;
char answer[50];
sprintf(answer, minus,a,b,a-b);
printf("%s\n%d",answer,(int)strlen(answer));
}
else {
*mode=Multiply;
char answer[50];
sprintf(answer, multiply,a,b,a*b);
printf("%s\n%d",answer,(int)strlen(answer));
}
}
else {
int a, b;
sscanf(question[position], "%d%d",&a,&b);
char answer[50];
if(*mode==Plus)
{
char answer[50];
sprintf(answer, plus,a,b,a+b);
printf("%s\n%d",answer,(int)strlen(answer));
}
else if (*mode==Minus) {
char answer[50];
sprintf(answer, minus,a,b,a-b);
printf("%s\n%d",answer,(int)strlen(answer));
}
else {
char answer[50];
sprintf(answer, multiply,a,b,a*b);
printf("%s\n%d",answer,(int)strlen(answer));
}
}
}
不知为何,本地跑可以通过,线上却不行
by huoyi7895123654 @ 2023-09-07 11:34:59
破案了,是getchar出错了