C语言实现学生管理系统

3 篇文章 0 订阅

写的比较粗糙,也算是给自己留个脚印吧,以后有时候再更新完善,有意见想法的可以提出来

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>


#define DEBUG
#define USERFILE "/opt/app/SMS/V_C/DB/user.dat"
#define STUFILE "/opt/app/SMS/V_C/DB/student.dat"


void myPrintf(char *fromat, ...);
void mainMenu();
int checkUser(char *username, char *password);
void administratorMenu(char *username, char *password);
void teacherMenu(char *username, char *password);
void studentMenu(char *username, char *password);
void loadUserInfo();
void loadStuInfo();
void printUserInfo();
void printStuInfo();
struct User *addUser(char *username, char *password);
struct User *delUser(char *username, char *password);
void showUsers();
void showOnlineUsers();
void changePassword(char *username, char *password);
void showStuScore(char *username, char *password);
void showAllStuScore(char *username, char *password);
struct Student *addStuInfo(char *username, char *password);
struct Student *delStuInfo(char *username, char *password);


struct Student
{
int num;
char name[10];
int mathScore;
int englishScore;
int scienceScore;
struct Student *next;
};


struct User
{
char name[10];
char password[10];
int level;
int active;
struct User *next;
};

struct User *userHead;
struct Student *stuHead;
void printUserInfo();
int fileUserFlag=0;
int fileStuFlag=0;


int main(int argc, char *argv[])
{
loadUserInfo();
loadStuInfo();
printUserInfo();
printStuInfo();
mainMenu();
return 0;
}


void mainMenu()
{
char username[10];
char password[10];
int ret;
int n=0;

while(n < 3)
{
system("clear");
printf("Welcome To Student Information Manage System\n");
printf("Username:");
scanf("%s", username);
getchar();
printf("Password:");
scanf("%s", password);
getchar();


ret = checkUser(username, password);


switch(ret){
case 1://administrator menu
n = 0;
administratorMenu(username, password);
break;
case 2://teacher menu
n = 0;
teacherMenu(username, password);
break;
case 3://student menu
n = 0;
studentMenu(username, password);
break;
default:
n++;
printf("username or password is wrong, please input again!\n");
printf("Press any key to continue\n");
getchar();
break;
}
}
}


int checkUser(char *username, char *password)
{
int ret=-1;
struct User *p1=NULL;


p1 = userHead;


while(p1 != NULL)
{
if(strcmp(p1->name, username) == 0 && strcmp(p1->password, password) == 0)
{
ret = p1->level;
p1->active=1;
}
p1 = p1->next;
}

return ret;
}


void administratorMenu(char *username, char *password)
{
int ch;
int n=0;


while(n < 3)
{
system("clear");

printf("Welcome administrator %s come back\n", username);
printf("1.add the user\n");
printf("2.delete the user\n");
printf("3.display users information\n");
printf("4.display online users\n");
printf("5.change administrator password\n");
printf("6.logout\n");
printf("Input your choice:");


scanf("%d", &ch);
getchar();


switch(ch)
{
case 1:
n = 0;
userHead = addUser(username, password);
break;
case 2:
n = 0;
userHead = delUser(username, password);
break;
case 3:
n = 0;
showUsers(username, password);
break;
case 4:
n = 0;
showOnlineUsers(username, password);
break;
case 5:
n = 0;
changePassword(username, password);
break;
case 6:
return;
break;
default:
n++;
printf("choice must be 1 ~ 6, please input again.\n");
printf("Press any key to continue...\n");
getchar();
break;
}
}
}


void teacherMenu(char *username, char *password)
{
int ch;
int n = 0;
while(n < 3)
{
system("clear");


printf("Welcome teacher %s come back\n", username);
printf("1.query student score\n");
printf("2.add student score\n");
printf("3.delete student score\n");
printf("4.change the password\n");
printf("5.logout\n");
printf("please input your choice:");


scanf("%d", &ch);
getchar();


switch(ch)
{
case 1:
n = 0;
showAllStuScore(username, password);
break;
case 2:
n = 0;
stuHead = addStuInfo(username, password);
break;
case 3:
n = 0;
stuHead = delStuInfo(username, password);
break;
case 4:
n = 0;
changePassword(username, password);
break;
case 5:
return;
break;
default:
n++;
printf("choice must be 1~5, please input again.\n");
printf("Press any key to continue...\n");
getchar();
break;
}
}
}


void studentMenu(char *username, char *password)
{
int n = 0;
int ch;

while(n < 3)
{
system("clear");


printf("Welcome student %s come back\n", username);
printf("1.query score\n");
printf("2.change the password\n");
printf("3.logout\n");
printf("Please input your choice:");


scanf("%d", &ch);
getchar();


switch(ch)
{
case 1:
n = 0;
showStuScore(username, password);
break;
case 2:
n = 0;
changePassword(username, password);
break;
case 3:
return;
break;
default:
n++;
printf("choice muse be 1~3, please input again\n");
printf("Press any key to continue...\n");
getchar();
break;
}


}


return;
}


