C语言程序设计学生籍贯信息记录簿
编制一个学生籍贯信息记录簿,每个学生信息包括:学号、姓名、籍贯。具体功能:
 (1)创建信息链表并以磁盘文件保存;
 (2)读取磁盘文件并显示输出所有学生的籍贯信息;
 (3)按学号或姓名查询其籍贯;
 (4)按籍贯查询并输出该籍贯的所有学生;
 (5)能添加、删除和修改学生的籍贯信息;
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>

typedef struct
{
    char id[12];
    char name[21];
fopen和open区别    char origin[101];
}student;

FILE *fp;
student stu,cache;

void Menu()
{
    system("cls");
    time_t t = time(0);
    char tmp[64];
    strftime(tmp, sizeof(tmp), "%Y/%m/%d %A",localtime(&t));
    puts(tmp);

    printf("\n");
    printf("1 --输入信息\n");
    printf("2 --显示所有信息\n");
    printf("3 --按学号查询\n");
    printf("4 --按姓名查询\n");
    printf("5 --按籍贯查询\n");
    printf("0 --退出\n\n\n请输入选项:");
}

void Creat_add()
{
    system("cls");
    fp=fopen("stu_origin.dat","rb+");
    if(fp==NULL)
    {
        fp=fopen("stu_origin.dat","wb+");
        if(fp==NULL)
        {printf("can't creat the file stu_origin.dat!\n");printf("Press any key ");getch();}
    }
    fseek(fp,0,SEEK_END);
    char next='y';
    while(next=='y'||next=='Y')
    {
    printf("请输入学号:");scanf("%s",stu.id);
    printf("请输入姓名:");scanf("%s",stu.name);
    printf("请输入籍贯:");scanf("%s",igin);
   
    printf("\n\n确定写入数据?(y/n)");
    if(getch()=='y'||getch()=='Y') fwrite(&stu,sizeof(stu),1,fp);

    printf("\n\n是否继续输入?(y/n)");next=getch();system("cls");
    }
    fclose(fp);
}
void Display_all()
{
    system("cls");
    fp=fopen("stu_origin.dat","rb");
    if(fp==NULL) {printf("can't open the file stu_origin.dat!\nPress any key to ");getch();}
    else
    {
    rewind(fp);printf("\n\n学号\t\t姓名\t\t籍贯\n");
    while(fread(&stu,sizeof(stu),1,fp))
    printf("%s\t%s\t%s\n",stu.id,stu.igin);

    fclose(fp);
    printf("\n\nPress any key to ");getch();
    }
}

int Check_Id(char *Id,char c)
{
    rewind(fp);
    while(fread(&cache,sizeof(stu),1,fp))
    {
        if(strcmp(Id,cache.id)==0)
        {
            printf("\n\n学号\t\t姓名\t\t籍贯\n");
            printf("%s\t%s\t%s\n",cache.id,cache.igin);
            return 0;
        }
    }
    printf("\n\n无此学生!\n");return 1;
}
int Check_Name(char *Name,char c)
{
    int n=0;
    rewind(fp);
    while(fread(&cache,sizeof(stu),1,fp))
    {
        if(strcmp(Name,cache.name)==0)
        {
            if(++n==1)printf("\n\n学号\t\t姓名\t\t籍贯\n");
            printf("%s\t%s\t%s\n",cache.id,cache.igin);
        }
    }
    if(!n){printf("\n\n无此学生!\n");return 1;}
    return 0;
}

int Check_Orgin(char *Origin,char c)
{
    int n=0;
    rewind(fp);
    while(fread(&cache,sizeof(stu),1,fp))
    {
        if(strcmp(igin)==0)
        {
            if(++n==1)printf("\n\n学号\t\t姓名\t\t籍贯\n");
            printf("%s\t%s\t%s\n",cache.id,cache.igin);
        }
    }
    if(!n){printf("\n\n无此学生!\n");return 1;}
    return 0;
}
void Search_id()
{
    fp=fopen("stu_origin.dat","rb");
    if(fp==NULL) {printf("can't open the file stu_origin.dat!\nPress any key to ");getch();}
    char k;
    do
    {system("cls");
    do{printf("请输入学号:");gets(stu.id);}while(Check_Id(stu.id,'s'));
    printf("\n\n继续查询?(y/n)");
    k=getch();
    }while(k=='y'||k=='Y');
   
    fclose(fp);
    printf("\nPress any key to ");getch();
}

void Search_name()
{
    fp=fopen("stu_origin.dat","rb");
    if(fp==NULL) {printf("can't open the file stu_origin.dat!\nPress any key to ");getch();}
    do
    {system("cls");
    do{printf("请输入姓名:");gets(stu.name);}while(Check_Name(stu.name,'s'));
    printf("\n\n继续查询?(y/n)");
    }while(getch()=='y'||getch()=='Y');
    fclose(fp);
    printf("\nPress any key to ");getch();
}
void Search_origin()
{
    fp=fopen("stu_origin.dat","rb");
    if(fp==NULL) {printf("can't open the file stu_origin.dat!\nPress any key to ");getch();}
    do
    {system("cls");
    do{printf("请输入籍贯:");igin);}while(Check_igin,'s'));
    printf("\n\n继续查询?(y/n)");
    }while(getch()=='y'||getch()=='Y');
    fclose(fp);
    printf("\nPress any key to ");getch();
}


void Call()
{
    char key=getch();
    if(key=='1') Creat_add();
    if(key=='2') Display_all();
    if(key=='3') Search_id();
    if(key=='4') Search_name();
    if(key=='5') Search_origin();
    if(key=='0') exit(1);
}

void main()
{
    while(1){Menu();Call();}
}

该程序还不能删除和修改信息

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。