记录第一次系统地写下一个实用代码
/*1.插入一条记录,包括需要采购物品的名称,采购的数量,单价,采购地点到商场的距离,用一个结构体实现。
2.对所有需要采购的物品按距离进行的排序。
3.对所有需要采购的物品按名称字典序进行的排序。
4.删除一条记录,按时间删除和按物品名删除。
5.查找对应时间需要采购的物品。
6.按物品名称查找相关信息(扩展需求:模糊搜索,比如名称为abc,输入a或输入b,也可显示该记录)。
7.修改某个物品需要采购的数量,单价。
8.到点提醒,到确切的时间,提醒老刘去采购相应的物品(获取系统时间即可)。*/
#include
#include
#include
#include
#include
#include
#define output "||||name:%s||number:%d||price:%.2lfyuan||place:%s||distance:%.2lfkm||time:%02do'clock||%02dminutes||%02dseconds||||\n"
#define DATA p->data.name,p->data.num,p->data.price,p->data.place,p->data.distance,p->data.hours,p->data.minutes,p->data.seconds
//定义购买清单结构体
struct item{
char name[30];
int num;
double price;
char place[30];
double distance;
int hours;
int minutes;
int seconds;
};
//定义链表
typedef struct Node{
struct item data;/*存放数据*/
struct Node* next;/*指针*/
}node,*head;
head l;
int ch;
/*菜单、进入界面
1.增添一条购物清单
2.删除一条购物清单
3.更改一条购物清单
4.输入时间来找到购物物品
5.输入物品名称来找到购物清单记录
6.按照距离排序
7.按照字典序排序
8.闹钟提醒服务*/
void list(){
printf("Welcome to the shopping listing!\n");
printf("Please enter a number as follows if you want to use the program:\n");
printf("1.Add a shopping record.\n");
printf("2.Delete a shopping record.\n");
printf("3.Modify a shopping record.\n");
printf("4.Enter the time to find the item.\n");
printf("5.Enter the name of the item to find the shopping records.\n");
printf("6.Sort by distance.\n");
printf("7.Sort in alphabetical order\n");
printf("I have alarm service~\n");
printf("Hope you enjoy using the programe\(^o^)/~\n");
printf("Please enter a number:\n");
}
//增添一条购物清单的函数
void add_list(void){
system("CLS");
char na[30],pl[30];
int nu,ho,mi,se;
double pr,di;
node *p,*last;
printf("Enter the name of item, please.\n");
scanf("%s",na);
while((ch=getchar())!=EOF&&ch !='\n');
printf("Enter the number of item, please.\n");
scanf("%d",&nu);
while((ch=getchar())!=EOF&&ch !='\n');
printf("Enter the price of item, please.\n");
scanf("%lf",&pr);
while((ch=getchar())!=EOF&&ch !='\n');
printf("Enter the place to buy the item, please.\n");
scanf("%s",pl);
while((ch=getchar())!=EOF&&ch !='\n');
printf("Enter the distance to the place(km), please.\n");
scanf("%lf",&di);
while((ch=getchar())!=EOF&&ch !='\n');
printf("Enter the time to buy the item, please(hours minutes seconds)\n");
scanf("%d %d %d",&ho,&mi,&se);
while((ch=getchar())!=EOF&&ch !='\n');
p=(node*)malloc(sizeof(node));
if(!p){
printf("\n allocate memory failure\n");
return;
}
p->data.num=nu;
p->data.price=pr;
p->data.distance=di;
p->next=NULL;
p->data.hours=ho;
p->data.minutes=mi;
p->data.seconds=se;
strcpy(p->data.name,na);
strcpy(p->data.place,pl);
last=l;
if(last){
while(last->next) last=last->next;
last->next=p;
}
else l=p;
printf("add success!\n");
printf(output,DATA);
printf("\n");
list();
return;
}
//找符合时间条件的清单
node* sear_t(int ho,int mi,int se){
node* s;
s=l->next;
while(s){
if(s->data.hours==ho&&s->data.minutes==mi&&s->data.seconds==se){
return s;
}
s=s->next;
}
return 0;
}
//找符合名字条件的清单
node* sear_n(char* ar){
node* s;
s=l->next;
while(s){
if(strcmp(s->data.name,ar)==0){
return s;
}
s=s->next;
}
return 0;
}
//删除一条购物清单的函数
void del_list(void){
system("CLS");
node *p,*h,*s;
int ho,mi,se;
int c,i;
char ar[30];
if(!((l)->next)){
printf("You hadn't creat a list yet!\n");
printf("\n");
list();
return;
}
printf("If you want to delete a record by time, enter 1\n");
printf("If you want to delete a record by name, enter 0\n");
scanf("%d",&i);
while((ch=getchar())!=EOF&&ch !='\n');
if(i){
printf("Enter the time, please.(hours minutes seconds)\n");
scanf("%d %d %d",&ho,&mi,&se);
while((ch=getchar())!=EOF&&ch !='\n');
p=sear_t(ho,mi,se);
if(!p){
printf("The record doesn't exist.\n");
printf("\n");
list();
return;
}
printf(output,DATA);
}
else{
printf("Enter the name, please.\n");
scanf("%s",ar);
while((ch=getchar())!=EOF&&ch !='\n');
p=sear_n(ar);
if(!p){
printf("The record doesn't exist.\n");
printf("\n");
list();
return;
}
printf(output,DATA);
}
printf("Are you sure to delete this record? Please enter(1/0),1=yes,0=no.\n");
scanf("%d",&c);
while((ch=getchar())!=EOF&&ch !='\n');
if(c==1){
h=l;
while(h->next!=p){
h=h->next;}
if(h){
h->next = p->next;
free(p);
}
printf("delete success!\n");
}
printf("\n");
list();
return;
}
//修改一条购物清单的函数
void mod_list(void){
system("CLS");
int n,c;
double pr;
char ar[30];
node *p,*h;
h=l;
printf("Enter the name of the item to search the record, please.\n");
scanf("%s",ar);
while((ch=getchar())!=EOF&&ch !='\n');
p=sear_n(ar);
if(!p){
printf("The record doesn't exist.\n");
printf("\n");
list();
return;
}
printf(output,DATA);
printf("Are you sure to modify the record?(1/0)1=yes,0=no.\n");
scanf("%d",&c);
while((ch=getchar())!=EOF&&ch !='\n');
if(c==1) {
printf("Enter the number and the price of the item you want to modify, please.(number price)\n");
scanf("%d %lf",&n,&pr);
while((ch=getchar())!=EOF&&ch !='\n');
p->data.num=n;
p->data.price=pr;
printf(output,DATA);
printf("modify success of %s!\n",ar);
}
printf("\n");
list();
return;
}
//按照输入时间查找需购物品的函数
void t_find(void){
system("CLS");
int ho,mi,se;
node *p;
printf("Enter the time, please.(hours minutes seconds)\n");
scanf("%d %d %d",&ho,&mi,&se);
while((ch=getchar())!=EOF&&ch !='\n');
p=sear_t(ho,mi,se);
if(!p){
printf("The record doesn't exsit!\n");
printf("\n");
list();
return;
}
printf("You need to buy:%s in%d:%d:%d.\n",p->data.name,ho,mi,se);
printf("Search success!\n");
printf("\n");
list();
return;
}
//物品名模糊搜索
node* mhsear_n(char* ar){
node* s;
s=l->next;
while(s){
if(strstr(s->data.name,ar)!=0){
return s;
}
s=s->next;
}
return 0;
}
//按照输入物品名称查找那一条购买清单的函数
void n_find(void){
system("CLS");
char ar[30];
node *p;
printf("Enter the name of the item, please.\n");
scanf("%s",ar);
while((ch=getchar())!=EOF&&ch !='\n');
p=mhsear_n(ar);
if(!p){
printf("Search failure!\n");
printf("\n");
list();
return;
}
printf(output,DATA);
printf("Search success!\n");
printf("\n");
list();
return;
}
//按照距离将购物清单进行排序的函数
void d_sort(void){
system("CLS");
node *p,*h,*c,*f;
double a,b;
int n=0,i,j;
f=l;
p=f->next;
if(!p) printf("The list is empty!\n");
else if(p->next==NULL) printf("You only have one record!\n");
else{
while(p){
n++;
p=p->next;
}
for(i=0;inext;
for(j=0;jnext;
c=p;
a=c->data.distance;
b=h->data.distance;
if(a>=b){
struct item temp=c->data;
c->data=h->data;
h->data=temp;
}
p=p->next;
}
}
p=f->next;
for(i=0;inext;
}
("Sort success!\n");
}
printf("\n");
list();
return;
}
//按照物品名字典序将购物清单排序的函数
void a_sort(void){
system("CLS");
node *p,*h,*c,*f;
int n=0,i,j;
f=l;
p=f->next;
if(p==NULL) printf("The list is empty!\n");
else if(p->next==NULL) printf("You only have one record!\n");
else{
while(p){
n++;
p=p->next;
}
for(i=0;inext;
for(j=0;jnext;
c=p;
if(strcmp(c->data.name,h->data.name)>=0){
struct item temp=c->data;
c->data=h->data;
h->data=temp;
}
p=p->next;
}
}
p=f->next;
for(i=0;inext;
}
("Sort success!\n");
}
printf("\n");
list();
return;
}
//到点提醒服务
unsigned __stdcall alarm_(void*){
node *p;
int hou,min,sec;
while(1){
hou=time(NULL)%(24*3600)/3600+8;
if(hou>=24){
hou=hou-24;
}
min=time(NULL)%3600/60;
sec=time(NULL)%60;
p=sear_t(hou,min,sec);
if(p){
printf("It's time! You need to buy:\n");
printf(output,DATA);
}
Sleep(1000);
}
}
int main(){
list();
int n;
HANDLE HH;
l=(node*)malloc(sizeof(node));
l->next=NULL;
unsigned id;
HH=(HANDLE)_beginthreadex(NULL,0,alarm_,0,NULL,&id);
while(scanf("%d",&n)!=EOF){
if(n==1) add_list();
else if(n==2) del_list();
else if(n==3) mod_list();
else if(n==4) t_find();
else if(n==5) n_find();
else if(n==6) d_sort();
else if(n==7) a_sort();
}
return 0;
}