UVALive7196 Find x

You have to implement a simple equation solver to evaluate expressions consisting of numbers, variables,
and addition/subtraction operators.
In the past, this type of problem was called a “find x” problem, however, an unintended consequence
of ubiquitous touch interfaces resulted in people fruitlessly pointing at the “x” on the screen, so we
now require the unnecessarily complex syntax specified below to avoid this all-too-common misunderstanding.
Input
Your input consists of an arbitrary number of records, but no more than 100. Each record starts with a
line containing a single integer n, with 3 ≤ n ≤ 32, denoting the number of symbols to follow, followed
on the next line by n single-character symbols, separated by a single space.
The symbols are selected from the following classes:
• NUM : Numbers, the characters ‘0’ through ‘9’. All numbers specified in the input will be
singledigit values;
• EQ : Equals, the character ‘=’;
• V AR : Variables, the characters ‘a’ through ‘z’ (only lowercase), one of which will appear exactly
once in any given equation;
• OP : Operators, the characters ‘+’ and ‘-’. An operator may appear as the first symbol in an
equation, or the first symbol after an EQ symbol — in this case it acts as a unary operator, which
negates the NUM or V AR immediately to its right if the OP is ‘-’, and does nothing if the OP
is ‘+’.
Valid equation syntax is
[OP] (NUM|V AR) (OP (NUM|V AR))* EQ [OP] (NUM|V AR) (OP (NUM|V AR))*
with the constraint that any given equation will contain only one instance of V AR (only one letter,
appearing at only one place). In the syntax, parentheses denote grouping, the Or symbol ‘|’ denotes
a choice between its left and right operands, the square brackets ‘[]’ indicate zero or one occurrences
of the enclosed term), and the ‘*’ symbol indicates zero or more occurrences of the term to its left.
The end of input is indicated by a line containing only the value ‘-1’.
Output
For each input record, output
V AR = answer
where V AR is the symbol used in the input equation, and answer is the solution to the equation.
Sample Input
7
4 - 5 - x = 2
8
- 4 - 5 - y = 2
9
+ 4 - 5 - z = + 2
8
+ 4 - 5 = a + 2
9
+ 4 - 5 = - d + 2
-1
Sample Output
x = -3
y = -11
z = -3
a = -3
d = 3

 

题目大意:解方程。

题解:讲道理其实好多地方没看懂……题目的意思还是根据测例连蒙带猜出来的orz,我英语太菜了。每个测例先输入该测例公式(只有一个变量)中非空格的字符数,然后输入这个公式,求出里面变量的值即可。计算的时候可以先记录等号位置,把变量移到等号右边,其他常量都放在等号左边,计算常量的和即可。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int n;
    int i;
    int posd;
    char a;
    char str[1000], s[5];
    while(cin>>n && n!=-1)
    {
        i=0;
        while(i<n)
        {
            scanf("%c", &a);
            if(a!=' ' && a!='\n')
            {
                if(a=='=') posd=i;
                str[i]=a;
                i++;
            }
        }
        str[n]=0;
        int sum=0;
        bool flag=0;//0+ 1-
        int x=0;
        for(i=0; i<posd; i++)
        {
            if(str[i]=='+') flag=0;
            else if(str[i]=='-') flag=1;
            else if(str[i]>='0' && str[i] <='9')
            {
                x=x*10+str[i]-'0';
                if(str[i+1]>='0' && str[i+1] <='9') continue;
                if(flag==0) sum+=x, x=0;
                else if(flag==1) sum-=x, x=0;
            }
            else if(str[i]>='a' && str[i]<='z')
            {
                if(flag==0) s[0]='-';
                else if(flag==1) s[0]='+';
                s[1]=str[i];
            }
        }
        flag=0;
        x=0;
        for(i=posd+1; i<n; i++)
        {
            if(str[i]=='+') flag=0;
            else if(str[i]=='-') flag=1;
            else if(str[i]>='0' && str[i] <='9')
            {
                x=x*10+str[i]-'0';
                if(str[i+1]>='0' && str[i+1] <='9') continue;
                if(flag==0) sum-=x, x=0;
                else if(flag==1) sum+=x, x=0;
            }
            else if(str[i]>='a' && str[i]<='z')
            {
                if(flag==0) s[0]='+';
                else if(flag==1) s[0]='-';
                s[1]=str[i];
            }
        }
        if(s[0]=='-') sum=-sum;
        printf("%c = %d\n", s[1], sum);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值