c语言输入一个一元一次方程显示答案的程序

来源:百度知道 编辑:UC知道 时间:2024/09/28 07:19:18
程序要求输入任意一个一元一次函数…然后回车显示结果…

我这个程序只能接受ax+b=c这种形式的方程.
其中,a=1时可以省略,+b也可以省略.a,b,c不能为表达式,必须为确定的浮点数.
a不能为0

#include "stdio.h"
#include "stdlib.h"
void main()
{
float a,b,c;
int i,j;
char str[81];

gets(str);
for(i=0;str[i]!='\0';i++)
if(str[i]=='x' || str[i]=='X')
break;
if(str[i]=='\0') printf("error!\n");
if(i==0) a=1;
else a=atof(str);
if(a==0)
{
printf("a mustn't be zero!\n");
exit(1);
}
if(str[++i]=='=')
{
b=0;
c=atof(str+i+1);
}
else
{
if(str[i]=='+') i++;
for(j=i;str[j]!='\0';j++)
if(str[j]=='=') break;
b=atof(str+i);
c=atof(str+j+1);
}
printf("x=%f\n",(c-b)/a);
}

更简单的一个小程序,见笑了:
#include<stdio.h>
void main()
{