消息关闭
    暂无新消息!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
    int code;
    char name[20];
    char type;
    int age;
    int chinese;
    int math;
    int total;
}stud[100];
struct node temp;
void menu();
int input();
void find(int);
void insert(int);
void deletel(int);
void print(int);
void sort(int);
void main()
{
    int n,c=0,d=0;
    while(1)
    {
        menu();
        scanf("%d",&n);
        switch(n)
        {
        case 1:system("cls");d=input();c+=d;break;
        case 2:system("cls");find(c);break;
        case 3:system("cls");insert(c);++c;break;
        case 4:system("cls");deletel(c);--c;break;
        case 5:system("cls");print(c);break;
        case 6:system("cls");sort(c);break;
        case 7:exit(0);break;
        default:
        printf("输入错误,请输入(1-7):\n");
        printf("按回车键继续...\n");
        getchar();
        }
    }
}
void menu()
{
    system("cls");
    printf("\t\t学生信息管理系统\n");
    printf("\t\t***************\n");
    printf("\t\t**  欢迎使用  **\n");
    printf("\t\t*[1]输入数据*\n");
    printf("\t\t*[2]查找数据*\n");
    printf("\t\t*[3]插入数据*\n");
    printf("\t\t*[4]删除数据*\n");
    printf("\t\t*[5]打印数据*\n");
    printf("\t\t*[6]排序数据*\n");
    printf("\t\t*[7]退出*\n");
    printf("\t\t**  欢迎使用 **\n");
    printf("\t\t***************\n");
    printf("\t\t请输入你的选项(1-7):");
}
int input()
{
    int i,n;
    printf("请输入要插入几位学生信息:\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("请输入第%d个学生学号:",i+1);
        scanf("%d",&stud[i].code);
        printf("第%d个学生姓名:",i+1);
        scanf("%s",&stud[i].name);
        getchar();
        printf("第%d个学生性别F/M:",i+1);
        scanf("%c",&stud[i].type);
        printf("第%d个学生年龄:",i+1);
        scanf("%d",&stud[i].age);
        printf("第%d个学生语文成绩:",i+1);
        scanf("%d",&stud[i].chinese);
        printf("第%d个学生数学成绩:",i+1);
        scanf("%d",&stud[i].math);
        stud[i].total=stud[i].chinese+stud[i].math;
    }
    return n;
}
void find(int c)
{
    int n,m,i,j,l;
    char k[20];
    system("cls");
    printf("输入1学号查找\n输入2按姓名查找\n");
    printf("请选择:");
    scanf("%d",&n);
    if(n==1)
    {
        printf("请输入学生学号:");
        scanf("%d",&m);
        for(i=0;i<c;i++)
        {
        if(stud[i].code==m)
        {
            printf("**********************\n");
            printf("学号:%d\n",stud[i].code);
            printf("姓名:%s\n",stud[i].name);
            printf("性别:%c\n",stud[i].type);
            printf("年龄:%d\n",stud[i].age);
            printf("语文成绩:%d\n",stud[i].chinese);
            printf("数学成绩:%d\n",stud[i].math);
            printf("总分:%d\n",stud[i].total);
            printf("**********************\n");
            printf("回车键返回\n");
            getchar();
            getchar();
        }
        }
    }
    else
        if(n==2)
        {
            getchar();
            printf("请输入姓名:");
            fgets(k,20,stdin);
            l=strlen(k);
            k[l-1]='\0';
            for(j=0;j<c;j++)
            {
                if(strcmp(k,stud[j].name)==0)
                {
                    printf("**********************\n");
                    printf("学号:%d\n",stud[j].code);
                    printf("姓名:%s\n",stud[j].name);
                    printf("性别:%c\n",stud[j].type);
                    printf("年龄:%d\n",stud[j].age);
                    printf("语文成绩:%d\n",stud[j].chinese);
                    printf("数学成绩:%d\n",stud[j].math);
                    printf("总分:%d\n",stud[j].total);
                    printf("**********************\n");
                    printf("回车键返回\n");
                    getchar();
                    getchar();
                }
            }
        }
        else
        {
            getchar();
            getchar();
        }
}
void insert(int c)
{
    int i,j;
    system("cls");
    printf("请输入要插入学生的信息:\n");
    printf("请输入学号:");
    scanf("%d",&temp.code);
    printf("请输入姓名:");
    scanf("%s",&temp.name);
    getchar();
    printf("请输入性别F/M:");
    scanf("%c",&temp.type);
    printf("请输入年龄:");
    scanf("%d",&temp.age);
    printf("请输入语文成绩:");
    scanf("%d",&temp.chinese);
    printf("请输入数学成绩:");
    scanf("%d",&temp.math);
    temp.total=temp.chinese+temp.math;
    for(i=0;i<c;i++)
    {
        if(temp.code<stud[i].code)
        {
            for(j=c;j>i;j--)
            {
            stud[j].code=stud[j-1].code;
            strcpy(stud[j].name,stud[j-1].name);
            stud[j].type=stud[j-1].type;
            stud[j].age=stud[j-1].age;
            stud[j].chinese=stud[j-1].chinese;
            stud[j].math=stud[j-1].math;
            stud[j].total=stud[j-1].total;
            }
        stud[j].code=temp.code;
        strcpy(stud[j].name,temp.name);
        stud[j].type=temp.type;
        stud[j].age=temp.age;
        stud[j].chinese=temp.chinese;
        stud[j].math=temp.math;
        stud[j].total=temp.total;
        break;
        }
    }
if(i>=c)
{
        stud[i].code=temp.code;
        strcpy(stud[i].name,temp.name);
        stud[i].type=temp.type;
        stud[i].age=temp.age;
        stud[i].chinese=temp.chinese;
        stud[i].math=temp.math;
        stud[i].total=temp.total;
}
}
void deletel(int c)
{
    int i,j,n;
    printf("请输入要删除的学号:\n");
    scanf("%d",&n);
    for(i=0;i<c;i++)
    {
        if(stud[i].code==n)
        {
        c=c-1;
        for(j=i;j<c;j++)
        {
            stud[j].code=stud[j+1].code;
            strcpy(stud[j].name,stud[j+1].name);
            stud[j].type=stud[j+1].type;
            stud[j].age=stud[j+1].age;
            stud[j].chinese=stud[j+1].chinese;
            stud[j].math=stud[j+1].math;
            stud[j].total=stud[j+1].total;
        }
        }
    }
    getchar();
    printf("按回车键继续...\n");
    getchar();
}
void print(int c)
{
    int i;
    system("cls");
    for(i=0;i<c;i++)
    {
        printf("********************\n");
        printf("学号:%d\n",stud[i].code);
        printf("姓名:%s\n",stud[i].name);
        printf("性别:%c\n",stud[i].type);
        printf("年龄:%d\n",stud[i].age);
        printf("语文成绩:%d\n",stud[i].chinese);
        printf("数学成绩:%d\n",stud[i].math);
        printf("总分:%d\n",stud[i].total);
        printf("**********************\n");
    }
    printf("按回车键继续...\n");
    getchar();
    getchar();
}
void sort(int c)
{
    int i,j,n;
    printf("学号排序\n");
    for(i=c-1;i>0;i--)
        for(j=0;j<i;j++)
            if(stud[j].code>stud[j+1].code)
            {
                temp.code=stud[j].code;
                strcpy(temp.name,stud[j].name);
                temp.type=stud[j].type;
                temp.age=stud[j].age;
                temp.chinese=stud[j].chinese;
                temp.math=stud[j].math;
                temp.total=stud[j].total;
                stud[j].code=stud[j+1].code;
                strcpy(stud[j].name,stud[j+1].name);
                stud[j].type=stud[j+1].type;
                stud[j].age=stud[j+1].age;
                stud[j].chinese=stud[j+1].chinese;
                stud[j].math=stud[j+1].math;
                stud[j].total=stud[j+1].total;
                stud[j+1].code=temp.code;
                strcpy(stud[j+1].name,temp.name);
                stud[j+1].type=temp.type;
                stud[j+1].age=temp.age;
                stud[j+1].chinese=temp.chinese;
                stud[j+1].math=temp.math;
                stud[j+1].total=temp.total;
            }
            getchar();
            getchar();
}

