前言
C语言管理系统简单来说就是使用C语言的链表,文件读写等知识实现数据的录入,添加,修改等。代码的实现过程并不复杂,主要是思路,系统都要实现什么操作,主方法中写什么,都要有什么函数。只要有了整体构架,实现起来很容易。
请看我的药店管理系统
实现过程分析
1.整体构架
要进行管理系统数据操作,肯定不只一个数据,所以要选用链表或数组用来操作数据,考虑到要进行添加,删除等操作用链表更方便安全。所以用有一个主链表来操作数据
struct st
{
char name[20];
int price;
int number;
int rnumber;
char kind[20];
struct st *next;
};
struct st *note = NULL;
药品信息是放在文件中得那么自然要读文件,返回结构体链表类型让note接收
struct st *read(void);
接下来用链表操作实现功能如添加函数
void append(struct st *note);
最后保存修改信息,即把修改好的数据写到文件中
void save(struct st *note); // 数据保存
2.功能模块图
3. 方法调用关系
这里的menu1()是密码设置
重要代码分析
登录模块(附加内容可不加)
运行后先进入登录页面,登录页面有登录和注册两项,没有注册的要先注册。
int choice;
printf("\n\n\n\n\n");
printf("\t\t\t\t#######################################################\n");
printf("\t\t\t\t#######################################################\n");
printf("\t\t\t\t## ##\n");
printf("\t\t\t\t## 1.登录 ##\n");
printf("\t\t\t\t## 2.注册 ##\n");
printf("\t\t\t\t## 0.退出 ##\n");
printf("\t\t\t\t#######################################################\n");
printf("\t\t\t\t#######################################################\n");
printf("\t\t\t\t请选择你将要进行的工作:");
scanf("%d",&choice);
switch(choice)
{
case 1: login();break;
case 2: logon();break;
case 0: break;
default :
printf("\t\t\t\t输入错误请从新输入\n");
Sleep(1000);
system("cls");
menu1();
break;
}
既然要登录注册肯定也与文件关联涉及读文件等这里用结构体储存用户名和用户密码
struct log
{
char username[20]; //用户名
char password[20]; //用户密码
};
注册:
struct log a,b;
FILE *fp;
char t[20];
printf("\t\t\t\t欢迎来到注册界面!\n");
fp = fopen("username.txt","r");
fread(&b, sizeof(struct log), 1, fp);
printf("\t\t\t\t请输入账号\n");
printf("\t\t\t\t");
scanf("%s",a.username);
while (1) //判断用户名是否重复
{
if (strcmp(a.username, b.username))
{
if (!feof(fp)) //如果没到文件结尾
fread(&b, sizeof(struct log), 1, fp);
else
break; //无重复用户名进入下一步
}
else
{
printf("\t\t\t\t此用户名已存在!请重新输入!\n");
Sleep(1000);
fclose(fp);
logon();
return;//这里的return不能少负责执行完logon();又进入这里
}
}
printf("\t\t\t\t请输入密码\n");
printf("\t\t\t\t");
scanf(" %s",a.password);
printf("\t\t\t\t请确认密码\n");
printf("\t\t\t\t");
scanf(" %s",t);
//判断两次密码是否一样,一样的话写入文件注册完成
do
{
if(!strcmp(a.password,t))
{
fp = fopen("username.txt","a");
fwrite(&a, sizeof(struct log), 1, fp);
printf("\t\t\t\t注册成功!\n");
Sleep(500);
fclose(fp);
system("cls");
menu1();
return;
}
else
{
printf("\t\t\t\t两次密码不一致!请重新输入!\n");
printf("\t\t\t\t");
scanf("%s",a.password);
printf("\t\t\t\t请确认密码\n");
printf("\t\t\t\t");
scanf("%s",t);
}
}while(1);
}
登录:
system("cls");
printf("\n\n\n");
struct log a,b;
FILE *fp;
printf("\t\t\t\t欢迎来到登录界面!\n");
fp = fopen("username.txt","r");
fread(&b, sizeof(struct log), 1, fp);
printf("\t\t\t\t请输入账号\n");
printf("\t\t\t\t");
scanf("%s",a.username);
//判断用户名是否存在
while (1)
{
if (strcmp(a.username, b.username)==0)
break;
else
{
if (!feof(fp))
fread(&b, sizeof(struct log), 1, fp);
else
{
printf("\t\t\t\t此用户名不存在!\n");
Sleep(500);
fclose(fp);
menu1();
}
}
}
printf("\t\t\t\t请输入密码\n");
printf("\t\t\t\t");
int i=0;
while((a.password[i]=getch())!='\r') //回车结束
{
if(a.password[i]=='\b') //退格
{
printf("\b \b");
i--;
}
else
{
printf("*"); //加密输入
i++;
}
}
a.password[i]='\0';
do
{
if (strcmp(a.password, b.password)==0)
{
fclose(fp);
printf("\n\t\t\t\t登录成功,欢迎使用!\n");
Sleep(500);
system("cls");
break;
}
else
{
printf("\t\t\t\t密码不正确!请重新输入!\n");
i=0;
while((a.password[i]=getch())!='\r')
{
printf("\t\t\t\t");
if(a.password[i]=='\b')
{
printf("\b \b");
i--;
}
else
{
printf("*");
i++;
}
}
a.password[i]='\0';
}
}while(strcmp(a.password, b.password)==0);
代码展示
//药店管理系统
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
struct st
{
char name[20];
int price;
int number;
int rnumber;
char kind[20];
struct st *next; //采用链表
};
struct log
{
char username[20];
char password[20];
};
struct st *read(void); //数据读入
struct st *input(); //录入数据
void append(struct st *note); //添加数据系统
void add1(struct st *note);
void add2(struct st *note);
void modify(struct st *note); //修改数据
void dlete(struct st *note); //删除数据
void find(struct st *note); // 查找数据系统
void find_1(struct st *note);
void find_2(struct st *note);
void count(struct st *note); //统计数据系统
void count_1(struct st *note);//按类别统计
void count_2(struct st *note);//按价位统计
void count_3(struct st *note);//按类别和价位统计
void sort(struct st *note); // 数据排序系统
void sort_1(struct st *note);
void sort_2(struct st *note);
void sort_3(struct st *note);
void print(struct st *note); // 数据打印
void save(struct st *note); // 数据保存
void logon(); //账号注册
void login(); //账号登录
void menu1();
void menu2();
int main()
{
menu1(); //用户登录注册
menu2(); //主菜单
}
void menu1()
{
//system("color b5");
int choice;
printf("\n\n\n\n\n");
printf("\t\t\t\t#######################################################\n");
printf("\t\t\t\t#######################################################\n");
printf("\t\t\t\t## ##\n");
printf("\t\t\t\t## 1.登录 ##\n");
printf("\t\t\t\t## 2.注册