简易学生信息管理系统

最近在复习 《数据结构》 线性表。今天耗费了整个下午加上晚上1个多小时的调整写出来一个小程序。诶,不得不感叹,代码什么的还是要经常写啊。

-------------------------------------------------------------

这个  《简易学生信息管理系统》 的功能如下:

1、能够输入、输出、删除、更新、排序信息;

2、学生的信息包括:姓名(10个字符)、ID(0~32767)、分数(0~150)

3、ID不允许重复(貌似忘记限制ID小于零的情况和分数超过范围了。 XP)

4、学生的人数上限是10个(额,这个可以多一点)

--------------------------------------------------------------

程序编译、运行的环境:

ubuntu 12.04.2LTS ; Linux  3.5.0-36-generic ; gcc version 4.6.3; Vi IMproved 7.3 

//This program is about managing students' info.  
  
#include <stdio.h>  
#include <string.h>  
typedef struct{  
    char name[10];  
    int id;  
    int score;  
}Info;  
  
  
void Menu();  
int Add_Info(Info *stu);  
void Print(Info *stu);  
void Del(Info *stu );  
void Sort(Info *stu);  
void Update(Info *stu );  
void Menu_Quit();  
  
int num=0;  
  
void main(){  
    Info stu[10];  
    int flag=1;  
    char choise;  
    Menu();  
    while(flag==1){  
        scanf (" %c",&choise);  
        switch(choise){  
                case 'a':   Add_Info(stu);  Menu(); break;  
                case 'd':   Del(stu);   Menu(); break;  
                case 's':   Sort(stu);  Menu(); break;  
                case 'u':   Update(stu);    Menu(); break;  
                case 'p':   Print(stu); Menu(); break;  
                case 'q':   flag=0; Menu_Quit();    break;  
                default:  
                    printf ("Sry,input Error!Plz Input again:");  
        }  
    }  
}  
  
void Menu(){  
    system("clear");  
    printf ("\n\n Practice\n\n");  
    printf ("==================================\n\n");  
    printf ("a.Add info            d.Del info\n");  
    printf ("u.Update info         p.Print info\n");  
    printf ("s.Sort                q.Quit\n");  
    printf ("\n\n==================================\n");  
    printf ("\nPlease input your choise:___\b\b");  
}  
  
int Add_Info(Info *stu){  
    printf ("You have choosed \"a\"!\n");  
    int i,j,numtmp=0,num1=0,num2=0;  
    Print(stu);  
    printf ("How many students:");  
    scanf ("%d",&numtmp);  
    if (num>10 || numtmp>10){  
        printf ("Sry,Overflow!!Plz try another way!\n");  
        return  0;  
    }  
    if (num){  
        num1=num;  
        num2=numtmp+num;  
    }  
    else{  
        num1=0;  
        num2=numtmp;  
    }  
    for (i=num1;i<num2;i++){  
        getchar();  
        printf ("==============================\n");  
        printf ("Plz input the %dst's name:",i+1);  
        gets(stu[i].name);  
        printf ("Plz input the %dst's id:",i+1);  
        scanf ("%d",&stu[i].id);  
      
        //IDs CANNOT repeat  
        if (num){  
            for (j=0;j<i;){  
                if (stu[j].id==stu[i].id){  
                    printf ("Error!ID repeated!!Plz input again:");  
                    scanf ("%d",&stu[i].id);  
                    j=0;  
                }  
                else  
                    j++;  
            }  
        }  
      
        printf ("Plz input the %dst's score:",i+1);  
        scanf ("%d",&stu[i].score);  
        num++;  
    }  
    printf ("\nDone with the input!\n");  
    printf ("Here's the new Database:\n");  
    Print(stu);  
    return 1;  
}   
  
void  Print(Info *stu){  
    int i;  
    printf ("\nThere're %d students' info in the database\n",num);  
    printf ("Name    ID    Score\n");  
    printf ("===================\n");  
    for (i=0;i<num;i++){  
        printf ("%s%6d%9d\n",stu[i].name,stu[i].id,stu[i].score);  
    }  
    printf ("===================\n\n");  
    printf ("press ANY key to continue");  
    getchar();  
    getchar();  
}  
   
