學生信息管理系統(C語言)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct student
{
    int id;
    char name[20];
    int age;
    char sex;
    char birthday[20];
    char address[20];
    char phone[15];
    char email[30];
    struct student *next;
}student;

student *head = NULL;
int length;
void create()
{
    student *p1,*p2;
    length = 0;
    p1 = (student *)malloc(sizeof(student));
    p1->id = -1;
    if(head == NULL)
    {
        head = p1;
    }
    printf("請輸入學生的學號、姓名、年齡、性別、出生年月、地址、電話、電子郵箱:\n");
    while(1)
    {
        p2 = (student *)malloc(sizeof(student));
        scanf("%d %s %d %c %s %s %s %s",&p2->id,p2->name,&p2->age,&p2->sex,&p2->birthday,&p2->address,p2->phone,p2->email);
        if(p2->id == 0)
        {
            printf("鏈表創建完成!\n");
            break;
        }
        length ++;
        p1->next = p2;
        p2->next = NULL;
        p1 = p1->next;
    }
    return ;
}

void LoadStudentInFromFile()
{
    student *p,*q;
    int c;
    FILE* f;
    f = fopen("input.txt","rb");
    if(f == NULL)
    {
        return ;
    }
    fseek(f,0,SEEK_SET);
    p = (student *)malloc(sizeof(student));
    p->next = NULL;
    head = p;
    while(!feof(f))
    {
        c = fgetc(f);
        if(c != -1)
        {
            fseek(f,-1,SEEK_CUR);
        }
        else
        {
            return ;
        }
        q = (student *)malloc(sizeof(student));
        fscanf(f,"%d",&q->id);
        fscanf(f,"%s",q->name);
        fscanf(f,"%d",&q->age);
        fscanf(f,"%c",&q->sex);
        fscanf(f,"%s",q->birthday);
        fscanf(f,"%s",q->address);
        fscanf(f,"%s",q->phone);
        fscanf(f,"%s",q->email);
        q->next = NULL;
        p->next = q;
        p = p->next;
        length ++;//鏈表長度
    }
}

void ModifyStudentInfo()
{
    student *p = head->next;
    int num;
    printf("請輸入要修改的學生的學號:");
    scanf("%d",&num);
    while(p != NULL)
    {
        if(p->id == num)
        {
            printf("修改前,學號為%d的學生信息如下:\n",num);
            printf("%d %s %d %c %s %s %s %s",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email);
            printf("請輸入學生的新電話:");
            getchar();
            gets(p->phone);
            printf("請輸入學生的新地址:");
            gets(p->address);
            printf("修改后,學號為%d的學生信息如下:\n",num);
            printf("%d %s %d %c %s %s %s %s",&p->id,p->name,&p->age,p->sex,p->birthday,p->address,p->phone,p->email);
            return ;
        }
        p = p->next;
    }
    if(p == NULL)
    {
        printf("該學號不存在!\n");
        return ;
    }
}
void display()
{
    student *p = head->next;
    printf("鏈表中所有的學生信息如下:\n");
    while(p != NULL)
    {
        printf("%d %s %d %c %s %s %s %s",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email);
        printf("\n");
        p = p->next;
    }
    return ;
}

void search()
{
    int num,x;
    char name[20];
    student *p = head->next;
    printf("請選擇查詢方式:\n");
    printf("1、按學號查詢\t2、按姓名查詢\n");
    scanf("%d",&x);
    if(x == 1)
    {
        printf("需要查找的學生學號為:");
        scanf("%d",num);
        while(p != NULL)
        {
            if(p->id == num)
            {
                printf("學號為%d的學生信息如下:\n",num);
                printf("%d %s %d %c %s %s %s %s",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email);
                return ;
            }
            p = p->next;
        }
        if(p == NULL)
        {
            printf("無此記錄!\n");
        }
    }
    else if(x == 2)
    {
        printf("需要查找的學生姓名為:");
        getchar();
        gets(name);
        p = head->next;
        while(p != NULL)
        {
            if(strcmp(p->name,name) == 0)
            {
                printf("學生姓名為%s的學生信息如下:\n",name);
                printf("%d %s %d %c %s %s %s %s",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email);
                return ;
            }
            p = p->next;
        }
        if(p == NULL)
        {
            printf("無此記錄!\n");
        }
    }
    return ;
}

void insert()
{
    int num,i;
    student *p,*q;
    p = head;

    printf("請輸入你要插入的位置:");
    scanf("%d",&num);
    if(num > length)
    {
        printf("找不到插入的位置\n");
        return ;
    }
    else
    {
        printf("請輸入你要插入的學生的信息:\n");
        q = (student *)malloc(sizeof(student));
        scanf("%d %s %d %c %s %s %s %s",&q->id,q->name,&q->age,&q->sex,q->birthday,q->address,q->phone,q->email);
        while(p != NULL)
        {
            if(p->id == q->id)
            {
                printf("該學號已經存在,無法插入!\n");
                return ;
            }
            p = p->next;
        }
        p = head;
        for(i=0; i<num; ++i)
        {
            p = p->next;
        }
        q->next = p->next;
        p->next = q;
        length ++;
        printf("插入成功!\n");
        return ;
    }
}

void Delete()
{
    int num;
    student *p,*q;
    q = head;
    p = head->next;
    printf("請輸入要刪除的學生的學號:\n");
    scanf("%d",&num);

    while(p != NULL)
    {
        if(p->id == num)
        {
            q->next = p->next;
            free(p);
            length --;
            printf("刪除成功!\n");
            return ;
        }
        p = p->next;
        q = q->next;
    }
    if(p == NULL)
    {
        printf("找不到要刪除的編號!\n");
        return ;
    }
}

void menu()
{
    printf("___________________________________________________\n");
    printf("|        學生信息管理系統          |\n");
    printf("|        0、退出系統              |\n");
    printf("|        1、錄入學生信息              |\n");
    printf("|        2、建立鏈表              |\n");
    printf("|        3、顯示鏈表              |\n");
    printf("|        4、查找鏈表中的某個元素          |\n");
    printf("|        5、刪除鏈表中指定學號的結點      |\n");
    printf("|        6、指定位置上插入一個新結點      |\n");
    printf("|        7、修改學生信息              |\n");
    printf("__________________________________________________\n");
    return ;
}

int main(void)
{
    int a;
    menu();
    while(1)
    {
        printf("請輸入相應的功能:");
        scanf("%d",&a);
        switch(a)
        {
        case 0:
            return 0;
        case 1:
            LoadStudentInFromFile();
            menu();
            break;
        case 2:
            create();
            menu();
            break;
        case 3:
            if(head)
            {
                display();
                menu();
            }
            else
            {
                printf("鏈表為空,請先建立鏈表!\n");
                menu();
            }
            break;
        case 4:
            if(head)
            {
                search();
                menu();
            }
            else
            {
                printf("鏈表為空,請先建立鏈表!\n");
                menu();
            }
            break;
        case 5:
            if(head)
            {
                Delete();
                menu();
            }
            else
            {
                printf("鏈表為空,請先建立鏈表!\n");
                menu();
            }
            break;
        case 6:
            if(head)
            {
                insert();
                menu();
            }
            else
            {
                printf("鏈表為空,請先建立鏈表!\n");
                menu();
            }
            break;
        case 7:
            if(head)
            {
                ModifyStudentInfo();
                menu();
            }
            else
            {
                printf("鏈表為空,請先建立鏈表!\n");
                menu();
            }
            break;
        default:
            break;
        }
    }
    system("pause");
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM