Files
sort-and-png/png/main.cpp
2018-06-17 14:30:24 +08:00

291 lines
9.0 KiB
C++

#include <iostream>
#include <cstring>
#include <iomanip>
struct term{
float coef;
int expn;
term *next;
};
void deletLastOne(term* in){
auto tmp = in;
while (tmp->next->next){
tmp = tmp->next;
}
delete tmp->next;
tmp->next = nullptr;
}
class Polynomial{
public:
char* name;
term *termList;
Polynomial(term*in,char* inName){name=inName;termList=in;}
~Polynomial(){
while(termList){
auto tmp = termList;
termList=termList->next;
delete tmp;
}
delete name;
}
term* operator+(Polynomial &ri);
term* operator-(Polynomial &ri);
term* operator*(Polynomial &ri);
term* derivative();
bool operator==(const char* in);
float operator()(float in);
float fPower(float m, int n){ //m^n
if(n==1) return m;
if(n==0) return 1;
float temp=fPower(m,n/2);
return (n%2==0 ? 1 : m)*temp*temp;
}
};
term* Polynomial::operator+(Polynomial &ri) {
auto* head = new term{0,0, nullptr};
auto end = head;
auto this_head = this->termList;
auto that_head = ri.termList;
while (this_head || that_head){
if(!that_head || (this_head && this_head->expn < that_head->expn)){
end->coef = this_head->coef;
end->expn = this_head->expn;
end->next = new term{0,0, nullptr};
end = end->next;
this_head = this_head->next;
continue;
}
if(!this_head || this_head->expn > that_head->expn){
end->coef = that_head->coef;
end->expn = that_head->expn;
end->next = new term{0,0, nullptr};
end = end->next;
that_head = that_head->next;
continue;
}
if(this_head->expn == that_head->expn){
end->coef = this_head->coef+that_head->coef;
end->expn = that_head->expn;
end->next = new term{0,0, nullptr};
end = end->next;
that_head = that_head->next;
this_head = this_head->next;
continue;
}
}
deletLastOne(head);
return head;
}
term* Polynomial::operator-(Polynomial &ri) {
auto* head = new term{0,0, nullptr};
auto end = head;
auto this_head = this->termList;
auto that_head = ri.termList;
while (this_head||that_head){
if(!that_head || (this_head && this_head->expn < that_head->expn)){
end->coef = this_head->coef;
end->expn = this_head->expn;
end->next = new term{0,0, nullptr};
end = end->next;
this_head = this_head->next;
continue;
}
if(!this_head || this_head->expn > that_head->expn){
end->coef = 0-that_head->coef;
end->expn = that_head->expn;
end->next = new term{0,0, nullptr};
end = end->next;
that_head = that_head->next;
continue;
}
if(this_head->expn == that_head->expn){
end->coef = this_head->coef - that_head->coef;
end->expn = that_head->expn;
end->next = new term{0,0, nullptr};
end = end->next;
that_head = that_head->next;
this_head = this_head->next;
continue;
}
}
deletLastOne(head);
return head;
}
term* Polynomial::operator*(Polynomial &ri) {
int numa = 0,numb=0;
term* tmp = this->termList;
while (tmp->next)tmp=tmp->next;
numa = tmp->expn;
tmp = ri.termList;
while (tmp->next)tmp=tmp->next;
numb = tmp->expn;
auto *coList = new float[numa+numb+1]{0};
term* la = this->termList ,*lb;
while (la){
lb = ri.termList;
while (lb){
coList[la->expn+lb->expn]=la->coef*lb->coef;
lb = lb->next;
}
la=la->next;
}
auto* head = new term{0,0, nullptr};
auto end = head;
for(int i = 0;i<=numa+numb;i++){
if(coList[i] == 0) continue;
end->coef = coList[i];
end->expn = i;
end->next = new term{0,0, nullptr};
end = end->next;
}
deletLastOne(head);
return head;
}
term* Polynomial::derivative() {
auto* head = new term{0,0, nullptr};
auto end = head;
auto tmp = this->termList;
while (tmp){
if(tmp->expn != 0) {
end->coef = tmp->coef * tmp->expn;
end->expn = tmp->expn - 1;
end->next = new term{0, 0, nullptr};
end = end->next;
}
tmp = tmp->next;
}
deletLastOne(head);
return head;
}
bool Polynomial::operator==(const char *in) {
return strcmp(this->name, in) == 0;
}
float Polynomial::operator()(float in) {
auto tmp = this->termList;
float out = 0;
while (tmp){
out += tmp->coef* fPower(in,tmp->expn);
tmp = tmp->next;
}
return out;
}
std::ostream & operator<<(std::ostream &out,const Polynomial &obj){
out << obj.name << "(x)=";
auto tmp = obj.termList;
while (tmp){
out << ' '<<(tmp->coef>0?"+":"")<<std::setprecision(2)<<std::fixed<< tmp->coef;
if(tmp->expn != 0) {
out << "x^" << tmp->expn;
}
tmp = tmp->next;
}
return out;
}
#include <list>
#include <algorithm>
using namespace std;
int main(){
list<Polynomial> a;
while (true){
cout << "当前暂存的函数有:" <<endl;
for(const auto &item : a){
cout << item <<endl;
}
cout << "请选择下面的操作:"<<endl;
cout << "1. 创建新的多项式" <<endl;
cout << "2. 多项式加法" << endl;
cout << "3. 多项式减法" <<endl;
cout << "4. 多项式乘法" <<endl;
cout << "5. 多项式求导" << endl;
cout << "6. 多项式带入求值" << endl;
cout << "0. 退出程序" <<endl;
int check;
cin >> check;
auto* name = new char[20];
int ex,num;float co;
auto * head = new term{0,0,nullptr};
auto end = head;
char c[20],b[20];
list<Polynomial>::iterator itb,itc;
switch (check){
case 1:
cout << "多项式名称:";
cin >> name;
cout << "请输入项数:";
cin >> num;
cout << "请按照次数从小到大的顺序输入 指数 和 系数"<< endl;
for(int i = 0;i<num;i++){
cin >> ex >> co;
end->expn = ex;
end->coef = co;
end->next = new term{0,0, nullptr};
end = end->next;
}
deletLastOne(head);
a.emplace_back(head,name);
cout << endl << "输入结束,您输入的表达式为:"<<endl;
cout << a.back() << endl;
break;
case 2:
case 3:
case 4:
cout << "请输入结果多项式的名称:";
cin >> name;
cout << "请输入两个运算多项式的名称 空格分隔:";
cin >> c >> b;
itc = find(a.begin(),a.end(),c);
itb = find(a.begin(),a.end(),b);
if(itc == a.end() || itb == a.end()){
cout << "输入的名字有误";
}else{
switch (check){
case 2:
a.emplace_back(*itc+*itb,name);
break;
case 3:
a.emplace_back(*itc-*itb,name);
break;
case 4:
a.emplace_back(*itc**itb,name);
break;
default:break;
}
cout << "输入结束,得到的表达式为:"<<endl;
cout << a.back() << endl;
}
break;
case 5:
cout << "请输入结果多项式的名称:";
cin >> name;
cout << "请输入需要计算的多项式名称:";
cin >> b;
itb = find(a.begin(),a.end(),b);
if(itb == a.end()){
cout << "输入的名字有误";
} else{
a.emplace_back((*itb).derivative(),name);
cout <<"输入结束,得到的表达式为:"<<endl;
cout << a.back() << endl;
}
break;
case 6:
cout << "请输入多项式的名称:";
cin >> name;
itb = find(a.begin(),a.end(),name);
if(itb == a.end()){
cout << "输入的名字有误";
} else{
cout << "请输入参数:";
cin >> co;
cout << "计算结果:";
cout << (*itb)(co) << endl;
}
break;
case 0:
return 0;
default:
break;
}
}
}