学生成绩管理系统 C语言链表版本

 可以实现管理学生姓名学号成绩信息,有一定的容错能力。
 源代码下载地址(http://www.oschina.net/code/snippet_2309129_46342%20%E2%80%9Clink%E2%80%9D)
/********************************************************* *程序描述:学生成绩管理系统 *运行环境:Windows 7 SP1 X64 *开发环境:CodeBlocks with Win7 *作者:耗子、 *时间:2015.03 **********************************************************/

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

const int TRUE = 1;
const int FALSE = 0;
const char BACK[] = "back";

typedef struct Student  //链表元素
{
    char name[100], number[100];
    double chinese, math, english;
    struct Student* next;
}LNode, *Student;

//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓声明函数↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
Student initLinkList();//初始化
void insLinkList(Student* pHead, char name[],//插入元素
                  char number[], double chinese,
                  double math, double english);
int  delLinkList(Student* pHead, char number[]);//删除元素
int  listLength(Student* pHead);//获取链表长度
int  findLinkList(Student* pHead, char number[]);//查找元素,返回位置
int  modLinkList(Student* pHead, char number[],//修改元素
                 double chinese, double math,
                 double english);
void   outPutLinkList(Student* pHead);//输出所有信息
double scoreLegal(char score[]);//判断分数合法
int    nameLegal(char name[]);//判断姓名合法
int    numberLegal(char number[]);//判断学号合法
int    emptyLinkList(Student* pHead);//判断链表是否为空

void swapNode(Student p, Student q);//负责交换
void putMain();//输出主UI
void putSort();//输出排序UI
int  isBack(char str[]);//判断返回
void error(char []);//错误警告
void welcome();//欢迎
void bay();//再见
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑声明函数↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//

//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓主函数↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
int main()
{
    system("color 0A");
    system("mode con cols=80 lines=25");
    Student pHead;
    Student q, p;
    char name[100], number[100], chinese[100], math[100],
    english[100];
    int op, n;                  //op是选择的选项
    double cn, ma, eng;
    int i, j, sum;
    pHead = initLinkList();
    welcome();
    while (1)
    {
        begin:
        q = pHead;
        putMain();
        op = (getch() - '0' );
        if (op == -1)
            continue;
        switch (op)
        {
            case 0://退出
            {
                bay();
                return 0;
            }
            case 1://添加信息
            {
                for (;;)
                {
                    printf ("\n\t\t\t叫什么?");
                    gets(name);
                    if (isBack(name))
                        goto begin;
                    if (nameLegal(name) == 0)
                        continue;
                    else
                        break;
                }
                number:
                for (;;)
                {
                    printf ("\t\t\t学号呢?");
                    gets(number);
                    if (isBack(number))
                        goto begin;
                    if (numberLegal(number) == 0)
                        continue;
                    else
                        break;
                }
                while (q != NULL)
                {
                    if (strcmp(q->number, number) == 0)
                    {
                        error("学号重复了噻~");
                        goto number;
                    }
                    else
                        q = q->next;
                }
                for (;;)
                {
                    printf ("\t\t\t语文多少分?\n\t\t\t");
                    gets(chinese);
                    if (isBack(chinese))
                        goto begin;
                    cn = scoreLegal(chinese);
                    if (cn == -1)
                        continue;
                    else
                        break;
                }
                for (;;)
                {
                    printf ("\t\t\t数学呢?\n\t\t\t");
                    gets(math);
                    if (isBack(math))
                        goto begin;
                    ma = scoreLegal(math);
                    if (ma == -1)
                        continue;
                    else
                        break;
                }
                for (;;)
                {
                    printf ("\t\t\tEnglish:\n\t\t\t");
                    gets(english);
                    if (isBack(english))
                        goto begin;
                    eng = scoreLegal(english);
                    if (eng == -1)
                        continue;
                    else
                        break;
                }
                insLinkList(pHead, name, number,
                            cn, ma, eng);
                printf ("\n\t\t\t学生信息添加完毕\a");
                getch();
                break;
            }
            case 2://删除信息
            {
                for(;;)
                {
                    if (emptyLinkList(pHead))
                        break;
                    printf ("\t\t\t要删除的学生的学号:");
                    gets(number);
                    if (isBack(number))
                        goto begin;
                    if (numberLegal(number) == 0)
                        continue;
                    if (delLinkList(pHead, number) == 1)
                    {
                        printf ("\t\t\t删除成功!");
                        getch();
                        break;
                    }
                    else
                        error ("没有找到该学生!");
                    continue;
                }
                break;
            }
            case 3://查找信息
            {
                if (emptyLinkList(pHead))
                    break;
                printf ("\n\t\t\t要查找谁的信息?");
                gets(number);
                if (isBack(number))
                    goto begin;
                if (numberLegal(number) == 0)
                    continue;
                findLinkList(pHead, number);
                break;
            }
            case 4://修改信息
            {
                if (emptyLinkList(pHead))
                    break;
                for (;;)
                {
                    printf ("\n\t\t\t要修改哪个的信息?");
                    gets(number);
                    if (isBack(number))
                        goto begin;
                    if (numberLegal(number) == 0)
                        continue;
                    else
                        break;
                }
                for (;;)
                {
                    if (strcmp(q->number, number) == 0)
                        break;
                    else
                        q = q->next;
                    if (q == NULL)
                    {
                        error("没有找到该学生!");
                        getch();
                        goto begin;
                    }
                }
                for (;;)
                {
                    printf ("\t\t\t语文多少分?\n\t\t\t");
                    gets(chinese);
                    if (isBack(chinese))
                        goto begin;
                    cn = scoreLegal(chinese);
                    if (cn == -1)
                        continue;
                    else
                        break;
                }
                for (;;)
                {
                    printf ("\t\t\t数学呢?\n\t\t\t");
                    gets(math);
                    if (isBack(math))
                        goto begin;
                    ma = scoreLegal(math);
                    if (ma == -1)
                        continue;
                    else
                        break;
                }
                for (;;)
                {
                    printf ("\t\t\tEnglish:\n\t\t\t");
                    gets(english);
                    if (isBack(english))
                        goto begin;
                    eng = scoreLegal(english);
                    if (eng == -1)
                        continue;
                    else
                        break;
                }
                modLinkList(pHead, number, cn, ma, eng);
                break;
            }
            case 5://输出信息
            {
                    if (emptyLinkList(pHead))
                        break;
                    q = pHead;
                    p = pHead;
                    sum = listLength(pHead);
                    putSort();
                    op = (getch() - '0');
                    switch (op)
                    {
                        case 0://退出
                            goto begin;
                        case 1://学号
                            {
                                for (i = 0; i < sum - 1; i++)
                                {
                                    for (j = 0; j < sum - i - 1; j++)
                                    {
                                        if (strcmp(p->number, q->number) > 0)
                                            swapNode(p, q);
                                        q = q->next;
                                    }
                                    p = p->next;
                                    q = p->next;
                                }
                                break;
                            }
                        case 2://语文
                            {
                                for (i = 0; i < sum - 1; i++)
                                {
                                    for (j = 0; j < sum - i - 1; j++)
                                    {
                                        if (q->chinese < p->chinese)
                                            swapNode(p, q);
                                        q = q->next;
                                    }
                                    p = p->next;
                                    q = p->next;
                                }
                                break;
                            }
                        case 3://数学
                            {
                                for (i = 0; i < sum - 1; i++)
                                {
                                    for (j = 0; j < sum - i - 1; j++)
                                    {
                                        if (q->math < p->math)
                                            swapNode(p, q);
                                        q = q->next;
                                    }
                                    p = p->next;
                                    q = p->next;
                                }
                                break;
                            }
                        case 4://英语
                            {
                                for (i = 0; i < sum - 1; i++)
                                {
                                    for (j = 0; j < sum - i - 1; j++)
                                    {
                                        if (q->english < p->english)
                                            swapNode(p, q);
                                        q = q->next;
                                    }
                                    p = p->next;
                                    q = p->next;
                                }
                                break;
                            }
                        case 5://总分
                            {
                                for (i = 0; i < sum - 1; i++)
                                {
                                    for (j = 0; j < sum - i - 1; j++)
                                    {
                                        if ((q->chinese + q->math + q->english)
                                            < (p->chinese + p->math + p->english))
                                            swapNode(p, q);
                                        q = q->next;
                                    }
                                    p = p->next;
                                    q = p->next;
                                }
                                break;
                            }
                        default:
                            break;
                    }
                    if (op >= 1 && op <= 5)
                        outPutLinkList(pHead);
                    break;
            default:
                break;
            }
        }
    }
    return 0;
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑主函数↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//

//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓链表操作↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
Student initLinkList()                          //初始化
{
    Student pHead;
    pHead = (Student)malloc(sizeof(LNode));
    pHead->next = NULL;
    return pHead;
}

void insLinkList(Student* pHead, char name[],   //插入元素
                 char number[], double chinese,
                 double math, double english)
{
    Student p = pHead;
    Student pNew = (Student)malloc(sizeof(LNode));
    while (p->next != NULL)
        p = p->next;
    strcpy(pNew->name, name);
    strcpy(pNew->number, number);
    pNew->chinese = chinese;
    pNew->math = math;
    pNew->english = english;
    pNew->next = NULL;
    p->next = pNew;
    return ;
}

int delLinkList(Student* pHead, char number[])  //删除元素
{
    Student p = pHead, q;
    while (p->next != NULL)
    {
        if (strcmp(p->next->number, number) == 0)
        {
            q = p->next->next;
            free(p->next);
            p->next = q;
            free(q);
            return TRUE;
        }
        p = p->next;
    }
    return FALSE;
}

int listLength(Student* pHead)                  //获得链表长度
{
    int n = 0;
    Student p = pHead;
    while (p != NULL)
    {
        p = p->next;
        n++;
    }
    return n;
}

int findLinkList(Student* pHead, char number[]) //查找元素
{
    Student p = pHead;
    p = p->next;
    double sum;
    while (p != NULL)
    {
        if (strcmp(p->number, number) == 0)
        {
            system ("CLS");
            sum = p->chinese + p->math + p->english;
            printf ("\n\n\t\t\t***********************\n\t\t\t* 该学生成 绩如下表 *\n");
            printf ("\t\t\t***********************\n");
            printf ("\t\t\t* %s *\n", p->number);
            printf ("\t\t\t***********************\n");
            printf ("\t\t\t* 姓名:%-8s *\n", p->name);
            printf ("\t\t\t* 语文:%-6.2f *\n", p->chinese);
            printf ("\t\t\t* 数学:%-6.2f *\n", p->math);
            printf ("\t\t\t* 英语:%-6.2f *\n", p->english);
            printf ("\t\t\t* 平均:%-7.2f *\n", sum / 3);
            printf ("\t\t\t* 总分:%-7.2f *\n", sum);
            printf ("\t\t\t***********************\n");
            getch();
            return TRUE;
        }
        p = p->next;
    }
    error("没有该学生的信息~");
    return FALSE;
}

int modLinkList(Student* pHead, char number[],  //修改元素
                double chinese, double math,
                double english)
{
    Student p = pHead;
    p = p->next;
    while (p != NULL)
    {
        if (strcmp(p->number, number) == 0)
        {
            p->chinese = chinese;
            p->math    = math;
            p->english = english;
            return TRUE;
        }
        p = p->next;
    }
    return FALSE;
}

void outPutLinkList(Student* pHead)             //输出所有元素
{
    if (emptyLinkList(pHead))
        return ;
    system ("CLS");
    Student p = pHead;
    p = p->next;
    printf ("\n\n\t\t\t 所有学生成绩如下\n");
    printf ("\n *******************************************************************\n");
    printf (" * 学 号 * 姓 名 * 语 文 * 数 学 * 英 语 * 总 分 *");
    printf ("\n *******************************************************************\n");
    while (p != NULL)
    {
        printf (" * %s *", p->number);
        printf (" %-8s *", p->name);
        printf (" % 6.2f * %6.2f * %6.2f *", p->chinese, p->math, p->english);
        printf (" %6.2f *\n", p->chinese + p->math + p->english);
        p = p->next;
    }
    printf (" *******************************************************************\n");
    getch();
    return ;
}

int emptyLinkList(Student* pHead)               //链表为否为空
{
    Student p = pHead;
    if (p->next == NULL)
    {
        error("没有任何学生信息!");
        return TRUE;
    }
    return FALSE;
}

void swapNode(Student p, Student q)             //交换节点内容
{
    Student temp = initLinkList();

    strcpy(temp->name, p->name);
    strcpy(p->name, q->name);
    strcpy(q->name, temp->name);

    strcpy(temp->number, p->number);
    strcpy(p->number, q->number);
    strcpy(q->number, temp->number);

    temp->chinese = p->chinese;
    p->chinese = q->chinese;
    q->chinese = temp->chinese;

    temp->math = p->math;
    p->math = q->math;
    q->math = temp->math;

    temp->english = p->english;
    p->english = q->english;
    q->english = temp->english;

    free(temp);
}

//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑链表操作↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//

//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓函数定义↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//

int isBack(char ch[])                           //判断返回
{
    if(strcmp(ch, BACK) == 0)
        return TRUE;
    return FALSE;
}

int numberLegal(char number[])                  //判断学号
{
    int i;
    if (strlen(number) != 11)//位数判断
    {
        error("学号应为十一位纯数字");
        return FALSE;
    }
    for (i = 0; i < 11; i++)
        if ( ! isdigit(number[i]))//是否是数字
        {
            error("学号应为十一位纯数字");
            return FALSE;
        }
    if (number[0] == 0)//学号首位
    {
        error("学号第一位不能为0");
        return FALSE;
    }
    return TRUE;
}

int nameLegal(char name[])                      //判断姓名
{
    int i, n = strlen(name);
    if (n <= 3)
    {
        error("科普:所谓姓名,一姓一名,最少得俩字儿");
        return FALSE;
    }
    for (i = 0; i < n; i++)
        if (name[i] >= 0)
        {
            error("既然是中国人,名字当然也是中国字了啊");
            return FALSE;
        }
    if (strlen(name) > 8)
    {
        error ("名字太长的话,请用缩写");
        return FALSE;
    }
    return TRUE;
}

double scoreLegal(char score[])                 //判断分数
{
    int n = strlen(score);
    int i, d = 0, g = 0;
    double q = 1, h = 0.1;
    double sum = 0;
    for (i = 0; i < n; i++)
        if (score[i] != '.' && (isdigit(score[i]) == 0))
        {
            error("分数由数字和小数点组成。。。");
            return -1;
        }
    for (i = 0; i < n; i++)
        if (score[i] == '.')
        {
            d = i;
            g++;
            if (g == 2)
            {
                error("小数点好多啊");
                return -1;
            }
        }
    if (score[0] == '.' || score[n - 1] == '.')
    {
        error("小数点不能出现在这么诡异的位置");
        return -1;
    }
    if (g == 1)
        i = d - 1;
    else
        i = n - 1;
    for ( ; i != -1; i--)
    {
        sum = sum + (score[i] - '0') * q;
        q *= 10;
    }
    if (g == 1)
    {
        for (i = d + 1; i < n; i++)
        {
            sum = sum + (score[i] - '0') * h;
            h *= 0.1;
        }
    }
    if (sum > 100.000000)
    {
        error("分数略大啊~");
        return -1;
    }
    return sum;
}

void error(char ch[])                           //提示错误
{
    system ("color 0C");
    printf ("\n\n\t\t\t");
    puts(ch);
    printf ("\n");
    printf ("\a");
    getch();
    system ("color 0A");
}

void welcome()                                  //欢迎
{
    system("title 学生成绩管理系统V1.0 By-耗子、");
    printf ("\n\n\n\n\t\t\t\t");
    printf ("\n\t\t********************************************");
    printf ("\n\t\t* 学生成绩管理系统V1.0 *");
    printf ("\n\t\t* *");
    printf ("\n\t\t* 欢迎使用 *");
    printf ("\n\t\t* *");
    printf ("\n\t\t* 耗子、 *");
    printf ("\n\t\t********************************************");
    getch();
}

void putMain()                                  //主UI
{
    system ("CLS");
    system("title 学生成绩管理系统V1.0 By-耗子、");
    printf ("\n\n");
    printf ("\t\t\t 学生成绩管理系统V1.0\n");
    printf ("\t\t\t************************\n");
    printf ("\t\t\t* 1、添加信息 *\n");
    printf ("\t\t\t* *\n");
    printf ("\t\t\t* 2、删除信息 *\n");
    printf ("\t\t\t* *\n");
    printf ("\t\t\t* 3、查找信息 *\n");
    printf ("\t\t\t* *\n");
    printf ("\t\t\t* 4、修改信息 *\n");
    printf ("\t\t\t* *\n");
    printf ("\t\t\t* 5、显示信息 *\n");
    printf ("\t\t\t* *\n");
    printf ("\t\t\t* 0、退出系统 *\n");
    printf ("\t\t\t************************\n");
    printf ("\t\t\t请选择:(0~5)");
}

void putSort()                                  //排序UI
{
    system("title 请选择排序方式 By-耗子、");
    system("CLS");
    printf ("\n\n");
    printf ("\t\t\t 请选择排序方式 \n");
    printf ("\t\t\t************************\n");
    printf ("\t\t\t* 1、学号排序 *\n");
    printf ("\t\t\t* *\n");
    printf ("\t\t\t* 2、语文成绩 *\n");
    printf ("\t\t\t* *\n");
    printf ("\t\t\t* 3、数学成绩 *\n");
    printf ("\t\t\t* *\n");
    printf ("\t\t\t* 4、英语成绩 *\n");
    printf ("\t\t\t* *\n");
    printf ("\t\t\t* 5、总分排序 *\n");
    printf ("\t\t\t* *\n");
    printf ("\t\t\t* 0、系统菜单 *\n");
    printf ("\t\t\t************************\n");
    printf ("\t\t\t请选择:(0~5)");
}

void bay()                                      //退出
{
    system("title 谢谢使用 By-耗子、");
    system ("CLS");
    printf ("\n\n\n\n\t\t\t\t");
    printf ("\n\t\t********************************************");
    printf ("\n\t\t* *");
    printf ("\n\t\t* *");
    printf ("\n\t\t* 谢谢使用 *");
    printf ("\n\t\t* *");
    printf ("\n\t\t* *");
    printf ("\n\t\t********************************************");
    printf ("\n\n\n\t\n\t\t -By.耗子\n");
    getch();
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑函数定义↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//

你可能感兴趣的:(链表,C语言)