c语言期末考最后一题是输入一个一元一次方程(字符串形式),并要求把x解出来
很遗憾太菜了,做了五十分钟和傻子没啥区别,今天早上无聊稍微写了下,基本框架出来了,但是仍然不能解决所有问题,它目前只能输入' + '不能输入' - ',先把笔记留下,后面有时间我会慢慢地补全代码,先记录下我的战损版代码
#include <iostream>
using namespace std;
int main()
{
char fc[100];
gets_s (fc);
//计算未知数x的系数(等号前的)
char* p = fc;
int num1 = 0;
while (*p&&*p!='=')
{
if (p == fc)
{
if (*p == 'x')
{
num1++;
}
}
else
{
if (*p == 'x')
{
if (*(p - 1) >= '1' && *(p - 1) <= '9')
{
num1 = num1 + (*(p-1) - 48);
}
else
num1++;
}
}
p++;
}
//查找等号后的x的系数
int num2 = 0;
while (*p)
{
if (*p == 'x')
{
if (*(p - 1) >= '1' && *(p - 1) <= '9')
{
num2 = num2 + (*(p - 1) - 48);
}
else
num2++;
}
p++;
}
int num_1 = num1 - num2;
cout << num1-num2 << endl;
//算其他的数字(等号前的)
num1 = 0, num2 = 0;
p = fc;
while (*p&&*p!='=')
{
if (*p >= '1' && *p <= '9')
{
if (*(p + 1) >= '0' && *(p+1) <= '9')
{
num1 = num1 + (*(p +1) - 48) + 10*(*p-48);
}
else
num1 = num1 + (*p-48);
}
p++;
}
//cout << num1 << endl;
//算等号后的数字
while (*p)
{
if (*p >= '1' && *p <= '9')
{
if (*(p + 1) >= '0' && *(p + 1) <= '9')
{
num2 = num2 + (*(p + 1) - 48) + 10 * (*p - 48);
}
else
num2 = num2 + (*p - 48);
}
p++;
}
//cout << num2 << endl;
int num_2 = num2 - num1;
cout << "x=" << 1.0 * num_2 / num_1 << endl;
system("pause");
return 0;
}
暂时下播,我还会回来的