当前位置:网站首页>Medical management system (C language course for freshmen)
Medical management system (C language course for freshmen)
2022-07-02 01:49:00 【Kobayashi】
Hospital drug management system
The basic content
Realize the registration of account , land , Retrieve and modify the password , Drug information includes drug name , Number , type , cost , Price and quantity . Drugs can be added, deleted, modified and checked , Drugs can be sorted .
Key code
while (scanf("%d", &select) != 1)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c"); // Discard all data in the input buffer
}
scanf Function returns the number of items successfully read ,%d You need to enter a number , If you enter a number ,scanf return 1, The program continues downward successfully , If you enter a string or something ,scanf No return 1, Then enter while loop , Empty the contents of the input buffer .
Please refer to https://blog.csdn.net/m0_38084854/article/details/103224574?spm=1001.2014.3001.5506
The flow chart is as follows





Source code github Address
https://github.com/c1336658570/ccode/tree/master/c%E8%AF%BE%E8%AE%BE
Array version source code
/* Drug management system */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MEDICINE_FILE "medicine.txt" // Drug file name
#define USER_FILE "user.txt" // Administrator files
typedef struct medicine
{
char drug_name[100]; // Name of drug
int drug_number; // Drug number
char drug_type[50]; // Drug type ( traditional Chinese medicine , Western medicine )
int cost; // cost
int price; // The price is
int num; // Remaining quantity of drugs
} medicine;
struct med
{
medicine medicine_array[500]; // Array for storing drugs
int size; // The size of the array used
} med;
typedef struct user_f
{
char name[50]; // account number
char passwd[50]; // password
} user_f;
typedef struct user
{
user_f userarray[50];
int size; // Number of Administrators
} user;
// Login related functions
void login_menu(user *userarr); // Login menu
void reg(user *userearr); // Registered account
bool login(user *userarr); // Login account
void revise_passwd(user *userarr); // Change Password
void get_passwd(user *userarr); // Retrieve password
void save_user(user *userarr); // Save account number to file
void read_user_file(user *userarr); // Read the administrator information from the file
// Functions related to drug operation
void showMenu(); // Print menu
void read_medicine_file(); // Read the drug information from the file
void add_medicine(); // Add drug information
void show_medicine(); // View all drug information
void drug_number_del(); // Delete drug information by number
void drug_name_del(); // Delete drug information by name
void drug_number_revise(); // Modify the drug information by number
void drug_name_revise(); // Modify the drug information by name
int drug_number_find(int num); // Search for drug information by number , Returns the subscript
void show_drug_number_find(); // Output the found drug information
int drug_name_find(char *name); // Search for drug information by name , Returns the subscript
void show_drug_name_find(); // Search for drug information by name , And output the drug information
void drug_number_sort(); // Sort the drug information by number
void drug_name_sort(); // Sort drug information by name
void save_medicine(); // Save drug information to file
int main(int argc, char *argv[])
{
int select = 15;
user userarr;
read_user_file(&userarr); // Read administrator information from file
login_menu(&userarr); // Login account menu
read_medicine_file(); // Read the information of drugs in the file
while (1)
{
showMenu(); // Print menu
while (scanf("%d", &select) != 1 || select < 0 || select > 10)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c"); // Discard all data in the input buffer
}
switch (select)
{
case 0:
// Exit procedure
exit(0);
break;
case 1:
// Add drug information
add_medicine();
break;
case 2:
// View all drug information
show_medicine();
break;
case 3:
// Delete drug information by number
drug_number_del();
break;
case 4:
// Delete drug information by name
drug_name_del();
break;
case 5:
// Modify the drug information by number
drug_number_revise();
break;
case 6:
// Modify the drug information by name
drug_name_revise();
break;
case 7:
// Search for drug information by number , And output the drug information
show_drug_number_find();
break;
case 8:
// Search for drug information by name , And output the drug information
show_drug_name_find();
break;
case 9:
// Sort the drug information by number
drug_number_sort();
break;
case 10:
// Sort drug information by name
drug_name_sort();
break;
}
}
return 0;
}
// Login menu
void login_menu(user *userarr)
{
while (1)
{
printf("\033[35m Welcome to the drug management system !\033[0m\n");
printf("\033[35m1、 Registered account \033[0m\n");
printf("\033[35m2、 Login account \033[0m\n");
printf("\033[35m3、 Change Password \033[0m\n");
printf("\033[35m4、 Retrieve password \033[0m\n");
printf("\033[35m0、 sign out \033[0m\n");
int select = 3, flag = 0;
while (scanf("%d", &select) != 1 || select < 0 || select > 4)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
switch (select)
{
case 0:
// Exit procedure
exit(0);
break;
case 1:
// Registered account
reg(userarr);
break;
case 2:
// Login account
if (login(userarr))
{
printf(" Login successful \n");
flag = 1;
}
else
{
printf(" Login failed , Program exit \n");
exit(-1);
}
break;
case 3:
revise_passwd(userarr); // Change Password
break;
case 4:
get_passwd(userarr); // Retrieve password
break;
}
if (flag == 1)
{
break;
}
}
}
// Registered account
void reg(user *userarr)
{
printf(" Please enter your account number \n");
scanf("%s", userarr->userarray[userarr->size].name);
printf(" Please input a password \n");
scanf("%s", userarr->userarray[userarr->size].passwd);
userarr->size++;
printf(" Registered successfully \n");
save_user(userarr);
}
// Login account
bool login(user *userarr)
{
char name[50], passwd[50];
int i, j;
printf("\033[35m Do you have 5 First chance to log in !\033[0m\n");
for (j = 0; j < 5; ++j)
{
printf("\033[35m The first %d Secondary logon \033[0m\n", j + 1);
printf("\033[35m Please enter your account number \033[0m\n");
scanf("%s", name);
printf("\033[35m Please enter your password \033[0m\n");
scanf("%s", passwd);
for (i = 0; i < userarr->size; ++i)
{
if (strcmp(userarr->userarray[i].name, name) == 0)
{
if (strcmp(userarr->userarray[i].passwd, passwd) == 0)
{
return true;
}
}
}
printf("\033[35m The first %d Login failed \033[0m\n", j + 1);
}
return false;
}
// Change Password
void revise_passwd(user *userarr)
{
char name[50], passwd[50];
int i;
printf(" Please enter the name you want to change \n");
scanf("%s", name);
printf(" Please enter the original password \n");
scanf("%s", passwd);
for (i = 0; i < userarr->size; ++i)
{
if (strcmp(name, userarr->userarray[i].name) == 0 && strcmp(passwd, userarr->userarray[i].passwd) == 0)
{
printf(" Please enter the new password \n");
scanf("%s", userarr->userarray[i].passwd);
printf(" Modification successful \n");
save_user(userarr);
return;
}
}
printf(" Wrong account or password \n");
}
// Retrieve password
void get_passwd(user *userarr)
{
char name[50];
int i;
printf(" Please enter the name you want to retrieve \n");
scanf("%s", name);
for (i = 0; i < userarr->size; ++i)
{
if (strcmp(name, userarr->userarray[i].name) == 0)
{
printf(" The password of this user is :%s\n", userarr->userarray[i].passwd);
return;
}
}
printf(" The user was not found \n");
}
// Save account number to file
void save_user(user *userarr)
{
int i;
FILE *fp = fopen(USER_FILE, "w");
if (fp == NULL)
{
return;
}
for (i = 0; i < userarr->size; ++i)
{
fprintf(fp, "%s %s\n", userarr->userarray[i].name, userarr->userarray[i].passwd);
}
fclose(fp);
}
// Read the administrator information from the file
void read_user_file(user *userarr)
{
do
{
userarr->size = 0;
memset(&userarr->userarray, 0, sizeof(userarr->userarray));
FILE *fp = fopen(USER_FILE, "r");
if (fp == NULL)
{
break;
}
while (fscanf(fp, "%s %s", &userarr->userarray[userarr->size].name,
&userarr->userarray[userarr->size].passwd) != EOF)
{
userarr->size++;
}
} while (0);
}
// Print menu
void showMenu()
{
printf("\033[34m-------------------------------------------\033[0m\n");
printf("\033[35m Please enter the number you want to select :\033[0m\n");
printf("\033[35m1、 Add drug information \033[0m\n");
printf("\033[35m2、 View all drug information \033[0m\n");
printf("\033[35m3、 Delete drug information by number \033[0m\n");
printf("\033[35m4、 Delete drug information by name \033[0m\n");
printf("\033[35m5、 Modify the drug information by number \033[0m\n");
printf("\033[35m6、 Modify the drug information by name \033[0m\n");
printf("\033[35m7、 Search for drug information by number \033[0m\n");
printf("\033[35m8、 Search for drug information by name \033[0m\n");
printf("\033[35m9、 Sort the drug information by number \033[0m\n");
printf("\033[35m10、 Sort drug information by name \033[0m\n");
printf("\033[35m0、 Exit the system \033[0m\n");
printf("\033[34m-------------------------------------------\033[0m\n");
}
// Read the drug information from the file
void read_medicine_file()
{
FILE *fp = fopen(MEDICINE_FILE, "r");
if (fp == NULL)
{
med.size = 0;
memset(&med.medicine_array, 0, sizeof(med.medicine_array));
}
else
{
while (fscanf(fp, "%s%d%s%d%d%d", &med.medicine_array[med.size].drug_name,
&med.medicine_array[med.size].drug_number,
&med.medicine_array[med.size].drug_type,
&med.medicine_array[med.size].cost,
&med.medicine_array[med.size].price,
&med.medicine_array[med.size].num) != EOF)
{
med.size++;
}
}
}
// Add drug information
void add_medicine()
{
int flag = 0;
int num; // Save the number for weight removal
printf(" Please enter the quantity of drugs you want to add \n");
int n, i;
while (scanf("%d", &n) != 1 || n < 0)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
// full name | Number | type | cost | The price is | Number
for (i = 0; i < n; ++i)
{
printf(" The first %d Information about drugs \n", i + 1);
printf("--------------------\n");
printf(" Please enter the name of the medicine \n");
scanf("%s", &med.medicine_array[med.size].drug_name);
printf(" Please enter the drug number \n");
med.medicine_array[med.size].drug_number = 0;
do
{
while (scanf("%d", &num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
int ret = drug_number_find(num);
if (ret != -1)
{
printf(" This number already exists , Please re-enter \n");
}
else
{
med.medicine_array[med.size].drug_number = num;
break;
}
} while (1);
printf(" Please enter the drug type \n");
scanf("%s", &med.medicine_array[med.size].drug_type);
printf(" Please enter the drug cost \n");
while (scanf("%d", &med.medicine_array[med.size].cost) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the drug price \n");
while (scanf("%d", &med.medicine_array[med.size].price) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the quantity of drugs \n");
while (scanf("%d", &med.medicine_array[med.size].num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
flag = 1;
med.size++;
}
if (flag == 1)
save_medicine(); // Save to file
}
// View all drug information
void show_medicine()
{
printf(" full name | Number | type | cost | The price is | Number \n");
int i;
for (i = 0; i < med.size; ++i)
{
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", med.medicine_array[i].drug_name,
med.medicine_array[i].drug_number,
med.medicine_array[i].drug_type,
med.medicine_array[i].cost,
med.medicine_array[i].price,
med.medicine_array[i].num);
}
}
// Delete drug information by number
void drug_number_del()
{
int flag = 0;
int i, j, n, number;
printf(" Please enter the number of drugs you want to delete \n");
while (scanf("%d", &n) != 1 || n < 0)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
for (i = 0; i < n; ++i)
{
printf(" The first %d Number of drugs \n", i + 1);
printf("--------------------\n");
printf(" Please enter the drug number to delete \n");
while (scanf("%d", &number) != 1)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
int ret = drug_number_find(number);
if (ret != -1)
{
flag = 1;
printf(" Delete successful , The deleted information is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", med.medicine_array[ret].drug_name,
med.medicine_array[ret].drug_number,
med.medicine_array[ret].drug_type,
med.medicine_array[ret].cost,
med.medicine_array[ret].price,
med.medicine_array[ret].num);
for (j = ret; j < med.size - 1; j++)
{
med.medicine_array[j] = med.medicine_array[j + 1];
}
med.size--;
}
else
{
printf(" Delete failed \n");
}
}
if (flag == 1)
save_medicine();
}
// Delete drug information by name
void drug_name_del()
{
int flag = 0;
int i, j, n;
char name[100];
printf(" Please enter the number of drugs you want to delete \n");
while (scanf("%d", &n) != 1 || n < 0)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
for (i = 0; i < n; ++i)
{
printf(" The first %d Names of drugs \n", i + 1);
printf("--------------------\n");
printf(" Please enter the name of the drug to be deleted \n");
scanf("%s", name);
int ret = drug_name_find(name);
if (ret != -1)
{
flag = 1;
printf(" Delete successful , The deleted information is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", med.medicine_array[ret].drug_name,
med.medicine_array[ret].drug_number,
med.medicine_array[ret].drug_type,
med.medicine_array[ret].cost,
med.medicine_array[ret].price,
med.medicine_array[ret].num);
for (j = ret; j < med.size - 1; j++)
{
med.medicine_array[j] = med.medicine_array[j + 1];
}
med.size--;
}
else
{
printf(" The drug was not found , Delete failed \n");
}
}
if (flag == 1)
save_medicine();
}
// Modify the drug information by number
void drug_number_revise()
{
int flag = 0;
int i, j, n, number; // Save the number to be modified
int num; // Save new number , Used to remove heavy
printf(" Please enter the quantity of drugs you want to modify \n");
while (scanf("%d", &n) != 1 || n < 0)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
for (i = 0; i < n; ++i)
{
printf(" The first %d Number of drugs \n", i + 1);
printf("--------------------\n");
printf(" Please enter the drug number to be modified \n");
while (scanf("%d", &number) != 1)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
int ret = drug_number_find(number);
if (ret != -1)
{
flag = 1;
printf(" Find the drug with this number , The drug information is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", med.medicine_array[ret].drug_name,
med.medicine_array[ret].drug_number,
med.medicine_array[ret].drug_type,
med.medicine_array[ret].cost,
med.medicine_array[ret].price,
med.medicine_array[ret].num);
printf(" Please enter new information \n");
printf(" Please enter the new name of the drug \n");
scanf("%s", &med.medicine_array[ret].drug_name);
printf(" Please enter the drug number \n");
med.medicine_array[ret].drug_number = -1; // Change the drug number to -1 Prevent the newly entered number from being the same as the current number , As a result, the number cannot be successfully entered
do
{
while (scanf("%d", &num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
int res = drug_number_find(num);
if (res != -1)
{
printf(" This number already exists , Please re-enter \n");
}
else
{
med.medicine_array[ret].drug_number = num;
break;
}
} while (1);
printf(" Please enter the drug type \n");
scanf("%s", &med.medicine_array[ret].drug_type);
printf(" Please enter the drug cost \n");
while (scanf("%d", &med.medicine_array[ret].cost) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the drug price \n");
while (scanf("%d", &med.medicine_array[ret].price) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the quantity of drugs \n");
while (scanf("%d", &med.medicine_array[ret].num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Modification successful !\n");
}
else
{
printf(" The drug with this number was not found \n");
}
}
if (flag == 1)
save_medicine();
}
// Modify the drug information by name
void drug_name_revise()
{
int flag = 0;
int i, j, n;
char name[100];
int num; // Save number , Used to remove heavy
printf(" Please enter the quantity of drugs you want to modify \n");
while (scanf("%d", &n) != 1 || n < 0)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
for (i = 0; i < n; ++i)
{
printf(" The first %d Names of drugs \n", i + 1);
printf("--------------------\n");
printf(" Please enter the name of the drug to be modified \n");
scanf("%s", &name);
int ret = drug_name_find(name);
if (ret != -1)
{
flag = 1;
printf(" Find the medicine with this name , The drug information is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", med.medicine_array[ret].drug_name,
med.medicine_array[ret].drug_number,
med.medicine_array[ret].drug_type,
med.medicine_array[ret].cost,
med.medicine_array[ret].price,
med.medicine_array[ret].num);
printf(" Please enter new information \n");
printf(" Please enter the new name of the drug \n");
scanf("%s", &med.medicine_array[ret].drug_name);
printf(" Please enter the drug number \n");
med.medicine_array[ret].drug_number = 0;
do
{
while (scanf("%d", &num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
int res = drug_number_find(num);
if (res != -1)
{
printf(" This number already exists , Please re-enter \n");
}
else
{
med.medicine_array[ret].drug_number = num;
break;
}
} while (1);
printf(" Please enter the drug type \n");
scanf("%s", &med.medicine_array[ret].drug_type);
printf(" Please enter the drug cost \n");
while (scanf("%d", &med.medicine_array[ret].cost) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the drug price \n");
while (scanf("%d", &med.medicine_array[ret].price) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the quantity of drugs \n");
while (scanf("%d", &med.medicine_array[ret].num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Modification successful !\n");
}
else
{
printf(" The drug with that name was not found \n");
}
}
if (flag == 1)
save_medicine();
}
// Search for drug information by number , Returns the subscript
int drug_number_find(int number)
{
int i;
for (i = 0; i < med.size; ++i)
{
if (number == med.medicine_array[i].drug_number)
{
return i;
}
}
return -1;
}
// Output the found drug information
void show_drug_number_find()
{
int number;
printf(" Please enter the number you want to find \n");
while (scanf("%d", &number) != 1)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
int ret = drug_number_find(number);
if (ret == -1)
{
printf(" Not found \n");
}
else
{
printf(" eureka , The message is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", med.medicine_array[ret].drug_name,
med.medicine_array[ret].drug_number,
med.medicine_array[ret].drug_type,
med.medicine_array[ret].cost,
med.medicine_array[ret].price,
med.medicine_array[ret].num);
}
}
// Search for drug information by name
int drug_name_find(char *name)
{
int i;
for (i = 0; i < med.size; ++i)
{
if (strcmp(name, med.medicine_array[i].drug_name) == 0)
return i;
}
return -1;
}
// Search for drug information by name , And output the drug information
void show_drug_name_find()
{
char name[100];
printf(" Please enter the name of the medicine you are looking for \n");
scanf("%s", name);
int ret = drug_name_find(name);
if (ret == -1)
{
printf(" Not found \n");
}
else
{
printf(" eureka , The drug information is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", med.medicine_array[ret].drug_name,
med.medicine_array[ret].drug_number,
med.medicine_array[ret].drug_type,
med.medicine_array[ret].cost,
med.medicine_array[ret].price,
med.medicine_array[ret].num);
}
}
int cmp1(const void *a, const void *b) // Ascending
{
return ((medicine *)a)->drug_number - ((medicine *)b)->drug_number;
}
int cmp2(const void *a, const void *b) // Descending
{
return ((medicine *)b)->drug_number - ((medicine *)a)->drug_number;
}
// Sort the drug information by number
void drug_number_sort()
{
int select = 0;
printf("1、 Ascending 2、 Descending 0、 sign out \n");
while (scanf("%d", &select) != 1 || select < 0 || select > 2)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
switch (select)
{
case 0:
break;
case 1:
qsort(&med.medicine_array, med.size, sizeof(med.medicine_array[0]), cmp1);
break;
case 2:
qsort(&med.medicine_array, med.size, sizeof(med.medicine_array[0]), cmp2);
break;
}
save_medicine();
}
int cmp3(const void *a, const void *b)
{
return (strcmp(((medicine *)a)->drug_name, ((medicine *)b)->drug_name) > 0);
}
int cmp4(const void *a, const void *b)
{
return (strcmp(((medicine *)a)->drug_name, ((medicine *)b)->drug_name) <= 0);
}
// Sort drug information by name
void drug_name_sort()
{
int select = 0;
printf("1、 Ascending 2、 Descending 0、 sign out \n");
while (scanf("%d", &select) != 1 || select < 0 || select > 2)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
switch (select)
{
case 0:
break;
case 1:
qsort(&med.medicine_array, med.size, sizeof(med.medicine_array[0]), cmp3);
break;
case 2:
qsort(&med.medicine_array, med.size, sizeof(med.medicine_array[0]), cmp4);
break;
}
save_medicine();
}
// Save drug information to file
void save_medicine()
{
int i;
FILE *fp = fopen(MEDICINE_FILE, "w");
if (fp == NULL)
{
return;
}
for (i = 0; i < med.size; ++i)
{
fprintf(fp, "%s %d %s %d %d %d\n", med.medicine_array[i].drug_name,
med.medicine_array[i].drug_number,
med.medicine_array[i].drug_type,
med.medicine_array[i].cost,
med.medicine_array[i].price,
med.medicine_array[i].num);
}
fclose(fp);
}
Linked list version source code ( Headed node )
/* Drug management system */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MEDICINE_FILE "medicine.txt" // Drug file name
#define USER_FILE "user.txt" // Administrator files
typedef struct medicine
{
char drug_name[100]; // Name of drug
int drug_number; // Drug number
char drug_type[50]; // Drug type ( traditional Chinese medicine , Western medicine )
int cost; // cost
int price; // The price is
int num; // Remaining quantity of drugs
struct medicine *next;
} medicine;
medicine *head; // Define the head pointer
typedef struct user_f
{
char name[50]; // account number
char passwd[50]; // password
} user_f;
typedef struct user
{
user_f userarray[50];
int size; // Number of Administrators
} user;
// Login related functions
void login_menu(user *userarr); // Login menu
void reg(user *userearr); // Registered account
bool login(user *userarr); // Login account
void revise_passwd(user *userarr); // Change Password
void get_passwd(user *userarr); // Retrieve password
void save_user(user *userarr); // Save account number to file
void read_user_file(user *userarr); // Read the administrator information from the file
// Functions related to drug operation
void showMenu(); // Print menu
void read_medicine_file(); // Read the drug information from the file
void add_medicine(); // Add drug information
void show_medicine(); // View all drug information
void drug_number_del(); // Delete drug information by number
void drug_name_del(); // Delete drug information by name
void drug_number_revise(); // Modify the drug information by number
void drug_name_revise(); // Modify the drug information by name
medicine *drug_number_find(int num); // Search for drug information by number , Returns the subscript
void show_drug_number_find(); // Output the found drug information
medicine *drug_name_find(char *name); // Search for drug information by name , Returns the subscript
void show_drug_name_find(); // Search for drug information by name , And output the drug information
void drug_number_sort(); // Sort the drug information by number
void drug_name_sort(); // Sort drug information by name
void save_medicine(); // Save drug information to file
int main(int argc, char *argv[])
{
int select = 15;
user userarr;
head = (medicine *)malloc(sizeof(medicine));
head->next = NULL;
read_user_file(&userarr); // Read administrator information from file
login_menu(&userarr); // Login account menu
read_medicine_file(); // Read the information of drugs in the file
while (1)
{
showMenu(); // Print menu
while (scanf("%d", &select) != 1 || select < 0 || select > 10)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c"); // Discard all data in the input buffer
}
switch (select)
{
case 0:
// Exit procedure
exit(0);
break;
case 1:
// Add drug information
add_medicine();
break;
case 2:
// View all drug information
show_medicine();
break;
case 3:
// Delete drug information by number
drug_number_del();
break;
case 4:
// Delete drug information by name
drug_name_del();
break;
case 5:
// Modify the drug information by number
drug_number_revise();
break;
case 6:
// Modify the drug information by name
drug_name_revise();
break;
case 7:
// Search for drug information by number , And output the drug information
show_drug_number_find();
break;
case 8:
// Search for drug information by name , And output the drug information
show_drug_name_find();
break;
case 9:
// Sort the drug information by number
drug_number_sort();
break;
case 10:
// Sort drug information by name
drug_name_sort();
break;
}
}
return 0;
}
// Login menu
void login_menu(user *userarr)
{
while (1)
{
printf("\033[35m Welcome to the drug management system !\033[0m\n");
printf("\033[35m1、 Registered account \033[0m\n");
printf("\033[35m2、 Login account \033[0m\n");
printf("\033[35m3、 Change Password \033[0m\n");
printf("\033[35m4、 Retrieve password \033[0m\n");
printf("\033[35m0、 sign out \033[0m\n");
int select = 3, flag = 0;
while (scanf("%d", &select) != 1 || select < 0 || select > 4)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
switch (select)
{
case 0:
// Exit procedure
exit(0);
break;
case 1:
// Registered account
reg(userarr);
break;
case 2:
// Login account
if (login(userarr))
{
printf(" Login successful \n");
flag = 1;
}
else
{
printf(" Login failed , Program exit \n");
exit(-1);
}
break;
case 3:
revise_passwd(userarr); // Change Password
break;
case 4:
get_passwd(userarr); // Retrieve password
break;
}
if (flag == 1)
{
break;
}
}
}
// Registered account
void reg(user *userarr)
{
printf(" Please enter your account number \n");
scanf("%s", userarr->userarray[userarr->size].name);
printf(" Please input a password \n");
scanf("%s", userarr->userarray[userarr->size].passwd);
userarr->size++;
printf(" Registered successfully \n");
save_user(userarr);
}
// Login account
bool login(user *userarr)
{
char name[50], passwd[50];
int i, j;
printf("\033[35m Do you have 5 First chance to log in !\033[0m\n");
for (j = 0; j < 5; ++j)
{
printf("\033[35m The first %d Secondary logon \033[0m\n", j + 1);
printf("\033[35m Please enter your account number \033[0m\n");
scanf("%s", name);
printf("\033[35m Please enter your password \033[0m\n");
scanf("%s", passwd);
for (i = 0; i < userarr->size; ++i)
{
if (strcmp(userarr->userarray[i].name, name) == 0)
{
if (strcmp(userarr->userarray[i].passwd, passwd) == 0)
{
return true;
}
}
}
printf("\033[35m The first %d Login failed \033[0m\n", j + 1);
}
return false;
}
// Change Password
void revise_passwd(user *userarr)
{
char name[50], passwd[50];
int i;
printf(" Please enter the name you want to change \n");
scanf("%s", name);
printf(" Please enter the original password \n");
scanf("%s", passwd);
for (i = 0; i < userarr->size; ++i)
{
if (strcmp(name, userarr->userarray[i].name) == 0 && strcmp(passwd, userarr->userarray[i].passwd) == 0)
{
printf(" Please enter the new password \n");
scanf("%s", userarr->userarray[i].passwd);
printf(" Modification successful \n");
save_user(userarr);
return;
}
}
printf(" Wrong account or password \n");
}
// Retrieve password
void get_passwd(user *userarr)
{
char name[50];
int i;
printf(" Please enter the name you want to retrieve \n");
scanf("%s", name);
for (i = 0; i < userarr->size; ++i)
{
if (strcmp(name, userarr->userarray[i].name) == 0)
{
printf(" The password of this user is :%s\n", userarr->userarray[i].passwd);
return;
}
}
printf(" The user was not found \n");
}
// Save account number to file
void save_user(user *userarr)
{
int i;
FILE *fp = fopen(USER_FILE, "w");
if (fp == NULL)
{
return;
}
for (i = 0; i < userarr->size; ++i)
{
fprintf(fp, "%s %s\n", userarr->userarray[i].name, userarr->userarray[i].passwd);
}
fclose(fp);
}
// Read the administrator information from the file
void read_user_file(user *userarr)
{
do
{
userarr->size = 0;
memset(&userarr->userarray, 0, sizeof(userarr->userarray));
FILE *fp = fopen(USER_FILE, "r");
if (fp == NULL)
{
break;
}
while (fscanf(fp, "%s %s", userarr->userarray[userarr->size].name,
userarr->userarray[userarr->size].passwd) != EOF)
{
userarr->size++;
}
} while (0);
}
// Print menu
void showMenu()
{
printf("\033[34m-------------------------------------------\033[0m\n");
printf("\033[35m Please enter the number you want to select :\033[0m\n");
printf("\033[35m1、 Add drug information \033[0m\n");
printf("\033[35m2、 View all drug information \033[0m\n");
printf("\033[35m3、 Delete drug information by number \033[0m\n");
printf("\033[35m4、 Delete drug information by name \033[0m\n");
printf("\033[35m5、 Modify the drug information by number \033[0m\n");
printf("\033[35m6、 Modify the drug information by name \033[0m\n");
printf("\033[35m7、 Search for drug information by number \033[0m\n");
printf("\033[35m8、 Search for drug information by name \033[0m\n");
printf("\033[35m9、 Sort the drug information by number \033[0m\n");
printf("\033[35m10、 Sort drug information by name \033[0m\n");
printf("\033[35m0、 Exit the system \033[0m\n");
printf("\033[34m-------------------------------------------\033[0m\n");
}
// Read the drug information from the file
void read_medicine_file()
{
FILE *fp = fopen(MEDICINE_FILE, "r");
if (fp == NULL)
{
return;
}
else
{
medicine *p = head;
while (1)
{
medicine *pNew = (medicine *)malloc(sizeof(medicine));
pNew->next = NULL;
if (fscanf(fp, "%s%d%s%d%d%d", pNew->drug_name,
&pNew->drug_number,
pNew->drug_type,
&pNew->cost,
&pNew->price,
&pNew->num) != EOF)
{
p->next = pNew;
p = pNew;
}
else
{
free(pNew);
break;
}
}
}
}
// Add drug information
void add_medicine()
{
int flag = 0;
int num; // Save the number for weight removal
printf(" Please enter the quantity of drugs you want to add \n");
int n, i;
while (scanf("%d", &n) != 1 || n < 0)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
// full name | Number | type | cost | The price is | Number
medicine *p, *q;
p = head;
while (p->next != NULL)
{
p = p->next;
}
for (i = 0; i < n; ++i)
{
q = (medicine *)malloc(sizeof(medicine));
printf(" The first %d Information about drugs \n", i + 1);
printf("--------------------\n");
printf(" Please enter the name of the medicine \n");
scanf("%s", q->drug_name);
printf(" Please enter the drug number \n");
q->drug_number = 0;
do
{
while (scanf("%d", &num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
medicine *ret = drug_number_find(num);
if (ret != NULL)
{
printf(" This number already exists , Please re-enter \n");
}
else
{
q->drug_number = num;
break;
}
} while (1);
printf(" Please enter the drug type \n");
scanf("%s", q->drug_type);
printf(" Please enter the drug cost \n");
while (scanf("%d", &q->cost) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the drug price \n");
while (scanf("%d", &q->price) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the quantity of drugs \n");
while (scanf("%d", &q->num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
flag = 1;
p->next = q;
p = q;
q->next = NULL;
}
if (flag == 1)
save_medicine(); // Save to file
}
// View all drug information
void show_medicine()
{
printf(" full name | Number | type | cost | The price is | Number \n");
medicine *p = head->next;
while (p != NULL)
{
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", p->drug_name,
p->drug_number,
p->drug_type,
p->cost,
p->price,
p->num);
p = p->next;
}
}
// Delete drug information by number
void drug_number_del()
{
int flag = 0;
int i, j, n, number;
printf(" Please enter the number of drugs you want to delete \n");
while (scanf("%d", &n) != 1 || n < 0)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
for (i = 0; i < n; ++i)
{
printf(" The first %d Number of drugs \n", i + 1);
printf("--------------------\n");
printf(" Please enter the drug number to delete \n");
while (scanf("%d", &number) != 1)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
medicine *ret = drug_number_find(number);
if (ret != NULL)
{
medicine *p = head;
while (p->next->drug_number != number)
{
p = p->next;
}
flag = 1;
printf(" Delete successful , The deleted information is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", ret->drug_name,
ret->drug_number,
ret->drug_type,
ret->cost,
ret->price,
ret->num);
p->next = p->next->next;
free(ret);
}
else
{
printf(" Delete failed \n");
}
}
if (flag == 1)
save_medicine();
}
// Delete drug information by name
void drug_name_del()
{
int flag = 0;
int i, j, n;
char name[100];
printf(" Please enter the number of drugs you want to delete \n");
while (scanf("%d", &n) != 1 || n < 0)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
for (i = 0; i < n; ++i)
{
printf(" The first %d Names of drugs \n", i + 1);
printf("--------------------\n");
printf(" Please enter the name of the drug to be deleted \n");
scanf("%s", name);
medicine *ret = drug_name_find(name);
if (ret != NULL)
{
medicine *p = head;
while (strcmp(p->next->drug_name, name) != 0)
{
p = p->next;
}
flag = 1;
printf(" Delete successful , The deleted information is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", ret->drug_name,
ret->drug_number,
ret->drug_type,
ret->cost,
ret->price,
ret->num);
p->next = ret->next;
free(ret);
}
else
{
printf(" The drug was not found , Delete failed \n");
}
}
if (flag == 1)
save_medicine();
}
// Modify the drug information by number
void drug_number_revise()
{
int flag = 0;
int i, j, n, number; // Save the number to be modified
int num; // Save new number , Used to remove heavy
printf(" Please enter the quantity of drugs you want to modify \n");
while (scanf("%d", &n) != 1 || n < 0)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
for (i = 0; i < n; ++i)
{
printf(" The first %d Number of drugs \n", i + 1);
printf("--------------------\n");
printf(" Please enter the drug number to be modified \n");
while (scanf("%d", &number) != 1)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
medicine *ret = drug_number_find(number);
if (ret != NULL)
{
flag = 1;
printf(" Find the drug with this number , The drug information is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", ret->drug_name,
ret->drug_number,
ret->drug_type,
ret->cost,
ret->price,
ret->num);
printf(" Please enter new information \n");
printf(" Please enter the new name of the drug \n");
scanf("%s", ret->drug_name);
printf(" Please enter the drug number \n");
ret->drug_number = -1; // Change the drug number to -1 Prevent the newly entered number from being the same as the current number , As a result, the number cannot be successfully entered
do
{
while (scanf("%d", &num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
medicine *res = drug_number_find(num);
if (res != NULL)
{
printf(" This number already exists , Please re-enter \n");
}
else
{
ret->drug_number = num;
break;
}
} while (1);
printf(" Please enter the drug type \n");
scanf("%s", ret->drug_type);
printf(" Please enter the drug cost \n");
while (scanf("%d", &ret->cost) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the drug price \n");
while (scanf("%d", &ret->price) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the quantity of drugs \n");
while (scanf("%d", &ret->num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Modification successful !\n");
}
else
{
printf(" The drug with this number was not found \n");
}
}
if (flag == 1)
save_medicine();
}
// Modify the drug information by name
void drug_name_revise()
{
int flag = 0;
int i, j, n;
char name[100];
int num; // Save number , Used to remove heavy
printf(" Please enter the quantity of drugs you want to modify \n");
while (scanf("%d", &n) != 1 || n < 0)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
for (i = 0; i < n; ++i)
{
printf(" The first %d Names of drugs \n", i + 1);
printf("--------------------\n");
printf(" Please enter the name of the drug to be modified \n");
scanf("%s", name);
medicine *ret = drug_name_find(name);
if (ret != NULL)
{
flag = 1;
printf(" Find the medicine with this name , The drug information is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", ret->drug_name,
ret->drug_number,
ret->drug_type,
ret->cost,
ret->price,
ret->num);
printf(" Please enter new information \n");
printf(" Please enter the new name of the drug \n");
scanf("%s", ret->drug_name);
printf(" Please enter the drug number \n");
ret->drug_number = 0;
do
{
while (scanf("%d", &num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
medicine *res = drug_number_find(num);
if (res != NULL)
{
printf(" This number already exists , Please re-enter \n");
}
else
{
ret->drug_number = num;
break;
}
} while (1);
printf(" Please enter the drug type \n");
scanf("%s", ret->drug_type);
printf(" Please enter the drug cost \n");
while (scanf("%d", &ret->cost) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the drug price \n");
while (scanf("%d", &ret->price) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Please enter the quantity of drugs \n");
while (scanf("%d", &ret->num) != 1)
{
printf(" Incorrect input , Please re-enter !\n");
scanf("%*[^\n]%*c");
}
printf(" Modification successful !\n");
}
else
{
printf(" The drug with that name was not found \n");
}
}
if (flag == 1)
save_medicine();
}
// Search for drug information by number , Returns the subscript
medicine *drug_number_find(int number)
{
medicine *p;
p = head->next;
while (p != NULL)
{
if (p->drug_number == number)
{
return p;
}
p = p->next;
}
return NULL;
}
// Output the found drug information
void show_drug_number_find()
{
int number;
printf(" Please enter the number you want to find \n");
while (scanf("%d", &number) != 1)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
medicine *ret = drug_number_find(number);
if (ret == NULL)
{
printf(" Not found \n");
}
else
{
printf(" eureka , The message is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", ret->drug_name,
ret->drug_number,
ret->drug_type,
ret->cost,
ret->price,
ret->num);
}
}
// Search for drug information by name
medicine *drug_name_find(char *name)
{
medicine *p, *q;
p = head->next;
while (p != NULL)
{
if (strcmp(name, p->drug_name) == 0)
{
return p;
}
p = p->next;
}
return NULL;
}
// Search for drug information by name , And output the drug information
void show_drug_name_find()
{
char name[100];
printf(" Please enter the name of the medicine you are looking for \n");
scanf("%s", name);
medicine *ret = drug_name_find(name);
if (ret == NULL)
{
printf(" Not found \n");
}
else
{
printf(" eureka , The drug information is \n");
printf("%-11s%-11d%-11s%-11d%-11d%-11d\n", ret->drug_name,
ret->drug_number,
ret->drug_type,
ret->cost,
ret->price,
ret->num);
}
}
int cmp1(const void *a, const void *b) // Ascending
{
return ((medicine *)a)->drug_number - ((medicine *)b)->drug_number;
}
int cmp2(const void *a, const void *b) // Descending
{
return ((medicine *)b)->drug_number - ((medicine *)a)->drug_number;
}
// Sort the drug information by number
void drug_number_sort()
{
int select = 0;
printf("1、 Ascending 2、 Descending 0、 sign out \n");
while (scanf("%d", &select) != 1 || select < 0 || select > 2)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
int i, count = 0, num;
medicine *p, *q, *tail;
p = head;
while (p->next != NULL)
{
count++;
p = p->next;
}
for (i = 0; i < count - 1; i++)
{
num = count - i - 1;
q = head->next;
p = q->next;
tail = head;
while (num--)
{
if (select == 1)
{
if (q->drug_number > p->drug_number)
{
q->next = p->next;
p->next = q;
tail->next = p;
}
}
else if (select == 2)
{
if (q->drug_number < p->drug_number)
{
q->next = p->next;
p->next = q;
tail->next = p;
}
}
tail = tail->next;
q = tail->next;
p = q->next;
}
}
save_medicine();
}
// Sort drug information by name
void drug_name_sort()
{
int select = 0;
printf("1、 Ascending 2、 Descending 0、 sign out \n");
while (scanf("%d", &select) != 1 || select < 0 || select > 2)
{
printf(" Incorrect input , Please re-enter \n");
scanf("%*[^\n]%*c");
}
int i, count = 0, num;
medicine *p, *q, *tail;
p = head;
while (p->next != NULL)
{
count++;
p = p->next;
}
for (i = 0; i < count - 1; i++)
{
num = count - i - 1;
q = head->next;
p = q->next;
tail = head;
while (num--)
{
if (select == 1)
{
if (strcmp(q->drug_name, p->drug_name) > 0)
{
q->next = p->next;
p->next = q;
tail->next = p;
}
}
else if (select == 2)
{
if (strcmp(q->drug_name, p->drug_name) < 0)
{
q->next = p->next;
p->next = q;
tail->next = p;
}
}
tail = tail->next;
q = tail->next;
p = q->next;
}
}
save_medicine();
}
// Save drug information to file
void save_medicine()
{
int i;
FILE *fp = fopen(MEDICINE_FILE, "w");
if (fp == NULL)
{
return;
}
medicine *p = head->next;
while (p != NULL)
{
fprintf(fp, "%s %d %s %d %d %d\n", p->drug_name,
p->drug_number,
p->drug_type,
p->cost,
p->price,
p->num);
p = p->next;
}
fclose(fp);
}
边栏推荐
- matlab 使用 audioread 、 sound 读取和播放 wav 文件
- "C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
- 人工智能在网络安全中的作用
- Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories
- Self drawing of menu items and CListBox items
- uTools
- Introduction to ffmpeg Lib
- What is AQS and its principle
- Leetcode, 3 repeatless longest subsequence
- PR second training
猜你喜欢

企业应该选择无服务器计算吗?

医药管理系统(大一下C语言课设)

PR second training

Self drawing of menu items and CListBox items

Implementation of Weibo system based on SSM

SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5

MPLS experiment operation

Number of palindromes in C language (leetcode)

1069. Division of convex polygons (thinking, interval DP)

Learning note 3 -- Key Technologies of high-precision map (Part 1)
随机推荐
Implementation principle of city selector component
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
matlab 实现语音信号重采样和归一化,并播放比对效果
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
人工智能在网络安全中的作用
卷积神经网络(包含代码与相应图解)
Construction and maintenance of business websites [10]
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
遷移雲計算工作負載的四個基本策略
Learn C language from scratch day 025 (maze)
MySQL如何解决delete大量数据后空间不释放的问题
[Floyd] post disaster reconstruction
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
new和malloc的区别
5g/4g pole gateway_ Smart pole gateway
游戏思考15:全区全服和分区分服的思考
Electronic Association C language level 1 33, odd even number judgment
Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
Failed to transform file 'xxx' to match attributes
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing