Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28759
  • 博文数量: 7
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-21 09:13
文章存档

2011年(1)

2008年(6)

我的朋友
最近访客

分类: C/C++

2008-09-24 11:21:56

基于VC6.0和gcc调试通过

该程序实现两个多项是的相加减,还有乘,运行时提示输入: 同时输入每一项的系数和指数,完成输入以0和任意的数字结束
加减法已经完全的实现,乘法相乘之后没有合并指数相同的项
#include
#include
//----------------------------------------------------------
typedef struct counter{
    int coef;
    int exp;
    struct counter *next;
}node, *linklist;

//链表的创建
//----------------------------------------------------------
linklist creat_list()
{
    int i = 1;
    int coef;
    int exp;
    int flag = 1;
    node *head, *p, *q;
    p = head = (node *)malloc(sizeof(node));
    while (flag) {//利用尾差法创建链表
        printf("please input quantic %d coef & exp: ", i++);
        scanf("%d%d", &coef, &exp);
        if (coef != 0) {
            q = (node *)malloc(sizeof(node));
            q -> coef = coef;
            q -> exp = exp;
            p -> next = q;
            p = q;
        } else {
            flag = 0;
            p -> next = NULL;
        }
    }/*while*/
    return head;
}/*creat_list*/

//打印链表
//----------------------------------------------------------
void show_list(linklist p) {
    
    p = p-> next;
    while (p != NULL){
    if (p -> coef > 0)
        printf("+%dX^e%d ", p -> coef, p -> exp);
    else
        printf("%dX^e%d ", p -> coef, p -> exp);
    p = p-> next;
    }
}/*show_list*/

//进行多项式的 + or -
//----------------------------------------------------------
linklist count_list(linklist heada, linklist headb, char alg) {

    int sum;
    node *headc, *p, *q, *r, *s, *t;
    p = heada -> next;
    q = headb -> next;
    r = heada;
    t = headc = (node *)malloc(sizeof(node));
    while (p != NULL && q  != NULL) {
        if(p -> exp == q -> exp) {//指数相等执行系数的相加减
            switch (alg) {//alg判断是 + or -
                case '+' :
                    sum = p -> coef + q -> coef;
                    break;
                case '-' :
                    sum = p -> coef - q -> coef;
                    break;
            }/*switch*/
            if(sum != 0) {//系数相加不等于 0 则把系数付给 p,并删掉 q,同时让p 、q 下移一个节点
                p -> coef = sum;
                r -> next = p;
                r = p;
                p = p -> next;
                s = q;
                q = q -> next;
                free (s);
            } else { //系数等于 0 ,p、q下移一个节点,并删除上一个p、q节点
                s = p;
                p = p -> next;
                free (s);
                s = q;
                q = q -> next;
                free(s);
            }
        } else {//指数不等的情况
             if (p -> exp < q -> exp) {//p 的指数小于q的指数,让p下移一个节点
                r -> next = p;
                r = p;
                p = p -> next;
             } else {//p的指数大于q的指数,把q插进去,并让q后移一个节点
                switch (alg) {
                    case '+' :       
                    r -> next = q;
                    r = q;
                    q = q -> next;
                    break;
                    case '-' :
                    q -> coef = -q -> coef;
                    r -> next = q;
                    r = q;
                    q = q -> next;
                    break;
                }
            }
        }
    }/*while*/
    if (p != NULL){//如果p 还有元素,让r只想p所剩下的节点
        r -> next = p;
    } else {
        while (q != NULL) {//如果q还有元素,判断+or-,让r慢慢向下移
            switch (alg) {
                case '+' :
                    r -> next = q;
                    r =q;
                    q = q -> next;
                    break;
                case '-' :
                    q -> coef = - q -> coef;
                    r -> next = q;
                    r = q;
                    q = q -> next;
            }
        }/*while*/
    }
    return heada;
}/*count_list*/

//多项式相乘
//----------------------------------------------------------
linklist ride_list(linklist heada, linklist headb) {
    int num;
    int sum;
    node *headc, *p, *q, *s, *t;
    p = heada -> next;
    t = headc = (node *)malloc(sizeof(node));
    while (p != NULL) {//创建一个新的链表用于存储两个多项式相乘的各项
        q = headb -> next;
        while(q != NULL) {
            sum = p -> coef * q -> coef;
            num = p -> exp + q -> exp;
            q = q -> next;
            s = (node *)malloc(sizeof(node));
            s -> coef = sum;
            s -> exp = num;
            t -> next = s;
            t = s;
        }
        p = p -> next;
    }
    t -> next = NULL;
    s = headc;   
    return headc;
}/*ride_list*/

//----------------------------------------------------------
int main() {   
    int status;
    int coef;
    int exp;
    char alg;
    char ch;
    node *p, *q, *r;
    p = NULL;
    q = NULL;
    do {
        printf("\nplease input your quantic and the coef is 0 to end\n");
        printf("\nplease input your first quantic\n\n");
       
        p =    creat_list(p, coef, exp); //创建地一个多项式
       
        printf("\nplease input your second quantic\n\n");
       
        q =    creat_list(q, coef, exp); //创建第二个多项式
       
        printf("please input the algorithm : ");
        alg = getchar();
        alg = getchar();//运算符的输入
        printf("the quantic: \n( ");
        show_list(p);
        printf(") %c ( ", alg);
        show_list(q);
        printf(") = ");
        printf("\n");
        if (alg == '*') {//选择相应的运算
            r = ride_list(p, q);//进行乘运算
        } else {
            r = count_list(p, q, alg);//进行加减运算
        }
        show_list(r);
        printf("\n");
        printf("do you want to go on ? (press 'y' to go on or press any key to exit): ");
        ch = getchar();
        fflush(stdin);
        if (ch == 'y' )
              status = 1;
            else
              status = 0;
    } while (status == 1);
    return 0;
}/*main*/
阅读(1475) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~