C语言-学生成绩管理系统-免费~ (不加文件操作 & 加文件操作)

匆忙之间帮朋友写的
排序的地方偷懒了, 用的c

没有文件操作

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

using namespace std;

#define MAXSTUDENT 1000

struct Student
{
    char Sname[20];
    int num;
    float chinese;
    float math;
    float english;
    float total;
    int rank;
};

//存储学生信息
struct Student Info[MAXSTUDENT];
//学生个数
int index = 0;

//插入学生信息
void insert(struct Student Info[]);
//成绩排序
void Sort(struct Student Info[], int l, int r);
//成绩输出
void output(struct Student Info[], int index);
//按姓名查找
int name_search(struct Student Info[], int index, char name[20]);
//按学号查找
int num_search(struct Student Info[], int index, int id);
//删除学生成绩信息
void Delete (struct Student Info[], int id);
//修改学生成绩信息
void modify(struct Student Info[], int index, int id);

bool cmp(struct Student a, struct Student b);

void menu()
{
    int tag = 0;
    printf("\t\t\t\t\t学生成绩管理系统的模拟\n");
    while(tag != 8)
	{
        printf("\t\t**************************请选择要进行的操作**************************\n");
        printf("\t\t\t\t\t1:录入学生成绩信息\n\t\t\t\t\t2:成绩排序\n\t\t\t\t\t3:成绩输出(修改后输出前请排序一下)\n\t\t\t\t\t4:按姓名查找\n\t\t\t\t\t5:按学号查找\n");
        printf("\t\t\t\t\t6:删除学生成绩信息\n\t\t\t\t\t7:修改学生成绩信息\n\t\t\t\t\t8:退出\n");
        scanf("%d", &tag);
        switch(tag)
		{
            case 1:
            {
                insert(Info);
                printf("插入成功!\n");
                printf("当前学生人数:%d\n", index);
                break;
            }
            case 2:
            {
                Sort(Info, 0, index);
                printf("排序成功!\n");
                printf("当前学生人数:%d\n", index);
                break;
            }
           case 3:
            {
                output(Info, index);
                break;
            }
            case 4:
            {
                char name[20];
                printf("请输入您要查找的学生的姓名:");
                scanf("%s", name);
                int pos = name_search(Info, index, name);
                if (pos != -1)
                {
                    printf("学生信息如下:\n姓名:%s,\t学号:%d,\t语文成绩:%1.1f,\t数学成绩:%1.1f,\t英语成绩:%1.1f\t总成绩:%1.1f\t\t排名:%d\n\n", Info[pos].Sname, Info[pos].num, Info[pos].chinese, Info[pos].math, Info[pos].english, Info[pos].total, Info[pos].rank);
                }
                else
                    printf("未查找到该学生!\n");
                break;
            }
            case 5:
            {
                int id;
                printf("请输入您要查找的学生的学号:");
                scanf("%d", &id);
                int pos = num_search(Info, index, id);
                if (pos != -1)
                {
                    printf("学生信息如下:\n姓名:%s,\t学号:%d,\t语文成绩:%1.1f,\t数学成绩:%1.1f,\t英语成绩:%1.1f\t总成绩:%1.1f\t\t排名:%d\n\n", Info[pos].Sname, Info[pos].num, Info[pos].chinese, Info[pos].math, Info[pos].english,Info[pos].total,Info[pos].rank);
                }
                else
                {
                    printf("未找到该学生!\n");
                }
                break;
            }
            case 6:
            {
                int id;
                printf("请输入您要删除学生的学号:");
                scanf("%d", &id);
                Delete (Info, id);
                printf("删除成功!\n");
                printf("当前学生人数:%d\n", index);
                break;
            }
           case 7:
            {
                int id;
                printf("请输入您要修改学生的学号:");
                scanf("%d", &id);
                modify(Info, index, id);
                printf("修改成功!\n");
                printf("当前学生人数:%d\n", index);
                break;
            }
        }
	}
}

int main()
{
    menu();
    return 0;
}

void insert(struct Student Info[])
{
    printf("请输入学生的姓名、学号、语文、数学、英语成绩:\n");
    scanf("%s %d %f %f %f", Info[index].Sname, &Info[index].num, &Info[index].chinese, &Info[index].math, &Info[index].english);
    Info[index].total = Info[index].math + Info[index].english + Info[index].chinese;
    index++;
}

void Sort(struct Student Info[], int l, int r)
{
    if (l > r)
        return;
    sort(Info, Info + index, cmp);
    for (int i = 0; i < index; i++)
        Info[i].rank = i + 1;
    for (int i = 0; i < index - 1; i ++)
        if (Info[i + 1].total == Info[i].total)
            Info[i + 1].rank = Info[i].rank;
}