void Del(Info *stu ){  
    int i,j,id,flag=0;  
    printf ("You have choised \"d\"!\n");  
    Print(stu);  
    printf ("Plz input the student's id that you want to delete:");  
    while (flag==0){  
        scanf ("%d",&id);  
        for (i=0;i<num;i++){  
            if (stu[i].id==id){  
                printf ("Found the student!! Deleting Info, plz wait...\n");  
                for (j=i;j<num-1;j++){  
                    strcpy(stu[j].name,stu[j+1].name);  
                    stu[j].id=stu[j+1].id;  
                    stu[j].score=stu[j+1].score;  
                }  
                flag=1;           
                system("sleep 1");  
            }  
        }  
        if (flag==0)  
            printf ("Wrong ID!Plz input again:");  
    }  
    num--;  
    printf ("\nDone with the delete!\n");  
    printf ("Here's the new Database:\n");  
    Print(stu);  
}   
  
void Update(Info *stu ){  
    int id,i,j,flag=0;  
    printf ("You have choosed \"u\"! !\n");   
    Print(stu);  
    printf ("Plz input the student's id that you want to update:");  
    while (flag==0){  
        scanf ("%d",&id);  
        for (i=0;i<num;i++){  
            if (stu[i].id==id){  
                printf ("Found the student!!\n");  
                printf ("Plz input ID:%d's name:",id);  
                getchar();  
                gets(stu[i].name);  
                printf ("Plz input ID:%d's score:",id);  
                scanf ("%d",&stu[i].score);  
                flag=1;           
            }  
        }  
        if (flag==0)  
            printf ("Wrong ID!Plz input again:");  
    }  
    printf ("\nDone with the update!\n");  
    printf ("Here's the new Database:\n");  
    Print(stu);  
}   
  
void Sort(Info *stu){  
    printf ("You have choosed \"s\"!\n");  
    Print (stu);  
    printf ("Sort by 1.ID; 2.Score:");  
    int choise,flag=0,i,j;  
    Info tmp;  
    while(flag==0){  
        scanf ("%d",&choise);  
        switch(choise){  
            case 1://id  
                for (i=0;i<num-1;i++){  
                    for (j=i+1;j<num;j++){  
                        if (stu[i].id>stu[j].id){  
                            strcpy(tmp.name,stu[i].name);  
                            tmp.id=stu[i].id;  
                            tmp.score=stu[i].score;  
  
                            strcpy(stu[i].name,stu[j].name);  
                            stu[i].id=stu[j].id;  
                            stu[i].score=stu[j].score;  
  
                            strcpy(stu[j].name,tmp.name);  
                            stu[j].id=tmp.id;  
                            stu[j].score=tmp.score;  
                        }  
                    }  
                }  
                Print(stu);  
                flag=1;  
                break;  
            case 2://score  
                for (i=0;i<num-1;i++){  
                    for (j=i+1;j<num;j++){  
                        if (stu[i].score<stu[j].score){  
                            strcpy(tmp.name,stu[i].name);  
                            tmp.id=stu[i].id;  
                            tmp.score=stu[i].score;  
  
                            strcpy(stu[i].name,stu[j].name);  
                            stu[i].id=stu[j].id;  
                            stu[i].score=stu[j].score;  
  
                            strcpy(stu[j].name,tmp.name);  
                            stu[j].id=tmp.id;  
                            stu[j].score=tmp.score;  
                        }  
                    }  
                }  
                Print(stu);  
                flag=1;  
                break;  
            default:  
                printf ("Wrong Choise!Plz input again:");  
        }  
    }  
}  
  
void Menu_Quit(){  
    printf ("\n\n##################################\n");  
    printf (" This is the END of this program!\n");  
    printf (" Thanks for trial!     Good luck!\n");  
    printf ("##################################\n");  
}
心得:

这样的程序虽然才200多行,不过还是有点小复杂。写的时候得先把框架打好,需要些什么功能,构建什么样的函数;用一些什么样的变量;注意细节,代码多了,很容易出错,调试起来很麻烦,这个只能得多写多练才能提高。


总结:

诶,勉强能拼凑出相应的功能。至于什么样的算法、结构更加简单、快速;怎样优化内存的使用;如果数据是几千几万怎么弄等等,这样的问题,现在的我还解决不了。只能考以后的学习来慢慢弥补啦。

你可能感兴趣的:(C语言,小程序,简易学生信息管理系统)