Fork me on GitHub

简单多项式求解

题目

简单多项式求解

思路

抽象出两个类,一个类为:“项”,另一个类为:“多项式”,“项”作为”多项式“这个类的成员

代码

#include<bits/stdc++.h>

using namespace std;

class term {
	private:
		double x; // 自变量默认值 
		bool isPositive; // 前缀符号 
		int index; // 指数
		int ratio; //系数 
	
	public:
		term(int m_x, bool m_isPositive, int m_index, int m_ratio):x(m_x), isPositive(m_isPositive), index(m_index), ratio(m_ratio){};
		double getVal(double x); // 计算单个项的结果 
};

class polynomial {
	private:
		
		double answer;
		int length; // 项的个数
		string  polynomial_str; 
		vector<term> term_arr;
		
	public:
		
		void transform(); // 将输入的多项式字符串转化为单个项  
		
		double getAnswer(double x); // 计算多项式结果 
		
		polynomial(string m_polynomail_str); // 初始化多项式 
		
		double returnAnswer(); // 获取结果 
		
		
};

// function
polynomial::polynomial(string m_polynomail_str) {
	this->polynomial_str = m_polynomail_str;
	this->length = 0;
	this->answer = 0;
}

void polynomial::transform() {
	int j;
	int m_x;
	bool m_isPositive;
	int m_index;
	int m_ratio;
	string operator_2 = "+-";
	string temp = "x*^";
	
	if (this->polynomial_str[0] != operator_2[1]) {
		this->polynomial_str = "+" + this->polynomial_str; // 格式化 
	}
	for (int i = 0; i < this->polynomial_str.length(); i += 1) {

		if (this->polynomial_str[i] == operator_2[1] || this->polynomial_str[i] == operator_2[0]) {
			// 一个新的项的开始
			if(i != 0 && this->polynomial_str[i - 1] == temp[2])
				continue;

			for (j = i + 1; j < this->polynomial_str.length(); j += 1) { // 找到这一项的长度 
				if (this->polynomial_str[j] == operator_2[0] || this->polynomial_str[j] == operator_2[1])
					if (this->polynomial_str[j - 1] == temp[2]) {
						continue;
					} else {
						break;	
					}
			}

			string str = this->polynomial_str.substr(i, j - i); 

			// 2+4*x^2-x
			m_x = 1;
			m_isPositive = false;
			m_ratio = 1;
			m_index = 1;
			char *end;
			
			// 符号判断 
			if (str[0] == operator_2[0]) {
				m_isPositive = true;
			}
			cout<<"项:"<<str<<endl;
			for (int m = 1; m < str.length(); m += 1) {
				if (str[m] == temp[1]) { // = *
					// 获取系数  
					string tempStr = str.substr(1, m - 1);
					m_ratio = static_cast<int>(strtol(tempStr.c_str(),&end,10));
				} else if (str[m] == temp[2]) {
					// 有指数
					if (str[m + 1] == operator_2[1]) {
						// 负指数
						string tempStr = str.substr(m + 2); 
						m_index = - static_cast<int>(strtol(tempStr.c_str(),&end,10));
					} else {
						// 正指数 
						string tempStr = str.substr(m + 1); 
						m_index = static_cast<int>(strtol(tempStr.c_str(),&end,10));
					}
				} else if (str[m] == temp[0]) {
					// 有 x
					m_x = 2; 
				} 
			}
			
			if (!m_ratio) {
				m_ratio = 1;
			}
			
			if (m_x == 1) {
				// 常数
				m_ratio = static_cast<int>(strtol(str.substr(1).c_str(),&end,10));
			}
			
			term A(m_x, m_isPositive, m_index, m_ratio);
			this->term_arr.push_back(A);
			this->length += 1; 
		}		
	}
}

double polynomial::getAnswer(double x) {
	for (int i = 0; i < this->length; i += 1) {
		this->answer += this->term_arr[i].getVal(x);
	}
}

// 2*x+3*x^-2-x+4-1 

double polynomial::returnAnswer() {
	return this->answer; 
}

double term::getVal(double x) {
	if (this->x == 2) {
		this->x = x;
	}
	
	double val; 
	if (this->isPositive)
		val = pow(this->x, index) * ratio;
	else 
		val = - pow(this->x, index) * ratio;
		
	return val;
}

void test01() {
	string poly_str;
	double x;
	cin>>poly_str;
	polynomial p(poly_str);
	p.transform();
	cin>>x;
	p.getAnswer(x);
	cout<<"结果:"<<p.returnAnswer()<<endl;
}

int main() {
	test01();
	return 0;
} 

测试:
2*x+3*x^-2-x+4-1
项:+2*x
项:+3*x^-2
项:-x
项:+4
项:-1
3
结果:6.33333

posted @ 2020-09-18 22:54  azoux  阅读(199)  评论(0编辑  收藏  举报