bool cmp(struct Student a, struct Student b)
{
    if (a.total == b.total)
    {
        if (a.chinese == b.chinese)
        {
            if (a.math == b.math)
            {
                return a.english > b.english;
            }
            else
            {
            return a.math > b.math;
            }
        }
        else
        {
            return a.chinese > b.chinese;
        }
    }
    else
    {
        return a.total > b.total;
    }
}

void output(struct Student Info[], int index)
{
    for (int i = 0; i < index; i++)
    {
         printf("姓名:%s,\t学号:%d,\t语文成绩:%1.1f,\t数学成绩:%1.1f,\t英语成绩:%1.1f\t总成绩:%1.1f\t\t排名:%d\n", Info[i].Sname, Info[i].num, Info[i].chinese, Info[i].math, Info[i].english, Info[i].total, Info[i].rank);
    }
}

int name_search(struct Student Info[], int index, char name[20])
{
    int pos = -1;
    for (int i = 0; i < index; i++)
    {
        if (!strcmp(name, Info[i].Sname))
        {
            printf("查找成功!\n");
            return i;
            break;
        }
    }
    return pos;
}

int num_search(struct Student Info[], int index, int id)
{
    int pos = -1;
    for (int i = 0; i < index; i++)
    {
        if (id == Info[i].num)
        {
            printf("查找成功!\n");
            return i;
            break;
        }
    }
    return pos;
}

void Delete (struct Student Info[], int id)
{
    int i;
    for (i = 0; i < index; i++)
    {
        if (id == Info[i].num)
        {
            for (int j = i + 1; j < index; j++)
            {
                Info[j - 1] = Info[j];
            }
            index--;
            break;
        }
    }
}

void modify(struct Student Info[], int index, int id)
{
    int i;
    for (i = 0; i < index; i++)
    {
        if (id == Info[i].num)
        {
            printf("请输入您修改后的成绩(语文、数学、英语):");
            scanf("%f %f %f", &Info[i].chinese, &Info[i].math, &Info[i].english);
            Info[i].total = Info[i].chinese + Info[i].math + Info[i].english;
        }
    }
}

带有文件操作

加文件操作
文件里面输出的格式没好好改
and 文件操作那里有点粗糙,为了方便,我就在原数组中改一下,然后再写进文件里面。

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

using namespace std;

#define MAXSTUDENT 1000

struct Student
{
    char Sname[20];
    int num;
    float chinese;
    float math;
    float english;
    float total;
    int rank;
};

//存储学生信息
struct Student Info[MAXSTUDENT];
//学生个数
int index = 0;
FILE *fp;

//插入学生信息
void insert(struct Student Info[]);
//成绩排序
void Sort(struct Student Info[], int l, int r);
//成绩输出
void output(struct Student Info[], int index);
//按姓名查找
int name_search(struct Student Info[], int index, char name[20]);
//按学号查找
int num_search(struct Student Info[], int index, int id);
//删除学生成绩信息
void Delete (struct Student Info[], int id);
//修改学生成绩信息
void modify(struct Student Info[], int index, int id);

bool cmp(struct Student a, struct Student b);

void menu()
{
    fp = fopen("score.txt", "w");
    if (fp == NULL)
        printf("文件打开失败!\n");
    else
    {
        fprintf(fp, "姓名\t学号\t语文成绩\t数学成绩\t英语成绩\t总成绩\t排名\n");
    }
    fclose(fp);

    int tag = 0;
    printf("\t\t\t\t\t学生成绩管理系统的模拟\n");
    while(tag != 8)
	{
        printf("\t\t**************************请选择要进行的操作**************************\n");
        printf("\t\t\t\t\t1:录入学生成绩信息\n\t\t\t\t\t2:成绩排序\n\t\t\t\t\t3:成绩输出\n\t\t\t\t\t4:按姓名查找\n\t\t\t\t\t5:按学号查找\n");
        printf("\t\t\t\t\t6:删除学生成绩信息\n\t\t\t\t\t7:修改学生成绩信息\n\t\t\t\t\t8:退出\n");
        scanf("%d", &tag);
        switch(tag)
		{
            case 1:
            {
                insert(Info);
                printf("插入成功!\n");
                printf("当前学生人数:%d\n", index);
                break;
            }
            case 2:
            {
                Sort(Info, 0, index);
                printf("排序成功!\n");
                printf("当前学生人数:%d\n", index);
                break;
            }
           case 3:
            {
                output(Info, index);
                break;
            }
            case 4:
            {
                char name[20];
                printf("请输入您要查找的学生的姓名:");
                scanf("%s", name);
                int pos = name_search(Info, index, name);
                if (pos != -1)
                {
                    printf("学生信息如下:\n姓名:%s,\t学号:%d,\t语文成绩:%1.1f,\t数学成绩:%1.1f,\t英语成绩:%1.1f\t总成绩:%1.1f\t\t排名:%d\n\n", Info[pos].Sname, Info[pos].num, Info[pos].chinese, Info[pos].math, Info[pos].english, Info[pos].total, Info[pos].rank);
                }
                else
                    printf("未查找到该学生!\n");
                break;
            }
            case 5:
            {
                int id;
                printf("请输入您要查找的学生的学号:");
                scanf("%d", &id);
                int pos = num_search(Info, index, id);
                if (pos != -1)
                {
                    printf("学生信息如下:\n姓名:%s,\t学号:%d,\t语文成绩:%1.1f,\t数学成绩:%1.1f,\t英语成绩:%1.1f\t总成绩:%1.1f\t\t排名:%d\n\n", Info[pos].Sname, Info[pos].num, Info[pos].chinese, Info[pos].math, Info[pos].english,Info[pos].total,Info[pos].rank);
                }
                else
                {
                    printf("未找到该学生!\n");
                }
                break;
            }
            case 6:
            {
                int id;
                printf("请输入您要删除学生的学号:");
                scanf("%d", &id);
                Delete (Info, id);
                printf("删除成功!\n");
                printf("当前学生人数:%d\n", index);
                break;
            }
           case 7:
            {
                int id;
                printf("请输入您要修改学生的学号:");
                scanf("%d", &id);
                modify(Info, index, id);
                printf("修改成功!\n");
                printf("当前学生人数:%d\n", index);
                break;
            }
        }
	}
}

