写完了 超出了我预计时间 6分钟
data.in内容
1 mul 2
4 div 1
5 add 6
8 sub 6
4 mod 3
运行结果
1 * 2
4 / 1
5 + 6
8 - 6
4 % 3
Press any key to continue
代码
#include
#include
#include
main()
{
int i,sel;
char line[100]={0},num1[5]={0},num2[5]={0},op[5]={0},oplist[5][5]={"mul","div","add","sub","mod"};
FILE *fp=fopen("data.in","r");;
if (fp==NULL)
{
printf("Error 1\n");
return 1;
exit(0);
}
while (fgets(line,sizeof(line)-1,fp))
{
sscanf(line,"%[0-9] %s %[0-9]\n",num1,op,num2);
printf("%s ",num1);
sel=-1;
for (i=0;i<5;i++)
{
if (strcmp(oplist[i],op)==0)
{
sel = i;
break;
}
}
switch (sel)
{
case 0:printf("* ");
break;
case 1:printf("/ ");
break;
case 2:printf("+ ");
break;
case 3:printf("- ");
break;
case 4:printf("%% ");
break;
default:
printf("Error 2\n");
return 2;
}
printf("%s\n",num2);
}
}