zoukankan      html  css  js  c++  java
  • 数据结构试验二

    #include"iostream"
    #include"stdio.h"
    #include"string.h"
    #include"algorithm"
    #include"queue"
    #include"stack"
    #include"ctype.h"
    #include"cmath"
    #define mx 1005
    using namespace std;
    
    struct poly //链表节点数据类型
    {
        double ceof;//系数
        int exp;//指数
        poly *next;
    };
    double A[mx],B[mx];//存放A和B表达式
    int adegree,bdegree,maxexp;//存放最高阶数
    
    void init(poly *&pl) //链表初始化
    {
        pl=new poly;//动态分配空间,如果出现错误则输出 allocate error
        if(pl==NULL){cout<<"allocate error!"<<endl;}
        else
        {
            pl->next=NULL;
        }
    }
    
    void input(poly *&pl,int degree,double X[]) //表达式的输入(存入链表中)
    {
        poly *t=pl;
        int i=0;
        while(i<=degree)
        {
            if(X[i]!=0) //系数为零的不用存
            {
                t->next=new poly;
                t=t->next;
                t->ceof=X[i];
                t->exp=i;
            }
            i++;
        }
        t->next=NULL;
    }
    
    void output(poly *&pl) //表达式的输出
    {
        poly *t=pl->next;
        cout<<"the polynomal is:";
        bool h=true;
        while(t!=NULL)
        {
            if(!h&&t->exp>0.0) cout<<"+";
                h=false;
            cout<<t->ceof;
            switch(t->exp) //根据阶数输出相应的项
            {
                case 0:break;
                case 1:cout<<"x";break;
                default:cout<<"x^"<<t->exp;
            }
            t=t->next;
        }
        cout<<endl;
    }
    
    void mul_poly(poly *&pla,poly *&plb,poly *&plc) //实现表达式相乘
    {
        double result[mx];//用来记录量表达式相乘后的得到的表达式的系数
        poly *ta,*tb;
        int i,k;
        if(adegree!=-1||bdegree!=-1) //考虑0多项式的情况
        {
            maxexp=adegree+bdegree;
            for(i=0;i<maxexp;i++) result[i]=0.0;
            ta=pla->next;
            while(ta!=NULL)
            {
                tb=plb->next;
                while(tb!=NULL)
                {
                    k=ta->exp+tb->exp;
                    result[k]+=(ta->ceof)*(tb->ceof);
                    tb=tb->next;
                }
                ta=ta->next;
            }
           input(plc,maxexp,result);
        }
    }
    
    int main()
    {
        poly *pla,*plb,*plc;
        int i,j,_exp;
        double _ceof;
        char ch;
        int case_=0;
        while(++case_)
        {
            cout<<"case "<<case_<<":"<<endl;
            memset(A,0,sizeof(A));
            memset(B,0,sizeof(B));
            init(pla);//初始化,这个操作一定要有
            init(plb);
            init(plc);
            ch='0';
            cout<<"input A poly:";
            while(ch!='
    ')//A表达式的输入
            {
                cin>>_ceof;
                getchar();getchar();
                cin>>_exp;
                ch=getchar();
                A[_exp]=_ceof;
                adegree=_exp;
            }
            input(pla,adegree,A);
            cout<<"input B poly:";
               ch='0';
            while(ch!='
    ')//B表达式的输入
            {
                cin>>_ceof;
                getchar();getchar();
                cin>>_exp;
                ch=getchar();
                B[_exp]=_ceof;
                bdegree=_exp;
            }
            input(plb,bdegree,B);
            mul_poly(pla,plb,plc);
            output(plc);//输出最终结果
            cout<<endl;
        }
        return 0;
    }
    View Code
    #include"iostream"
    
    #include"stdio.h"
    
    #include"string.h"
    
    #include"algorithm"
    
    #include"queue"
    
    #include"stack"
    
    #include"ctype.h"
    
    #include"cmath"
    
    #define mx 1005
    
    using namespace std;
    
    struct poly //链表节点数据类型
    
    {
    
    double ceof;//系数
    
    int exp;//指数
    
    poly *next;
    
    };
    
    double A[mx],B[mx];//存放A和B表达式
    
    int adegree,bdegree,maxexp;//存放最高阶数
    
    void init(poly *&pl) //链表初始化
    
    {
    
    pl=new poly;//动态分配空间,如果出现错误则输出 allocate error
    
    if(pl==NULL){cout<<"allocate error!"<<endl;}
    
    else
    
    {
    
    pl->next=NULL;
    
    }
    
    }
    
    void input(poly *&pl,int degree,double X[]) //表达式的输入(存入链表中)
    
    {
    
    poly *t=pl;
    
    int i=0;
    
    while(i<=degree)
    
    {
    
    if(X[i]!=0) //系数为零的不用存
    
    {
    
    t->next=new poly;
    
    t=t->next;
    
    t->ceof=X[i];
    
    t->exp=i;
    
    }
    
    i++;
    
    }
    
    t->next=NULL;
    
    }
    
    void output(poly *&pl) //表达式的输出
    
    {
    poly *t=pl->next;
    
    cout<<"the polynomal is:";
    
    bool h=true;
    
    while(t!=NULL)
    
    {
    
    if(!h) cout<<"+";
    
    h=false;
    
    if(t->ceof!=1)
    
    cout<<t->ceof;
    
    switch(t->exp) //根据阶数输出相应的项
    
    {
    
    case 0:break;
    
    case 1:cout<<"x";break;
    
    default:cout<<"x^"<<t->exp;
    
    }
    
    t=t->next;
    
    }
    
    cout<<endl;
    
    }
    
    void insert_poly(poly *&pl,double ceof_,int exp_)
    
    {
    
    poly *t,*pre,*newpoly;
    
    t=pl->next;pre=pl;
    
    while(t!=NULL&&t->exp>exp_)
    
    {
    
    pre=t;
    
    t=t->next;
    
    }
    
    if(t!=NULL&&t->exp==exp_)
    
    {
    
    t->ceof+=ceof_;
    
    if(t->ceof==0)//如果多项式系数为零,则删除
    
    {
    
    pre->next=t->next;
    
    delete t;
    
    }
    
    }
    
    else
    
    {
    
    newpoly=new poly;
    
    newpoly->ceof=ceof_;
    
    newpoly->exp=exp_;
    
    pre->next=newpoly;
    
    newpoly->next=t;
    
    }
    
    }
    
    void mul_poly(poly *&pla,poly *&plb,poly *&plc) //用插入法实现表达式相乘
    
    {
    
    poly *ta,*tb;
    
    if(adegree!=-1&&bdegree!=-1) //没有零多项式,执行相乘运算
    
    {
    
    ta=pla->next;
    
    while(ta!=NULL)//模拟笔算多项式相乘
    
    {
    
    tb=plb->next;
    
    while(tb!=NULL)
    
    {
    
    insert_poly(plc,(ta->ceof)*(tb->ceof),ta->exp+tb->exp);
    
    tb=tb->next;
    
    }
    
    ta=ta->next;
    
    }
    
    }
    
    }
    
    int main()
    
    {
    
    poly *pla,*plb,*plc;
    
    int i,j,_exp;
    
    double _ceof;
    
    char ch;
    
    int case_=0;
    
    while(++case_)
    
    {
    
    cout<<"case "<<case_<<":"<<endl;
    
    memset(A,0,sizeof(A));
    
    memset(B,0,sizeof(B));
    
    init(pla);//初始化,这个操作一定要有
    
    init(plb);
    
    init(plc);
    
    ch='0';
    
    cout<<"input A poly:";
    
    while(ch!='
    ')//A表达式的输入
    
    {
    
    cin>>_ceof;
    
    getchar();getchar();
    
    cin>>_exp;
    
    ch=getchar();
    
    A[_exp]=_ceof;
    
    adegree=_exp;
    
    }
    
    input(pla,adegree,A);
    
    cout<<"input B poly:";
    
    ch='0';
    
    while(ch!='
    ')//B表达式的输入
    
    {
    
    cin>>_ceof;
    
    getchar();getchar();
    
    cin>>_exp;
    
    ch=getchar();
    
    B[_exp]=_ceof;
    
    bdegree=_exp;
    
    }
    
    input(plb,bdegree,B);
    
    mul_poly(pla,plb,plc);
    
    output(plc);//输出最终结果
    
    cout<<"insert one formula to the poly:";
    
    cin>>_ceof;
    
    getchar();getchar();
    
    cin>>_exp;
    
    insert_poly(plc,_ceof,_exp);
    
    output(plc);//输出插入一项后的最终结果
    
    cout<<endl;
    
    }
    
    }
    View Code
    #include"iostream"
    
    #include"stdio.h"
    
    #include"algorithm"
    
    #include"string.h"
    
    #include"cmath"
    
    #define mx 1005
    
    using namespace std;
    
    int matrix[mx][mx];
    
    //三元组的结构定义
    
    struct thNode //非零元素
    
    {
    
    int row,col;
    
    int item;
    
    };
    
    struct thMatrix
    
    {
    
    thNode *data;
    
    int m,n,t;//分别表示行数、列数和非零元素个数
    
    };
    
    //初始化三元组表
    
    void initMatrix_Th(thMatrix &M)
    
    {
    
    M.data=new thNode[mx+1];
    
    M.m=0;
    
    M.n=0;
    
    M.t=0;
    
    }
    
    //快速转置
    
    void transMatrix_2(thMatrix M,thMatrix &T)
    
    {
    
    initMatrix_Th(T);
    
    T.m=M.n;
    
    T.n=M.m;
    
    T.t=M.t;
    
    int i,j,p,q;
    
    int num[100];
    
    int cpot[100];
    
    if(T.t)
    
    {
    
    for(i=1;i<=M.n;i++)
    
    num[i]=0;//M中每一列的非零元素个数初始化为0
    
    for(i=1;i<=M.t;i++)
    
    ++num[M.data[i].col];//M中每一列的非零元素个数存储到一个数组中
    
    cpot[1]=1;
    
    for(i=2;i<=M.n;i++)
    
    cpot[i]=cpot[i-1]+num[i-1];//每一列第一个非零元素的位置
    
    for(p=1;p<=M.t;p++)
    
    {
    
    j=M.data[p].col;
    
    q=cpot[j];//确定第j列第一个非零元素的位置
    
    T.data[q].row=M.data[p].col;
    
    T.data[q].col=M.data[p].row;
    
    T.data[q].item=M.data[p].item;
    
    ++cpot[j];//指向下一个位置
    
    }
    
    }
    
    }
    
    //创建一个三元组
    
    void creatMatrix(thMatrix &M)
    
    {
    
    initMatrix_Th(M);
    
    int i,j,k=1;
    
    cout<<"输入矩阵的行数和列数:";
    
    cin>>M.m>>M.n;
    
    for(i=1;i<=M.m;i++)
    
    {
    
    for(j=1;j<=M.n;j++)
    
    {
    
    cin>>matrix[i][j];
    
    if(abs(matrix[i][j])>0)
    
    {
    
    M.data[k].row=i;
    
    M.data[k].col=j;
    
    M.data[k++].item=matrix[i][j];
    
    }
    
    }
    
    }
    
    M.t=k-1;
    
    }
    
    //输出一个三元组
    
    void outputMatrix(thMatrix M)
    
    {
    
    int i;
    
    cout<<"矩阵的行和列:"<<M.m<<" "<<M.n<<endl;
    
    cout<<"非零元素个数:"<<M.t<<endl;
    
    for(i=1;i<=M.t;i++)
    
    {
    
    cout<<M.data[i].row<<" "<<M.data[i].col<<" "<<M.data[i].item<<endl;
    
    }
    
    }
    
    int main()
    
    {
    
    thMatrix M,T;
    
    initMatrix_Th(M);
    
    cout<<"输入一个矩阵:"<<endl;
    
    creatMatrix(M);
    
    cout<<"输出该矩阵的三元组表:"<<endl;
    
    outputMatrix(M);
    
    transMatrix_2(M,T);
    
    cout<<"输出该矩阵的转置矩阵的三元组表:"<<endl;
    
    outputMatrix(T);
    
    return 0;
    
    }
    View Code
  • 相关阅读:
    chkconfig命令
    PHP中的WebService
    MySQL 中联合查询效率分析
    javascript中json对象长度
    Replace Pioneer
    c++ 调用matlab程序
    ubuntu 安装 sublime
    一些地址收藏
    学习笔记草稿
    Redis Cluster 集群使用(3)
  • 原文地址:https://www.cnblogs.com/acm-jing/p/4417588.html
Copyright © 2011-2022 走看看