int main()
{
    menu();
    return 0;
}

void insert(struct Student Info[])
{
    printf("请输入学生的姓名、学号、语文、数学、英语成绩:\n");
    scanf("%s %d %f %f %f", Info[index].Sname, &Info[index].num, &Info[index].chinese, &Info[index].math, &Info[index].english);
    fp = fopen("score.txt", "a");
    if (fp == NULL)
        printf("文件打开失败!");
    else
        fprintf(fp, "%s \t%d \t%1.1f\t\t%1.1f\t\t%1.1f\t\t%1.1f\t\t%d\n", Info[index].Sname, Info[index].num, Info[index].chinese, Info[index].math, Info[index].english,Info[index].total, Info[index].rank);
    fclose(fp);
    Info[index].total = Info[index].math + Info[index].english + Info[index].chinese;
    index++;
    Sort(Info, 0, index);
}

void Sort(struct Student Info[], int l, int r)
{
    if (l > r)
        return;
    sort(Info, Info + index, cmp);
    for (int i = 0; i < index; i++)
        Info[i].rank = i + 1;
    for (int i = 0; i < index - 1; i ++)
        if (Info[i + 1].total == Info[i].total)
            Info[i + 1].rank = Info[i].rank;

    fp = fopen("score.txt", "w");
    if (fp == NULL)
        printf("文件打开失败!\n");
    else
    {
        fprintf(fp, "姓名\t学号\t语文成绩\t数学成绩\t英语成绩\t总成绩\t排名\n");
        for (int i = 0; i < index; i++)
        {
            fprintf(fp, "%s \t%d \t%1.1f\t\t%1.1f\t\t%1.1f\t\t%1.1f\t\t%d\n", Info[i].Sname, Info[i].num, Info[i].chinese, Info[i].math, Info[i].english,Info[i].total, Info[i].rank);
        }
    }
    fclose(fp);
}

bool cmp(struct Student a, struct Student b)
{
    if (a.total == b.total)
    {
        if (a.chinese == b.chinese)
        {
            if (a.math == b.math)
            {
                return a.english > b.english;
            }
            else
            {
            return a.math > b.math;
            }
        }
        else
        {
            return a.chinese > b.chinese;
        }
    }
    else
    {
        return a.total > b.total;
    }
}

void output(struct Student Info[], int index)
{
    for (int i = 0; i < index; i++)
    {
         printf("姓名:%s,\t学号:%d,\t语文成绩:%1.1f,\t数学成绩:%1.1f,\t英语成绩:%1.1f\t总成绩:%1.1f\t\t排名:%d\n", Info[i].Sname, Info[i].num, Info[i].chinese, Info[i].math, Info[i].english, Info[i].total, Info[i].rank);
    }
}

int name_search(struct Student Info[], int index, char name[20])
{
    int pos = -1;
    for (int i = 0; i < index; i++)
    {
        if (!strcmp(name, Info[i].Sname))
        {
            printf("查找成功!\n");
            return i;
            break;
        }
    }
    return pos;
}

int num_search(struct Student Info[], int index, int id)
{
    int pos = -1;
    for (int i = 0; i < index; i++)
    {
        if (id == Info[i].num)
        {
            printf("查找成功!\n");
            return i;
            break;
        }
    }
    return pos;
}

