老师写的,我改了一下
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #define N 28 5 int count=0; 6 void menu(); 7 void insert(); 8 void show(); 9 void del(); 10 void password(int id); 11 int box[N]={0}; 12 int main() 13 { 14 int x; 15 while(1) 16 { 17 menu(); 18 scanf("%d",&x); 19 fflush(stdin);//清理内存 20 switch(x) 21 { 22 case 1:insert();break; 23 case 2:del();break; 24 case 3:show(); break; 25 case 0:exit(0);break; 26 } 27 } 28 return 0; 29 } 30 31 void menu() 32 { 33 system("cls"); 34 int i,j; 35 printf(" ┌──────────────┐ "); 36 printf(" │ 超市存=★=包系统 │ "); 37 printf(" ├──────────────┤ "); 38 for(j=0;j<N/7;j++) 39 { 40 //箱子 41 printf(" │"); 42 for(i=j*7;i<j*7+7;i++) 43 { 44 if(box[i]==0) printf(" □ "); 45 else printf(" ■ "); 46 } 47 printf("│ "); 48 //箱号 49 printf(" │"); 50 for(i=j*7;i<j*7+7;i++) 51 { 52 printf(" %2d ",i+1); 53 } 54 printf("│ "); 55 } 56 printf(" ├──────────────┤ "); 57 printf(" │【1】存包【2】取包【0】退出 │ "); 58 printf(" │【3】显示当前储物箱使用情况 │ "); 59 printf(" │ 请按照相应提示操作 │ "); 60 printf(" │ 并记住密码 │ "); 61 printf(" │ 忘记密码与管理员联系 │ "); 62 printf(" └──────────────┘ "); 63 printf("按相应数字键,回车确认:"); 64 } 65 66 void insert() 67 { 68 int id; 69 srand((unsigned)time(NULL)); 70 while(1) 71 { 72 printf("箱号:"); 73 scanf("%d",&id); 74 fflush(stdin); 75 if(id==0) return; 76 //查找id是否已存在 77 if(box[id-1]!=0) printf("该箱已满,重选! "); 78 else 79 {count++; break;} 80 } 81 do{ 82 box[id-1]=rand(); 83 }while(box[id-1]<10000);//得到5位密码 84 password(id); 85 printf("密码:%d ",box[id-1]); 86 system("pause"); 87 88 } 89 90 void del() 91 { 92 int id,pa; 93 while(1) 94 { 95 printf("取箱号:"); 96 scanf("%d",&id); 97 fflush(stdin); 98 if(id==0) return; 99 if(box[id-1]==0) printf("箱子为空,重找! "); 100 else 101 break; 102 } 103 while(1) 104 { 105 printf("您的密码:"); 106 scanf("%d",&pa); 107 fflush(stdin); 108 if(pa==0) return; 109 if(pa!=box[id-1]) printf("密码错误,重输,必要时联系管理员!"); 110 else 111 { 112 count--; break; 113 } 114 } 115 box[id-1]=0; 116 } 117 118 void show() 119 { 120 if(count==28) 121 printf("储物箱已满! "); 122 else 123 { 124 printf("当前已用%d个箱子,剩余%d个箱子! ",count,28-count); 125 } 126 system("pause"); 127 } 128 void password(int id) 129 { 130 FILE *fp; 131 //取当前系统时间 132 time_t t=time(NULL); 133 char *time=ctime(&t); 134 fp=fopen("password.txt","a"); 135 fprintf(fp,"id=%d,password=%d,time=%s",id,box[id-1],time); 136 fclose(fp); 137 }