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

不说了不说了,,,
链表感觉白学了(学了吗?)= =
鲍鱼鲍鱼。。。。哇哇哇
http://paste.ubuntu.com/15464514/

//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还是不会写。。。。呜呜呜

你可能感兴趣的:(链表)