Demo entry 6799597

c

   

Submitted by wu on May 20, 2019 at 18:24
Language: C++. Code size: 10.4 kB.

/*
  中山大学数据科学与计算机学院程序设计
  吴子静 18340175
  2019/05/07
  大项目2 一个多项式计算机的设计实现

  多项式 polynomial
*/


#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string.h>
#include<fstream>
#include <math.h>//在求值时应用pow 
#include <conio.h>//应用getch()函数; 
#include<Windows.h>

using namespace std;

class POLY {

	public:
		int l[10];//[]中数字代表指数,l[i]的值为系数,那么最高应该为9次
		string name;
		//int num=0;
		POLY() {
			for(int i=0; i<10; i++)
				l[i]=0;
		}

		POLY& operator =(POLY a) {
			this->name=a.name;
			for(int i=0; i<10; i++)
				l[i]=a.l[i];
		}
		friend ostream& operator <<(ostream &os, POLY other);

		friend POLY operator +(POLY a1, POLY a2);
		friend POLY operator -(POLY a1, POLY a2);
		friend POLY operator *(POLY a1, POLY a2);
		friend POLY operator *(POLY b, int c);//c为常数

		POLY derivation() { //求导;
			POLY k;
			this->l[0]=0;
			for(int i=1; i<10; i++) {
				if(l[i]!=0)
					k.l[i-1]=i*this->l[i];
			}
			return k;
		}

		int calculate(int k) { //求值
			int sum=0;
			for(int i=0; i<10; i++) {
				if(this->l[i]!=0)
					sum+=this->l[i]*pow(k,i);
			}
			return sum;
		}


};

ostream& operator <<(ostream &os, POLY p) {
	int i, j;
	for(i=0; i<10; i++) {
		if(i==0) {
			if(p.l[i]!= 0) {
					os<<p.l[i];
					break;
			}
		} else {
			if(p.l[i]!= 0) {
				if(p.l[i]==1) {
					os<<"x^"<<i;
					break;
				} else os<<p.l[i]<<"x^"<<i;
				break;
			}
		}

	}
	for(j=i+1; j<10; j++) {
		if(p.l[j] > 0) {
			if(p.l[j]==1)
				os<<"+x^"<<j;
			else
				os<<'+'<<p.l[j]<<"x^"<<j;
		} else if(p.l[j] < 0) {
			if(p.l[j]==-1)
				os<<"-"<<"x^"<<j;
			else
				os<<p.l[j]<<"x^"<<j;
		}
	}
	cout<<endl;
	return os;

}

POLY operator+(POLY a1, POLY a2) {
	POLY sum;
	for(int i=0; i<10; i++) {
		sum.l[i]=a1.l[i]+a2.l[i];
	}
	return sum;
}

POLY operator-(POLY a1, POLY a2) {
	POLY sum;
	for(int i=0; i<10; i++) {
		sum.l[i]=a1.l[i]-a2.l[i];
	}
	return sum;
}
POLY operator *(POLY a1, POLY a2) {
	POLY sum;

	for(int p=0; p<10; p++) {
		for(int i=0; i<10; i++) {
			for(int j=0; j<10; j++) {
				if(i+j==p)sum.l[p]+=a1.l[i]*a2.l[j];
			}
		}
	}

	return sum;
}

POLY operator*(POLY b, int c) { //与常数的相乘
	POLY sum;
	for(int i=0; i<10; i++) {
		sum.l[i]=b.l[i]*c;
	}
	return sum;
}

bool operator ==(POLY a1,POLY a2) {
	for(int i=0; i<10; i++) {
		if(a1.l[i]!=a2.l[i])return 0;
	}
	return 1;
}



POLY ans[100];//最多可以保存一百个多项式
int count=0;

//子函数如下:
void WriteFile(POLY other);
void readlist();
POLY input();
void MeanPrint1();
void MeanPrint2();
void MeanPrint3();
void MeanPrint4();
void ReturnM();//随时返回菜单功能

void ReturnM() {

}

void WriteFile(POLY other) {                                  //写文件进list
	int i;
	ofstream fout;
	fout.open("POLYlist.txt", ios::app);
	fout <<other.name << '=';
	for (i = 0; i <10 ; i++) {
		if (other.l[i] != 0) {
			fout << '(' <<other.l[i]<< ',' << i << ')';
		}
	}
	fout << endl;
	fout << flush;
	fout.close();
}

void readlist() { 					 //读取list中多项式项目
	ifstream fin("POLYlist.txt");
	string p1;//p1格式为p=(1,2)(3,4);
	while(fin >> p1) {//逐词读取
		if (isalpha(p1[0]) && p1[1] != '\0') {   //输入格式正确时
			string name_;
			int i;
			for(i = 0; i < p1.size(); i++)
				if (p1[i] == '=') {
					name_ = p1.substr(0, i);//截取了名字的部分
					break;
				}
			ans[count].name = name_;
			for(int j=i; j<p1.length() ; j++) {
				if(p1[j]=='(')ans[count].l[p1[j+3]-'0']=p1[j+1]-'0';
			}
			count++;
		}
	}
	fin.close();
}

