購物表(動態連結串列)+鬧鐘提醒(多執行緒)
阿新 • • 發佈:2018-11-12
基本連結串列的應用
增 刪 查 找 排 模糊查詢
核心程式碼如下:
#include <stdio.h> #include <math.h> #include <string.h> #include <malloc.h> #include <stdlib.h> #include <pthread.h> #include <windows.h> #define len sizeof(stu) typedef struct student//定義一個結構體 { char name[100]; int num; double value; double length; double time; int hour; int minu; int sec; } ; typedef struct node//建立一個指標域,結構域 { struct student date ; struct node *next; } stu; int z=1; int n; int Tips()//選單 { int p; 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("<----------9.距離排序---------------->\n"); printf("<----------10.名稱排序---------------->\n"); printf("<----------0.退出------------------->\n"); if(z==1) printf("<------30秒內未應答將自動關閉------------>"); z--; printf("\n"); scanf("%d",&p); return p; } stu*head=(stu *)malloc(len); stu * creat()//建立連結串列 { stu *p1,*p2; int i,t,k=1; n=0; printf("請輸入輸入第一個物品資訊\n"); p1=head; printf("輸入第%d個資訊數目\n請輸入物品的時間和內容格式如\n 物品名:pen(筆) \n 數量:1\n 單價2.0\n 距離5.0\n 日期:6.3\n 時10\n 分30\n 秒10\n",k++); printf("物品名稱:"); scanf("%s",p1->date.name); printf("數量:"); scanf("%d",&p1->date.num); printf("單價:"); scanf("%lf",&p1->date.value); printf("距離:"); scanf("%lf",&p1->date.length); printf("日期月.日:"); scanf("%lf",&p1->date.time); printf("時:"); scanf("%d",&p1->date.hour); printf("分:"); scanf("%d",&p1->date.minu); printf("秒:"); scanf("%d",&p1->date.sec); n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p2->next=0; return head; } void print(stu * head)//顯示 { stu * p; printf("此時的%d個物品資訊是:\n",n); p=head; if(head!=0) { while(p!=0) { printf("物品名稱%s\n數量%d\n單價%.2lf元\n距離%.2lf公里\n日期%.2lf\n時:%d h\n分:%d m\n秒:%d s\n", p->date.name,p->date.num,p->date.value,p->date.length,p->date.time,p->date.hour,p->date.minu,p->date.sec); p=p->next; } } } stu * del(stu * head,int hour,int minu,int sec)//刪除(通過時間來刪除) { stu *p1,*p2; if(head==0) printf("表空!\n"); else { p1=head; while(p1->date.hour!=hour && p1->date.minu!=minu&&p1->date.sec!=sec && p1->next!=0) { p2=p1; p1=p1->next; } if(p1->date.hour==hour&&p1->date.minu==minu&&p1->date.sec==sec) { if(p1==head) { head=p1->next; p1->date.sec=61; free(head); } else { p2->next=p1->next; free(p1); } printf("刪除成功\n"); n=n-1; free(p1); } else printf("沒有這個物品!\n"); } return head; } stu * dels(stu * head,char name[])//刪除(通過名稱刪除) { stu *p1,*p2; if(head==0) printf("表空!\n"); else { p1=head; while(strcmp(p1->date.name,name)!=0 && p1->next!=0) { p2=p1; p1=p1->next; } if(strcmp(p1->date.name,name)==0) { if(p1==head) { head=p1->next; p1->date.sec=61; // printf("%d",head); free(p1); } else { p2->next=p1->next; free(p1); } printf("刪除成功\n"); n=n-1; } else printf("沒有這個物品!\n"); } return head; } stu * search(stu * head,int hour,int minu)//搜尋(時間搜尋) { stu *p1,*p2; if(head==0) printf("表空!\n"); else { p1=head; while(p1->date.hour!=hour && p1->date.minu!=minu&&p1->next!=0 ) { p2=p1; p1=p1->next; } if(p1->date.hour==hour&&p1->date.minu==minu) { printf("物品名稱%s\n數量%d\n單價%.2lf元\n距離%.2lf公里\n日期%.2lf\n時:%d h\n分:%d m\n秒:%d s\n", p1->date.name,p1->date.num,p1->date.value,p1->date.length,p1->date.time,p1->date.hour,p1->date.minu,p1->date.sec); printf("查詢成功\n"); } else printf("沒有這個物品!\n"); } return head; } stu * seach(stu * head,char name[])//模糊查詢(姓名模糊查詢) { stu *p1; int number=0; if(head==0) printf("表空!\n"); else { for(p1=head; p1!=NULL; p1=p1->next) { if(strstr(p1->date.name,name)!=0) { printf("物品名稱%s\n數量%d\n單價%.2lf元\n距離%.2lf公里\n日期%.2lf\n時:%d h\n分:%d m\n秒:%d s\n", p1->date.name,p1->date.num,p1->date.value,p1->date.length,p1->date.time,p1->date.hour,p1->date.minu,p1->date.sec); printf("查詢成功\n"); number++; } } if(number==0) printf("沒有這個物品!\n"); } return head; } stu * change(stu * head,char name[])//修改(按名稱) { stu *p1,*p2; if(head==0) printf("表空!\n"); else { p1=head; while(strcmp(p1->date.name,name)!=0 && p1->next!=0) { p2=p1; p1=p1->next; } if(strcmp(p1->date.name,name)==0) { printf("物品名稱 數量 單價 距離 日期 時 分 秒\n"); scanf("%s%d%lf%lf%lf%d%d%d", p1->date.name,&p1->date.num,&p1->date.value,&p1->date.length,&p1->date.time,&p1->date.hour,&p1->date.minu,&p1->date.sec); printf("修改成功\n"); printf("物品名稱%s\n數量%d\n單價%.2lf元\n距離%.2lf公里\n日期%.2lf\n時:%d h\n分:%d m\n秒:%d s\n", p1->date.name,p1->date.num,p1->date.value,p1->date.length,p1->date.time,p1->date.hour,p1->date.minu,p1->date.sec); } else printf("沒有這個物品!\n"); } return head; } stu *insert(stu * head, stu * stud)//新增 { stu *p1,*p2,*p0; p0=stud; p1=head; if(head==0) { head=p0; stud->next=0; } else { while(p1->date.num<p0->date.num && p1->next!=0) { p2=p1; p1=p1->next; } if(p1->date.num>=p0->date.num) { if(p1==head) { head=p0; p0->next=p1; } else { p2->next=p0; p0->next=p1; } } else { p1->next=p0,p0->next=0; } } n=n+1; return head; } stu * pall(stu * head)//排序(距離) { stu *p1,*p2; int j=0; int i=0; if(head==0) printf("表空!\n"); { struct student temp; for(p1=head; p1!=NULL; p1=p1->next) { for(p2=p1->next; p2!=NULL; p2=p2->next) { if(p1->date.length>p2->date.length) { temp = p2->date; p2->date = p1->date; p1->date = temp; } } } printf("排序成功\n"); print(head); } return head; } stu * palls(stu * head)//排序(名字) { stu *p1,*p2; int j=0; int i=0; if(head==0) printf("表空!\n"); { struct student temp; for(p1=head; p1!=NULL; p1=p1->next) { for(p2=p1->next; p2!=NULL; p2=p2->next) { if(strcmp(p1->date.name,p2->date.name)>0) { temp = p2->date; p2->date = p1->date; p1->date = temp; } } } printf("排序成功\n"); print(head); } return head; } stu* maining() { stu * head=0,*stud; int m; int hour,minu,sec; int i=1; char name[100]; while(i>0) { system("cls"); i=Tips(); switch(i) { case 1: { system("cls"); printf("開始輸入資訊:\n"); head=creat(); system("cls"); print(head); printf("請輸入0返回\n"); scanf("%d",&m); while(m!=0) { printf("請輸入0返回\n"); scanf("%d",&m); } break; } case 2: { system("cls"); printf("輸入要刪除的時間:"); printf("時: 分: 秒:\n "); scanf("%d%d%d",&hour,&minu,&sec); head=del(head,hour,minu,sec); print(head); printf("請輸入0返回\n"); scanf("%d",&m); while(m!=0) { printf("請輸入0返回\n"); scanf("%d",&m); } break; } case 3://名字刪除 { system("cls"); printf("輸入要刪除的名稱:"); scanf("%s",name); head=dels(head,name); print(head); printf("請輸入0返回\n"); scanf("%d",&m); while(m!=0) { printf("請輸入0返回\n"); scanf("%d",&m); } break; } case 5: { system("cls"); stud=(stu *)malloc(len); printf("物品名稱:"); scanf("%s",&stud->date.name); printf("數量:"); scanf("%d",&stud->date.num); printf("單價:"); scanf("%lf",&stud->date.value); printf("距離:"); scanf("%lf",&stud->date.length); printf("日期:"); scanf("%lf",&stud->date.time); printf("時:"); scanf("%d",&stud->date.hour); printf("分:"); scanf("%d",&stud->date.minu); printf("秒:"); scanf("%d",&stud->date.sec); head=insert(head,stud); print(head); printf("請輸入0返回\n"); scanf("%d",&m); while(m!=0) { printf("請輸入0返回\n"); scanf("%d",&m); } break; } case 4: { system("cls"); print(head); printf("請輸入0返回\n"); scanf("%d",&m); while(m!=0) { printf("請輸入0返回\n"); scanf("%d",&m); } break; } case 6: { system("cls"); printf("輸入要搜尋的時間:\n"); printf("時: 分:\n"); scanf("%d%d",&hour,&minu); head=search(head,hour,minu); printf("請輸入0返回\n"); scanf("%d",&m); while(m!=0) { printf("請輸入0返回\n"); scanf("%d",&m); } break; } case 7: { system("cls"); printf("輸入要搜尋的名字:"); scanf("%s",name); head=seach(head,name); printf("請輸入0返回\n"); scanf("%d",&m); while(m!=0) { printf("請輸入0返回\n"); scanf("%d",&m); } break; } case 8: { system("cls"); printf("輸入要修改的名稱:"); scanf("%s",name); head=change(head,name); printf("請輸入0返回\n"); scanf("%d",&m); while(m!=0) { printf("請輸入0返回\n"); scanf("%d",&m); } break; } case 9: { system("cls"); printf("排序:"); head=pall(head); printf("請輸入0返回\n"); scanf("%d",&m); while(m!=0) { printf("請輸入0返回\n"); scanf("%d",&m); } break; } case 10: { system("cls"); printf("排序:"); head=palls(head); printf("請輸入0返回\n"); scanf("%d",&m); while(m!=0) { printf("請輸入0返回\n"); scanf("%d",&m); } break; } } } } stu* time()//(鬧鐘) { stu *p1; SYSTEMTIME time; SYSTEMTIME clock; GetLocalTime(&time); while(1) { Sleep(1000); GetLocalTime(&time); for(p1=head; p1!=NULL; p1=p1->next) { if(p1->date.hour==time.wHour&&p1->date.minu==time.wMinute&&p1->date.sec==time.wSecond) { printf("該買%s了!要購買%d個噢!\a\a\a\n",p1->date.name,p1->date.num); break; } // printf("%d\n",p1); } // printf("%d",head); } } void* tprocess1(void* args)//執行緒1 { maining(); return NULL; } void* tprocess2(void* args)執行緒2 { Sleep(35000); time() ; return NULL; } int main()//多執行緒 { pthread_t t1; pthread_t t2; pthread_create(&t1,NULL,tprocess1,NULL); pthread_create(&t2,NULL,tprocess2,NULL); pthread_join(t1,NULL); pthread_join(t2,NULL); free(head); return 0; }
總結:動態連結串列與多執行緒的運用需要十分注意。