中国石油大学华东数据结构实验二

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

const int maxn=1e4+5;

struct Node
{
    int temp[maxn];
    int summary=0;
};
struct Node a,b;
int true_or_false;
void Add(){
    printf("C(x)=");
    Node s;
    int flag=0;
    int ma = max(a.summary,b.summary);
    for(int i=0;i<maxn;i++){
        s.temp[i]=0;
    }
    for(int i=0;i<=ma;i++){
        s.temp[i]=a.temp[i]+b.temp[i];
    }
    for(int i=0;i<=ma;i++){
        if(s.temp[i]!=0){
            flag=1;
            break;;
        }
    }
    if(!flag)printf("0\n");
    else{
        true_or_false=0;
        for(int i=0;i<=ma;i++){
            if(s.temp[i]!=0) {
                if (true_or_false == 0) {
                    true_or_false = 1;
                    if (s.temp[i] < 0) {
                        if (i == 0)printf("%d", s.temp[i]);
                        else if (i == 1)printf("%dx", s.temp[i]);
                        else {
                            if (s.temp[i] == -1) {
                                printf("-");
                                printf("x^%d", i);
                            }
                            else printf("%dx^%d", s.temp[i], i);
                        }
                    }
                    else {
                        if (i == 0)printf("%d", s.temp[i]);
                        else if (i == 1)printf("%dx", s.temp[i]);
                        else {
                            if (s.temp[i] == 1)printf("x^%d", i);
                            else printf("%dx^%d", s.temp[i], i);
                        }
                    }
                }
                else{
                    if(s.temp[i]<0){
                        if(i==1)printf("%dx",s.temp[i]);
                        else{
                            if(s.temp[i]==-1){
                                printf("-");
                                printf("x^%d",i);
                            }
                            else printf("%dx^%d",s.temp[i],i);
                        }
                    }
                    else{
                        if(i==1){
                            printf("+");
                            printf("%dx",s.temp[i]);
                        }
                        else{
                            if(s.temp[i]==1){
                                printf("+");
                                printf("x^%d",i);
                            }
                            else{
                                printf("+");
                                printf("%dx^%d",s.temp[i],i);
                            }
                        }
                    }
                }
            }
        }
        printf("\n");
    }
}
void Sub(){
    printf("C(x)=");
    Node s;
    int flag=0;
    int ma = max(a.summary,b.summary);
    for(int i=0;i<maxn;i++){
        s.temp[i]=0;
    }
    for(int i=0;i<=ma;i++)s.temp[i]=a.temp[i]-b.temp[i];
    for(int i=0;i<=ma;i++){
        if(s.temp[i]!=0){
            flag=1;
            break;
        }
    }
    if(flag==0)printf("0");
    else{
        true_or_false=0;
        for(int i=0;i<=ma;i++){
            if(s.temp[i]!=0){
                if(true_or_false==0){
                    true_or_false=1;
                    if(s.temp[i]<0){
                        if(i==0)printf("%d",s.temp[i]);
                        else if(i==1)printf("%dx",s.temp[i]);
                        else{
                            if(s.temp[i]==-1) {
                                printf("-");
                                printf("x^%d", i);
                            }
                            else printf("%dx^%d",s.temp[i],i);
                        }
                    }
                    else{
                        if(i==0)printf("%d",s.temp[i]);
                        else if(i==1)printf("%dx",s.temp[i]);
                        else{
                            if(s.temp[i]==1)printf("x^%d",i);
                            else printf("%d^%d",s.temp[i],i);
                        }
                    }
                }
                else{
                    if(s.temp[i]<0){
                    if(i==1){
                        printf("%dx",s.temp[i]);
                    }
                    else{
                        if(s.temp[i]==-1){
                            printf("-");
                            printf("x^%d",i);
                        }
                        else{
                            printf("%dx^%d",s.temp[i],i);
                        }
                    }
                    }
                    else{
                        printf("+");
                        if(s.temp[i]==1){
                            printf("x^%d",i);
                        }
                        else{
                            if(s.temp[i]==1)printf("x^%d",i);
                            else{
                                printf("%dx^%d",s.temp[i],i);
                            }
                        }
                    }
                }
            }
        }
        printf("\n");
    }
}
void Muti(){
    printf("C(x)=");
    Node s;
    int flag=0;
    int muti=a.summary*b.summary;
    for(int i=0;i<maxn;i++){
        s.temp[i]=0;
    }
    for(int i=0;i<=a.summary;i++){
        for(int j=0;j<=b.summary;j++){
            s.temp[i+j]+=a.temp[i]*b.temp[j];
        }
    }
    for(int i=0;i<=muti;i++){
        if(s.temp[i]!=0){
            flag=1;
            break;
        }
    }
    if(!flag)printf("0");
    else{
        true_or_false=0;
        for(int i=0;i<=muti;i++){
            if(s.temp[i]!=0){
                if(true_or_false==0){
                    true_or_false=1;
                    if(s.temp[i]<0){
                        if(i==0)printf("%d",s.temp[i]);
                        else if(i==1){
                            printf("%dx",s.temp[i]);
                        }
                        else{
                            if(s.temp[i]==-1){
                                printf("-");
                                printf("x^%d",i);
                            }
                            else printf("%dx^%d",s.temp[i],i);
                        }
                    }
                    else{
                        if(i==0)printf("%d",s.temp[i]);
                        else if(i==1)printf("%dx",s.temp[i]);
                        else{
                            if(s.temp[i]==1)printf("x^%d",i);
                            else{
                                printf("%dx^%d",s.temp[i],i);
                            }
                        }
                    }
                }
                else{
                    if(s.temp[i]<0) {
                        if (i == 1)printf("%dx", s.temp[i]);
                        else {
                            if (s.temp[i] == -1) {
                                printf("-");
                                printf("x^%d", i);
                            } else {
                                printf("%dx^%d", s.temp[i], i);
                            }
                        }
                    }
                        else{
                            if(i==1){
                                printf("+");
                                printf("%dx",s.temp[i]);
                            }
                            else{
                                if(s.temp[i]==1){
                                    printf("+");
                                    printf("x^%d",i);
                                }
                                else{
                                    printf("+");
                                    printf("%dx^%d",s.temp[i],i);
                                }
                            }


                    }
                }
            }
        }
        printf("\n");
    }
}
void Daoshu(){
    printf("C(x)=");
    Node s;
    int flag=0;
    for(int i=0;i<maxn;i++){
        s.temp[i]=0;
    }
    for(int i=0;i<a.summary;i++){
        int j=i+1;
        s.temp[i]=j*a.temp[j];
    }
    for(int i=0;i<a.summary;i++){
        if(s.temp[i]!=0){
            flag=1;
            break;
        }
    }
    if(!flag)printf("0");
    else {
        true_or_false = 0;
        for (int i = 0; i <a.summary; i++) {
            if (s.temp[i] != 0) {
                if (true_or_false == 0) {
                    true_or_false = 1;
                    if (s.temp[i] < 0) {
                        if (i == 0)printf("%d", s.temp[i]);
                        else if (i == 1) {
                            printf("%dx", s.temp[i]);
                        } else {
                            if (s.temp[i] == -1) {
                                printf("-");
                                printf("x^%d", i);
                            } else printf("%dx^%d", s.temp[i], i);
                        }
                    } else {
                        if (i == 0)printf("%d", s.temp[i]);
                        else if (i == 1)printf("%dx", s.temp[i]);
                        else {
                            if (s.temp[i] == 1)printf("x^%d", i);
                            else {
                                printf("%dx^%d", s.temp[i], i);
                            }
                        }
                    }
                } else {
                    if (s.temp[i] < 0) {
                        if (i == 1)printf("%dx", s.temp[i]);
                        else {
                            if (s.temp[i] == -1) {
                                printf("-");
                                printf("x^%d", i);
                            } else {
                                printf("%dx^%d", s.temp[i], i);
                            }
                        }
                    } else {
                        if (i == 1) {
                            printf("+");
                            printf("%dx", s.temp[i]);
                        } else {
                            if (s.temp[i] == 1) {
                                printf("+");
                                printf("x^%d", i);
                            } else {
                                printf("+");
                                printf("%dx^%d", s.temp[i], i);
                            }
                        }


                    }
                }
            }
        }
        printf("\n");
    }
}
void Qiu(){
    float x;
    float ans=0;
    float tem=1;
    cin>>x;
    for(int i=0;i<=a.summary;i++){
        if(i!=0)tem=tem*x;
        ans+=a.temp[i]*tem;

    }
    printf("%.2f\n",ans);
}
int main()
{

    char ch;
    int lef,rig;
    a.summary=b.summary=0;
    for(int i=0;i<maxn;i++){
        a.temp[i]=b.temp[i]=0;
    }
    cin>>ch;
    while(cin>>lef){
        if(lef==0)break;
        else {
            cin>>rig;
            a.temp[rig]=lef;
            a.summary=max(rig,a.summary);
        }
    }
    while(cin>>lef){
        if(lef==0)break;
        else {
            cin>>rig;
            b.temp[rig]=lef;
            b.summary=max(rig,a.summary);
        }
    }
    while(cin>>ch){
        if(ch=='X')break;
        else if(ch=='C'){

        }
        else if(ch=='P'){
            Add();
        }
        else if(ch=='S'){
            Sub();
        }
        else if(ch=='M'){
            Muti();
        }
        else if(ch=='D'){
            Daoshu();
        }
        else if(ch=='V'){
            Qiu();
        }
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值