POLY input() {                            //读取键盘输入的多项式
	string p1;
	cin >> p1;
	POLY p2;//一个用来暂且存储该多项式的冷漠无情的机器
	for(int i=0; i<p1.length() ; i++) {
		if(p1[i]=='(')p2.l[p1[i+3]-'0']=p1[i+1]-'0';
	}
	return p2;
}

void help() {
	cout<<"——————————————注意事项————————————————"<<endl;
	cout<<"1.输入多项式格式:(2,1)(3,4);"<<endl;
	cout<<"2.输入括号必须为英文半角括号,不然会出现乱码;"<<endl;
	cout<<"3.删除功能尚未开发,但可以退出程序在文件中删除;"<<endl;
	getch();
}

void MeanPrint1() {
	system("cls");
	cout<<"                             欢迎使用W.1.0.多项式计算器^v^!"<<endl;
	cout<<"                                    ------主菜单------"<<endl;
	cout<<"                                    (1)多项式管理"<<endl;
	cout<<"                                    (2)多项式运算"<<endl;
	cout<<"                                    (3)其他扩展应用"<<endl;
	cout<<"                                    (4)退出程序"<<endl;
	cout<<"                                    (0)使用注意事项"<<endl;
	cout<<"选择您将要进行的操作:"<<endl;
	int num;
	cin>>num;
	switch(num) {
		case 1:
			system("cls");
			MeanPrint2();
			break;
		case 2:
			system("cls");
			MeanPrint3();
			break;
		case 3:
			system("cls");
			MeanPrint4();
			break;
		case 4:
			system("cls");
			cout<<"您已成功退出计算器程序!"<<endl;
			//cout<<"                                                         感谢使用!"<<endl;
			exit (0);
			break;
		case 0:
			help();
			break;
		default: {
			cout<<"输入有误,请按正确提示输入!"<<endl;
			system("pause");
			break;
		};
	}


}

void MeanPrint2() {

	cout<<"                                  ------多项式管理------"<<endl;
	cout<<"                                   (1)存储多项式"<<endl;
	cout<<"                                   (2)显示已保存的多项式"<<endl;
	cout<<"                                   (3)求值"<<endl;
	cout<<"                                   (4)返回主菜单"<<endl;
	cout<<"选择您将要进行的操作:"<<endl;

	int num;
	cin>>num;
	string p;
	switch(num) {
		case 1: {
			string name_;
			POLY temp;
			cout<<"请按格式输入一个多项式(形如(1,2)(3,1) ):"<<endl;
			temp=input();
			cout<<"是否保存这个多项式(Y / N)?"<<endl;
			char c;
			cin>>c;
			if(c == 'Y' || c=='y') {
				cout<<"请输入这个多项式的名字,并按回车保存:";
				cin>>name_;
				temp.name=name_;
				ans[count]=temp;
				count++;
				cout<<"保存成功^^!"<<endl;
				WriteFile(temp);
				getch();
				system("cls");
			}
		}
		break;
		case 2: {
			cout<<"————————————目前已保存的多项式列表—————————————"<<endl;
			cout<<"已保存多项式数目:"<<count<<endl;
			for(int i=0; i<count; i++) {
				cout<<ans[i].name<<":"<<ans[i];
			}
			cout<<"————————————————————————————————————"<<endl<<endl;
			cout<<"...........................按任意键返回主菜单"<<endl;
			getch();
		}

		break;
		case 3: {
			cout<<"请输入您想要求值的多项式:"<<endl;
			POLY temp;
			temp=input();
			int x;
			cout<<"请输入变量x的值:";
			cin>>x;
			int sum;

			sum=temp.calculate(x);
			cout<<"结果为:"<<sum<<endl;
			cout<<"...................按任意键返回主菜单"<<endl;
			getch();
		}
		break;
		case 0:
			system("cls");
			MeanPrint1();
			break;
	}
}

