⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 学生管理系统.c

📁 学生管理系统
💻 C
字号:
#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 

typedef struct stud /*学生信息结构*/
{ 
    long num; 
    char name[20]; 
    float score; 
}Stud; 

typedef struct node 
{ 
    Stud student; 
    struct node *next; 
}Node; 
Node *head=NULL;
Node *last=NULL;

void read(void); 
void inser(long b); 
void print(); 
void find(long b); 
int Find(long b);
void searchname(char *s); 
Node * del(long n); 
void sort(int flag); 

void menu();
void main() 
{ 
    char    choose; 
    int     flag=1; 

    while (flag) 
    { 
        menu(); /*调用功能菜单函数,显示菜单项。*/
        printf(" 请选择:"); 
        choose=getchar(); 

        switch(choose) 
        {  
            case '1':{
                read(); /*调用建立链表的函数;输出链表信息*/
                print();
                printf("\nPress any key Continue "); 
                getchar();}

                break; 
            case '2':{ /*调用按学号查找学生信息的函数;并输出查找结果信息*/
                    long c; 
                    printf("input the number you want to find:"); 
                    scanf("%ld",&c); 
                    find(c);
                    printf("\nPress any key Continue."); 
                    getchar(); 
					getchar(); 
                    break; }
            case '3':{
            /*调用按姓名查找学生信息的函数;并输出查找结果信息;   */
                char s[20]; 
                printf("input the name you want to find:"); 
                scanf("%s",s); 
                searchname(s);
                printf("\n Press any key Continue."); 
                getchar(); 
                getchar(); 
                break; }
            case '4': {
            /*调用根据学号删除某个学生信息的函数;并输出删除后的链表信息;*/
                Node *h; 
                long n; 
                printf("input the number you want to delete:"); 
                scanf("%ld",&n); 
                h=del(n); 
                if(h==NULL) printf("No find the student \n"); 
                else print();
                printf("\n Press any key Continue."); 
                getchar(); 
                getchar(); 
                break;}
            case '5': {
                /*调用插入新的学生信息的函数;并输出插入后的链表信息; */
                long a; 
                printf("input the number for the new:\n"); 
                scanf("%ld",&a); 
                inser(a);
                print();
                printf("\n Press any key Continue."); 
                getchar(); 
                getchar(); 
                break;}
            case '6': {
                /*调用按分数降序排序输出的函数;并输出排序后的链表信息; */
                int a;
				printf("change sort mode\n");
				printf("0--number sort     1--score sort\n");
				printf("please change:");
				scanf("%ld",&a); 
				if(a==1)
					sort(1);
				else if(a==0)
					sort(0);
				else{
					printf("change err\n");
					return ;
				}
				print();
                printf("\nPress any key Continue."); 
                getchar(); 
                getchar(); 
                break; }
            case '0': {
                /*结束程序运行!*/
                flag=0; 
                printf("\n *** The End! ***\n"); 
                break;}
            default: printf("\n Wrong Selection !(选择错误,重选)\n"); 
            getchar(); 
        } 
    } 
} 

void menu() /*综合作业功能菜单 */
{ 
    printf(" \n 学 生 信 息 管 理 系 统\n"); 
    printf(" \n 菜 单\n\n"); 
    printf(" \n 1. 建 立 链 表 并 显 示 \n"); 
    printf(" \n 2. 查 找 某 学 号 的 学 生 信 息 \n"); 
    printf(" \n 3. 查 找 某 姓 名 的 学 生 信 息 \n"); 
    printf(" \n 4. 删 除 某 个 学 号 的 学 生\n"); 
    printf(" \n 5. 插 入 新 的 学 生 信 息 \n"); 
    printf(" \n 6. 按 分 数 降 序 排 序 输 出 \n");
    printf(" \n 0. 退 出\n\n"); 
} 

void read(void) 
{ 
    long a; 
    printf("input the number:"); 
    scanf("%ld",&a); 
    while(a>0){  
        inser(a); 
        printf("input the number:"); 
        scanf("%ld",&a); 
    } 
}
void print() 
{ 
    Node *p=head; 
    printf("学号\t 姓名\t 成绩\t\n"); 
    while(p!=NULL){ 
        printf("%ld\t %s\t %f\t\n",p->student.num,p->student.name,p->student.score); 
        p=p->next; 
    } 
    printf("\n"); 
} 

void inser(long b) 
{ 

    Node *p=NULL;  
    if(head==NULL){        
		printf("input the name,score:");
		p=(Node *)malloc(sizeof(Node)); 
        p->student.num=b; 
        scanf("%s %f",p->student.name,&p->student.score); 
        p->next=NULL;
		last=head=p;
    } 

    else if(head!=NULL&&Find(b)){ 
        printf("input the name,score:"); 
        p=(Node *)malloc(sizeof(Node)); 
        p->student.num=b; 
        scanf("%s %f",p->student.name,&p->student.score); 
        p->next=NULL;
		last->next=p;
		last=p;
    } 
    else if(!Find(b)) 
        printf("error input a differ number \n"); 
} 
 
void find(long b) 
{ 
    Node *p=head;
    while(p!=NULL&&b!=p->student.num) 
        p=p->next; 
	if(!p){
		printf("No found\n");
	}
	else {
		printf("学号\t 姓名\t 成绩\t\n");
		printf("%ld\t %s\t %f\t\n",p->student.num,p->student.name,p->student.score);
	}
} 
int Find(long b) 
{ 
    Node *p=head;
    while(p!=NULL&&b!=p->student.num) 
        p=p->next; 
	if(!p){
		return 1;
	}
	return 0;
} 
void searchname(char *s) 
{ 
    Node *p=head; 
    int flag=0; 
    printf("学号\t 姓名\t 成绩\t\n"); 
    while(p!=NULL) 
    { 
        if(strcmp(p->student.name,s)==0) 
        { 
            printf("%ld\t %s\t %f\t\n",p->student.num,p->student.name,p->student.score); 
            flag=1; 
            p=p->next; 
            continue; 
        } 
        else p=p->next; 
    }
    if(!flag) printf("No find"); 
} 
Node * del(long n) 
{ 
    Node *p=head,*q; 
    while(p->student.num!=n){ 
        q=p; 
        p=p->next; 
    } 
    if(p==NULL) return p; 
    else if(p==head) head=p->next; 
    else q->next=p->next;
    return head; 
} 
void sort(int flag) 
{ 
    /*flag==1 按分数排序 else 按学号排序*/
    Node *p1,*p2,*k; 
    float t1; 
    long t2; 
    char s[20]; 
    for(p1=head;p1->next;p1=p1->next) 
    { 
        k=p1; 
        for(p2=p1->next;p2;p2=p2->next) 
            if(flag==1&&k->student.score<p2->student.score||!flag&&k->student.num>p2->student.num) 
				k=p2; 
        if(k!=p1){
            t1=p1->student.score; 
            p1->student.score=k->student.score; 
            k->student.score=t1; 
            t2=p1->student.num; 
            p1->student.num=k->student.num; 
            k->student.num=t2; 
            strcpy(s,p1->student.name); 
            strcpy(p1->student.name,k->student.name); 
            strcpy(k->student.name,s); 
        } 
    } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -