是第二次输入药品标号002,再按回车,屏幕没有向第一次一样跳出药品名让我输入,而且什么都输不进去了 ,请各位大家指点下
#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 function[100];//药品功效
char otc;//是否为OTC,是为T否为N
int lye;//保质期:2020年
int lmo;//1月
int lda;//1日
int price;//价格
int stock;//库存
}doc;
char nu[5];
char nam[20];
char fun[100];
char ot;
int ly;
int lm;
int ld;
int pri;
int sto;
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%s\t%c\t%d\t%d\t%d\t%d\t%d\n", temp.data.num, temp.data.name, temp.data.function, &temp.data.otc, &temp.data.lye, &temp.data.lmo, &temp.data.lda, &temp.data.price, &temp.data.stock) != 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("药品功效:");
scanf("%s", fun);
strcpy(p->data.function, fun);
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", &pri);
p->data.price = pri;
p->next = q->next;
q->next = p;
printf("库存:");
scanf("%d", &sto);
p->data.stock = sto;
p->next = q->next;
q->next = p;
/*
FILE* med = fopen("medicine.txt", "a");
fprintf(med, "药品序号:%s 名称:%s 药品功效:%s OTC:%c 生产日期:%d年%d月%d日 价格:%d 库存:%d\n", p->data.num, p->data.name,p->data.function, p->data.otc, p->data.lye, p->data.lmo, p->data.lda, p->data.price p->data.stock);
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("药品功效:");
scanf("%s", fun);
strcpy(p->data.function, fun);
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", &pri);
p->data.price = pri;
printf("库存:");
scanf("%d", &sto);
p->data.stock = sto;
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("药品功效:%s\n", p->data.function);
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.price);
printf("药品库存:%d\n", p->data.stock);
getchar(); getchar();
break;
}
q = q->next;
}
if (q->next == NULL) {
printf("没有当前药品信息!");
getchar(); getchar();
}
}
void rankprice(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.price > p->data.price) {
/先复制q结点信息到tmp/
strcpy(tmp->data.num, q->data.num);
strcpy(tmp->data.name, q->data.name);
strcpy(tmp->data.function, q->data.function);
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.price = q->data.price;
tmp->data.stock = q->data.stock;
/再复制p结点信息到q/
strcpy(q->data.num, p->data.num);
strcpy(q->data.name, p->data.name);
strcpy(q->data.function, q->data.function);
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.price = p->data.price;
q->data.stock = q->data.stock;
/最后复制exchange结点信息到p/
strcpy(p->data.num, tmp->data.num);
strcpy(p->data.name, tmp->data.name);
strcpy(p->data.function, tmp->data.function);
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.price = tmp->data.price;
p->data.stock = tmp->data.stock;
}
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%s\t%c\t%d\t%d\t%d\t%d\t%d\n", c->data.num, c->data.name, c->data.function, c->data.otc, c->data.lye, c->data.lmo, c->data.lda, c->data.price, c->data.stock);
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 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%s\t%c\t%d\t%d\t%d\t%d\t%d\n", p->data.num, p->data.name, p->data.function, p->data.otc, p->data.lye, p->data.lmo, p->data.lda, p->data.price, p->data.stock);
p = p->next;
}
fclose(med);
printf("保存成功!");
getchar(); getchar();
}
void lookthrough(linklist* L)//统计总药品剩余量及浏览药品信息
{
long int sum = 0;
linklist* p = L->next;
if (p == NULL) {
printf("当前没有任何药品信息!");
getchar(); getchar();
return;
}
while (p)
{
printf(" 药品序号:%s 名称:%s 药品功效:%s OTC:%c 生产日期:%d年%d月%d日 价格:%d 库存:%d \n\n", p->data.num, p->data.name, p->data.function, p->data.otc, p->data.lye, p->data.lye, p->data.lmo, p->data.lda, p->data.price ,p->data.stock);
p = p->next;
}
while (p != NULL) {
sum += p->data.stock;
p = p->next;
}
printf("总药品剩余量为:%ld", sum);
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:
rankprice(L);
break;
case 6:
finddelayed(L);
break;
case 7:
document(L);
break;
case 8:lookthrough(L);
break;
default:
printf("请输入正确的选项\n");
break;
}
}
}