void loadUserInfo()
{
FILE *fp=NULL;
char buffer[128];
char *delim=":";
struct User *p1=NULL;
struct User *p2=NULL;
int n=1;

fp = fopen(USERFILE, "r");
if(NULL == fp)
{
perror("fopen error");
}


while(fgets(buffer, 128, fp))
{
p1 = (struct User *)malloc(sizeof(struct User));
if(NULL == p1)
{
printf("%s %d: malloc error\n", __func__, __LINE__);
return ;
}


if(n == 1)
{
userHead = p1;
}
else
{
p2->next = p1;
}


strcpy(p1->name, strtok(buffer, delim));
strcpy(p1->password, strtok(NULL, delim));
p1->level = atoi(strtok(NULL, delim));
p1->active = atoi(strtok(NULL, delim));


p2 = p1;
n++;
}


p2->next = NULL;

return;
}


void printUserInfo()
{
struct User *p1=NULL;


p1 = userHead;


while(p1 != NULL)
{
printf("%s\t%s\t%d\t%d\n", p1->name, p1->password,p1->level,p1->active);
p1 = p1->next;
}


return ;
}


void printStuInfo()
{
struct Student *p1=NULL;

p1 = stuHead;

while(p1 != NULL)
{
printf("%d\t%s\t%d\t%d\t%d\n", p1->num, p1->name, p1->mathScore, p1->englishScore, p1->scienceScore);
p1 = p1->next;
}


return;
}


void loadStuInfo()
{
FILE *fp=NULL;
struct Student *p1=NULL;
struct Student *p2=NULL;
char buffer[128];
int n=1;


fp = fopen(STUFILE, "r");
if(NULL == fp)
{
perror("fopen errpr");
}


while(fgets(buffer, 128, fp))
{
p1 = (struct Student *)malloc(sizeof(struct Student));
if(NULL == p1)
{
printf("%s %d:malloc error\n");
return;
}

if(n == 1)
{
stuHead = p1;
}
else
{
p2->next = p1;
}


p1->num=atoi(strtok(buffer, " "));
strcpy(p1->name, strtok(NULL, " "));
p1->mathScore = atoi(strtok(NULL, " "));
p1->englishScore = atoi(strtok(NULL, " "));
p1->scienceScore = atoi(strtok(NULL, " "));

p2 = p1;
n++;
}


p2->next = NULL;

return ;
}


struct User *addUser(char *username, char *password)
{
system("clear");


struct User *p1=NULL;
struct User *p2=NULL;
char name[10];
char passwd[10];
int level;

printf("Input name:");
scanf("%s", name);
getchar();
printf("Input passwd:");
scanf("%s", passwd);
getchar();
printf("Input level:");
scanf("%d", &level);
getchar();

p1 = userHead;


while(p1->next != NULL)
{
if(strcmp(p1->name, name) == 0)
{
strcpy(p1->password, passwd);
p1->level = level;
fileUserFlag = 1;
return userHead;
}

p1 = p1->next;
}


if(strcmp(p1->name, name) == 0)
{
strcpy(p1->password, passwd);
p1->level = level;
fileUserFlag = 1;
return userHead;
}




p2 = (struct User*)malloc(sizeof(struct User));
if(NULL == p2)
{
printf("%s %d:malloc error");
return userHead;
}


strcpy(p2->name, name);
strcpy(p2->password, passwd);
p2->level = level;
p2->active = 0;

p1->next = p2;
p2->next = NULL;


fileUserFlag = 1;


return userHead;
}


struct User *delUser(char *username, char *password)
{
system("clear");


struct User *p1=NULL;
char name[10];


printf("Input delete user name:");
scanf("%s", name);
getchar();


if(strcmp(userHead->name, name) == 0)
{
userHead = userHead->next;
fileUserFlag = 1;
return userHead;
}


p1 = userHead;
while(p1->next != NULL)
{
if(strcmp(p1->next->name, name) == 0)
{
p1->next = p1->next->next;
fileUserFlag = 1;
return userHead;
}
p1 = p1->next;
}

printf("Press any key to continue...\n");
getchar();


return userHead;
}
void showUsers()
{
system("clear");


struct User *p1=NULL;


if(userHead == NULL)
{
return;
}

p1 = userHead;


printf("%-10s%-10s%-10s%-10s\n", "name", "password", "level", "active");
do{
printf("%-10s%-10s%-10d%-10d\n", p1->name, p1->password, p1->level, p1->active);
p1 = p1->next;
}while(p1 != NULL);

printf("Press any key to continue...\n");
getchar();


return;
}
void showOnlineUsers()
{
system("clear");


struct User *p1=NULL;


if(userHead == NULL)
{
return;
}

p1 = userHead;


printf("%-10s%-10s%-10s%-10s\n", "name", "password", "level", "active");
do{
if(p1->active == 1){
printf("%-10s%-10s%-10d%-10d\n", p1->name, p1->password, p1->level, p1->active);
}
p1 = p1->next;
}while(p1 != NULL);


printf("Press any key to continue...\n");
getchar();

return;


}
void changePassword(char *username, char *password)
{
system("clear");


char oldPassword[10];
char newPassword1[10];
char newPassword2[10];
struct User *p1=NULL;


printf("Input oldPassword:");
scanf("%s", oldPassword);
getchar();
printf("Input newPassword:");
scanf("%s", newPassword1);
getchar();
printf("Verify the newPassword:");
scanf("%s", newPassword2);
getchar();




if(strcmp(password, oldPassword) != 0)
{
printf("old password is not the same\n");
printf("Press any key to continue...\n");
getchar();
return ;
}

if(strcmp(newPassword1, newPassword2) != 0)
{
printf("new password is not the same\n");
printf("Press any key to continue...\n");
getchar();
return;
}

if(userHead == NULL)
{
return;
}

p1 = userHead;


do{
if(strcmp(p1->name, username) == 0)
{
strcpy(p1->password, newPassword1);
fileUserFlag = 1;
printf("Press any key to continue...\n");
getchar();
return;
}
p1 = p1->next;
}while(p1 != NULL);

printf("Press any key to continue...\n");
getchar();
return ;
}


