求c语言大神帮忙解决。

是这样的,想请分析下,求c语言大神帮忙解决。
最新回答
流连染紫旳悯〆

2024-12-01 00:41:10

#include <stdio.h>
#include <stdlib.h>
#include "string.h"

void process(char* s,char* ref)
{
int p,i,j=0,f=0;
char a[10]={"\0"};
char b[10]={"\0"};
int len=strlen(s);
for(i=0;i<len;i++)
{
if(s[i]>'0' && s[i]<'9')
{
if(f==0)
{
a[j++]=s[i];
}
else if(f!=0)
{
b[j++]=s[i];
}
}
else if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/')
{
j=0;
switch(s[i])
{
case '+':
f=1;
break;
case '-':
f=2;
break;
case '*':
f=3;
break;
case '/':
f=4;
break;
}
}
}
switch(f)
{
case 1:
sprintf(ref,"%s=%d",s,atoi(a)+atoi(b));
break;
case 2:
sprintf(ref,"%s=%d",s,atoi(a)-atoi(b));
break;
case 3:
sprintf(ref,"%s=%d",s,atoi(a)*atoi(b));
break;
case 4:
if(atoi(b)==0)
{
sprintf(ref,"错误,除数不能为0");
break;
}
sprintf(ref,"%s=%d",s,atoi(a)/atoi(b));
break;
default:
sprintf(ref,"输入错误,请重新输入");
}
}

int main(int argc, char *argv[]) 
{
char s[20]={"\0"};
char* ref=(char*)malloc(sizeof(char)*40);
printf("输入end结束程序\n");
while(1)
{
printf("\n");
memset(s,'\0',20);
scanf("%s",s);
if(strcmp(s,"end")==0)
{
break;
}
process(s,ref);
if(strcmp(ref,"error")==0)
{
continue;
}

printf("\n%s\n-------------------\n",ref);
}
return 0;
}
风铃鹿

2024-12-01 00:31:43

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
char str[16];
char buf[8];
char *p;
int x,y,z;
printf("please enter:\n");
while(1)
{
bzero(str,16);
scanf("%s",str);
p = strstr(str,"+");
if(p)
{
bzero(buf,8);
strncpy(buf,str,strlen(str)-strlen(p));
x = atoi(buf);
bzero(buf,8);
strncpy(buf,p+1,strlen(p)-1);
y = atoi(buf);
z = x+y;
printf("%d+%d=%d\n",x,y,z);
}
else
{
p = strstr(str,"-");
if(p)
{
bzero(buf,8);                
                         strncpy(buf,str,strlen(str)-strlen(p));
                         x = atoi(buf);
                         bzero(buf,8);
                         strncpy(buf,p+1,strlen(p)-1);
                         y = atoi(buf);
z = x-y;
printf("%d-%d=%d\n",x,y,z);
}
else
{
p = strstr(str,"*");
if(p)
{
bzero(buf,8);                
                         strncpy(buf,str,strlen(str)-strlen(p));
                         x = atoi(buf);
                         bzero(buf,8);
                         strncpy(buf,p+1,strlen(p)-1);
                         y = atoi(buf);
z = x*y;
printf("%d*%d=%d\n",x,y,z);
}
else
{
p = strstr(str,"/");
if(p)
{
bzero(buf,8);                
                         strncpy(buf,str,strlen(str)-strlen(p));
                         x = atoi(buf);
                         bzero(buf,8);
                         strncpy(buf,p+1,strlen(p)-1);
                         y = atoi(buf);
if(y==0)
{
printf("error!,enter again!\n");
continue;
}
z = x/y;
printf("%d/%d=%d\n",x,y,z);
}
else
{
printf("error!,enter again!\n");
}
}
}
}
}
}
槐序廿柒

2024-12-01 00:37:20

给你个思路,读取输入的字符串,按位读取,一直读到符号为止,按照符号判定+-*/,if else总会用的吧
追问
懂了