|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 hy19970612 于 2012-2-20 23:12 编辑
请大家帮我看看 找找 BUG 我一定修改 !只是那个只有一个分数 !以后会继续完善!请大家帮忙找错!找BUG!- #include <stdio.h>//用的编译器为:Code::Blocks SVN 通过!vc6不给 不知道为什么!
- #include <stdlib.h>
- #include<windows.h>
- struct student//会逐完善!
- {
- int num;
- float fs;
- struct student *next;
- };
- // ------------------------ 函数声明区 ------------------------------//
- struct student *del(struct student *head);
- struct student *link(void);
- struct student *insert(struct student *head);
- struct student *insertxz(struct student *head);
- struct student * xzcz(int xz,struct student *phead);
- struct student * xg(struct student *head);
- void print_head(struct student *head);
- struct student * xg_xz(struct student *head);
- int print_head_xz(struct student *head);
- int xzreturn(int max,int min);// 最大值,最小值
- void xxlr(struct student *p);
- // ------------------------ 函数声明区 ------------------------------//
- int n = 0;
- int main()
- {
- printf("\t 以下任何情况中都您都可以用“Ctrl + C”组合键来整个结束程序!\n");
- int num = 0,xz = 0;
- struct student *head;
- printf("\n\n\t\t\t 欢迎进入学生分数管理系统!\n");
- printf("\n\t\t\t 开始创建动态链表!\n");
- head = link();
- printf("\n\t\t\t 创建完毕......");
- printf("\n\t\t\t 请选择操作......");
- while( 7 == 7)
- {
- printf("\n\t\t\t 1.删除 2.插入 3.输出 4.修改 5.退出");
- fflush( stdin );
- head = xzcz(xzreturn(6,0),head);
- }
- system("pause");
- return 0;
- }
- struct student *link(void)//测试无误!
- {
- struct student *p1,*p2,*head;
- p1 = p2 = (struct student*)malloc(sizeof(struct student));
- xxlr(p1);
- if(p1->num == 0)
- {
- return(head = NULL);
- }
- else
- {
- while(p1->num)
- {
- n++;
- if(n==1)
- {
- head = p1;
- }
- else
- {
- p2->next = p1;
- }
- p2 = p1;
- p1 = (struct student*)malloc(sizeof(struct student));
- xxlr(p1);
- }
- p2->next = NULL;
- return(head);
- }
- }
- void print_head(struct student * head)//测试无误!
- {
- if(head == NULL)
- {
- printf("\t\t\t 您这是空 Link,无法输出!\n");
- }
- else
- {
- printf("\n\t\t\t 一共 %d 位学生\n",n);
- while(head)
- {
- printf("\n\t\t\t 学号:%d \n\t\t\t 分数:%f\n",head->num,head->fs);
- head = head->next;
- }
- }
- }
- struct student *del(struct student *head)//测试无误!
- {
- struct student *p1,*p2;
- int num;
- p2 = p1 = head;
- if(p1 == NULL)
- {
- printf("\n\t\t\t 您这是空 Link 无法删除!\n");
- return head;
- }
- printf("\n\t\t\t 请输入您要删除结点的Num:");
- fflush(stdin);
- scanf("%d",&num);
- while(p1)
- {
- if(p1->num == num)
- {
- if(p1 == head)
- {
- p2 = p1->next;
- n--;
- head = p2;
- free(p1);
- printf("\n\t\t\t 删除成功......\n");
- return head;
- }
- else
- {
- p2 ->next = p1->next;
- free(p1);
- n--;
- printf("\n\t\t\t 删除成功......\n");
- return head;
- }
- }
- else
- {
- p2 = p1;
- p1 = p1->next;
- }
- }
- printf("\n\t\t 删除失败.....没有找到结点!\n");
- return head;
- }
- struct student *insertxz(struct student *head)
- {
- int xz,i;
- printf("\t\t\t 1.单次插入 2.多次插入\n");
- xz = xzreturn(2,0);
- if(1 == xz)
- {
- head = insert(head);
- return(head);
- }
- else if(2 == xz)
- { printf("\t\t\t 请输入要插入的次数!");
- i = xzreturn(99999,0);
- while(i--)
- {
- head = insert(head);
- }
- return(head);
- }
- }
- struct student *delxz(struct student *head)
- {
- int xz,i;
- printf("\t\t\t 1.单次删除 2.多次删除\n");
- xz = xzreturn(2,0);
- if(1 == xz)
- {
- head = del(head);
- return(head);
- }
- else if(2 == xz)
- { printf("\t\t\t 请输入要删除的次数!最多可以删除%d次\n",n);
- i = xzreturn(n,0);
- while(i--)
- {
- head = del(head);
- }
- return(head);
- }
- }
- int print_head_xz(struct student *head)
- {
- int xz,num;
- int i = n;
- struct student *p1;
- p1 = head;
- if(NULL == head)
- {
- printf("\n\t\t\t 此为空Link!\n");
- return (0);
- }
- printf("\t\t\t 1.查找输出 2.全部输出 \n");
- xz = xzreturn(2,0);
- if(1 == xz)
- {
- printf("\t\t\t 请输入学号:");
- fflush(stdin);
- scanf("%d",&num);
- while(i--)
- {
- if(head ->num != num || head->next != NULL)
- {
- p1 = head;
- head = head ->next;
- }
- else
- {
- continue;
- }
- }
- if(head == NULL)
- {
- printf("\t\t\t 没有找到您所输入的学号!");
- }
- else
- {
- printf("\n\t\t\t 学号:%d \n\t\t\t 分数:%f\n",p1->num,p1->fs);
- }
- }
- else if(2 == xz)
- {
- print_head(head);
- }
- }
- struct student *insert(struct student *head)
- {
- struct student *phead,*p1,*p2,*pinsert;
- p1 = p2 = phead = head;
- pinsert = (struct student *)malloc(sizeof(struct student));
- xxlr(pinsert);
- printf("\n\t\t\t 正在插入中,请稍后......\n");
- while(NULL == phead)
- {
- phead = pinsert;
- pinsert->next = NULL;
- printf("\n\t\t\t 插入成功......\n");
- n++;
- return(head = phead);
- }
- while(pinsert->num > p1->num && p1->next != NULL)
- {
- p2 = p1;
- p1 = p1->next;
- }
- if(pinsert->num <= p1->num)
- {
- if(p1->next == phead->next)
- {
- phead = pinsert;
- pinsert ->next = p2;
- printf("\n\t\t\t 插入成功......\n");
- n++;
- return(head = phead);
- }
- else
- {
- p2->next = pinsert;
- pinsert->next = p1;
- printf("\n\t\t\t 插入成功......\n");
- n++;
- return(head = phead);
- }
- }
- else
- {
- p1->next = pinsert;
- pinsert ->next = NULL;
- printf("\n\t\t\t 插入成功......\n");
- n++;
- return(head = phead);
- }
- return(head=phead);
- }
- int xzreturn(int max ,int min)//测试无误
- {
- int xz;
- char str[256];
- printf("\n\t\t\t 选项:");
- scanf("%s",str);
- xz = atoi(str);//Windows API 函数 atoi 把一个字符串变为整数型返回,如果字符串为数字则返回数字如果为字母符号返回0!
- while(xz > max || xz <= min)
- {
- printf("\n\t\t\t 错误......");
- printf("\n\t\t\t 选项:");
- fflush(stdin);
- scanf("%s",str);
- xz = atoi(str);
- }
- return(xz);
- }
- struct student *xzcz(int xz,struct student *phead)//测试无误
- {
- if(1 == xz)
- { if(1 == n)
- {
- phead = del(phead);
- }
- else
- {
- phead = delxz(phead);
- }
- }
- else if(2 == xz)
- {
- phead = insertxz(phead);
- }
- else if(3 == xz)
- {
- if(1 == n)
- {
- print_head(phead);
- }
- else
- {
- print_head_xz(phead);
- }
- }
- else if(4 == xz)
- {
- if(1 == n)
- {
- phead = xg(phead);
- }
- else
- {
- phead = xg_xz(phead);
- }
- }
- else if(5 == xz)
- {
- exit(0);
- }
- return(phead);
- }
- struct student * xg(struct student *head)
- {
- int i = n,num,scanfreturn;
- struct student *p1,*p2;
- p1 = p2 = head;
- if(NULL == p1)
- {
- printf("\n\t\t\t 此为空Link!\n");
- return (head);
- }
- printf("\t\t\t 请输入修改Num:");
- fflush(stdin);
- while(!(scanfreturn = scanf("%d",&num)))
- {
- printf("\t\t\t 对不起!您输入的不合理!请重新输入!\n");
- printf("\t\t\t 请输入修改Num:");
- fflush(stdin);
- }
- while(i--)
- {
- if(p1 ->num != num || p1->next != NULL)
- {
- p2 = p1;
- p1 = p1 ->next;
- }
- else
- {
- continue;
- }
- }
- if(p1 == NULL)
- {
- printf("\t\t\t 没有找到您所输入的学号!");
- return (head);
- }
- else
- {
- printf("\t\t\t 请输入分数:");
- fflush(stdin);
- scanf("%f",&p2->fs);
- while(p2->fs < 0 || p2->fs > 100)
- {
- printf("\t\t\t 对不起!您输入的不合理!请重新输入!\n");
- printf("\t\t\t 请输入分数:");
- fflush(stdin);
- scanf("%f",&p2->fs);
- }
- if( 1 == scanfreturn)
- {
- printf("\t\t\t 修改完成......\n");
- return(head);
- }
- else if(0 == scanfreturn)
- {
- printf("\t\t\t 修改失败......\n\t\t\t 请选择您输入的是否正确!");
- return(head);
- }
- }
- }
- struct student * xg_xz(struct student *head)
- {
- int xz,i;
- printf("\t\t\t 1.单次修改 2.多次修改\n");
- xz = xzreturn(2,0);
- if(1 == xz)
- {
- head = xg(head);
- return(head);
- }
- else if(2 == xz)
- { printf("\t\t\t 请输入要删除的次数!,最多可以修改%d次\n",n);
- i = xzreturn(n,0);
- while(i--)
- {
- head = xg(head);
- }
- return(head);
- }
- }
- void xxlr(struct student *p)
- {
- int scanfreturn = 0;
- bhlh:
- printf("\n\t\t\t 请输入学号:");
- fflush(stdin);
- scanfreturn = scanf("%d",&p->num);
- if(0 == scanfreturn)
- {
- printf("\t\t\t 对不起!您输入的不合理!请重新输入!\n");
- goto bhlh;
- }
- bhlf:
- printf("\t\t\t 请输入分数:");
- fflush(stdin);
- scanf("%f",&p->fs);
- if(p->fs < 0 || p->fs > 100)
- {
- printf("\t\t\t 对不起!您输入的不合理!请重新输入!\n");
- goto bhlf;
- }
- }
复制代码 |
|