| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 460 人关注过本帖
标题:哪位高人帮我改一下代码
只看楼主 加入收藏
腊月寒风
Rank: 1
等 级:新手上路
帖 子:6
专家分:3
注 册:2010-10-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
哪位高人帮我改一下代码
//实现简单的学生成绩信息管理软件。
   // 学生信息包括:学号、姓名、4门课程的成绩(计算机,数学,英语,物理)。
   // 要实现的功能:学生信息的录入、修改、删除和查询。
#include<stdio.h>
int n;
struct student
    {
        int id;
        char name[20];
        float com,math,en,phy;
    }stu[20];
    struct student *a=&stu[0];
void record(struct student *p)
{
    int i;
    printf("Input the number of students.\n");
    scanf("%d",&n);
    printf("Input the information of each student.\n");
    for(i=0;i<n;i++)
        scanf("%d%s%f%f%f%f",&(p+i)->id,(p+i)->name,&(p+i)->com,&(p+i)->math,&(p+i)->en,&(p+i)->phy);
    getchar();
}
void correct(struct student *p)
{
    int i,b,flag=1;
    printf("Input the ID of a student which you want to correct.\n");
    scanf("%d",&b);
    for(i=0;i<n;i++)
       if(b==(p+i)->id)
       {
          scanf("%d%s%f%f%f%f",&(p+i)->id,(p+i)->name,&(p+i)->com,&(p+i)->math,&(p+i)->en,&(p+i)->phy);
          flag=0;
       }
       if(flag)
        printf("No such student.\n");
       getchar();
}
void Delete(struct student *p)
{
    int i,b,flag=1;
    printf("Input the ID of a student you want to delete.\n");   
    scanf("%d",&b);
    for(i=0;i<n;i++)
        if(b==(p+i)->id)
        {
            (p+i)->id='\0';
            flag=0;
        }
        if(flag)
        printf("No such student.\n");
        getchar();
}
void search(struct student *p)
{
    int i,b,flag=1;
    printf("Input the ID of a student you want to search.\n");   
    scanf("%d",&b);
    for(i=0;i<n;i++)
        if(b==(p+i)->id)
        {  
            printf("%-4d%-10s%-6.1f%-6.1f%-6.1f%-6.1f\n",(p+i)->id,(p+i)->name,(p+i)->com,(p+i)->math,(p+i)->en,(p+i)->phy);
            flag=0;
        }
        if(flag)
        printf("No such student.\n");
        getchar();
}
int main()
{
    char c;
    while(1)
    {
        printf("A record  B correct  C delete  D search  E end\n");
        scanf("%c",&c);
        if(c=='A')       record(a);
        else if(c=='B')  correct(a);
        else if(c=='C')  Delete(a);
        else if(c=='D')  search(a);
        else if(c=='E')  break;
    }
    return 0;
}
//第二次输入学生数据会把以前的数据覆盖;希望帮我改一下,谢谢!




   




搜索更多相关主题的帖子: 管理软件 计算机 学生成绩 英语 姓名 
2011-03-02 21:29
baobaoisme
Rank: 7Rank: 7Rank: 7
来 自:AVATAR
等 级:黑侠
帖 子:260
专家分:506
注 册:2010-7-9
收藏
得分:7 
你程序写的就是这个功能,每次带入的都是数组的起始地址,自然就覆盖数据了
你要保留数据,那么就返回记录的位置或者返回指针,也可以选择改函数参数,具体实现方法看你自己想法了
2011-03-02 21:44
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:7 
如上楼所说,程序改动如下:
#include<stdio.h>
int n=0;
struct student
    {
        int id;
        char name[20];
        float com;
        float math;
        float en;
        float phy;
    }stu[20];
    struct student *a=&stu[0];
struct student *record(struct student *p)
{
    int i;
    printf("Input the number of students.\n");
    scanf("%d",&n);
    printf("Input the information of each student.\n");
    for(i=0;i<n;i++)
    scanf("%d%s%f%f%f%f",&(p+i)->id,(p+i)->name,&(p+i)->com,&(p+i)->math,&(p+i)->en,&(p+i)->phy);
    getchar();
    p=&stu[n];
    return p;
}
void correct(struct student *p)
{
    int i,b,flag=1;
    printf("Input the ID of a student which you want to correct.\n");
    scanf("%d",&b);
    for(i=0;i<n;i++)
       if(b==(p+i)->id)
       {
          scanf("%d%s%f%f%f%f",&(p+i)->id,(p+i)->name,&(p+i)->com,&(p+i)->math,&(p+i)->en,&(p+i)->phy);
          flag=0;
       }
       if(flag)
       printf("No such student.\n");
       getchar();
}
void Delete(struct student *p)
{
    int i,b,flag=1;
    printf("Input the ID of a student you want to delete.\n");   
    scanf("%d",&b);
    for(i=0;i<n;i++)
        if(b==(p+i)->id)
        {
            (p+i)->id=0;
            flag=0;
        }
        if(flag)
        printf("No such student.\n");
        getchar();
}
void search(struct student *p)
{
    int i,b,flag=1;
    printf("Input the ID of a student you want to search.\n");   
    scanf("%d",&b);
    for(i=0;i<n;i++)
        if(b==(p+i)->id)
        {  
            printf("%-4d%-10s%-6.1f%-6.1f%-6.1f%-6.1f\n",(p+i)->id,(p+i)->name,(p+i)->com,(p+i)->math,(p+i)->en,(p+i)->phy);
            flag=0;
        }
        if(flag)
        printf("No such student.\n");
        getchar();
}
int main()
{
    char c;
    struct student *p=&stu[0];
    while(1)
    {
        printf("A record  B correct  C delete  D search  E end\n");
        scanf("%c",&c);
        if(c=='A')       p=record(p);
        else if(c=='B')  correct(a);
        else if(c=='C')  Delete(a);
        else if(c=='D')  search(a);
        else if(c=='E')  break;
    }
    return 0;
}

   唯实惟新 至诚致志
2011-03-02 22:29
biancku
Rank: 2
等 级:论坛游民
帖 子:41
专家分:19
注 册:2010-11-30
收藏
得分:7 
将record函数修改一下:
student * record(struct student *p)
{
    int i;
    printf("Input the number of students.\n");
    scanf("%d",&n);
    printf("Input the information of each student.\n");
    for(i=0;i<n;i++)
        scanf("%d%s%f%f%f%f",&(p+i)->id,(p+i)->name,&(p+i)->com,&(p+i)->math,&(p+i)->en,&(p+i)->phy);
    return  p+i;
}
主函数修改一下:
int main()
{
    char c;
    student * st=stu;
    while(1)
    {
        printf("A record  B correct  C delete  D search  E end.\n");
        scanf("%c",&c);
        if(c=='A')       st=record(st);
        else if(c=='B')  correct(a);
        else if(c=='C')  Delete(a);
        else if(c=='D')  search(a);
        else if(c=='E')  break;
    }
    return 0;
}
原因就像楼主所说。
2011-03-02 22:36
腊月寒风
Rank: 1
等 级:新手上路
帖 子:6
专家分:3
注 册:2010-10-16
收藏
得分:0 
谢谢指导
2011-03-03 00:05
快速回复:哪位高人帮我改一下代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012957 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved