编程介的小学生 2017-08-11 01:21 采纳率: 20.5%
浏览 662
已采纳

Equation Solver

Description

Write a program that can solve linear equations with one variable.
Input

The input will contain a number of equations, each one on a separate line. All equations are strings of less than 100 characters which strictly adhere to the following grammar (given in EBNF):
Equation := Expression '=' Expression

Expression := Term { ('+' | '-') Term }

Term := Factor { '*' Factor }

Factor := Number | 'x' | '(' Expression ')'

Number := Digit | Digit Number

Digit := '0' | '1' | ... | '9'

Although the grammar would allow to construct non-linear equations like "x*x=25", we guarantee that all equations occuring in the input file will be linear in x. We further guarantee that all sub-expressions of an equation will be linear in x too. That means, there won't be test cases like x*x-x*x+x=0 which is a linear equation but contains non-linear sub-expressions (x*x).
Note that all numbers occuring in the input are non-negative integers, while the solution for x is a real number.
Output

For each test case, print a line saying "Equation #i (where i is the number of the test case) and a line with one of the following answers:
If the equation has no solution, print "No solution.".
If the equation has infinitely many solutions, print "Infinitely many solutions.".
If the equation has exactly one solution, print "x = solution" where solution is replaced by the appropriate real number (printed to six decimals).

Print a blank line after each test case.
Sample Input

x+x+x=10
4*x+2=19
3*x=3*x+1+2+3
(42-6*7)*x=2*5-10
Sample Output

Equation #1
x = 3.333333

Equation #2
x = 4.250000

Equation #3
No solution.

Equation #4
Infinitely many solutions.

  • 写回答

1条回答

  • threenewbee 2017-08-25 15:45
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