fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dos.h>
  4. #define PTIME 20
  5. #define SQty 1000
  6. #define fname "material.txt"
  7. #define fcode "code.txt"
  8. #define ftemp "temp.txt"
  9.  
  10. FILE *fp,*fp1,*fp2;
  11. char stoday[11];
  12. int day_today=0;
  13.  
  14. struct date
  15. {
  16. int da_year;
  17. int da_mon;
  18. int da_day;
  19. };
  20.  
  21. int main(void)
  22. {
  23. void print(void);
  24. void edit_code(void);
  25. void check_reserve(void);
  26. void record(char type);
  27. char* qtyCalc(char check_no[10],int s_day,int e_day);
  28.  
  29. {
  30. struct date d;
  31. getdate(&d);
  32. sprintf(stoday,"%d/%d/%d",d.da_year,d.da_mon,d.da_day);
  33. day_today=dayCalc(stoday);
  34. }
  35.  
  36. for(;;)
  37. {
  38. clrscr();
  39. 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");
  40. printf("Please enter choice?");
  41. switch(getche())
  42. {
  43. case'0':exit(0);
  44. case'1':record('+');break;
  45. case'2':record('-');break;
  46. case'3':print();break;
  47. case'4':edit_code();break;
  48. case'5':check_reserve();break;
  49. default:printf("Input error!Please input 0~5\n");break;
  50. }
  51. }
  52. return 0;
  53. }
  54.  
  55. int dayCalc(char sdate[])
  56. {
  57. long year,month,day;
  58. char temp;
  59. int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
  60. int i,total;
  61. sscanf(sdate,"%ld%c%ld%c%ld",&year,&temp,&month,&temp,&day);
  62. total=day+(month>2&&((year%4==0&&year%100!=0)||year%400==0));
  63. for(i=0;i<month-1;total+=days[i++]);
  64. return total;
  65. }
  66.  
  67. void record(char type)
  68. {
  69. long total=0,m_amount=0,in_amt=0,out_amt=0;
  70. char m_no[10],m_date[11],m_rdate[11];
  71. char *s_qty,temp;
  72. char* qtyCalc(char check_no[10],int s_day,int e_day);
  73. printf("\n\n*****************************************************\n");
  74. fflush(stdin);
  75. printf("No:");
  76. gets(m_no);
  77. s_qty=qtyCalc(m_no,1,day_today+PTIME);
  78.  
  79. sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
  80. if(total<SQty)
  81. {
  82. printf("The current safety stock qty is %ld\n",total);
  83. }
  84. printf("Date(yyyy/mm/dd):");
  85. gets(m_date);
  86. printf("Quantity:");
  87. scanf("%ld",m_amount);
  88. if((fp=fopen(fname,"at"))==NULL)
  89. printf("file open error!\n"),exit(1);
  90.  
  91. fprintf(fp,"%10s%15s%10ld%2c%15s\n",m_no,m_date,m_amount,type,stoday);
  92. if(fclose(fp)==-1)
  93. printf("file close error!\n"),exit(1);
  94. }
  95.  
  96. void print(void)
  97. {
  98. void search1(void);
  99. void search2(void);
  100. for(;;)
  101. {
  102. clrscr();
  103. printf("\nsearch & print database\n\(1)query single material\n\(2)query all materials\n\(0)back to mainmenu\n");
  104. printf("enter choice?");
  105. switch(getche())
  106. {
  107. case'0':main();break;
  108. case'1':search1();break;
  109. case'2':search2();break;
  110. default:printf("input error!please input0~2\n");break;
  111. }
  112. }
  113. }
  114.  
  115. void edit_code(void)
  116. {
  117. char temp[20],code_no[10],code_name[20];
  118. printf("\n\n***************************************\n");
  119. fflush(stdin);
  120. printf("No:");
  121. gets(temp);
  122. if((fp1=fopen(fcode,"a+t"))==NULL)
  123. printf("file open error!(code file)\n"),exit(1);
  124.  
  125. while(fscanf(fp1,"%10s%20s",code_no,code_name)!=EOF)
  126. {
  127. if(strcmp(code_no,temp)==0)
  128. {
  129. fclose(fp1);
  130. printf("duplicate code NO!\n");
  131. printf("go back to main menu.please press any key......");
  132. getche();
  133. main();
  134. }
  135. }
  136. fprintf(fp1,"%10s",temp);
  137. printf("Name:");
  138. fprintf(fp1,"%20s",gets(temp));
  139. fprintf(fp1,"\n");
  140. if(fclose(fp1)==-1)
  141. printf("file close error!\n"),exit(1);
  142. }
  143.  
  144. void check_reserve(void)
  145. {
  146. char code_no[10],code_name[20];
  147. long total=0,in_amt=0,out_amt=0;
  148. char *s_qty,temp;
  149. int e_day=0;
  150.  
  151. if((fp1=fopen(fcode,"r"))==NULL)
  152. printf("file read error(code file)\n"),exit(1);
  153.  
  154. printf("\n--------------------------------------------\n");
  155. printf("material no material name safety stock");
  156. printf("\n--------------------------------------------\n");
  157.  
  158. e_day=day_today+PTIME;
  159. while(fscanf(fp1,"%10s%20s",code_no,code_name)!=EOF)
  160. {
  161. s_qty=qtyCalc(code_no,1,e_day);
  162. sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
  163. if(total<SQty)
  164. printf("%s%22s%18ld\n",code_no,code_name,total);
  165. }
  166. if(fclose(fp1)==-1)
  167. printf("\n\npress any key to continue...!");
  168. getche();
  169. }
  170.  
  171. char* qtyCalc(char check_no[10],int s_day,int e_day)
  172. {
  173. char m_no[10],m_name[20],m_date[11],m_rdate[11],type;
  174.  
  175. long m_amount,total=0,in_amt=0,out_amt=0;
  176. int m_day=0;
  177. static char sqty[30];
  178.  
  179. if((fp=fopen(fname,"r"))==NULL)
  180. printf("file read error(database file)!"),exit(1);
  181.  
  182. while(fscanf(fp,"%s%s%ld%c%s",m_no,m_date,&m_amount,&type,m_rdate)!=EOF)
  183. {
  184. if((strcmp(m_no,check_no)==0)&&(s_day<=m_day)&&(m_day<=e_day))
  185. {
  186. if(type=='+')
  187. total+=m_amount,in_amt+=m_amount;
  188. if(type=='-')
  189. total-=m_amount,out_amt+=m_amount;
  190. }
  191. }
  192. sprintf(sqty,"%ld%ld%ld",in_amt,out_amt,total);
  193. if(fclose(fp)==-1)
  194. printf("file close error!\n"),exit(1);
  195. return sqty;
  196. }
  197.  
  198. void search1(void)
  199. {
  200. char search_no[10],m_no[10],m_date[11],type,m_rdate[11],p,*s_qty,temp;
  201. char Q[31][1];
  202. long Q1[31][3];
  203. long m_amount=0,in_amt=0,out_amt=0,total=0,ftotal=0;
  204. int m_day,i,j=0;
  205.  
  206. for(i=0;i<=30;i++)
  207. {
  208. strcpy(Q[i],"");
  209. Q1[i][0]=0,Q1[i][1]=0,Q1[i][2]=0;
  210. }
  211. printf("\n\n查詢今日前後15天的進出貨狀況\n");
  212. fflush(stdin);
  213. printf("\nPlease input material NO:");
  214. gets(search_no);
  215. s_qty=qtyCalc(search_no,1,day_today-16);
  216.  
  217. if((fp=fopen(fname,"r"))==NULL)
  218. printf("file read error...!"),exit(1);
  219.  
  220. sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
  221. printf("\nthe safety stock of no.%s:%ld\n",search_no,total);
  222. ftotal=total;
  223. printf("\n-------------------------------------------------------\n");
  224. printf(" date order sale stock\n");
  225. printf("--------------------------------------------------------\n");
  226.  
  227. while(fscanf(fp,"%s%s%ld%c%s",m_no,m_date,&m_amount,&type,m_rdate)!=EOF)
  228. {
  229. m_day=dayCalc(m_date);
  230. if((strcmp(m_no,search_no)==0)&&((day_today-15)<=m_day)&&(m_day<=(day_today+15)))
  231. {
  232. if(type=='+')
  233. {
  234. Q1[m_day-day_today+15][0]+=m_amount;
  235. Q1[m_day-day_today+15][2]+=m_amount;
  236. }
  237. if(type=='-')
  238. {
  239. Q1[m_day-day_today+15][1]+=m_amount;
  240. Q1[m_day-day_today+15][2]-=m_amount;
  241. }
  242. if(strcmp(Q[m_day-day_today+15],"")==0)
  243. strcpy(Q[m_day-day_today+15],m_date);
  244. }
  245.  
  246. }
  247. if(fclose(fp)==-1)
  248. printf("file close error!\n"),exit(1);
  249.  
  250. for(i=0;i<=30;i++)
  251. {
  252. Q1[i][2]=Q1[i][2]+total;
  253. total=Q1[i][2];
  254. if(strcmp(Q[i],"")!=0)
  255. {
  256. printf("%10s%15ld%15ld%15ld\n",Q[i],Q1[i][0],Q1[i][1],Q1[i][2]),j++;
  257. if(j==15)
  258. printf("--more--\n"),getch();
  259. }
  260. }
  261. fflush(stdin);
  262. printf("do you want to save the result(Y/N)?");
  263. scanf("%c",&p);
  264. if((p=='Y')||(p=='y'))
  265. {
  266. if((fp2=fopen(ftemp,"w"))==NULL)
  267. printf("file open error!\n"),exit(1);
  268.  
  269. fprintf(fp2,"\nthe safety stock of no%s:%ld\n",search_no,ftotal);
  270. fprintf(fp2,"\n----------------------------------------------\n");
  271. fprintf(fp2," date order sale stock\n");
  272. fprintf(fp2,"\n----------------------------------------------\n");
  273.  
  274. for(i=0;i<=30;i++)
  275. {
  276. if(strcmp(Q[i],"")!=0)
  277. fprintf(fp2,"%10s%15ld%15ld%15ld\n",Q[i],Q1[i][0],Q1[i][1],Q1[i][2]);
  278. }
  279. if(fclose(fp2)==-1)
  280. printf("file close error!\n"),exit(1);
  281.  
  282. printf("\nfile name:temp.txt\n");
  283. }
  284. printf("\npress any key to continue...!");
  285. getche();
  286. }
  287.  
  288. void search2(void)
  289. {
  290. char search_date[11];
  291. char code_no[10],code_name[20];
  292. char *s_qty,temp,p;
  293. int day_search=0;
  294. long in_amt=0,out_amt=0,total=0;
  295. fflush(stdin);
  296. printf("\n查詢至輸入的日期截止,所有進出貨的存貨狀況\n");
  297. printf("please input the deadline(yyyy/mm/dd):");
  298. gets(search_date);
  299. day_search=dayCalc(search_date);
  300. printf("do you want to save the result(Y/N)?");
  301. scanf("%c",&p);
  302. printf("\n---------------------------------------------\n");
  303. printf("material no order sale stock\n");
  304. printf("----------------------------------------------\n");
  305. if((p=='Y')||(p=='y'))
  306. {
  307. if((fp2=fopen(ftemp,"w"))==NULL)
  308. printf("file open error!\n"),exit(1);
  309.  
  310. fprintf(fp2,"\nthe deadline:%s\n",search_date);
  311. fprintf(fp2,"\n----------------------------------------------\n");
  312. fprintf(fp2,"material no order sale stock\n");
  313. fprintf(fp2,"----------------------------------------------\n");
  314. }
  315. if((fp1=fopen(fcode,"r"))==NULL)
  316. printf("file read error...(code file)\n"),exit(1);
  317.  
  318. while(fscanf(fp1,"%10s%20s",code_no,code_name)!=EOF)
  319. {
  320. s_qty=qtyCalc(code_no,1,day_search);
  321. sscanf(s_qty,"%ld%c%ld%c%ld",&in_amt,&temp,&out_amt,&temp,&total);
  322. printf("%10s%15ld%15ld%15ld\n",code_no,in_amt,out_amt,total);
  323. if((p=='Y')||(p=='y'))
  324. fprintf(fp2,"%10s%15ld%15ld%15ld\n",code_no,in_amt,out_amt,total);
  325. total=0,in_amt=0,out_amt=0;
  326. }
  327. if(fclose(fp1)==-1)
  328. printf("file close error!\n"),exit(1);
  329.  
  330. if((p=='Y')||(p=='y'))
  331. {
  332. if(fclose(fp2)==-1)
  333. printf("file close error!\n"),exit(1);
  334.  
  335. printf("\nfile name:temp.txt\n");
  336. }
  337. printf("\npress any key to continue...!");getche();
  338. }
  339.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty