xuduo1017 2015-06-13 17:22 采纳率: 100%
浏览 2958
已采纳

C语言:在子函数中修改结构变量中元素的值

要写一个处理学生成绩信息的程序,使用单向链表,创建,遍历已经没有问题,但在修改结点的数据时出现问题,输入数据后程序就停止运行。
修改的思路是先根据学号定位到指定结点,然后修改数据,修改函数如下

 void Correct(float *a,float *b,float *c,float *d,float *e,float *f)
{
    printf("请依次输入学生正确的的英语 数学 物理 C语言成绩\n");
    scanf("%f%f%f%f",a,b,c,d);
    *e=*a+*b+*c+*d;
    *f=*e/4;
}

程序执行到上面时总是出现问题,不知道是什么原因,也尝试过其他修改的方法:只传结点的地址,同样出现问题;查找与修改函数合并,找到结点直接修改也是有问题,不知道为什么,代码有些长,还希望有人可以指导一下,非常感谢。
完整代码如下:

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu{
    char num[15];
    char name[20];
    float EngSco;
    float MathSco;
    float PhySco;
    float CSco;
    float TotalSco;
    float AveSco;
    int GoOn;
    struct stu * next;
};
void PrtMenu();
void CreatList(struct stu **headp);/*创建链表并输入数据*/
void PrtInf(struct stu *headp);/*输出链表中的某些数据*/
void Correct(float *a,float *b,float *c,float *d,float *e,float *f);/*修改链表中某结点元素的数据*/
void PrtData(struct stu *headp);/*输出链表中的某些数据*/
struct stu* Search(struct stu *headp);/*定位需要修改的结点的地址*/
int main()
{
    int choice; 
    struct stu *head=NULL,*revise=NULL;
Order: 
    PrtMenu();
    scanf("%d",&choice);
    if(choice<0||choice>5){
        printf("指令错误,请重新输入指令\n");
        goto Order;
    }
    else
    switch(choice)
    {
        case 0:
            system("CLS");
            break;
        case 1:
            CreatList(&head);break;
        case 2:
            PrtInf(head);break;
        case 3:
            revise=Search(head); 
            Correct(&(revise->EngSco),&(revise->MathSco),&(revise->PhySco),&(revise->CSco),&(revise->TotalSco),&(revise->AveSco));break;
        case 4:
            PrtData(head);break;
        case 5:
            return 0;
    }
    goto Order;
    return 0;
}
void PrtMenu()
{
    printf("-----功能选择-----\n");
    printf("0.清屏并显示菜单\n");
    printf("1.输入学生信息\n");
    printf("2.输出学生各项信息\n");
    printf("3.修改学生指定数据项的内容\n");
    printf("4.输出各位同学的学号、姓名、四门课的总成绩和平均成绩\n");
    printf("5.退出系统\n");
    printf("------------------\n");
    printf("请输入指令前的序号\n");
} 
void CreatList(struct stu **headp)
{
    struct stu *loc_head=NULL,*tail;
    loc_head=(struct stu*)malloc(sizeof(struct stu));
    printf("请输入学生的学号\n");
    scanf("%s",loc_head->num);
    printf("请输入学生的姓名\n");
    scanf("%s",loc_head->name);
    printf("请依次输入学生的英语 数学 物理 C语言成绩\n");
    scanf("%f%f%f%f",&loc_head->EngSco,&loc_head->MathSco,&loc_head->PhySco,&loc_head->CSco);
    loc_head->TotalSco=loc_head->EngSco+loc_head->MathSco+loc_head->PhySco+loc_head->CSco;
    loc_head->AveSco=loc_head->TotalSco/4;
    tail=loc_head;
    printf("继续输入请按1,输入完成请按0\n");
    scanf("%d",&tail->GoOn);
    if(tail->GoOn==0)
    goto end;
    else
    while(1)
    {
        tail->next=(struct stu*)malloc(sizeof(struct stu));
        tail=tail->next;
        printf("请输入学生的学号\n");
        scanf("%s",tail->num);
        printf("请输入学生的姓名\n");
        scanf("%s",tail->name);
        printf("请依次输入学生的英语 数学 物理 C语言成绩\n");
        scanf("%f%f%f%f",&tail->EngSco,&tail->MathSco,&tail->PhySco,&tail->CSco);
        tail->TotalSco=tail->EngSco+tail->MathSco+tail->PhySco+tail->CSco;
        tail->AveSco=tail->TotalSco/4;
        printf("继续输入请按1,输入完成请按0\n");
        scanf("%d",&tail->GoOn);
        if(!tail->GoOn)
        break; 
    }
end:
    tail->next=NULL;
    *headp=loc_head;
}
void PrtInf(struct stu *headp)
{
    struct stu *current=headp;
    printf("学号\t姓名\t英语\t高数\t物理\tC语言\t\n");
    while(current)
    {
        printf("%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t\n",current->num,current->name,current->EngSco,current->MathSco,current->PhySco,current->CSco);
        current=current->next;
    }
}
void Correct(float *a,float *b,float *c,float *d,float *e,float *f)
{
    printf("请依次输入学生正确的的英语 数学 物理 C语言成绩\n");
    scanf("%f%f%f%f",a,b,c,d);
    *e=*a+*b+*c+*d;
    *f=*e/4;
}
void PrtData(struct stu *headp)
{
    struct stu *current=headp;
    printf("学号\t姓名\t总分\t平均分\t\n");
    while(current)
    {
        printf("%s\t%s\t%.2f\t%.2f\t\n",current->num,current->name,current->TotalSco,current->AveSco);
        current=current->next;
    }
}
struct stu* Search(struct stu *headp)
{
    struct stu *current=headp;
    char aim[15];
    int flag;
    printf("请输入要修改数据的学生的学号\n");
    scanf("%s",aim);
    while(current)
    {
    flag=strcmp(current->num,aim);
    if(flag=0)
    break;
    else current=current->next;
    }
    return current;
}
  • 写回答

2条回答 默认 最新

  • 纵横车 2015-06-13 19:27
    关注

    是前面Search()函数出的问题,if()中的要是“==”=======================================。。。。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)