学生选课管理系统

完整代码

#include "stdafx.h"
#include 
#include 
#include 
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void c_add();//增加课程
void c_delete();
void c_searchm();//查询函数
void index();//首页
void Denlu();//登入
void bye_sys();//退出界面
void Statistics();
void Student_Modify();
int Student_SearchByIndex(char id[]);

struct stu
{
	char ID[20];//学生学号
    char Name[20];//学生姓名

	int c_num;//课程编号
    char c_name[20];//课程名字
    int c_score;//课程学分
	int c_nelepeo;//课程已选人数
};
struct stu students[20];
int num=0;

void index()//首页
{
	int in;
    do
    {
        printf("\n\n\n");
		printf("\t\t\t\t    学生选课管理系统\n\n");
		printf("\t\t\t1、用户登入*****************************\n\n");
		printf("\t\t\t2、输入数据*****************************\n\n");
		printf("\t\t\t3、查询数据*****************************\n\n");
		printf("\t\t\t4、删除数据*****************************\n\n");
		printf("\t\t\t5、修改数据*****************************\n\n");
		printf("\t\t\t6、统计数据*****************************\n\n");
		printf("\t\t\t7、退出本系统***************************\n");
        printf("\n\t 请选择服务种类:");
        scanf("%d", &in);
        if (in != 1 && in != 2 && in != 3 && in != 4 && in != 5 && in != 6)
        {
            printf("\n\t没有这项服务\t按任意重新选择");
            getchar(); getchar();
        }
		if (in == 7)//退出系统
        {
            bye_sys();
        }
    } while (in != 1 && in != 2 && in != 3 && in != 4 && in != 5 && in != 6);
    switch (in)
    {
    case 1:Denlu(); break;
    case 2:c_add(); break;
    case 3:c_searchm(); break;
    case 4:c_delete(); break;
    case 5:Student_Modify();break;
	case 6:Statistics();break;
    }
}

void Denlu()//登入
{
	int name;
	int password;
	for(int i=2;i>=0;i--)
	{
		printf("请输入用户名(提示学号):\n");
		scanf("%d",&name);
		printf("请输入密码(提示学号):\n");
		scanf("%d",&password);
		if(password!=416252)
		{
			if(i==0)
			{
				printf("账号已经被锁定不能进入系统!");
				exit(0);
			}
			else
				printf("密码有误!还有%d次机会!\n",i);
		}
		else
		{
			printf("密码正确,登陆成功!\n");
			break;
		}
	}
	index();
}

void Statistics()//统计
{
	int i,j;
	struct stu tmp;
	for (i=0;i<num;i++)
	{
		for (j=1;j<num-i;j++)
		{
			if (students[j-1].c_nelepeo<students[j].c_nelepeo)
			{
				tmp=students[j-1];
				students[j-1]=students[j];
				students[j]=tmp;
			}
		}
	}
	printf("\t\t统计成功");
	index();
}

int Student_SearchByIndex(char id[])
{
	int i;
	for (i=0;i<num;i++)
	{
		if (strcmp(students[i].ID,id)==0)
		{
			return i;
		}
	}
	return -1;
}

/*修改信息*/
void Student_Modify()
{
	while(1)
	{
		char id[20];
		int index;
		printf("请输入要修改的学生的学号:");
		scanf("%s",&id);
		getchar();
		index=Student_SearchByIndex(id);
		if (index==-1)
		{
			printf("学生不存在!\n");
		}
		else
		{
			printf("-- 请输入新值--\n");
			printf("请输入学生学号:");
			scanf("%s",&students[index].ID);
			getchar();
			printf("请输入学生姓名:");
			scanf("%s",&students[index].Name);
			getchar();
			printf("请输入课程编号:");
			scanf("%d",&students[index].c_num);
			getchar();
			printf("请输入课程名字:");
			scanf("%s",&students[index].c_name);
			getchar();
			printf("请输入课程学分:");
			scanf("%f",&students[index].c_score);
			getchar();
			printf("请输入课程已选人数:");
			scanf("%f",&students[index].c_nelepeo);
			getchar();
		}
		printf("是否继续?(1/0)");
		if (getchar()=='0')
		{
			break;
		}
	}
	index();
}

void bye_sys()//退出界面
{
    exit(0);
}

