POJ2252解题报告 解一元一次方程

Equation Solver
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 427   Accepted: 266

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.
题意:解一元一次方程
思路:设置两个整型,一个保存当前得到的常数,另一个保存X的系数,然后递归求解.....
#include<iostream>
using namespace std;

char in[200],a[200];
int pos;

void cal(int &NUM,int &XISHU)
{
	int nowNUM=0,nowXISHU=0,sign=1;
	for(;pos<strlen(in);pos++)
		if(in[pos]>='0'&&in[pos]<='9')
			nowNUM=nowNUM*10+in[pos]-48;
		else if(in[pos]=='x')
		{
			if(nowNUM)
			{
				nowXISHU=nowNUM;
				nowNUM=0;
			}
			else
				nowXISHU=1;
		}
		else if (in[pos]=='(')
		{
			pos++;
			cal(nowNUM,nowXISHU);
		}
		else if(in[pos]==')')
		{
			nowNUM*=sign;
			NUM+=nowNUM;
			XISHU+=nowXISHU;
			return ;
		}
		else if(in[pos]=='+')
		{	
			NUM+=nowNUM*sign;
			XISHU+=nowXISHU*sign;
			nowXISHU=0;		nowNUM=0;
			sign=1;		
		}
		else if(in[pos]=='-')
		{	
			NUM+=nowNUM*sign;
			XISHU+=nowXISHU*sign;
			nowXISHU=0;		nowNUM=0;
			sign=-1;	
		}
		else if(in[pos]=='*')
		{
			nowNUM*=sign;
			nowXISHU*=sign;
			sign=1;
			pos++;
			for(int nnum=0,nxishu=0;pos<strlen(in)+1;pos++)
				if(in[pos]=='(')
				{
					pos++;
					cal(nnum,nxishu);
				}
				else if(in[pos]>='0'&&in[pos]<='9')
					nnum=nnum*10+in[pos]-48;
				else if(in[pos]=='x')
				{
					nxishu=1;
				}
				else 
				{
					if(nxishu)
						nowXISHU=nowNUM*nxishu;
					else
						nowXISHU=nowXISHU*nnum;
					nowNUM*=nnum;
					pos--;
					break;
				}
		}
		NUM+=nowNUM*sign;
		XISHU+=nowXISHU*sign;
		return ;
}


int main()
{
	int t=0;
	while(cin>>a&&++t)
	{
		int i;
		for(i=0;a[i]!='=';i++)
			in[i]=a[i];
		in[i]='/0';
		int NUM=0,XISHU=0,NUM1=0,XISHU1=0;
		pos=0;
		cal(NUM,XISHU);
		strcpy(in,&a[i+1]);
		pos=0;
		cal(NUM1,XISHU1);
		
		XISHU-=XISHU1;
		NUM1-=NUM;

		if(XISHU==0&&NUM1!=0)
			cout<<"Equation #"<<t<<endl<<"No solution."<<endl<<endl;
		else if(XISHU==0&&NUM1==0)
			cout<<"Equation #"<<t<<endl<<"Infinitely many solutions."<<endl<<endl;
		else 
		{
			printf("Equation #%d/nx = %.6lf/n/n",t,double(NUM1)/double(XISHU));

		}
	}

}
 

你可能感兴趣的:(POJ2252解题报告 解一元一次方程)