fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <dos.h>
  5. #define PTIME 20
  6. #define SQty 1000
  7. #define fname "material.txt"
  8. #define fcode "code.txt"
  9. #define ftemp "temp.txt"
  10.  
  11. FILE *fp,*fp1,*fp2;
  12. char stoday[11];
  13. int day_today=0;
  14.  
  15. /*struct tm
  16. {
  17.   int da_year;
  18.   int da_mon;
  19.   int da_day;
  20. };*/
  21.  
  22. struct data
  23. {
  24. char m_no[10];
  25. char m_name[20];
  26. char m_date[11];
  27. long m_amount;
  28. char type;
  29. char m_rdate[11];
  30. struct data* left;
  31. struct data* right;
  32. };
  33.  
  34. void load(FILE* fp);
  35. void make_tree(struct data* m_data,struct data** head);
  36. void traverse_tree(struct data** head);
  37.  
  38. struct data* head;
  39.  
  40. int main(void)
  41. {
  42. FILE* fp=fopen("material.txt","r");
  43. load(fp);
  44.  
  45. void print(void);
  46. void edit_code(void);
  47. void check_reserve(void);
  48. void record(char type);
  49. char* qtyCalc(char check_no[10],int s_day,int e_day);
  50.  
  51. {
  52. //struct tm d;
  53. struct tm* local;
  54. time_t t;
  55. t=time(NULL);
  56. local=localtime(&t);
  57.  
  58. sprintf(stoday,"%d/%d/%d",local->tm_year+1900,local->tm_mon+1,local->tm_mday);
  59.  
  60. //printf("%s\n",stoday);
  61. day_today=dayCalc(stoday);
  62. }
  63.  
  64. for(;;)
  65. {
  66. //clrscr();
  67. //system("cls");
  68. printf("\nMain Menu\n\(1)Sale.\n\(2)Order.\n\(3)Search and Print.\n\(4)Edit Code list\n\(5)Safety Stock\n\(0)Quit\n");
  69. printf("Please enter choice?");
  70. switch(getche())
  71. {
  72. case'0':exit(0);
  73. case'1':record('+');break;
  74. case'2':record('-');break;
  75. case'3':print();break;
  76. case'4':edit_code();break;
  77. case'5':check_reserve();break;
  78. default:printf("Input error!Please input 0~5\n");break;
  79. }
  80. }
  81. return 0;
  82. }
  83.  
  84. void load(FILE* fp)
  85. {
  86. char m_no[10],m_name[20],m_date[11],m_rdate[11],type;
  87. long m_amount;
  88. head=NULL;
  89. while(fscanf(fp,"%s%s%ld%c%s",m_no,m_date,&m_amount,&type,m_rdate)!=EOF)
  90. {
  91. struct data* m_data=(struct data*)malloc(sizeof(struct data));
  92. strcpy(m_data->m_no,m_no);
  93. strcpy(m_data->m_date,m_date);
  94. m_data->m_amount=m_amount;
  95. m_data->type=type;
  96. strcpy(m_data->m_rdate,m_rdate);
  97. m_data->left=NULL;
  98. m_data->right=NULL;
  99.  
  100. make_tree(m_data,&head);
  101. printf("%s\n",head->m_no);
  102. }
  103. traverse_tree(&head);
  104. }
  105.  
  106. void make_tree(struct data* t_data,struct data** t_head)
  107. {
  108. //printf("------------------------------");
  109. if(*t_head==NULL)
  110. {
  111. t_head=&t_data;
  112.  
  113. }
  114. else
  115. {
  116. if(strcmp((*t_head)->m_no,t_data->m_no)>0)
  117. {
  118. {
  119. make_tree(t_data,&((*t_head)->left));
  120. }
  121. }
  122. else if(strcmp((*t_head)->m_no,t_data->m_no)<=0)
  123. {
  124. make_tree(t_data,&((*t_head)->right));
  125. }
  126. }
  127. // printf("%s\n",(*t_head)->m_no);
  128. }
  129.  
  130. int dayCalc(char sdate[])
  131. {
  132. long year,month,day;
  133. char temp;
  134. int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
  135. int i,total;
  136. sscanf(sdate,"%ld%c%ld%c%ld",&year,&temp,&month,&temp,&day);
  137. total=day+(month>2&&((year%4==0&&year%100!=0)||year%400==0));
  138. for(i=0;i<month-1;total+=days[i++]);
  139. return total;
  140. }
  141.  
  142. void record(char type)
  143. {
  144. long total=0,m_amount=0,in_amt=0,out_amt=0;
  145. char m_no[10],m_date[11],m_rdate[11];
  146. char *s_qty,temp;
  147. char* qtyCalc(char check_no[10],int s_day,int e_day);
  148. printf("\n\n*****************************************************\n");
  149. fflush(stdin);
  150. printf("No:");
  151. gets(m_no);
  152. //printf("%s\n",m_no);
  153. s_qty=qtyCalc(m_no,1,day_today+PTIME);
  154. //printf("%s\n",s_qty);
  155. sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
  156. if(total<SQty)
  157. {
  158. printf("The current safety stock qty is %ld\n",total);
  159. }
  160. printf("Date(yyyy/mm/dd):");
  161. gets(m_date);
  162. printf("Quantity:");
  163. scanf("%ld",&m_amount);
  164.  
  165. if((fp=fopen(fname,"at"))==NULL)
  166. printf("file open error!\n"),exit(1);
  167.  
  168. fprintf(fp,"%10s%15s%10ld%c%15s\n",m_no,m_date,m_amount,type,stoday);
  169. if(fclose(fp)==-1)
  170. printf("file close error!\n"),exit(1);
  171. }
  172.  
  173. void print(void)
  174. {
  175. void search1(void);
  176. void search2(void);
  177. for(;;)
  178. {
  179. //clrscr();
  180. system("cls");
  181. printf("\nsearch & print database\n\(1)query single material\n\(2)query all materials\n\(0)back to mainmenu\n");
  182. printf("enter choice?");
  183. switch(getche())
  184. {
  185. case'0':main();break;
  186. case'1':search1();break;
  187. case'2':search2();break;
  188. default:printf("input error!please input0~2\n");break;
  189. }
  190. }
  191. }
  192.  
  193. void edit_code(void)
  194. {
  195. char temp[20],code_no[10],code_name[20];
  196. printf("\n\n***************************************\n");
  197. fflush(stdin);
  198. printf("No:");
  199. gets(temp);
  200. if((fp1=fopen(fcode,"a+t"))==NULL)
  201. printf("file open error!(code file)\n"),exit(1);
  202.  
  203. while(fscanf(fp1,"%10s%20s",code_no,code_name)!=EOF)
  204. {
  205. if(strcmp(code_no,temp)==0)
  206. {
  207. fclose(fp1);
  208. printf("duplicate code NO!\n");
  209. printf("go back to main menu.please press any key......");
  210. getche();
  211. main();
  212. }
  213. }
  214. fprintf(fp1,"%10s",temp);
  215. printf("Name:");
  216. fprintf(fp1,"%20s",gets(temp));
  217. fprintf(fp1,"\n");
  218. if(fclose(fp1)==-1)
  219. printf("file close error!\n"),exit(1);
  220. }
  221.  
  222. void check_reserve(void)
  223. {
  224. char code_no[10],code_name[20];
  225. long total=0,in_amt=0,out_amt=0;
  226. char *s_qty,temp;
  227. int e_day=0;
  228.  
  229. if((fp1=fopen(fcode,"r"))==NULL)
  230. printf("file read error(code file)\n"),exit(1);
  231.  
  232. printf("\n--------------------------------------------\n");
  233. printf("material no material name safety stock");
  234. printf("\n--------------------------------------------\n");
  235.  
  236. e_day=day_today+PTIME;
  237. while(fscanf(fp1,"%10s%20s",code_no,code_name)!=EOF)
  238. {
  239. s_qty=qtyCalc(code_no,1,e_day);
  240. sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
  241. if(total<SQty)
  242. printf("%s%22s%18ld\n",code_no,code_name,total);
  243. }
  244. if(fclose(fp1)==-1)
  245. printf("\n\npress any key to continue...!");
  246. getche();
  247. }
  248.  
  249. char* qtyCalc(char check_no[10],int s_day,int e_day)
  250. {
  251. char m_no[10],m_name[20],m_date[11],m_rdate[11],type;
  252.  
  253. long m_amount,total=0,in_amt=0,out_amt=0;
  254. int m_day=0;
  255. static char sqty[30];
  256.  
  257. if((fp=fopen(fname,"r"))==NULL)
  258. printf("file read error(database file)!"),exit(1);
  259.  
  260. while(fscanf(fp,"%s%s%ld%c%s",m_no,m_date,&m_amount,&type,m_rdate)!=EOF)
  261. {
  262. // printf("%ld%c\n",m_amount,type);
  263. m_day=dayCalc(m_date);
  264. if((strcmp(m_no,check_no)==0)&&(s_day<=m_day)&&(m_day<=e_day))
  265. {
  266. if(type=='+')
  267. total+=m_amount,in_amt+=m_amount;
  268. if(type=='-')
  269. total-=m_amount,out_amt+=m_amount;
  270. }
  271. // printf("%ld%ld%ld\n",in_amt,out_amt,total);
  272. }
  273. sprintf(sqty,"%ld/%ld/%ld",in_amt,out_amt,total);
  274. //printf("%s",sqty);
  275. if(fclose(fp)==-1)
  276. printf("file close error!\n"),exit(1);
  277. return sqty;
  278. }
  279.  
  280. void search1(void)
  281. {
  282. char search_no[10],m_no[10],m_date[11],type,m_rdate[11],p,*s_qty,temp;
  283. char Q[31][11];
  284. long Q1[31][3];
  285. long m_amount=0,in_amt=0,out_amt=0,total=0,ftotal=0;
  286. int m_day,i,j=0;
  287.  
  288. for(i=0;i<=30;i++)
  289. {
  290. strcpy(Q[i],"");
  291. Q1[i][0]=0,Q1[i][1]=0,Q1[i][2]=0;
  292. }
  293. printf("\n\n查詢今日前後15天的進出貨狀況\n");
  294. fflush(stdin);
  295. printf("\nPlease input material NO:");
  296. gets(search_no);
  297. s_qty=qtyCalc(search_no,1,day_today-16);
  298.  
  299. if((fp=fopen(fname,"r"))==NULL)
  300. printf("file read error...!"),exit(1);
  301.  
  302. sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
  303. printf("\nthe safety stock of no.%s:%ld\n",search_no,total);
  304. ftotal=total;
  305. printf("\n-------------------------------------------------------\n");
  306. printf(" date order sale stock\n");
  307. printf("--------------------------------------------------------\n");
  308.  
  309. while(fscanf(fp,"%s%s%ld%c%s",m_no,m_date,&m_amount,&type,m_rdate)!=EOF)
  310. {
  311. m_day=dayCalc(m_date);
  312. if((strcmp(m_no,search_no)==0)&&((day_today-15)<=m_day)&&(m_day<=(day_today+15)))
  313. {
  314. if(type=='+')
  315. {
  316. Q1[m_day-day_today+15][0]+=m_amount;
  317. Q1[m_day-day_today+15][2]+=m_amount;
  318. }
  319. if(type=='-')
  320. {
  321. Q1[m_day-day_today+15][1]+=m_amount;
  322. Q1[m_day-day_today+15][2]-=m_amount;
  323. }
  324. if(strcmp(Q[m_day-day_today+15],"")==0)
  325. strcpy(Q[m_day-day_today+15],m_date);
  326. }
  327.  
  328. }
  329. if(fclose(fp)==-1)
  330. printf("file close error!\n"),exit(1);
  331.  
  332. for(i=0;i<=30;i++)
  333. {
  334. Q1[i][2]=Q1[i][2]+total;
  335. total=Q1[i][2];
  336. if(strcmp(Q[i],"")!=0)
  337. {
  338. printf("%10s%15ld%15ld%15ld\n",Q[i],Q1[i][0],Q1[i][1],Q1[i][2]),j++;
  339. if(j==15)
  340. printf("--more--\n"),getch();
  341. }
  342. }
  343. fflush(stdin);
  344. printf("do you want to save the result(Y/N)?");
  345. scanf("%c",&p);
  346. if((p=='Y')||(p=='y'))
  347. {
  348. if((fp2=fopen(ftemp,"w"))==NULL)
  349. printf("file open error!\n"),exit(1);
  350.  
  351. fprintf(fp2,"\nthe safety stock of no%s:%ld\n",search_no,ftotal);
  352. fprintf(fp2,"\n----------------------------------------------\n");
  353. fprintf(fp2," date order sale stock\n");
  354. fprintf(fp2,"\n----------------------------------------------\n");
  355.  
  356. for(i=0;i<=30;i++)
  357. {
  358. if(strcmp(Q[i],"")!=0)
  359. fprintf(fp2,"%10s%15ld%15ld%15ld\n",Q[i],Q1[i][0],Q1[i][1],Q1[i][2]);
  360. }
  361. if(fclose(fp2)==-1)
  362. printf("file close error!\n"),exit(1);
  363.  
  364. printf("\nfile name:temp.txt\n");
  365. }
  366. printf("\npress any key to continue...!");
  367. getche();
  368. }
  369.  
  370. void search2(void)
  371. {
  372. char search_date[11];
  373. char code_no[10],code_name[20];
  374. char *s_qty,temp,p;
  375. int day_search=0;
  376. long in_amt=0,out_amt=0,total=0;
  377. fflush(stdin);
  378. printf("\n查詢至輸入的日期截止,所有進出貨的存貨狀況\n");
  379. printf("please input the deadline(yyyy/mm/dd):");
  380. gets(search_date);
  381. day_search=dayCalc(search_date);
  382. printf("do you want to save the result(Y/N)?");
  383. scanf("%c",&p);
  384. printf("\n---------------------------------------------\n");
  385. printf("material no order sale stock\n");
  386. printf("----------------------------------------------\n");
  387. if((p=='Y')||(p=='y'))
  388. {
  389. if((fp2=fopen(ftemp,"w"))==NULL)
  390. printf("file open error!\n"),exit(1);
  391.  
  392. fprintf(fp2,"\nthe deadline:%s\n",search_date);
  393. fprintf(fp2,"\n----------------------------------------------\n");
  394. fprintf(fp2,"material no order sale stock\n");
  395. fprintf(fp2,"----------------------------------------------\n");
  396. }
  397. if((fp1=fopen(fcode,"r"))==NULL)
  398. printf("file read error...(code file)\n"),exit(1);
  399.  
  400. while(fscanf(fp1,"%10s%20s",code_no,code_name)!=EOF)
  401. {
  402. s_qty=qtyCalc(code_no,1,day_search);
  403. // printf("%d\n",day_search);
  404. // printf("%s\n",s_qty);
  405. sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
  406. /*printf("s_qty=%s\n",s_qty);
  407.   printf("in_amt=%d\n",in_amt);
  408.   printf("out_amt=%d\n",out_amt);
  409.   printf("total=%d\n",total);*/
  410. printf("%8s %10ld %10ld %10ld\n",code_no,in_amt,out_amt,total);
  411. if((p=='Y')||(p=='y'))
  412. fprintf(fp2,"%10s %15ld %15ld %15ld\n",code_no,in_amt,out_amt,total);
  413. total=0,in_amt=0,out_amt=0;
  414. }
  415. if(fclose(fp1)==-1)
  416. printf("file close error!\n"),exit(1);
  417.  
  418. if((p=='Y')||(p=='y'))
  419. {
  420. if(fclose(fp2)==-1)
  421. printf("file close error!\n"),exit(1);
  422.  
  423. printf("\nfile name:temp.txt\n");
  424. }
  425. printf("\npress any key to continue...!");getche();
  426. }
  427.  
  428. void traverse_tree(struct data** head)
  429. {
  430. //printf("----------------------\n");
  431. if(*head==NULL)
  432. {
  433. printf("------------------------\n");
  434. return;
  435. }
  436. else
  437. {
  438. // printf("------------------------\n");
  439. printf("%s %d\n",(*head)->m_no,(*head)->m_date);
  440. traverse_tree(&((*head)->left));
  441. traverse_tree(&((*head)->right));
  442. }
  443. // printf("------------------------\n");
  444. }
  445.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty