注册 登录
编程论坛 数据结构与算法

帮忙,分析这个程序

发布于 2010-04-28 09:49, 478 次点击
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>

typedef struct stu
{
    char Name[15];
    char Id[15];
    int Com;
    int Math;
    int Eng;
    int Phy;
    stu * next;
}stu;
stu* s = NULL;
stu* Luru(void);
void Xiugai(stu* head);
void Delete(stu* head);
void Chaxun(stu* head);
int main()
{
    int n;
    printf("录入学生信息按1\n修改学生信息按2\n删除学生信息按3\n查询学生信息按4\n退出按0\n");
    scanf("%d",&n);
    while(n!=0){
            if(n==1) s=Luru();
            if(n==2) Xiugai(s);
            if(n==3) Delete(s);
            if(n==4) Chaxun(s);
            scanf("%d",&n);
            }
            system("pause");
            return 0;
}

stu* Luru(void)
{
    struct stu * head = NULL,*p,*q;
    int i,m;
    printf("请输入学生个数:");
        scanf("%d",&m);
    p=( stu * ) malloc(sizeof( stu));
    if(p==NULL) {
        printf("分配空间失败");
        exit(0);
    }        
    head=p;
    for(i=1;i<=m;i++){
        q=(struct stu * )malloc(sizeof(struct stu));
        if(q==NULL) {
        printf("分配空间失败");
        exit(0);
        }
            printf("姓名 学号 计算机 数学 英语 物理:");
            scanf("%s %s %d %d %d %d",q->Name,q->Id,&q->Com,&q->Math,&q->Eng,&q->Phy);
            p->next=q;
            p=q;
    }
    p->next=NULL;
    printf("信息添加成功!\n");
    return head;
}
void Xiugai(stu* head)
{
    if( !head )
    {
        printf("No students msg!\n");
        return;
    }
    char a[15];
    stu* p;
    int k,i;
    printf("请输入要修改的学生数:");
    scanf("%d",&k);
    for(i=1;i<=k;i++){
        printf("请输入要修改的学生的学号:");
        scanf("%s",a);
        p=head;
        p=p->next;
        while(strcmp(a,p->Id)!=0)
        p=p->next;
        if(strcmp(a,p->Id)==0){
        printf("请重新输入该学生的信息:");
        scanf("%s %s %d %d %d %d",p->Name,p->Id,&p->Com,&p->Math,&p->Eng,&p->Phy);
        }
        else printf("学号有误,未找到");
        
    }
   
}
void Chaxun(stu* head)
{
    if( !head )
    {
        printf("No students msg!\n");
        return;
    }
        char a[15];
        stu* p;
        printf("请输入要查询学生的学号:(退出按0)");
        scanf("%s",a);
        while(strcmp(a,"0")!=0){
            p=head;
             p=p->next;
            while(strcmp(a,p->Id)!=0)
            p=p->next;
            if(strcmp(a,p->Id)==0)
                printf("姓名:%s 学号:%s 计算机:%d 数学:%d 英语:%d 物理:%d",p->Name,p->Id,p->Com,p->Math,p->Eng,p->Phy);
             else printf("学号有误。");
            scanf("%s",a);
        }
}
void Delete(stu* head){
    if( !head )
    {
        printf("No students msg!\n");
        return;
    }
    char a[15];
    stu* p;
    stu* q;
    printf("请输入要删除学生的学号:(退出按0)");
    scanf("%s",a);
    while(strcmp(a,"0")!=0){
        p=head;
        while(strcmp(a,p->next->Id)!=0)
            p=p->next;
        if(strcmp(a,p->next->Id)==0){
            q=p->next;
            p->next=p->next->next;
            free(q);
        }
        scanf("%s",a);
    }
}
帮忙,分析这个程序吧???谢谢
1 回复
#2
2010-04-28 17:20
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>

typedef struct stu
{
    char Name[15];
    char Id[15];
    int Com;
    int Math;
    int Eng;
    int Phy;
    stu * next;
}stu;//定义了一个存储学生信息链表
stu* s = NULL;
stu* Luru(void);
void Xiugai(stu* head);
void Delete(stu* head);
void Chaxun(stu* head);
int main()
{
    int n;
    printf("录入学生信息按1\n修改学生信息按2\n删除学生信息按3\n查询学生信息按4\n退出按0\n");
    scanf("%d",&n);
    while(n!=0){
            if(n==1) s=Luru();
            if(n==2) Xiugai(s);
            if(n==3) Delete(s);
            if(n==4) Chaxun(s);
            scanf("%d",&n);
            }
            system("pause");
            return 0;
}

stu* Luru(void)
{
    struct stu * head = NULL,*p,*q;
    int i,m;
    printf("请输入学生个数:");
        scanf("%d",&m);
    p=( stu * ) malloc(sizeof( stu));//申请动态存储空间
    if(p==NULL) {
        printf("分配空间失败");
        exit(0);
    }        
    head=p;
    for(i=1;i<=m;i++){
        q=(struct stu * )malloc(sizeof(struct stu));
        if(q==NULL) {
        printf("分配空间失败");
        exit(0);
        }
            printf("姓名 学号 计算机 数学 英语 物理:");
            scanf("%s %s %d %d %d %d",q->Name,q->Id,&q->Com,&q->Math,&q->Eng,&q->Phy);
            p->next=q;
            p=q;
    }
    p->next=NULL;
    printf("信息添加成功!\n");
    return head;
}
void Xiugai(stu* head)
{
    if( !head )//链表为空则 没有信息
    {
        printf("No students msg!\n");
        return;
    }
    char a[15];
    stu* p;
    int k,i;
    printf("请输入要修改的学生数:");
    scanf("%d",&k);
    for(i=1;i<=k;i++){
        printf("请输入要修改的学生的学号:");
        scanf("%s",a);
        p=head;
        p=p->next;
        while(strcmp(a,p->Id)!=0)//判断学好是否相等
        p=p->next;
        if(strcmp(a,p->Id)==0){
        printf("请重新输入该学生的信息:");
        scanf("%s %s %d %d %d %d",p->Name,p->Id,&p->Com,&p->Math,&p->Eng,&p->Phy);
        }
        else printf("学号有误,未找到");
        
    }
   
}
void Chaxun(stu* head)
{
    if( !head )
    {
        printf("No students msg!\n");
        return;
    }
        char a[15];
        stu* p;
        printf("请输入要查询学生的学号:(退出按0)");
        scanf("%s",a);
        while(strcmp(a,"0")!=0){
            p=head;
             p=p->next;
            while(strcmp(a,p->Id)!=0)
            p=p->next;
            if(strcmp(a,p->Id)==0)
                printf("姓名:%s 学号:%s 计算机:%d 数学:%d 英语:%d 物理:%d",p->Name,p->Id,p->Com,p->Math,p->Eng,p->Phy);
             else printf("学号有误。");
            scanf("%s",a);
        }
}
void Delete(stu* head){
    if( !head )
    {
        printf("No students msg!\n");
        return;
    }
    char a[15];
    stu* p;
    stu* q;
    printf("请输入要删除学生的学号:(退出按0)");
    scanf("%s",a);
    while(strcmp(a,"0")!=0){
        p=head;
        while(strcmp(a,p->next->Id)!=0)
            p=p->next;
        if(strcmp(a,p->next->Id)==0){
            q=p->next;
            p->next=p->next->next;
            free(q);
        }
        scanf("%s",a);
    }
}
1