逆波兰计算器加强版:多项式

Posted by Cww97 on 2016-03-22

版权声明:本文为博主原创文章,未经博主允许不得转载。原文所在http://blog.csdn.net/cww97 https://blog.csdn.net/cww97/article/details/50951528
不说了不说了,,,
链表感觉白学了(学了吗?)= =
鲍鱼鲍鱼。。。。哇哇哇
http://paste.ubuntu.com/15464514/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//cww  多项式计算器 
#include<stack>
#include<cstdio>
#include<iostream>
using namespace std;

struct term{
int degree;
double coefficient;
term *next;
term (){coefficient=0;degree=0;next=NULL;}
};

void append(term *head,double cft,int dge){//尾插
term *nw=new term;//只有new没有free= =
nw->coefficient=cft;//用完请重启 23333
nw->degree=dge;
term *q=head;
while (q->next!=NULL){q=q->next;}
q->next=nw;
}
//===============================================
void print(term *head){
term *p=head->next;
bool first=1;
for(;p!=NULL;p=p->next){
if (first){first=0;if (p->coefficient<0)printf("-");}//符号
else if (p->coefficient<0)printf("-");
else printf("+");
double r=(p->coefficient>=0)?p->coefficient:-(p->coefficient);
if (r!=1)printf("%.0lf",r);
if (p->degree>1)printf("X^%d",p->degree);
if (p->degree==1)printf("X");
if (r==1&&p->degree==0)printf("1");
}
if (first)printf("0");//空
puts("");
}
//===============================================
term *Plus(term *p,term *q){
p=p->next;q=q->next;
term *ans=new term;

while (p!=NULL||q!=NULL){
if (p==NULL){//比书上多了个判断NULL。。。
append(ans,q->coefficient,q->degree);
q=q->next;
}else if (q==NULL){
append(ans,p->coefficient,p->degree);
p=p->next;
}else if (p->degree > q->degree){
append(ans,p->coefficient,p->degree);
p=p->next;
}else if (p==NULL||p->degree < q->degree){
append(ans,q->coefficient,q->degree);
q=q->next;
}else {
double cft=p->coefficient+q->coefficient;
term *nw=new term;
if (cft)append(ans,cft,p->degree);
p=p->next;q=q->next;
}
}
return ans;
}

term *Minus(term *p,term *q){
p=p->next;q=q->next;
term *ans=new term;
while (p!=NULL||q!=NULL){
if (p==NULL){//同上同上
append(ans,q->coefficient,q->degree);
q=q->next;
}else if (q==NULL){
append(ans,p->coefficient,p->degree);
p=p->next;
}else if (p->degree > q->degree){
append(ans,p->coefficient,p->degree);
p=p->next;
}else if (p->degree < q->degree){
append(ans,-q->coefficient,q->degree);
q=q->next;
}else {
double cft=p->coefficient-q->coefficient;
term *nw=new term;
if (cft)append(ans,cft,p->degree);
p=p->next;q=q->next;
}
}
return ans;
}

term *mul(term *head,double cft,int dge){
term *p=head->next;
for (;p!=NULL;){//单项乘多项
p->coefficient=p->coefficient*cft;
p->degree=p->degree+dge;
p=p->next;
}
return head;
}

term *Copy(term *p){//复制一个多项式。。。。
term *head=new term;
for (p=p->next;p!=NULL;p=p->next){
append(head,p->coefficient,p->degree);
}
return head;
}

term *mult (term *p,term *q){//乘法
term *ans=new term;
term *qq=Copy(q);
for (p=p->next;p!=NULL;p=p->next){
term *tmp=mul(qq,p->coefficient,p->degree);
qq=Copy(q);//乘完之后,q你变了- -
ans=Plus(ans,tmp);
}
return ans;
}
//**********************************************
//有个print在上面
//如果你的输入不合法,,,嘿嘿嘿
term *read(){//读入多项式
term *head=new term;
puts("Give me a polynoimal:");
double cft;//coefficient
int dge; //degree
printf("Enter a coeffficient(end with 0):");
scanf("%lf",&cft);
for (;cft!=0;){
printf("Enter a degree:");
scanf("%d",&dge);
append(head,cft,dge);
printf("Enter a coeffficient(end with 0):");
scanf("%lf",&cft);
}
return head;
}
//==============我是分割线====================
stack<term*>s;

char getorder(){//获取指令
while (1){
printf("Select command and press<Enter>:");
char ch; cin>>ch;
if (ch=='?'||ch=='='||ch=='+'||
ch=='-'||ch=='*'||ch=='/'||
ch=='q'||ch=='Q')return ch;
puts("Please enter a valid command:");
puts("[?]push to stack [=]print top");
puts("[+] [-] [*] [/] you know them");
puts("[Q]uit =======Bazinga!=======");
}
}

bool solve(char ch){//逆波兰计算器主进程
term *x,*y;
if (ch=='q'||ch=='Q'){puts("88");return 0;}
if (ch=='?'){s.push(read());}
if (ch=='='){
if (s.empty())puts("Stack empty");
else {term *top=s.top();print(top);}
}
if (ch=='+'||ch=='-'||ch=='*'){
if (s.empty())puts("Stack empty");
else{
x=s.top(); s.pop();
if (s.empty()){
puts("Stack has just one entry");
s.push(x);
}else{
y=s.top();s.pop();
switch (ch){
case'+':s.push(Plus(x,y));break;
case'-':s.push(Minus(x,y));break;
case'*':s.push(mult(x,y));break;
}
}
}
}
return 1;
}
//==========cww=2016=3=21=23:57============
int main(){
//freopen("fuck.in","r",stdin);
//freopen("fuck.out","w",stdout);
puts("welcome");
while (!s.empty())s.pop();
while (solve(getorder())){}
return 0;
}

再次感谢静静(鲁迅)的语法指导

其实自己写傻了
根本不用链表
直接来个栈套栈就好了。。。
谁让鲍鱼要求了呢T_T
就当链表练习把。。。。class还是不会写。。。。呜呜呜