药店药品管理系统(c语言版,使用链表)
一、声明后面所需要的结构体和函数
声明所需要的结构体、链表节点和函数部分
// 定义用户结构体
struct user {char username[20];char password[20];
};/*建立一个结构体储存商品信息*/
struct medicine
{char name[20];int price;int number;
};struct node
{struct medicine mc;struct node*next;
};/*注册用户的函数声明*/
void register_user();/*加密密码的函数声明*/
void encrypt_password(char*password);/*解密密码的函数声明*/
void decrypt_password(char*password);/*登录用户的函数声明*/
int login_user();/*删除用户的函数声明*/
void delate_user();/*修改密码的函数声明*/
void modisy_password(); /*欢迎界面的函数声明*/
void welcome();/*输入药品信息的函数声明*/
void inputmedicine(struct node* head);/*打印药品信息报表的函数声明*/
void printmedicine(struct node*head);/*计数药品的种数的函数声明*/
void countmedicine(struct node*head);/*寻找药品信息的函数声明*/
void findmedicine(struct node*head);/*修改药品信息的函数声明*/
void modisymedicine(struct node*head);/*删除药品信息的函数声明*/
void delatemedicine(struct node*head);/*给药品排序的函数声明*/
void sortmedicine(struct node*head);/*数据插入的函数声明*/
void insertmedicine(struct node* head);/*数据统计函数*/
void statisticsmedicine(struct node* head);/*保存药品信息的函数声明*/
void keepmedicine(struct node*head);/*读取保存药品信息的文件的函数声明*/
void loadmedicine(struct node* head);
二、页面设计
本系统设置了用户界面和欢迎界面
1.用户界面
printf("*************************\n");/*用户界面*/
printf("*\t用户界面\t*\n");
printf("*************************\n");
printf("* 1.注册用户\t*\n");
printf("* 2.登录账号\t*\n");
printf("* 3.删除用户\t*\n");
printf("* 4.修改密码\t*\n");
printf("* 5.退出系统\t*\n");
printf("*************************\n");
printf("* 请输入您的选择 *\n");
printf("*************************\n");
为了实现功能而配合使用的switch case多分支结构
int choice;
scanf("%d", &choice);
switch (choice)
{case 1:register_user();break;case 2:logged_in = login_user();if (!logged_in) {printf("登录失败,请重试。\n");}break;case 3:delate_user();break;case 4:modisy_password();break;case 5:printf("欢迎下次使用~");return;default:printf("无效的选择,请重新输入。\n");
}
2.欢迎界面
void welcome()
{printf("*************************\n");printf("* 药店药品管理系统\t*\n");printf("*************************\n");printf("* 1输入药品信息\t*\n");printf("* 2打印药品信息报表\t*\n");printf("* 3查找对应药品\t*\n");printf("* 4修改药品信息\t*\n");printf("* 5删除药品信息\t*\n");printf("* 6给药品排序\t\t*\n");printf("* 7药品数据插入\t*\n");printf("* 8简单统计药品数量\t*\n");printf("* 9药品数据综合统计\t*\n");printf("* 10关闭程序\t\t*\n");printf("*************************\n");printf("* 请输入您的选择\t*\n");printf("*************************\n");
}
为了实现功能而配合使用的switch case多分支结构
int num;
scanf("%d",&num);
switch(num)
{case 1:inputmedicine(head);break;/*输入药品信息*/case 2:printmedicine(head);break;/*打印药品信息报表*/case 3:findmedicine(head);break;/*查询对应药品信息*/case 4:modisymedicine(head);break;/*修改药品信息*/case 5:delatemedicine(head);break;/*删除药品信息*/case 6:sortmedicine(head);break;/*给药品排序*/case 7:insertmedicine(head);break;/*在随意位置插入药品信息*/case 8:countmedicine(head);break;/*简单统计药品数量*/case 9:statisticsmedicine(head);break;/*药品数据综合统计*/case 10:printf("欢迎下次使用~\n");return;default:printf("请重新输入数字\n");break;
}
三、用户界面调用的函数
1.注册用户函数
让用户输入用户名和密码,给密码加密后,将其存入"./user.info"文件中
void register_user()
{struct user new_user;printf("请输入用户名: ");scanf("%s", new_user.username);printf("请输入密码: ");scanf("%s", new_user.password);encrypt_password(new_user.password);/*加密密码*/FILE *file = fopen("./users.info", "ab");if (file == NULL) {printf("打开文件失败\n");return;}fwrite(&new_user, sizeof(struct user), 1, file);fclose(file);printf("注册成功!\n");system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/
}
2.加密密码函数
该函数在注册用户函数、登录账号函数、修改密码函数和删除用户函数等函数里调用,采取了凯撒加密的方法,将密码导入函数,给每一位加3
void encrypt_password(char*password)
{while(*password){*password=*password+3;password++;}
}
3.解密密码函数
相当于加密密码的反函数,将每一位密码导入函数,给每一位减3
void decrypt_password(char*password)
{while(*password){*password=*password-3;password++;}
}
4.登录用户函数
让用户输入用户名和密码,与存储用户信息的文件里的信息比较,如果相同则登陆成功,可以进入欢迎界面了
int login_user()
{char username[20];char password[20];printf("请输入用户名: ");scanf("%s", username);printf("请输入密码: ");scanf("%s", password);system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/ FILE *file = fopen("./users.info", "rb");if (file == NULL) {printf("未找到用户文件\n");return 0;}struct user temp;while (fread(&temp, sizeof(struct user), 1, file) == 1) {decrypt_password(temp.password);if (strcmp(username, temp.username) == 0 && strcmp(password, temp.password) == 0) {fclose(file);return 1;}}fclose(file);return 0;
}
5.删除用户函数
在用户输入用户名和密码后,与文件里存储的信息比较,如果不同则输出该用户不存在,如果相同,则建立一个临时文件,用来储存其他用户数据,再将原来的储存文件(里面只剩下要删除的用户信息了)删除,将临时文件改名为原来的名字即可,最后通过found判断是否成功删除函数
void delate_user()
{char username[30], password[30];int found = 0;printf("输入用户名: ");scanf("%s", username);printf("输入密码: ");scanf("%s", password);FILE *file = fopen(USER_FILE, "rb");if (!file) {printf("用户文件不存在!\n");return;}FILE *temp_file = fopen("temp.txt", "wb");if (!temp_file) {printf("无法创建临时文件!\n");fclose(file);return;}struct user temp;while (fread(&temp, sizeof(struct user), 1, file) == 1) {decrypt_password(temp.password);if (strcmp(username, temp.username) == 0 && strcmp(password, temp.password) == 0) {found = 1;printf("用户 %s 已被删除!\n", username);} else {encrypt_password(temp.password);fwrite(&temp, sizeof(struct user), 1, temp_file);}}fclose(file);fclose(temp_file);remove(USER_FILE);rename("temp.txt", USER_FILE);if (!found) {printf("用户名或密码错误,无法删除用户!\n");}system("pause");system("cls");
}
6.修改密码函数
先让用户输入用户名和密码,与储存文件里面的信息比较,如果不同则输出该用户不存在,如果相同,则建立一个临时文件来储存要修改的用户信息和其他不修改的用户信息,将原文件删掉,将新建立的文件改名为原文件名,再根据found判断用户是否成功修改了密码
void modisy_password()
{char username[30], old_password[30], new_password[30];int found = 0;printf("输入用户名: ");scanf("%s", username);printf("输入旧密码: ");scanf("%s", old_password);FILE *file = fopen(USER_FILE, "rb");if (!file) {printf("用户文件不存在!\n");return;}FILE *temp_file = fopen("temp.txt", "wb");if (!temp_file) {printf("无法创建临时文件!\n");fclose(file);return;}struct user temp;while (fread(&temp, sizeof(struct user), 1, file) == 1){decrypt_password(temp.password);if (strcmp(username, temp.username) == 0 && strcmp(old_password, temp.password) == 0) {found = 1;printf("输入新密码: ");scanf("%s", new_password);strcpy(temp.password, new_password);encrypt_password(temp.password);fwrite(&temp, sizeof(struct user), 1, temp_file);} else {encrypt_password(temp.password);fwrite(&temp, sizeof(struct user), 1, temp_file);}}fclose(file);fclose(temp_file);remove(USER_FILE);rename("temp.txt", USER_FILE);if (found) {printf("密码修改成功!\n");} else {printf("用户名或旧密码错误,无法修改密码!\n");}system("pause");system("cls");
}
四、欢迎界面调用的函数
1.输入药品信息函数
建立一个新节点,向系统申请一个node大小的空间fresh,后面指向空,防止运行时链表跑到奇怪的地方上去了,再来遍历链表,把fresh接在链表末端,再调用一下保存药品信息函数来保存
void inputmedicine(struct node*head)
{struct node*fresh=malloc(sizeof(struct node));fresh->next=NULL;printf("请输入药品的名称,价格(元),数量(盒):\n");scanf("%s %d %d",fresh->mc.name,&fresh->mc.price,&fresh->mc.number);struct node*move=head;while(move->next!=NULL){move=move->next;}move->next=fresh;keepmedicine(head);/*保存药品信息*/system("pause");/*暂停代码运行*/system("cls");/*清空运行界面,防止内容太过杂乱*/
}
2.打印药品信息报表函数
遍历链表,把链表每一节点的内容都按格式输出出来
void printmedicine(struct node*head)
{struct node*move=head->next;while(move!=NULL){printf("名称:%s\t\t价格(元):%d\t数量(盒):%d\n",move->mc.name,move->mc.price,move->mc.number);move=move->next;}system("pause");/*暂停代码运行*/system("cls");/*清空运行界面,防止内容太过杂乱*/
}
3.简单统计药品数量函数
遍历链表,每经历一个节点就加1,遍历完后即可知道药品数量
void countmedicine(struct node*head)
{int num=0;/*计数药品种数*/ struct node*move=head->next;while(move!=NULL){num++;move=move->next;}printf("药品共有%d种\n",num);system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/
}
4.查询药品信息函数
本函数设计了4种查询方式,按名称查询,按数量查询,按名称+数量组合查询,按名称+价格组合查询,以switch case分支结构分别将其列出来
void findmedicine(struct node*head)
{printf("输入1按名称查询\n输入2按数量查询\n输入3按名称数量组合查询\n输入4按名称价格组合查询\n"); int b;scanf("%d",&b);struct node*move=head->next;int num;int pc; char name[20];switch(b) {case 1: printf("请输入所找药品的名称\n"); /*通过名字查找药品信息*/ scanf("%s",name);while(move!=NULL){if(strcmp(name,move->mc.name)==0){printf("价格(元):%d,数量(盒):%d\n",move->mc.price,move->mc.number);system("pause");system("cls");return;}move=move->next;}printf("未找到该药品\n");system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/break;case 2: printf("请输入所找药品的数量(盒)\n"); /*通过数量查找药品信息*/ scanf("%d",&num);while(move!=NULL){if(num==move->mc.number){printf("名称:%s,价格(元):%d\n",move->mc.name,move->mc.price);system("pause");system("cls");return;}move=move->next;}printf("未找到该药品\n");system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/break;case 3: printf("请输入所找药品的名称和数量(盒)\n"); /*通过名称和数量组合查找药品信息*/ scanf("%s %d",name,&num);while(move!=NULL){if(num==move->mc.number&&strcmp(name,move->mc.name)==0){printf("价格(元):%d\n",move->mc.price);system("pause");system("cls");return;}move=move->next;}printf("未找到该药品\n");system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/break;case 4: printf("请输入所找药品的名称和价格(元)\n"); /*通过名称和价格组合查找药品信息*/ scanf("%s %d",name,&pc);while(move!=NULL){if(pc==move->mc.price&&strcmp(name,move->mc.name)==0){printf("数量(盒):%d\n",move->mc.number);system("pause");system("cls");return;}move=move->next;}printf("未找到该药品\n");system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/break;default:printf("无效输入");return;}
}
5.修改药品信息函数
输入要修改药品的名称,遍历数组来找系统中是否有该药品,如果有,则来修改该药品信息,如果没有则输出“未找到该药品信息”
void modisymedicine(struct node*head)
{printf("请输入要修改药品的名称\n");char name[20];scanf("%s",name);struct node*move=head->next;while(move!=NULL){if(strcmp(name,move->mc.name)==0){printf("请输入药品名称、价格和数量:");scanf("%s %d %d",move->mc.name,&move->mc.price,&move->mc.number);keepmedicine(head); system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/return;}move=move->next;}printf("未找到药品信息\n");system("pause");/*暂停代码运行*/system("cls");/*清空运行界面,防止内容太过杂乱*/
}
6.删除药品信息函数
输入要删除的药品名称,遍历数组来找系统中是否有该药品,如果有,则建立一个node类型的tmp指针暂存该药品信息,再将其上一个节点的指针指向其下一个节点,将tmp空间释放来删除该药品信息,如果没有则输出“未找到该药品信息”
void delatemedicine(struct node*head)
{printf("请输入要删除药品的名称\n");char name[20];scanf("%s",name);struct node*move=head; while(move->next!=NULL){if(strcmp(name,move->next->mc.name)==0) {struct node*tmp=move->next; move->next=move->next->next; free(tmp);tmp=NULL;keepmedicine(head);printf("删除成功\n");system("pause");/*暂停代码运行*/system("cls");/*清空运行界面,防止内容太过杂乱*/return;}move=move->next;}printf("未找到该药品\n");system("pause");/*暂停代码运行*/system("cls");/*清空运行界面,防止内容太过杂乱*/
}
7.给药品排序函数
该函数设计了两张排序方式,按价格排序和安数量排序,以switch case分支结构来实现选择功能,以价格排序为例,输入1后进入case1,以冒泡排序来将链表里药品排序(这里是从小到大排列),排好后调用打印药品函数给用户展示一下,再调用保存药品信息函数保存一下排好的顺序
void sortmedicine(struct node* head) {printf("输入1按价格排序,输入2按数量排序\n");/*价格由低到高,数量由大到小*/int b;scanf("%d", &b);struct node* turn;struct node* move;switch (b) {case 1:for (turn = head->next; turn->next != NULL; turn = turn->next)/*给药品按价格排序*/{for (move = head->next; move->next != NULL; move = move->next){if (move->mc.price > move->next->mc.price){struct medicine tmp = move->mc;move->mc = move->next->mc;move->next->mc = tmp;}}}printmedicine(head);keepmedicine(head);break;case 2:for (turn = head->next; turn->next != NULL; turn = turn->next)/*给药品按数量排序*/{for (move = head->next; move->next != NULL; move = move->next){if (move->mc.number < move->next->mc.number){struct medicine tmp = move->mc;move->mc = move->next->mc;move->next->mc = tmp;}}}printmedicine(head);keepmedicine(head);break;default:printf("无效输入");break;}
}
8.插入函数
先建立好node类型的fresh和move指针,填入要插入位置后,再写药品的相关信息,再来以一个for循环,将 move 指针移动到要插入位置的前一个节点。循环条件确保 move 指针移动到正确的位置,move->next != NULL 用于防止在链表长度不足时越界访问。如果指定的插入位置超出了当前链表的长度,此时将其接入到链表末端
void insertmedicine(struct node* head)
{int position;struct node* fresh = malloc(sizeof(struct node));struct node* move = head;int i;printf("请输入要插入的位置(从1开始计数): ");scanf("%d", &position);printf("请输入药品的名称,价格(元),数量(盒):\n");scanf("%s %d %d", fresh->mc.name, &fresh->mc.price, &fresh->mc.number);for (i = 1; i < position - 1 && move->next != NULL; i++){move = move->next;}if (i != position - 1 || move->next == NULL && i < position - 1)/*如果位置超出链表长度,将新节点插入到链表末尾*/{move->next = fresh;fresh->next = NULL;}else{fresh->next = move->next;move->next = fresh;}keepmedicine(head);printf("插入成功\n");system("pause");system("cls");
}
9.综合统计药品数据函数
遍历链表,将所有的药品数量加起来,将所有药品的价钱加起来,输出总数和总价
void statisticsmedicine(struct node* head)
{ struct node* move = head->next; int totalNumber = 0; /*用于统计药品总数量*/int totalPrice = 0; /*用于统计药品总价格*/while (move != NULL) {totalNumber += move->mc.number;totalPrice += move->mc.price * move->mc.number;move = move->next;}printf("药品总数量: %d 盒\n", totalNumber);printf("药品总价格: %d 元\n", totalPrice);system("pause");system("cls");
}
10.保存药品信息函数
建立一个文件来储存药品信息,使用while循环将链表中的所有数据写入文件中保存
void keepmedicine(struct node*head)
{FILE*file=fopen("./gs.info","w");if(file==NULL){printf("打开文件失败\n");return;}struct node*move=head->next;while(move!=NULL){if(fwrite(&move->mc,sizeof(struct medicine),1,file)!=1){printf("保存出现错误\n");}move=move->next;}fclose(file);/*关闭文件*/
}
11.读取储存药品信息的文件
先打开该文件,如果没有文件则跳过读取,创建node类型的move指针,用来在链表里移动,以便将新节点添加到链表结尾,创建一个临时变量temp来储存读取到的药品信息,使用fread来读取文件的数据,将它们一一写入到链表中
void loadmedicine(struct node* head)
{FILE* file = fopen("./gs.info", "r");/*打开对应文件*/ if (!file) {printf("没有药品文件,跳过读取\n");return;}struct node* move = head;struct medicine temp;while (fread(&temp, sizeof(struct medicine), 1, file) == 1){struct node* fresh = malloc(sizeof(struct node));fresh->mc = temp;fresh->next = NULL;move->next = fresh;move = fresh;}fclose(file); printf("读取成功\n");
}
五、总结
以上是写该系统的总体思路,如果有不好的地方欢迎大佬的批评指正(✿✿ヽ(°▽°)ノ✿)
系统原代码如下
#include<stdio.h>
#include<stdlib.h>
#include<string.h>#define USER_FILE "./users.info"// 定义用户结构体
struct user {char username[20];char password[20];
};/*建立一个结构体储存商品信息*/
struct medicine
{char name[20];int price;int number;
};struct node
{struct medicine mc;struct node*next;
};/*注册用户的函数声明*/
void register_user();/*加密密码的函数声明*/
void encrypt_password(char*password);/*解密密码的函数声明*/
void decrypt_password(char*password);/*登录用户的函数声明*/
int login_user();/*删除用户的函数声明*/
void delate_user();/*修改密码的函数声明*/
void modisy_password(); /*欢迎界面的函数声明*/
void welcome();/*输入药品信息的函数声明*/
void inputmedicine(struct node* head);/*打印药品信息报表的函数声明*/
void printmedicine(struct node*head);/*计数药品的种数的函数声明*/
void countmedicine(struct node*head);/*寻找药品信息的函数声明*/
void findmedicine(struct node*head);/*修改药品信息的函数声明*/
void modisymedicine(struct node*head);/*删除药品信息的函数声明*/
void delatemedicine(struct node*head);/*给药品排序的函数声明*/
void sortmedicine(struct node*head);/*数据插入的函数声明*/
void insertmedicine(struct node* head);/*数据统计函数*/
void statisticsmedicine(struct node* head);/*保存药品信息的函数声明*/
void keepmedicine(struct node*head);/*读取保存药品信息的文件的函数声明*/
void loadmedicine(struct node* head);#include"medicinesystem.h"int main()
{int logged_in = 0;while (!logged_in){printf("*************************\n");/*用户界面*/ printf("*\t用户界面\t*\n");printf("*************************\n");printf("* 1.注册用户\t*\n");printf("* 2.登录账号\t*\n");printf("* 3.删除用户\t*\n");printf("* 4.修改密码\t*\n");printf("* 5.退出系统\t*\n");printf("*************************\n");printf("* 请输入您的选择 *\n");printf("*************************\n");int choice;scanf("%d", &choice);switch (choice){case 1:register_user();break;case 2:logged_in = login_user();if (!logged_in) {printf("登录失败,请重试。\n");}break;case 3:delate_user();break;case 4:modisy_password();break;case 5:printf("欢迎下次使用~");return;default:printf("无效的选择,请重新输入。\n");}}struct node*head=malloc(sizeof(struct node));head->next=NULL;loadmedicine(head);/*读取储存药品信息的文件*/ while(1) {welcome();/*欢迎界面*/ int num;scanf("%d",&num);switch(num){case 1:inputmedicine(head);break;/*输入药品信息*/case 2:printmedicine(head);break;/*打印药品信息报表*/case 3:findmedicine(head);break;/*查询对应药品信息*/case 4:modisymedicine(head);break;/*修改药品信息*/case 5:delatemedicine(head);break;/*删除药品信息*/case 6:sortmedicine(head);break;/*给药品排序*/case 7:insertmedicine(head);break;/*在随意位置插入药品信息*/case 8:countmedicine(head);break;/*简单统计药品数量*/case 9:statisticsmedicine(head);break;/*药品数据综合统计*/case 10:printf("欢迎下次使用~\n");return;default:printf("请重新输入数字\n");break;}}return 0;
}/*注册用户*/
void register_user()
{struct user new_user;printf("请输入用户名: ");scanf("%s", new_user.username);printf("请输入密码: ");scanf("%s", new_user.password);encrypt_password(new_user.password);/*加密密码*/FILE *file = fopen("./users.info", "ab");if (file == NULL) {printf("打开文件失败\n");return;}fwrite(&new_user, sizeof(struct user), 1, file);fclose(file);printf("注册成功!\n");system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/
}/*加密密码*/
void encrypt_password(char*password)
{while(*password){*password=*password+3;password++;}
}/*解密密码*/
void decrypt_password(char*password)
{while(*password){*password=*password-3;password++;}
}/*登录用户*/
int login_user()
{char username[20];char password[20];printf("请输入用户名: ");scanf("%s", username);printf("请输入密码: ");scanf("%s", password);system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/ FILE *file = fopen("./users.info", "rb");if (file == NULL) {printf("未找到用户文件\n");return 0;}struct user temp;while (fread(&temp, sizeof(struct user), 1, file) == 1) {decrypt_password(temp.password);if (strcmp(username, temp.username) == 0 && strcmp(password, temp.password) == 0) {fclose(file);return 1;}}fclose(file);return 0;
}/*删除用户*/
void delate_user()
{char username[30], password[30];int found = 0;printf("输入用户名: ");scanf("%s", username);printf("输入密码: ");scanf("%s", password);FILE *file = fopen(USER_FILE, "rb");if (!file) {printf("用户文件不存在!\n");return;}FILE *temp_file = fopen("temp.txt", "wb");if (!temp_file) {printf("无法创建临时文件!\n");fclose(file);return;}struct user temp;while (fread(&temp, sizeof(struct user), 1, file) == 1) {decrypt_password(temp.password);if (strcmp(username, temp.username) == 0 && strcmp(password, temp.password) == 0) {found = 1;printf("用户 %s 已被删除!\n", username);} else {encrypt_password(temp.password);fwrite(&temp, sizeof(struct user), 1, temp_file);}}fclose(file);fclose(temp_file);remove(USER_FILE);rename("temp.txt", USER_FILE);if (!found) {printf("用户名或密码错误,无法删除用户!\n");}system("pause");system("cls");
}/*修改密码*/
void modisy_password()
{char username[30], old_password[30], new_password[30];int found = 0;printf("输入用户名: ");scanf("%s", username);printf("输入旧密码: ");scanf("%s", old_password);FILE *file = fopen(USER_FILE, "rb");if (!file) {printf("用户文件不存在!\n");return;}FILE *temp_file = fopen("temp.txt", "wb");if (!temp_file) {printf("无法创建临时文件!\n");fclose(file);return;}struct user temp;while (fread(&temp, sizeof(struct user), 1, file) == 1){decrypt_password(temp.password);if (strcmp(username, temp.username) == 0 && strcmp(old_password, temp.password) == 0) {found = 1;printf("输入新密码: ");scanf("%s", new_password);strcpy(temp.password, new_password);encrypt_password(temp.password);fwrite(&temp, sizeof(struct user), 1, temp_file);} else {encrypt_password(temp.password);fwrite(&temp, sizeof(struct user), 1, temp_file);}}fclose(file);fclose(temp_file);remove(USER_FILE);rename("temp.txt", USER_FILE);if (found) {printf("密码修改成功!\n");} else {printf("用户名或旧密码错误,无法修改密码!\n");}system("pause");system("cls");
}/*欢迎界面*/
void welcome()
{printf("*************************\n");printf("* 药店药品管理系统\t*\n");printf("*************************\n");printf("* 1输入药品信息\t*\n");printf("* 2打印药品信息报表\t*\n");printf("* 3查找对应药品\t*\n");printf("* 4修改药品信息\t*\n");printf("* 5删除药品信息\t*\n");printf("* 6给药品排序\t\t*\n");printf("* 7药品数据插入\t*\n");printf("* 8简单统计药品数量\t*\n");printf("* 9药品数据综合统计\t*\n");printf("* 10关闭程序\t\t*\n");printf("*************************\n");printf("* 请输入您的选择\t*\n");printf("*************************\n");
}/*输入药品信息*/
void inputmedicine(struct node*head)
{struct node*fresh=malloc(sizeof(struct node));fresh->next=NULL;printf("请输入药品的名称,价格(元),数量(盒):\n");scanf("%s %d %d",fresh->mc.name,&fresh->mc.price,&fresh->mc.number);struct node*move=head;while(move->next!=NULL){move=move->next;}move->next=fresh;keepmedicine(head);/*保存药品信息*/system("pause");/*暂停代码运行*/system("cls");/*清空运行界面,防止内容太过杂乱*/
}/*打印药品信息报表*/
void printmedicine(struct node*head)
{struct node*move=head->next;while(move!=NULL){printf("名称:%s\t\t价格(元):%d\t数量(盒):%d\n",move->mc.name,move->mc.price,move->mc.number);move=move->next;}system("pause");/*暂停代码运行*/system("cls");/*清空运行界面,防止内容太过杂乱*/
}/*简单统计药品数量*/
void countmedicine(struct node*head)
{int num=0;/*计数药品种数*/ struct node*move=head->next;while(move!=NULL){num++;move=move->next;}printf("药品共有%d种\n",num);system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/
}/*查询药品信息*/
void findmedicine(struct node*head)
{printf("输入1按名称查询\n输入2按数量查询\n输入3按名称数量组合查询\n输入4按名称价格组合查询\n"); int b;scanf("%d",&b);struct node*move=head->next;int num;int pc; char name[20];switch(b) {case 1: printf("请输入所找药品的名称\n"); /*通过名字查找药品信息*/ scanf("%s",name);while(move!=NULL){if(strcmp(name,move->mc.name)==0){printf("价格(元):%d,数量(盒):%d\n",move->mc.price,move->mc.number);system("pause");system("cls");return;}move=move->next;}printf("未找到该药品\n");system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/break;case 2: printf("请输入所找药品的数量(盒)\n"); /*通过数量查找药品信息*/ scanf("%d",&num);while(move!=NULL){if(num==move->mc.number){printf("名称:%s,价格(元):%d\n",move->mc.name,move->mc.price);system("pause");system("cls");return;}move=move->next;}printf("未找到该药品\n");system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/break;case 3: printf("请输入所找药品的名称和数量(盒)\n"); /*通过名称和数量组合查找药品信息*/ scanf("%s %d",name,&num);while(move!=NULL){if(num==move->mc.number&&strcmp(name,move->mc.name)==0){printf("价格(元):%d\n",move->mc.price);system("pause");system("cls");return;}move=move->next;}printf("未找到该药品\n");system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/break;case 4: printf("请输入所找药品的名称和价格(元)\n"); /*通过名称和价格组合查找药品信息*/ scanf("%s %d",name,&pc);while(move!=NULL){if(pc==move->mc.price&&strcmp(name,move->mc.name)==0){printf("数量(盒):%d\n",move->mc.number);system("pause");system("cls");return;}move=move->next;}printf("未找到该药品\n");system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/break;default:printf("无效输入");return;}
}/*修改药品信息*/
void modisymedicine(struct node*head)
{printf("请输入要修改药品的名称\n");char name[20];scanf("%s",name);struct node*move=head->next;while(move!=NULL){if(strcmp(name,move->mc.name)==0){printf("请输入药品名称、价格和数量:");scanf("%s %d %d",move->mc.name,&move->mc.price,&move->mc.number);keepmedicine(head); system("pause");/*暂停代码运行*/ system("cls");/*清空运行界面,防止内容太过杂乱*/return;}move=move->next;}printf("未找到药品信息\n");system("pause");/*暂停代码运行*/system("cls");/*清空运行界面,防止内容太过杂乱*/
}/*删除药品信息*/
void delatemedicine(struct node*head)
{printf("请输入要删除药品的名称\n");char name[20];scanf("%s",name);struct node*move=head; while(move->next!=NULL){if(strcmp(name,move->next->mc.name)==0) {struct node*tmp=move->next; move->next=move->next->next; free(tmp);tmp=NULL;keepmedicine(head);printf("删除成功\n");system("pause");/*暂停代码运行*/system("cls");/*清空运行界面,防止内容太过杂乱*/return;}move=move->next;}printf("未找到该药品\n");system("pause");/*暂停代码运行*/system("cls");/*清空运行界面,防止内容太过杂乱*/
}/*给药品排序*/
void sortmedicine(struct node* head) {printf("输入1按价格排序,输入2按数量排序\n");/*价格由低到高,数量由大到小*/int b;scanf("%d", &b);struct node* turn;struct node* move;switch (b) {case 1:for (turn = head->next; turn->next != NULL; turn = turn->next)/*给药品按价格排序*/{for (move = head->next; move->next != NULL; move = move->next){if (move->mc.price > move->next->mc.price){struct medicine tmp = move->mc;move->mc = move->next->mc;move->next->mc = tmp;}}}printmedicine(head);keepmedicine(head);break;case 2:for (turn = head->next; turn->next != NULL; turn = turn->next)/*给药品按数量排序*/{for (move = head->next; move->next != NULL; move = move->next){if (move->mc.number < move->next->mc.number){struct medicine tmp = move->mc;move->mc = move->next->mc;move->next->mc = tmp;}}}printmedicine(head);keepmedicine(head);break;default:printf("无效输入");break;}
}/*在随意位置插入药品信息*/
void insertmedicine(struct node* head) {int position;struct node* fresh = malloc(sizeof(struct node));struct node* move = head;int i;printf("请输入要插入的位置(从1开始计数): ");scanf("%d", &position);printf("请输入药品的名称,价格(元),数量(盒):\n");scanf("%s %d %d", fresh->mc.name, &fresh->mc.price, &fresh->mc.number);for (i = 1; i < position - 1 && move->next != NULL; i++){move = move->next;}if (i != position - 1 || move->next == NULL && i < position - 1)/*如果位置超出链表长度,将新节点插入到链表末尾*/{move->next = fresh;fresh->next = NULL;}else{fresh->next = move->next;move->next = fresh;}keepmedicine(head);printf("插入成功\n");system("pause");system("cls");
}/* 综合数据统计函数 */
void statisticsmedicine(struct node* head)
{ struct node* move = head->next; int totalNumber = 0; /*用于统计药品总数量*/int totalPrice = 0; /*用于统计药品总价格*/while (move != NULL) {totalNumber += move->mc.number;totalPrice += move->mc.price * move->mc.number;move = move->next;}printf("药品总数量: %d 盒\n", totalNumber);printf("药品总价格: %d 元\n", totalPrice);system("pause");system("cls");
}/*保存药品信息*/
void keepmedicine(struct node*head)
{FILE*file=fopen("./gs.info","w");if(file==NULL){printf("打开文件失败\n");return;}struct node*move=head->next;while(move!=NULL){if(fwrite(&move->mc,sizeof(struct medicine),1,file)!=1){printf("保存出现错误\n");}move=move->next;}fclose(file);/*关闭文件*/
}/*读取储存药品信息的文件*/
void loadmedicine(struct node* head)
{FILE* file = fopen("./gs.info", "r");/*打开对应文件*/ if (!file) {printf("没有药品文件,跳过读取\n");return;}struct node* move = head;struct medicine temp;while (fread(&temp, sizeof(struct medicine), 1, file) == 1){struct node* fresh = malloc(sizeof(struct node));fresh->mc = temp;fresh->next = NULL;move->next = fresh;move = fresh;}fclose(file); printf("读取成功\n");
}
相关文章:
药店药品管理系统(c语言版,使用链表)
一、声明后面所需要的结构体和函数 声明所需要的结构体、链表节点和函数部分 // 定义用户结构体 struct user {char username[20];char password[20]; };/*建立一个结构体储存商品信息*/ struct medicine {char name[20];int price;int number; };struct node {struct medi…...
Gparted重新分配swap空间之后,linux电脑读不到swap空间
问题背景 lsblk 显示存在物理设备(如 /dev/nvme0n1),但 swapon --show 无输出 说明 系统未启用任何 Swap 设备 问题原因分析 /etc/fstab 中 Swap 的 UUID 配置错误 从图片中看到执行 sudo swapon -a 时报错: swapoff: cannot fin…...
Paramiko 使用教程
目录 简介安装 Paramiko连接到远程服务器执行远程命令文件传输示例 简介 Paramiko 是一个基于 Python 的 SSH 客户端库,它提供了在网络上安全传输文件和执行远程命令的功能。本教程将介绍 Paramiko 的基本用法,包括连接到远程服务器、执行命令、文件传输…...
第一节:Vben Admin 最新 v5.0初体验
系列文章目录 基础篇 第一节:Vben Admin介绍和初次运行 第二节:Vben Admin 登录逻辑梳理和对接后端准备 第三节:Vben Admin登录对接后端login接口 第四节:Vben Admin登录对接后端getUserInfo接口 第五节:Vben Admin权…...
ARCGIS国土超级工具集1.5更新说明
ARCGIS国土超级工具集V1.5版本更新说明:因作者近段时间工作比较忙及正在编写ARCGISPro国土超级工具集(截图附后)的原因,故本次更新为小更新(没有增加新功能,只更新了已有的工具)。本次更新主要修…...
CNN:卷积到底做了什么?
卷积神经网络(Convolutional Neural Network, CNN) 是一种深度学习模型,专门用于处理具有网格结构的数据(如图像、视频等)。它在计算机视觉领域表现卓越,广泛应用于图像分类、目标检测、图像分割等任务。CN…...
AI应用开发之扣子第二课-AI翻译(第1节/共2节)
简介 共分为两节介绍,内容简单易懂,步骤详细,可以避免很多坑,建议直接上手操作(预估30分钟)。 AI应用开发之扣子第二课学习-AI翻译(第1节/共2节):业务逻辑实现 AI应用…...
linux学习 3.用户的操作
用户 建议在系统操作的时候不要一直使用root用户,因为root用户具有最高权限,你可能因为某些操作影响了你的系统,采用子用户则可以避免这一点 这里的学习不用太深入,掌握如何创建删除切换即可(除非你要做详细的用户管理࿰…...
leetcode 139. Word Break
这道题用动态规划解决。 class Solution { public:bool wordBreak(string s, vector<string>& wordDict) {unordered_set<string> wordSet;for(string& word:wordDict){wordSet.insert(word);}int s_len s.size();//s的下标从1开始起算,dp[j]…...
Vue与React组件化设计对比
组件化是现代前端开发的核心思想之一,而Vue和React作为两大主流框架,在组件化设计上既有相似之处,也存在显著差异。本文将从语法设计、数据管理、组件通信、性能优化、生态系统等多个方向,结合实例详细对比两者的特点。 一、模板…...
Leetcode刷题 由浅入深之哈希表——242. 有效的字母异位词
目录 (一)字母异位词的C实现 写法一(辅助数组) (二)复杂度分析 时间复杂度 空间复杂度 (三)总结 【题目链接】242.有效的字母异位词 - 力扣(LeetCode) …...
自动化构建工具:makemakefile
在Windows中,我们写C代码或者C代码都需要用先找到一款合适的编译器,用来方便我们更好的完成代码,比如说vs2019,这些工具的特点是集成了多种开发所需的功能,如代码编辑、编译、调试、版本控制等,无需在不同的…...
刷题 | 牛客 - js中等10题(更ing)1/54知识点解答
知识点汇总: Array.from(要转换的对象, [mapFn], [thisArg ]):将类数组对象(Array-like)/可迭代对象(Iterable)转为真正的数组。 第二参 mapFn 是 类似 Array.prototype.map 的回调函数,加工…...
Ubuntu 20.04.6编译安装COMFAST CF-AX90无线网卡驱动
目录 0 前言 1 CF-AX90无线网卡驱动 1.1 驱动下载 1.2 驱动准备 2 编译安装驱动 2.1 拷贝驱动依赖到系统 2.2 驱动安装编译 3 重启 0 前言 COMFAST CF-AX90或者说AIC8800D80的Linux版本驱动不支持高版本的linux内核,实测目前仅支持最高5.15的内核。Ubuntu2…...
将python项目打包成Windows后台服务
前文,我开发了一个基于windows11与本地deepseek实现的语音助手,之前是通过CMD直接执行项目的main.py文件。但是这样不适合移植,现在想将其生成一个exe文件,以及部署成windows的后台服务。 关于语音助手的开发与发布,可以看的CSDN文章:一个基于windows11与本地deepseek实…...
PPT无法编辑怎么办?原因及解决方法全解析
在日常办公中,我们经常会遇到需要编辑PPT的情况。然而,有时我们会发现PPT文件无法编辑,这可能由多种原因引起。今天我们来看看PPT无法编辑的几种常见原因,并提供实用的解决方法,帮助你轻松应对。 原因1:文…...
安全用电基础知识及隐患排查重点
安全用电是电气安全的一个重要方面,作为普通人员,必须学会基础的用电知识和技巧,才能保障自己和家庭的安全。 以下是安全用电的基础知识及隐患排查重点: 一、基础知识 1.电压:单位为伏特(V)&a…...
Laravel 使用通义灵码 - AI 辅助开发提升效率
一、引言 Laravel 是 PHP 常用的一种后端开发框架,遵循 MVC(模型 - 视图 - 控制器)架构,以简洁、优雅的语法和强大的功能著称,旨在提升开发效率并简化复杂任务的实现。然而,它的开发习惯可能与传统的 PHP …...
签到功能---实现签到接口
文章目录 概要整体架构流程技术细节小结 概要 需求分析以及接口设计 由KEY的结构可知,要签到,就必须知道是谁在哪一天签到,也就是两个信息: 当前用户 当前时间 这两个信息我们都可以自己获取,因此签到时ÿ…...
JavaScript爬虫基础篇:HTTP 请求与响应
在互联网的世界里,数据无处不在。无论是新闻资讯、商品信息,还是社交媒体动态,这些数据都以各种形式存储在服务器上。而爬虫,就是我们获取这些数据的得力助手。今天,我们就来聊聊爬虫的基础——HTTP 请求与响应&#x…...
Python中的count()方法
文章目录 Python中的count()方法基本语法在不同数据类型中的使用1. 列表(List)中的count()2. 元组(Tuple)中的count()3. 字符串(String)中的count() 高级用法1. 指定搜索范围2. 统计复杂元素 注意事项 Python中的count()方法 前言:count()是Python中用于序列类型&a…...
LWIP_MQTT连接ONENET
前言: 使用正点原子STM32F407, LWIP,MQTT demo,验证LwIP的MQTT连接ONENET物联网平台,测试整个链路是否畅通,后面再详细分析LWIP移植和MQTT协议的使用。 26 基于 MQTT 协议连接 OneNET 服务器 本章主要介绍 lwIP 如何通过 MQTT 协议将设备连接到 OneNET…...
代码随想录刷题|Day20(组合总数,组合总数2、分割回文串)
回溯算法 Part02 组合总数 力扣题目链接 代码随想录链接 视频讲解 题目描述: 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你…...
文件描述符(File Descriptor, FD)详解及利用方法
文件描述符(FD)是 Linux/Unix 系统中用于访问文件、管道、套接字等 I/O 资源的整数标识符。每个进程默认打开 3 个标准文件描述符: FD名称默认绑定设备用途0stdin键盘标准输入(读取数据)1stdout终端屏幕标准输出&…...
Minecraft盔甲机制详解(1.9之后)
Minecraft的盔甲有很多种,但是评判盔甲的好坏,通常玩家会使用一个变量来评判——护甲值 护甲值的机制很简单,一格护甲值 (半个灰色的衣服图标)最多能提供4%的防御 护甲值在不开作弊的生存模式理论上限是20点…...
ArcGIS Desktop使用入门(四)——9版本与10版本区别
系列文章目录 ArcGIS Desktop使用入门(一)软件初认识 ArcGIS Desktop使用入门(二)常用工具条——标准工具 ArcGIS Desktop使用入门(二)常用工具条——编辑器 ArcGIS Desktop使用入门(二&#x…...
R语言之环境清理
有时候 R 环境中残留的变量可能会导致警告,可以尝试清理工作空间并重新加载数据。 警告信息: In mget(objectNames, envir ns, inherits TRUE) : 重新评估被中断的许诺 # 观察前6行 head(iris)# 观察数据结构 str(iris)# 探知数据的极值和分位数,以及…...
javaSE————网络编程套接字
网络编程套接字~~~~~ 好久没更新啦,蓝桥杯爆掉了,从今天开始爆更嗷; 1,网络编程基础 为啥要有网络编程呢,我们进行网络通信就是为了获取丰富的网络资源,说实话真的很神奇,想想我们躺在床上&a…...
FreeRTOS二值信号量详解与实战教程
FreeRTOS二值信号量详解与实战教程 📚 作者推荐:想系统学习FreeRTOS嵌入式开发?请访问我的FreeRTOS开源学习库,内含从入门到精通的完整教程和实例代码! 1. 二值信号量核心概念解析 二值信号量(Binary Semaphore)是Fre…...
Java命名规则
在 Java 项目中,命名规则遵循一定的约定俗成规范,目的是提高代码可读性和团队协作效率。以下是 Java 项目命名的核心规则和常见实践: 一、项目整体命名 项目名 使用小写字母 短横线(kebab-case)…...