void c_searchm()//查询数据
{
    int id, i = 0, j, inp, k;
    int flag = 0;
    FILE* fp;
    //struct stu coursesear[20];
	//struct stu students[20];
    if ((fp = fopen("course.dat", "rb")) == NULL) //判断课程信息是否存在
    {
        system("cls");
        printf("\n\n\t 课程不存在\t 按任意键返回");
        getchar(); getchar();
       
    }
    else//存在的话循环读取文件中的信息直到文件指针到文件尾部
    {
        do
        {
            fread(&students[i], sizeof(struct stu), 1, fp);
            i = i + 1;
        } while (!feof(fp));
        fclose(fp);
    }
    system("cls");
    printf("\n\n\t\t\t 请输入你要查询的课程编号: ");
    scanf("%d", &id);
    do//检查输入是否符合要求
    {
        for (j = 0; j < i - 1; j++)
        {
            if (id == students[j].c_num)
            {
                flag++;
                k = j;
            }
        }
        if (flag == 1)
        {
            system("cls");
			printf("\n\n\t 学生学号:%s\n", students[k].ID);
            printf("\n\n\t 学生名字:%s\n", students[k].Name);
            printf("\n\n\t 课程编号:%d\n", students[k].c_num);
            printf("\n\n\t 课程名字:%s\n", students[k].c_name);
            printf("\n\n\t 课程学分:%d\n", students[k].c_score);
			printf("\n\n\t 课程已选人数:%d\n", students[k].c_nelepeo);
            getchar(); getchar();
            printf("\n\n\t1.回目录继续查询\t2.退出查询系统\n");
            printf("请选择: ");
            scanf("%d", &inp);
            switch (inp)
            {
            case 1:c_searchm();
			case 2:index();
            default:
            {
                printf("\n\t 没有这项服务\t 请按任意键重新输入");
                getchar(); getchar();
            }
            }
        }
        else
        {
            system("cls");
            printf("\n\n\t 不存在对应记录\n");
            printf("\n\n\t1.回目录继续查询\t2.退出查询系统\n");
            printf("请选择: ");
            scanf("%d", &inp);
            switch (inp)
            {
            case 1:c_searchm();
			case 2:index();
            default:
            {
                printf("\n\t 没有这项服务\t 请按任意键重新输入");
                getchar(); getchar();
            }
            }
        }
    } while (inp != 1);
}

void c_add()//插入数据
{
    int i = 0, j, inp;
    FILE* fp;
	struct stu student_new;
	//struct stu students[20];
    system("cls");
	printf("\n\n\t\t 请输入信息");
    printf("\n\n\t\t1.学生学号:");
    scanf("%s", &student_new.ID);
    printf("\n\n\t\t2.学生名字:");
    scanf("%s", &student_new.Name);
    printf("\n\n\t\t3.课程编号:");
    scanf("%d", &student_new.c_num);
    printf("\n\n\t\t4.课程名称:");
    scanf("%d", &student_new.c_name);
    printf("\n\n\t\t5.课程学分:");
    scanf("%d", &student_new.c_score);
	printf("\n\n\t\t5.课程已选人数:");
    scanf("%d", &student_new.c_nelepeo);
    if ((fp = fopen("course.dat", "rb")) != NULL)
    {
        do
        {
            fread(&students[i], sizeof(struct stu), 1, fp);
            i = i + 1;
        } while (!feof(fp));
        fclose(fp);
        for (j = 0; j <= i - 1; j++)
            if (student_new.c_num == students[j].c_num)
            {
                system("cls");
                printf("\n\n\t\t\t 对不起,这个编号已经使用过\n\n\t\t 按任意键返回管理菜单");
                getchar(); getchar();
                break;
            }
    }
    if ((fp = fopen("course.dat", "rb")) == NULL)
    {
        fp = fopen("course.dat", "wb");
    }
    else
    {
        fclose(fp);
        fp = fopen("course.dat", "ab");
    }
    fwrite(&student_new, sizeof(struct stu), 1, fp);
    fclose(fp);
    printf("\n\n\t\t 信息输入完毕\t 按任意键继续");
	num++;
    getchar(); getchar();
    do
    {
        system("cls");
        printf("\n\n\t\t1.继续输入");
        printf("\n\n\t\t2.返回主页");
        printf("\n\n\t\t 请选择");
        scanf("%d", &inp);
        if (inp != 1 && inp != 2)
        {
            printf("输入不符合要求\t 按任意键重新选择");
            getchar(); getchar();
        }
    } while (inp != 1 && inp != 2);
    switch (inp)
    {
    case 1:c_add(); break;
    case 2:index(); break;
    }
}

void c_delete()//查询后删除
{
    FILE* fp;
    int i = 0, j, n, k, flag = 0;
    //struct stu courses[20];
    printf("\n\n\t\t 请输入你要删除的课程编号: ");
    scanf("%d", &n);
    if ((fp = fopen("course.dat", "rb")) != NULL)
    {
        do
        {
            fread(&students[i], sizeof(struct stu), 1, fp);
            i = i + 1;
        } while (!feof(fp));
        fclose(fp);
    }
    else
    {
        printf("\n\n\t\t 不存在任何课程\t 按任意键返回");
        getchar(); getchar();
      
    }
    for (j = 0; j < i - 1; j++)
    {
        if (students[j].c_num == n)
        {
            flag = 1;
            fp = fopen("course.dat", "wb");
            for (k = 0; k < j; k++)
                fwrite(&students[k], sizeof(struct stu), 1, fp);
            fclose(fp);
            fp = fopen("course.dat", "ab");
            for (k = j + 1; k < i - 1; k++)
            {
                fwrite(&students[k], sizeof(struct stu), 1, fp);
            }
            printf("\n\n\t\t 记录已删除! \t 按任意键返回\n\t\t");
            getchar(); getchar();
          
            fclose(fp);
        }
    }
    if (flag == 0)
    {
        printf("\n\n\t\t 不存在对应的课程\t 按任意键返回");
        getchar(); getchar();
    }
}

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

你可能感兴趣的:(c++,算法,c语言)