鱼C论坛

 找回密码
 立即注册
查看: 2036|回复: 2

关于双链表任意数加减法问题

[复制链接]
发表于 2017-6-22 11:16:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
//有哪位同学能告诉我怎么整啊 为什么都是错555555
#include<stdio.h>
#include<stdlib.h>

#define DT sizeof(struct answer)//链表大小后面动态内存

struct answer *creat();//创建链表
void print(struct student *head);//打印链表

struct answer
{
        double num;//输入数
        char ch;//符号
        struct answer *next;//链接下一个链表
};
int n;//计数n 全局变量
void main()
{
        struct answer *w,*p;
        w = creat();
        p = w;
        print(p);

        printf("\n\n");
        system("pause");

}
struct answer creat()
{
        struct answer *head;
        struct answer *p1,*p2;

        p1 = p2 =(struct answer *)malloc(DT);
       
        printf("请输入数和符号:");
        scanf("%f %c",&p1->num,&p1->ch);

        head = NULL;
        n=0;
        while((p1->ch)!='=')
        {
                n++;
                if( 1 == n)
                {
                        head = p1;
                }
                else
                {
                        p2->next = p1;
                }

                p2 = p1;
                p1 =(struct student *)malloc(DT);
                scanf("%f %c",&p1->num,&p1->ch);
        }
        p2->next = NULL;
        return head;

}
void print(struct student *head)
{
        struct student *p;
        printf("\n There are %d records!\n\n",n);
        p = head;
        if(head)
        {
                do
                {
                        switch(p->ch)
                        {
                        case '+':(p->num) = (p->num) + (p->next->num);
                     (p->ch) = (p->next->ch);
                                         (p->next) = (p->next->next);
                                         p = head; break;
                        case '-':(p->num) = (p->num) - (p->next->num);
                     (p->ch) - (p->next->ch);
                                         (p->next) = (p->next->next);
                                         p = head; break;
                        }
                }while(p);
                printf("%f",p->num);

        }

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-22 12:36:52 | 显示全部楼层
只看到struct answer,木有看到struct student哦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-15 14:08:26 | 显示全部楼层
json 发表于 2017-6-22 12:36
只看到struct answer,木有看到struct student哦

3q
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 02:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表