void MeanPrint3() {
	cout<<"                                      ------多项式运算------"<<endl;
	cout<<"                                          (1)加法"<<endl;
	cout<<"                                          (2)减法"<<endl;
	cout<<"                                          (3)多项式与常数乘法"<<endl;
	cout<<"                                          (4)多项式与多项式乘法"<<endl;
	cout<<"                                          (0)返回主菜单"<<endl;
	cout<<"选择您将要进行的操作:"<<endl;

	int num;
	cin>>num;
	switch(num) {
		case 1: {
			cout<<"----进行加法运算----"<<endl;
			string s;
			POLY a, b, temp;
			cout<<"请输入第一个多项式:";
			a=input();
			cout<<"请输入第二个多项式:";
			b=input();
			temp = a + b;
			cout<<"计算结果是:"<<endl;
			cout<<temp<<endl;
			cout<<"是否保存这个多项式(Y / N)"<<endl;
			char c;
			cin>>c;
			if(c == 'Y' || c == 'y') {
				cout<<"请输入这个多项式的名字,并按回车保存:";
				cin>>s;
				temp.name=s;
				ans[count++]=temp;
				cout<<"保存成功!"<<endl;
				WriteFile(temp);
				getch();
				system("cls");
			}
		}
		break;
		case 2: {
			cout<<"----进行减法运算----"<<endl;
			string s;
			POLY a, b, temp;
			cout<<"请输入第一个多项式(作为减数):";
			a=input();
			cout<<"请输入第二个多项式(作为被减数):";
			b=input();
			temp = a - b;
			cout<<"计算结果是:"<<endl;
			cout<<temp<<endl;
			cout<<"是否保存这个多项式(Y / N)"<<endl;
			char c;
			cin>>c;
			if(c == 'Y' || c == 'y') {
				cout<<"请输入这个多项式的名字,并按回车保存:";
				cin>>s;
				temp.name=s;
				ans[count++]=temp;
				cout<<"保存成功!"<<endl;
				WriteFile(temp);
				getch();
				system("cls");
			}
		}
		break;
		case 3: {
			cout<<"----进行多项式与常数的乘法运算----"<<endl;
			string s;
			POLY a, temp;
			int b;
			cout<<"请输入多项式:";
			a=input();
			cout<<"请输入常数:";
			cin>>b;
			temp = a*b;
			cout<<"计算结果是:"<<endl;
			cout<<temp<<endl;
			cout<<"是否保存这个多项式(Y / N)"<<endl;
			char c;
			cin>>c;
			if(c == 'Y' || c == 'y') {
				cout<<"请输入这个多项式的名字,并按回车保存:";
				cin>>s;
				temp.name=s;
				ans[count++]=temp;
				cout<<"保存成功!"<<endl;
				WriteFile(temp);
				getch();
				system("cls");
			}
		}
		break;

		case 4: {
			cout<<"----进行多项式与多项式的乘法----"<<endl;
			string s;
			POLY a, b, temp;
			cout<<"请输入第一个多项式:";
			a=input();
			cout<<"请输入第二个多项式:";
			b=input();
			temp = a * b;
			cout<<"计算结果是:"<<endl;
			cout<<temp<<endl;
			cout<<"是否保存这个多项式(Y / N)"<<endl;
			char c;
			cin>>c;
			if(c == 'Y' || c == 'y') {
				cout<<"请输入这个多项式的名字,并按回车保存:";
				cin>>s;
				temp.name=s;
				ans[count++]=temp;
				cout<<"保存成功!"<<endl;
				WriteFile(temp);
				getch();
				system("cls");
			}
		}
		break;

		case 0://返回主菜单
			system("cls");
			MeanPrint1();
			break;
	}
}

void MeanPrint4() {
	cout<<"                                          ------其它扩展应用------"<<endl;
	cout<<"                                          (1)多项式求导"<<endl;
	cout<<"                                          (2)比较两个多项式是否相等"<<endl;
	cout<<"                                          (0)返回主菜单"<<endl;
	cout<<"选择您将要进行的操作:"<<endl;

	int num;
	cin>>num;
	switch(num) {
		case 1: {
			string name_;
			cout<<"请输入想要求导的多项式:"<<endl;
			POLY a;
			a=input();
			POLY r;//求导的result
			r=a.derivation();
			cout<<"求导的结果为";
			cout<<r;
			cout<<"是否保存这个多项式(Y / N)"<<endl;
			char c;
			cin>>c;
			if(c == 'Y' || c == 'y') {
				cout<<"请输入这个多项式的名字,并按回车保存:"<<endl;
				cin>>name_;
				r.name=name_;
				ans[count++]=r;
				cout<<"保存成功"<<endl;
				WriteFile(r);
				system("cls");
			}

		}
		break;

		case 2: {
			POLY a,b;
			cout<<"请输入第一个多项式:";
			a=input();
			cout<<"请输入第二个多项式:";
			b=input();
			if(a==b)
				cout<<"[两个多项式相等]"<<endl;
			else
				cout<<"[两个多项式不等]"<<endl;
			cout<<endl;

			cout<<".............按任意键返回主菜单"<<endl;
			getch();
		}
		break;
		case 0:
			system("cls");
			MeanPrint1();
			break;
	}
}



int main() {
	readlist();
	while(1) {
		MeanPrint1();
	}
	return 0;
}

This snippet took 0.04 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).