void showStuScore(char *username, char *password)
{
system("clear");

struct Student *p1=NULL;

if(stuHead == NULL)
{
return;
}

p1 = stuHead;
do{
if(strcmp(p1->name, username) == 0)
{
printf("%-15s%-15s%-15s%-15s%-15s\n", "num", "name", "mathScore", "englishScore", "scienceScore");
printf("%-15d%-15s%-15d%-15d%-15d\n", p1->num, p1->name, p1->mathScore, p1->englishScore, p1->scienceScore);
printf("Press any key to continue...\n");
getchar();
return;
}
p1 = p1->next;
}while(p1 != NULL);


return;
}


void myPrintf(char *format, ...)
{
va_list ap;
va_start(ap, format);
printf("%s %d:", __func__, __LINE__);
vprintf(format, ap);
}


void showAllStuScore(char *username, char *password)
{
system("clear");


struct Student *p1=NULL;


if(stuHead == NULL)
{
return;//no student info
}


p1 = stuHead;
printf("%-15s%-15s%-15s%-15s%-15s\n", "num", "name", "mathScore", "englishScore", "scienceScore");
do{
printf("%-15d%-15s%-15d%-15d%-15d\n", p1->num, p1->name, p1->mathScore, p1->englishScore, p1->scienceScore);
p1 = p1->next;
}while(p1 != NULL);


printf("Press any key to continue...\n");
getchar();


return;
}


struct Student *addStuInfo(char *username, char *password)
{
system("clear");


struct Student *p1=NULL;
struct Student *p2=NULL;
int num;
char name[10];
int mathScore;
int englishScore;
int scienceScore;

printf("Input num:");
scanf("%d", &num);
getchar();
printf("Input name:");
scanf("%s", name);
getchar();
printf("Input mathScore:");
scanf("%d", &mathScore);
getchar();
printf("Input englishScore:");
scanf("%d", &englishScore);
getchar();
printf("Input scienceScore:");
scanf("%d", &scienceScore);
getchar();

if(stuHead == NULL)
{
return stuHead;
}

p1 = stuHead;


while(p1->next != NULL)
{
if(strcmp(p1->name, name) == 0)
{
p1->num = num;
p1->mathScore = mathScore;
p1->englishScore = englishScore;
p1->scienceScore = scienceScore;
fileStuFlag = 1;
return stuHead;
}
p1 = p1->next;
}


if(strcmp(p1->name, name) == 0)
{
p1->num = num;
p1->mathScore = mathScore;
p1->englishScore = englishScore;
p1->scienceScore = scienceScore;
fileStuFlag = 1;
return stuHead;
}




p2 = (struct Student*)malloc(sizeof(struct Student));
if(NULL == p2)
{
printf("%s %d:malloc error");
return stuHead;
}


p2->num = num;
strcpy(p2->name, name);
p2->mathScore = mathScore;
p2->englishScore = englishScore;
p2->scienceScore = scienceScore;

p1->next = p2;
p2->next = NULL;


fileStuFlag = 1;


printf("Press any key to continue...\n");
getchar();


return stuHead;
}


struct Student *delStuInfo(char *username, char *password)
{
system("clear");


struct Student *p1=NULL;
char name[10];


printf("Input delete student name:");
scanf("%s", name);
getchar();


if(strcmp(stuHead->name, name) == 0)
{
stuHead = stuHead->next;
return stuHead;
}


p1 = stuHead;
while(p1->next != NULL)
{
if(strcmp(p1->next->name, name) == 0)
{
p1->next = p1->next->next;
return stuHead;
}
p1 = p1->next;
}


printf("Press any key to continue...\n");
getchar();


return stuHead;
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值