#include?<string.h>
#define?N?1000
typedef?struct?Remedy?{
char?SerialNumber[30];//?編號
char?Appellation[50];?//?名稱
unsigned?Stock;//?庫存
double?Price;?//?銷售價
char?Factory[60];?//?制造商
char?ManufactureDate[11];?//?生產日期
double?StockValue;//?進貨價格
unsigned?PledgeQuality;//?保質期
}Remedy;
Remedy?a[N];
int?n?=?0;
//?返回字符串s中含有子串t的個數
long?substr(char?*s,char?*t)?{
char?*p?=?s,*q?=?t;
long?amount?=?0;
while(*p)?{
while(*q)?{
if(*q?!=?*p)?{
p++;
break;
}
p++;
q++;
}
if(*q?==?'\0')?{
amount++;
q?=?t;
}
}
return?amount;
}
void?Show(Remedy?obj)?{
printf("\t編號:?%s\n",obj.SerialNumber);
printf("\t名稱:?%s\n",obj.Appellation);
printf("\t庫存:?%u\n",obj.Stock);
printf("\t銷?售?價:?%.2lf\n",obj.Price);
printf("\t生產廠家:?%s\n",obj.Factory);
printf("\t生產日期:?%s\n",obj.ManufactureDate);
printf("\t進貨價格:?%.2lf\n",obj.StockValue);
printf("\t保?質?期:?%u\n\n",obj.PledgeQuality);
}
void?Print()?{
int?i;
for(i?=?0;?i?<?n;?++i)
Show(a[i]);
}
void?Advance()?{
int?i,flag;
char?sn[30];
printf("\t編號(0?返回):");
while(scanf("%s",sn),strcmp(sn,"0")?!=?0)?{
flag?=?1;
for(i?=?0;?i?<?n;?++i)?{
if(strcmp(a[i].SerialNumber,sn)?==?0)?{
printf("***?編號不能重復,請重新輸入!\n");
flag?=?0;
break;
}
}
if(flag)?{
strcpy(a[n].SerialNumber,sn);
printf("\t藥品名稱:");scanf("%s",a[n].Appellation);
printf("\t進貨數量:");scanf("%u",&a[n].Stock);
printf("\t銷?售?價:");scanf("%lf",&a[n].Price);
printf("\t生產廠家:");scanf("%s",a[n].Factory);
printf("\t生產日期:");scanf("%s",a[n].ManufactureDate);
printf("\t進貨價格:");scanf("%.2lf",&a[n].StockValue);
printf("\t保?質?期:");scanf("%u",&a[n].PledgeQuality);
++n;
}
printf("\t編號(0?返回):");
}
}
void?Delete()?{
int?i,j,flag;
char?sn[30];
printf("\t編號(0?返回):");
while(scanf("%s",sn),strcmp(sn,"0")?!=?0)?{
flag?=?1;
for(i?=?0;?i?<?n;?++i)?{
if(strcmp(a[i].SerialNumber,sn)?==?0)?{
for(j?=?i;?j?<?n?-?1;?++j)
a[j]?=?a[j?+?1];
--n;
flag?=?0;
}
}
if(flag)?printf("\t對不起,沒有編號是:《%s》的藥品!\n",sn);
printf("\t編號(0?返回):");
}
}
//?精確查詢
void?QuerySN()?{
int?i,flag?=?1;
char?sn[30];
printf("\t請輸入藥品編號:");
scanf("%s",sn);
for(i?=?0;?i?<?n;?++i)?{
if(strcmp(a[i].Appellation,sn)?==?0)?{
Show(a[i]);
flag?=?0;
break;?//?編號是惟壹的
}
}
if(flag)?printf("\t對不起,沒有編號是:《%s》的藥品!\n",sn);
}
//?模糊查詢
void?QueryName()?{
int?i,flag?=?1;
char?name[30];
printf("\t請輸入藥品名稱:");
scanf("%s",name);
for(i?=?0;?i?<?n;?++i)?{
if(substr(a[i].SerialNumber,name))?{
Show(a[i]);
flag?=?0;
}
}
if(flag)?printf("\t對不起,沒有名稱是:《%s》的藥品!\n",name);
}
//?精確查詢
void?QueryFactory()?{
int?i,flag?=?1;
char?factory[30];
printf("\t請輸入藥品編號:");
scanf("%s",factory);
for(i?=?0;?i?<?n;?++i)?{
if(strcmp(a[i].Factory,factory)?==?0)?{
Show(a[i]);
flag?=?0;
}
}
if(flag)?printf("\t對不起,沒有制造商是:《%s》的廠家!\n",factory);
}
int?main()?{
int?an;
do?{
printf("\t***************************\n");
printf("\t*?1、添加藥品?*\n");
printf("\t*?2、刪除藥品?*\n");
printf("\t*?3、顯示庫存?*\n");
printf("\t*?4、按編號查詢*\n");
printf("\t*?5、按名稱查詢*\n");
printf("\t*?6、按生產廠家查詢*\n");
printf("\t*?0、退出?*\n");
printf("\t***************************\n");
printf("\n\t請選擇:");
scanf("%d",&an);
switch(an)?{
case?1?:?Advance();?break;
case?2?:?Delete();?break;
case?3?:?Print();?break;
case?4?:?QuerySN();?break;
case?5?:?QueryName();?break;
case?6?:?QueryFactory();?break;
case?0?:?break;
default?:?printf("\t選擇錯誤!\n");?break;
}
}while(an);
printf("\t");
return?0;
}