花了一天时间用C语言做了一个学生信息管理系统,有一些想要总结的地方,在此记录一下。
虽然是一个简单的系统,简单到只有增删改查功能,而且,保存数据的地方不是数据库,而是文件,但是,我还是按照了做大型项目时采用的“三层架构”的思路来写:大体上就是:底层,和数据库打交道;中间层,处理用户输入的业务逻辑,并将错误信息反馈给用户;表面层,就是和将界面展现给用户,接受用户的输入。
下面是,总结的一些好的代码:
void back_button()/*在子菜单下,用户按两下回车键,返回主菜单*/
{
printf("Please press \"Enter button\" two times to back previous menu.\n");
char ch;
do
{
while (fflush(stdin), ch = getch(), ch != ‘\r‘);
if ((ch = getch()) == ‘\r‘){
break;
}
} while (1);
system("cls");
}
void sub_choose_1()/*子菜单1:根据学生姓名搜索学生信息*/
{
system("cls");
char stu_name[STU_NAME_SIZE];
memset(stu_name,0,STU_NAME_SIZE);/*将字符串初始化为0*/
loops_search_stu_name:printf("Please input a name for search: \n");
fflush(stdin);
fgets(stu_name, STU_NAME_SIZE, stdin);
if (stu_name[0] == 0){
return;
}
strProcess(stu_name);/*将fgets()添加的‘\n‘去掉*/
if (strlen(stu_name) == 0){
printf("Student‘s name cannot empty!Please input again.\n");
goto loops_search_stu_name;
}
stu_pNode stu;
stu = searchStuInfoByName(stuList,stu_name);
showAllStuInfo(stu);
back_button();
}
(总结一下,由于fgets()的返回值是输入字符串的指针或者NULL,所以没法用返回值进行判断,也就没法用while循环。而scanf()函数的返回值是:正确接受时,返回接受的个数,没正确接受时,返回0,遇到Ctrl+Z时,返回EOF(即-1),所以,可以用循环进行处理)
void sub_choose_2()/*子菜单2:根据学生学号搜索学生信息*/
{
system("cls");
int stu_id, input_info;
printf("Please input a sno for search: \n");
printf("Sno: ");
while (fflush(stdin), input_info = scanf("%d", &stu_id), input_info == EOF || input_info != 1){
if (input_info == EOF){
return;
}
printf("Sno is a number,please input again.\n");
printf("Sno: ");
}
stu_pNode stu;
stu = searchStuInfoById(stuList,stu_id);
showOneStuInfo(stu);
back_button();
}
loop_modify_name:printf("Name: ");
if (fflush(stdin), fgets(new_stu.stu_name, STU_NAME_SIZE, stdin)){/*用户敲回车,此条件成立*/
strProcess(new_stu.stu_name);
}
else{/*用户输入Ctrl+Z,此条件成立*/
strcpy(new_stu.stu_name, stu_info->stu_name);
}
if (strlen(new_stu.stu_name) == 0){
printf("Name must be not empty.If don‘t want to modify,press Ctrl+Z.\n");
goto loop_modify_name;
}
void userNameprocess(char *str)
{
while (*str != 10){
++str;
}
*str = 0;
}
void backspace(int *length,char **ch)
{
--(*ch);
--(*length);
putchar(8);
putchar(32);
putchar(8);
}
void passwordGets(char *psw)
{
char ch;
int psw_length = 0, sign = 0;
while ((ch = getch()) != ‘\r‘){/*用户没有输入回车键时,继续监测键盘*/
if (psw_length < USER_PASSWORD_LEN - 1){/*允许输入的最大长度限制*/
if (ch == 8){/*如果是退格键*/
if (psw_length >0){
sign = 1;
backspace(&psw_length, &psw);
}
else{/*长度小于0,即退格到开头了,响铃警报*/
putchar(7);
}
}
else{/*不是退格键,继续前进*/
sign = 0;
*(psw++) = ch;
++psw_length;
}
if (!sign){/*退格键标记,当用户按下退格键时,不再打印‘*‘.*/
putchar(‘*‘);
}
}
else if (ch == 8){/*长度超过限制,为能重新输入,即满足输入条件(lentgh<psw_length < USER_PASSWORD_LEN - 1),在按退格键时,退一格*/
backspace(&psw_length, &psw);
}
else{/*长度已经超过最大限制了,如继续输入非回车非退格键,则响铃警报*/
putchar(7);
}
}
*psw = ‘\0‘;
}
int login()
{
char user_name[USER_NAME_SIZE];
char user_password[USER_PASSWORD_LEN];
user_pNode user;
int count = 3;
do{
--count;
system("cls");
printf("enter user_name: ");
fgets(user_name, USER_NAME_SIZE + 1, stdin);/*获取用户名*/
userNameprocess(user_name);
system("cls");
printf("enter usr_password: ");
passwordGets(user_password);/*获取用户密码*/
user = searchUserAccountInfoByName(userList, user_name);
if (user){
if (strcmp(user->user_password, user_password) != 0){
if (count == 0){
system("cls");
printf("You have already input Wrong 3 times,system will be exit.\n");
}
else{
printf("\nPassword wrong,please try again!you have %d times.\n", count);
system("pause");
}
}
else{
return user->role_type;
}
}
else{
if (count == 0){
system("cls");
printf("You have already input Wrong 3 times,system will be exit.\n");
}
else{
printf("\nUser name is not exist,please try again!you have %d times.\n", count);
system("pause");
}
}
} while (count);
exit(-1);
}
原文地址:http://blog.csdn.net/nyist327/article/details/30853529