void Delete (struct Student Info[], int id)
{
    int i;
    for (i = 0; i < index; i++)
    {
        if (id == Info[i].num)
        {
            for (int j = i + 1; j < index; j++)
            {
                Info[j - 1] = Info[j];
            }
            index--;
            break;
        }
    }

    fp = fopen("score.txt", "w");
    if (fp == NULL)
        printf("文件打开失败!\n");
    else
    {
        fprintf(fp, "姓名\t学号\t语文成绩\t数学成绩\t英语成绩\t总成绩\t排名\n");
        for (int i = 0; i < index; i++)
        {
            fprintf(fp, "%s \t%d \t%1.1f\t\t%1.1f\t\t%1.1f\t\t%1.1f\t\t%d\n", Info[i].Sname, Info[i].num, Info[i].chinese, Info[i].math, Info[i].english,Info[i].total, Info[i].rank);
        }
    }
    fclose(fp);
    Sort(Info, 0, index);
}

void modify(struct Student Info[], int index, int id)
{
    int i;
    for (i = 0; i < index; i++)
    {
        if (id == Info[i].num)
        {
            printf("请输入您修改后的成绩(语文、数学、英语):");
            scanf("%f %f %f", &Info[i].chinese, &Info[i].math, &Info[i].english);
            Info[i].total = Info[i].chinese + Info[i].math + Info[i].english;
        }
    }
    fp = fopen("score.txt", "w");
    if (fp == NULL)
        printf("文件打开失败!\n");
    else
    {
        fprintf(fp, "姓名\t学号\t语文成绩\t数学成绩\t英语成绩\t总成绩\t\t排名\n");
        for (int i = 0; i < index; i++)
        {
            fprintf(fp, "%s \t%d \t%1.1f\t\t%1.1f\t\t%1.1f\t\t%1.1f\t\t%d\n", Info[i].Sname, Info[i].num, Info[i].chinese, Info[i].math, Info[i].english,Info[i].total, Info[i].rank);
        }
    }
    fclose(fp);
    Sort(Info, 0, index);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
纯C语言学生成绩管理系统(以包含文件的形式写的),结构体,链表,数组,以下是main.c #include #include #include #define m 3/*宏定义m的值为3*/ struct node/*链表初始化*/ { char name[20]; int no; float score[m]; float sum; float avg; struct node *next; }; char ch[m+4][20]={{&quot;学号&quot;},{&quot;姓名&quot;},{&quot;语文&quot;},{&quot;数学&quot;},{&quot;英语&quot;},{&quot;总分&quot;},{&quot;平均分&quot;}};/*定义并初始化一个全局二维字符数组*/ #include &quot;save.c&quot;/*包含保存文件*/ #include &quot;read.c&quot;/*包含读取文件*/ #include &quot;output.c&quot;/*包含打印文件*/ #include &quot;set.c&quot;/*包含录入文件*/ #include &quot;demand.c&quot;/*包含查询文件*/ #include &quot;sort.c&quot;/*包含排序文件*/ #include &quot;modified.c&quot;/*包含修改文件*/ #include &quot;add.c&quot;/*包含添文件*/ #include &quot;del.c&quot;/*包含删除文件*/ void main() { int n; printf(&quot;\n\t\t\t欢迎使用学生成绩管理系统\n\n&quot;); printf(&quot;\t\t\t\t\t\t制 作: XIA XIA\n&quot;); do { printf(&quot;\n\n1:学生成绩录入,并保存\n&quot;); printf(&quot;2:学生成绩查询\n&quot;); printf(&quot;3:学生成绩的排序\n&quot;); printf(&quot;4:学生成绩的修改\n&quot;); printf(&quot;5:学生成绩的打印\n&quot;); printf(&quot;6:学生信息的添\n&quot;); printf(&quot;7:学生信息的删除\n&quot;); printf(&quot;0:退出学生成绩管理系统\n\n\n&quot;); printf(&quot;输入你要执行操作的相应序号\n&quot;); scanf(&quot;%d&quot;,&amp;n);/*输入相就的操作的序号*/ switch (n) { case 1: set();break;/*调用录入函数*/ case 2: demand();break;/*调用查询函数*/ case 3: sort();break;/*调用排序函数*/ case 4: modified();break;/*调用修改函数*/ case 5: output();break;/*调用打印函数*/ case 6: add();break;/*调用添函数*/ case 7: del();break;/*调用删除函数*/ case 0: printf(&quot;正在退出学生成绩管理系统......\n&quot;);exit(0);/*直到输入&ldquo;0&rdquo;退出学生成绩管理系统*/ default:printf(&quot;输入错误码,请重新输入\n&quot;); } }while(1); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值