计算器的改良

S a m p l e Sample Sample I n p u t Input Input
6a-5+1=2-2a
S a m p l e Sample Sample O u t p u t Output Output
a=0.750

T r a i n Train Train o f of of T h o u g h t Thought Thought

一元二次方程
最后可以化成 k x + b = 0 kx + b = 0 kx+b=0
再化简可得 x = − b k x = -\frac{b}{k} x=kb
所以只需要求出 b b b(将常数相加,等号右边的要 ∗ − 1 *-1 1)
k k k(将未知数的系数相加,等号右边同上)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n, m, l = 1, k, t = 1, tot;
char s, a;
int main()
{
	while(cin>>s)
	{
		if(s == '+')m += t * k * l, l = 1, tot = 0, k = 0;
		if(s == '-')m += t * k * l, l = -1, tot = 0, k = 0;
		if(s == '=')m += t * k * l, t = -1, tot = 0, k = 0, l = 1;
		if(s >= '0' && s <= '9')
		{
			k = k * 10 + s - '0', tot = 1;
		}
		if(s >= 'a' && s <= 'z')
		{
			a = s;
			if(tot)n += t * k * l;
			else n += l * t;
			k = 0, tot = 1;
		}
	}
	m += t * k * l;
	double ans = double(-m * 1.0 / n);
	if (ans == -0.0) ans = 0;
	printf("%c=%.3f", a, ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值