包括基本的药品信息管理系统功能(查找,存储,添加,删除,修改,打印,读取)
并有计算药品一日用量和有效日期的功能、按类别显示药品信息
c可以吗
哪药品需要哪些属性信息啊?
仅供参考:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义药品信息类
class Medicine
{
public:
Medicine(string name, string category, double price, int quantity,
string expiration)
: name_(name),
category_(category),
price_(price),
quantity_(quantity),
expiration_(expiration) {}
// 计算药品一日用量的函数
double GetDailyDosage(int days)
{
return quantity_ / days;
}
// 获取药品信息的函数
string GetInfo()
{
return "Name: " + name_ + "\nCategory: " + category_ + "\nPrice: " +
to_string(price_) + "\nQuantity: " + to_string(quantity_) +
"\nExpiration: " + expiration_ + "\n";
}
// 获取药品名称的函数
string GetName() { return name_; }
// 获取药品类别的函数
string GetCategory() { return category_; }
// 获取药品价格的函数
double GetPrice() { return price_; }
// 获取药品数量的函数
int GetQuantity() { return quantity_; }
// 获取药品有效日期的函数
string GetExpiration() { return expiration_; }
private:
string name_; // 药品名称
string category_; // 药品类别
double price_; // 药品价格
int quantity_; // 药品数量
string expiration_; // 药品有效日期
};
// 定义药品信息管理类
class MedicineManager
{
public:
// 查找药品信息的函数
Medicine *Find(string name)
{
for (Medicine *medicine : medicines_)
{
if (medicine->GetName() == name)
{
return medicine;
}
}
return nullptr;
}
// 添加药品信息的函数
void Add(Medicine *medicine) { medicines_.push_back(medicine); }
// 删除药品信息的函数
void Remove(Medicine *medicine)
{
medicines_.erase(remove(medicines_.begin(), medicines_.end(), medicine),
medicines_.end());
}
// 修改药品信息的函数
void Update(Medicine *medicine, string name, string category, double price,
int quantity, string expiration)
{
medicine->name_ = name;
medicine->category_ = category;
medicine->price_ = price;
medicine->quantity_ = quantity;
medicine->expiration_ = expiration;
}
// 打印药品信息的函数
void Print()
{
for (Medicine *medicine : medicines_)
{
cout << medicine->GetInfo();
}
}
// 读取药品信息的函数
void Read(string file_name)
{
// ...
}
// 按类别显示药品信息的函数
void DisplayByCategory(string category)
{
for (Medicine *medicine : medicines_)
{
if (medicine->GetCategory() == category)
{
cout << medicine->GetInfo();
}
}
}
private:
vector<Medicine *> medicines_; // 药品信息列表
};
int main()
{
MedicineManager manager;
// 添加药品信息
manager.Add(new Medicine("Aspirin", "Pain relief", 2.5, 100, "2022-12-31"));
manager.Add(new Medicine("Ibuprofen", "Pain relief", 3.0, 200, "2022-06-30"));
manager.Add(new Medicine("Paracetamol", "Pain relief", 1.5, 300, "2023-01-31"));
manager.Add(new Medicine("Amoxicillin", "Antibiotic", 5.0, 50, "2022-07-31"));
// 打印药品信息
manager.Print();
// 查找药品信息
Medicine *medicine = manager.Find("Aspirin");
if (medicine != nullptr)
{
cout << "Found medicine: " << medicine->GetInfo();
}
else
{
cout << "Medicine not found." << endl;
}
// 修改药品信息
manager.Update(medicine, "Aspirin", "Pain relief", 2.5, 150, "2023-01-31");
// 打印药品信息
manager.Print();
// 按类别显示药品信息
cout << "Pain relief medicines:" << endl;
manager.DisplayByCategory("Pain relief");
// 删除药品信息
manager.Remove(medicine);
// 打印药品信息
manager.Print();
return 0;
}
提供参考实例【C++课设——药品管理系统】,链接:https://blog.csdn.net/chenqwer2580/article/details/118828038
药品管理系统
借鉴下
https://blog.csdn.net/chenqwer2580/article/details/118828038
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<Windows.h>
#include<time.h>
struct user
{
char useword[100];
char password[100];
};
typedef struct medicine
{
char num[5];//药品编号
char name[20];//药品名
char otc;//是否为OTC,是为T否为N
int lye;//保质期:2020年
int lmo;//1月
int lda;//1日
int number;//剩余量
}doc;
char nu[5];
char nam[20];
char ot;
int ly;
int lm;
int ld;
int num;
typedef struct lnode
{
doc data;
struct lnode *next;
}linklist;
void surface()
{
printf(" *********************************************************\n");
printf(" * *\n");
printf(" * Welcome to the medicine management system *\n");
printf(" * Press any key to continue *\n");
printf(" * *\n");
printf(" *********************************************************\n");
}
void menu()
{
printf(" *********************************************************\n");
printf(" * *\n");
printf(" * 药店管理系统 *\n");
printf(" * 1:增加药品信息 *\n");
printf(" * 2:删除药品信息 *\n");
printf(" * 3:修改药品信息 *\n");
printf(" * 4:查找药品信息 *\n");
printf(" * 5:按照药品剩余量排序 *\n");
printf(" * 6:查找是否有过期药品 *\n");
printf(" * 7:统计总药品剩余量 *\n");
printf(" * 8:将信息保存至文件 *\n");
printf(" * 0:退出系统 *\n");
printf(" * *\n");
printf(" *********************************************************\n");
}
linklist* loadfile()
{
linklist* q, * k, * head = (linklist*)malloc(sizeof(linklist));
linklist temp;
FILE* r = fopen("medicine.txt", "r");
k = q = head;
head->next = NULL;
if (r == NULL){
r = fopen("medicine.txt", "w");
if (r == NULL){
printf("打开文件出错!\n");
return NULL;
}
fclose(r);
return head;
}
while (fscanf(r, "%s\t%s\t%c\t%d\t%d\t%d\t%d\n", temp.data.num, temp.data.name, &temp.data.otc, &temp.data.lye, &temp.data.lmo, &temp.data.lda, &temp.data.number) != EOF){
temp.next = NULL;
q = (linklist*)malloc(sizeof(linklist));
*q = temp;
k->next = q;
k = q;
}
k->next = NULL;
fclose(r);
return head;
}
int password()//密码
{
int n=5,i;
struct user use;
struct user constrast;
struct user write;
FILE* fp = fopen("user.txt", "a");
FILE* fu = fopen("user.txt", "r");
while (1) {
system("cls");
printf(" *********************************************************\n");
printf(" * *\n");
printf(" * 1:登录用户 *\n");
printf(" * 2:注册用户 *\n");
printf(" * *\n");
printf(" *********************************************************\n");
scanf("%d", &i);
system("cls");
if (i == 1) {
if ((fscanf(fu, "%s\t%s\n", constrast.useword, constrast.password) != EOF)) {
while (1) {
printf("剩余次数:%d 次\n", n--);
printf("请输入用户名:");
scanf("%s", write.useword);
printf("\n");
printf("请输入密码:");
scanf("%s", write.password);
if ((strcmp(write.useword, constrast.useword) == 0) && (strcmp(write.password, constrast.password)) == 0)
return 1;
else {
system("cls");
printf("请再尝试一次:\n");
}
if (n == 0) {
printf("抱歉,登陆失败\n");
return 0;
}
}
}
else {
printf("请先注册!\n");
n++;
break;
}
}
else if (i == 2) {
printf("请输入用户名:");
scanf("%s", use.useword);
printf("\n");
printf("请输入密码:");
scanf("%s", use.password);
fprintf(fp, "%s\t%s\n", use.useword,use.password);
fclose(fp);
system("cls");
printf("注册成功!\n");
}
else
printf("请输入正确的选项\n");
}
}
void addmedicine(linklist *L)//增加药品信息
{
printf("请输入药品信息:\n");
printf("药品编号:");
scanf("%s", nu);
linklist* q = L;
while (q->next != NULL){
if (strcmp(q->next->data.num, nu) == 0){
printf("此药品已存在\n");
getchar(); getchar();
break;
}
q = q->next;
}
if (q->next == NULL){
linklist* p;
p = (linklist*)malloc(sizeof(linklist));
strcpy(p->data.num, nu);
printf("药品名:");
scanf("%s", nam);
strcpy(p->data.name, nam);
printf("是否为OTC,是为T否为N:");
getchar();
scanf("%c", &ot);
p->data.otc = ot;
printf("保质期\n年份:");
scanf("%d", &ly);
printf("月份:");
scanf("%d", &lm);
printf("日期:");
scanf("%d", &ld);
p->data.lye = ly;
p->data.lmo = lm;
p->data.lda = ld;
printf("剩余量:");
scanf("%d", &num);
p->data.number = num;
p->next = q->next;
q->next = p;
/*
FILE* med = fopen("medicine.txt", "a");
fprintf(med, "药品序号:%s 名称:%s OTC:%c 生产日期:%d年%d月%d日 剩余量:%d\n", p->data.num, p->data.name, p->data.otc, p->data.lye, p->data.lmo, p->data.lda, p->data.number);
fclose(med);*/
}
}
void deletemedicine(linklist *L)//删除药品信息
{
printf("请输入药品信息:\n");
printf("药品编号:");
scanf("%s", nu);
linklist *q = L,*p;
while (q->next != NULL) {
if (strcmp(q->next->data.num, nu) == 0) {
p = q->next;
q->next = p->next;
free(p);
printf("删除成功!");
getchar(); getchar();
break;
}
q = q->next;
}
if (q->next == NULL) {
printf("没有当前药品信息!");
getchar(); getchar();
}
}
void changemedicine(linklist *L)//修改药品信息
{
printf("请输入药品信息:\n");
printf("药品编号:");
scanf("%s", nu);
linklist *q = L,*p;
while (q->next != NULL) {
if (strcmp(q->next->data.num, nu) == 0) {
p = q->next;
printf("已查到药品信息\n");
printf("请输入新药品信息:\n");
printf("药品名:");
scanf("%s", nam);
strcpy(p->data.name, nam);
printf("是否为OTC,是为T否为N:");
getchar();
scanf("%c", &ot);
p->data.otc = ot;
printf("保质期\n年份:");
scanf("%d", &ly);
printf("月份:");
scanf("%d", &lm);
printf("日期:");
scanf("%d", &ld);
p->data.lye = ly;
p->data.lmo = lm;
p->data.lda = ld;
printf("剩余量:");
scanf("%d", &num);
p->data.number = num;
printf("修改成功!");
getchar(); getchar();
break;
}
q = q->next;
}
if (q->next == NULL) {
printf("没有当前药品信息!");
getchar(); getchar();
}
}
void findmedicine(linklist *L)//查找药品信息
{
printf("请输入药品信息:\n");
printf("药品编号:");
scanf("%s", nu);
linklist* q = L, * p;
while (q->next != NULL) {
if (strcmp(q->next->data.num, nu) == 0) {
p = q->next;
printf("药品名:%s\n",p->data.name);
printf("是否为OTC:%c\n", p->data.otc);
printf("保质期:%d年%d月%d日\n", p->data.lye,p->data.lmo,p->data.lda);
printf("剩余量:%d\n", p->data.number);
getchar(); getchar();
break;
}
q = q->next;
}
if (q->next == NULL) {
printf("没有当前药品信息!");
getchar(); getchar();
}
}
void ranknumber(linklist* L)//按照药品剩余量排序
{
long int sum = 0;
linklist* r = L->next,*q,*p;
if (r == NULL) {
printf("当前没有任何药品信息!");
getchar(); getchar();
return;
}
while (r != NULL) {
p = r;
q = r->next;
linklist* tmp;//用于排序时暂存节点
tmp = (linklist*)malloc(sizeof(linklist));
while (q != NULL){
if (q->data.number > p->data.number){
/*先复制q结点信息到tmp*/
strcpy(tmp->data.num, q->data.num);
strcpy(tmp->data.name, q->data.name);
tmp->data.otc = q->data.otc;
tmp->data.lye = q->data.lye;
tmp->data.lmo = q->data.lmo;
tmp->data.lda = q->data.lda;
tmp->data.number = q->data.number;
/*再复制p结点信息到q*/
strcpy(q->data.num, p->data.num);
strcpy(q->data.name, p->data.name);
q->data.otc = p->data.otc;
q->data.lye = p->data.lye;
q->data.lmo = p->data.lmo;
q->data.lda = p->data.lda;
q->data.number = p->data.number;
/*最后复制exchange结点信息到p*/
strcpy(p->data.num, tmp->data.num);
strcpy(p->data.name, tmp->data.name);
p->data.otc = tmp->data.otc;
p->data.lye = tmp->data.lye;
p->data.lmo = tmp->data.lmo;
p->data.lda = tmp->data.lda;
p->data.number = tmp->data.number;
}
q = q->next;
}
r = r->next;
}
printf("排序后的药品信息为:\n");
linklist* c=L->next;
while (c != NULL) {
FILE* med = fopen("medicine.txt", "a");
printf("%s\t%s\t%c\t%d\t%d\t%d\t%d\n", c->data.num, c->data.name, c->data.otc, c->data.lye, c->data.lmo, c->data.lda, c->data.number);
c = c->next;
}
getchar(); getchar();
}
void finddelayed(linklist* L)//查找是否有过期药品
{
int flag = 1;
int year, month, day;
time_t timep;
struct tm* p;
time(&timep);
p = gmtime(&timep);
day = p->tm_mday;/*获取当前月份日数,范围是1-31*/
month = 1 + p->tm_mon;/*获取当前月份,范围是0-11,所以要加1*/
year = 1900 + p->tm_year;/*获取当前年份,从1900开始,所以要加1900*/
linklist* q = L->next;
if (q == NULL) {
printf("当前没有任何药品信息!");
getchar(); getchar();
return;
}
while (q != NULL) {
if (q->data.lye < year) {
printf("%s已过期\n", q->data.name);
flag = 0;
}
else if (q->data.lye == year && q->data.lmo < month) {
printf("%s已过期\n", q->data.name);
flag = 0;
}
else if (q->data.lye == year && q->data.lmo == month && q->data.lda < day) {
printf("%s已过期\n", q->data.name);
flag = 0;
}
q = q->next;
}
if(flag)
printf("当前没有过期药品\n");
getchar(); getchar();
}
void medicinesum(linklist* L)//统计总药品剩余量
{
long int sum=0;
linklist* p = L->next;
if (p == NULL) {
printf("当前没有任何药品信息!");
getchar(); getchar();
return;
}
while (p != NULL) {
sum += p->data.number;
p = p->next;
}
printf("总药品剩余量为:%ld",sum);
getchar(); getchar();
}
void document(linklist* L)//将信息保存至文件
{
linklist* p = L->next;
if (p == NULL) {
printf("当前没有任何药品信息!");
getchar(); getchar();
return;
}
FILE* med = fopen("medicine.txt", "w");
while (p!=NULL) {
fprintf(med, "%s\t%s\t%c\t%d\t%d\t%d\t%d\n", p->data.num, p->data.name, p->data.otc, p->data.lye, p->data.lmo, p->data.lda, p->data.number);
p = p->next;
}
fclose(med);
printf("保存成功!");
getchar(); getchar();
}
int main(void)
{
int i, n, flag=1;
system("cls");//清屏
surface();
getchar();
system("cls");
i=password();
if (i == 0)
return 0;
linklist* L;
L = loadfile();
while(1)
{
system("cls");
menu();
scanf("%d", &n);
switch (n)
{
case 0:
printf("谢谢使用");
return 0;
case 1:
addmedicine(L);
break;
case 2:
deletemedicine(L);
break;
case 3:
changemedicine(L);
break;
case 4:
findmedicine(L);
break;
case 5:
ranknumber(L);
break;
case 6:
finddelayed(L);
break;
case 7:
medicinesum(L);
break;
case 8:
document(L);
break;
default:
printf("请输入正确的选项\n");
break;
}
}
}
/*
num name OTC ly lm ld number
1 001 阿莫西林 N 2022 8 22 720
2 002 板蓝根 Y 2021 7 10 1080
3 003 维C Y 2024 1 25 1920
4 004 头孢 N 2020 5 20 400
5 005 999感冒灵 Y 2018 3 12 32
*/
/*
系统功能列表:
序号 功能 输入 输出 函数定义
无 登录 密码 无 password()
0 退出系统 0 无 无
1 增加药品信息 1 无 addlinklist()
2 删除药品信息 2 无 deletemedicine()
3 修改药品信息 3 无 changemedicine()
4 查找药品信息 4 药品信息 findmedicine()
5 按照药品剩余量排序 5 剩余量顺序 ranknumber()
6 查找是否有过期药品 6 T/N finddelayed()
7 统计总药品剩余量 7 药品总数 medicinesum()
8 将信息保存至文件 8 保存成功 document()
*/
/*
(0)系统启动
(1)欢迎页面
(1)注册或登录系统
(1)显示菜单
(2)选则要进行的操作
1-8:
addlinklist
addmedicine
deletemedicine
changemedicine
findmedicine
ranknumber
rankdate
finddelayed
medicinesum
(3)退出系统
*/