1个回答

︿ 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
    int code;
    char name[20];
    char type;
    int age;
    int chinese;
    int math;
    int total;
}stud[100];
struct node temp;
void menu();
int input();
void find(int);
void insert(int);
void deletel(int);
void print(int);
void sort(int);

int main()
{
    int n,c=0,d=0;
    while(1)
    {
        menu();
        scanf("%d",&n);
        switch(n)
        {
            case 1:system("cls");d=input();c+=d;break;
            case 2:system("cls");find(c);break;
            case 3:system("cls");insert(c);++c;break;
            case 4:system("cls");deletel(c);--c;break;
            case 5:system("cls");print(c);break;
            case 6:system("cls");sort(c);break;
            case 7:exit(0);break;
            default:
                   printf("输入错误,请输入(1-7):\n");
                   printf("按回车键继续...\n");
                   getchar();
        }
    }

    return 0;
}
void menu(void)
{
    system("cls");
    printf("\t\t学生信息管理系统\n");
    printf("\t\t***************\n");
    printf("\t\t**  欢迎使用  **\n");
    printf("\t\t*[1]输入数据*\n");
    printf("\t\t*[2]查找数据*\n");
    printf("\t\t*[3]插入数据*\n");
    printf("\t\t*[4]删除数据*\n");
    printf("\t\t*[5]打印数据*\n");
    printf("\t\t*[6]排序数据*\n");
    printf("\t\t*[7]退出*\n");
    printf("\t\t**  欢迎使用 **\n");
    printf("\t\t***************\n");
    printf("\t\t请输入你的选项(1-7):");
}
int input()
{
    int i,n;
    printf("请输入要插入几位学生信息:\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("请输入第%d个学生学号:",i+1);
        scanf("%d",&stud[i].code);
        printf("第%d个学生姓名:",i+1);
        scanf("%s", stud[i].name);
        getchar();
        printf("第%d个学生性别F/M:",i+1);
        scanf("%c",&stud[i].type);
        printf("第%d个学生年龄:",i+1);
        scanf("%d",&stud[i].age);
        printf("第%d个学生语文成绩:",i+1);
        scanf("%d",&stud[i].chinese);
        printf("第%d个学生数学成绩:",i+1);
        scanf("%d",&stud[i].math);
        stud[i].total = stud[i].chinese + stud[i].math;
    }
    return n;
}
void find(int c)
{
    int n,m,i,j,l;
    char k[20];
    system("cls");
    printf("输入1学号查找\n输入2按姓名查找\n");
    printf("请选择:");
    scanf("%d",&n);
    if(n==1)
    {
        printf("请输入学生学号:");
        scanf("%d",&m);
        for(i=0;i<c;i++)
        {
            if(stud[i].code==m)
            {
                printf("**********************\n");
                printf("学号:%d\n",stud[i].code);
                printf("姓名:%s\n",stud[i].name);
                printf("性别:%c\n",stud[i].type);
                printf("年龄:%d\n",stud[i].age);
                printf("语文成绩:%d\n",stud[i].chinese);
                printf("数学成绩:%d\n",stud[i].math);
                printf("总分:%d\n",stud[i].total);
                printf("**********************\n");
                printf("回车键返回\n");
                getchar();
                getchar();
            }
        }
    }
    else
        if(n==2)
        {
            getchar();
            printf("请输入姓名:");
            fgets(k,20,stdin);
            l=strlen(k);
            k[l-1]='\0';
            for(j=0;j<c;j++)
            {
                if(strcmp(k,stud[j].name)==0)
                {
                    printf("**********************\n");
                    printf("学号:%d\n",stud[j].code);
                    printf("姓名:%s\n",stud[j].name);
                    printf("性别:%c\n",stud[j].type);
                    printf("年龄:%d\n",stud[j].age);
                    printf("语文成绩:%d\n",stud[j].chinese);
                    printf("数学成绩:%d\n",stud[j].math);
                    printf("总分:%d\n",stud[j].total);
                    printf("**********************\n");
                    printf("回车键返回\n");
                    getchar();
                    getchar();
                }
            }
        }
        else
        {
            getchar();
            getchar();
        }
}
void insert(int c)
{
    int i,j;
    system("cls");
    printf("请输入要插入学生的信息:\n");
    printf("请输入学号:");
    scanf("%d",&temp.code);
    printf("请输入姓名:");
    scanf("%s", temp.name);
    getchar();
    printf("请输入性别F/M:");
    scanf("%c",&temp.type);
    printf("请输入年龄:");
    scanf("%d",&temp.age);
    printf("请输入语文成绩:");
    scanf("%d",&temp.chinese);
    printf("请输入数学成绩:");
    scanf("%d",&temp.math);
    temp.total=temp.chinese+temp.math;
    for(i=0;i<c;i++)
    {
        if(temp.code<stud[i].code)
        {
            for(j=c;j>i;j--)
            {
                stud[j].code=stud[j-1].code;
                strcpy(stud[j].name,stud[j-1].name);
                stud[j].type=stud[j-1].type;
                stud[j].age=stud[j-1].age;
                stud[j].chinese=stud[j-1].chinese;
                stud[j].math=stud[j-1].math;
                stud[j].total=stud[j-1].total;
            }
            stud[j].code=temp.code;
            strcpy(stud[j].name,temp.name);
            stud[j].type=temp.type;
            stud[j].age=temp.age;
            stud[j].chinese=temp.chinese;
            stud[j].math=temp.math;
            stud[j].total=temp.total;
            break;
        }
    }
    if(i>=c)
    {
        stud[i].code=temp.code;
        strcpy(stud[i].name,temp.name);
        stud[i].type=temp.type;
        stud[i].age=temp.age;
        stud[i].chinese=temp.chinese;
        stud[i].math=temp.math;
        stud[i].total=temp.total;
    }
}
void deletel(int c)
{
    int i,j,n;
    printf("请输入要删除的学号:\n");
    scanf("%d",&n);
    for(i=0;i<c;i++)
    {
        if(stud[i].code==n)
        {
            c=c-1;
            for(j=i;j<c;j++)
            {
                stud[j].code=stud[j+1].code;
                strcpy(stud[j].name,stud[j+1].name);
                stud[j].type=stud[j+1].type;
                stud[j].age=stud[j+1].age;
                stud[j].chinese=stud[j+1].chinese;
                stud[j].math=stud[j+1].math;
                stud[j].total=stud[j+1].total;
            }
        }
    }
    getchar();
    printf("按回车键继续...\n");
    getchar();
}
void print(int c)
{
    int i;
    system("cls");
    for(i=0;i<c;i++)
    {
        printf("********************\n");
        printf("学号:%d\n",stud[i].code);
        printf("姓名:%s\n",stud[i].name);
        printf("性别:%c\n",stud[i].type);
        printf("年龄:%d\n",stud[i].age);
        printf("语文成绩:%d\n",stud[i].chinese);
        printf("数学成绩:%d\n",stud[i].math);
        printf("总分:%d\n",stud[i].total);
        printf("**********************\n");
    }
    printf("按回车键继续...\n");
    getchar();
    getchar();
}
void sort(int c)
{
    int i,j,n;
    printf("学号排序\n");
    for(i=c-1;i>0;i--)
        for(j=0;j<i;j++)
            if(stud[j].code>stud[j+1].code)
            {
                temp      = stud[j];
                stud[j]   = stud[j+1];
                stud[j+1] = temp;
                /*
                temp.code=stud[j].code;
                strcpy(temp.name,stud[j].name);
                temp.type=stud[j].type;
                temp.age=stud[j].age;
                temp.chinese=stud[j].chinese;
                temp.math=stud[j].math;
                temp.total=stud[j].total;
                stud[j].code=stud[j+1].code;
                strcpy(stud[j].name,stud[j+1].name);
                stud[j].type=stud[j+1].type;
                stud[j].age=stud[j+1].age;
                stud[j].chinese=stud[j+1].chinese;
                stud[j].math=stud[j+1].math;
                stud[j].total=stud[j+1].total;
                stud[j+1].code=temp.code;
                strcpy(stud[j+1].name,temp.name);
                stud[j+1].type=temp.type;
                stud[j+1].age=temp.age;
                stud[j+1].chinese=temp.chinese;
                stud[j+1].math=temp.math;
                stud[j+1].total=temp.total;
                */
            }
    getchar();
    getchar();
}

两处错误,scanf输入name时,去掉前面的&;另外,排序时,不需要将结构体中的所有元素分别交换,直接给结构体赋值即可。详见上面的代码;
另外,二分查找需要先将程序执行一下排序(可以使用回调函数和函数指针),然后在使用折半查找。顺序查找不需要。