| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 554 人关注过本帖
标题:[求助]
只看楼主 加入收藏
瑶瑶
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2005-9-27
收藏
 问题点数:0 回复次数:1 
[求助]

#include <stdio.h> #include <stdlib.h> #include <ctype.h> /* include the function file */ #include "semifun.c"

/* the main function */ int main() { bool flag = true; int choice = 0; while(flag == true) /* print the menu */ { system("cls"); printf("*****************SALES MONITORING SYSTEM**************\n\n"); printf("1.Add new item details\n"); printf("2.Record Sale details\n"); printf("3.Delete existing Item details\n"); printf("4.Display Sale details based on Item number\n"); printf("5.Display Report on Sales Target Achieved Items\n"); printf("6.Quit\n\n"); printf("Enter choice:"); if(scanf("%d", &choice) != 1) { fflush(stdin); system("pause"); continue; } if(choice > 6 || choice < 1) { printf("the input number must be >=1 and <=6 's integer!\n"); system("pause"); continue; } switch(choice) { case 1: add_item();break; case 2: add_sales();break; case 3: del();break; case 4: Dis_sales();break; case 5: Dis_Report();break; default: flag = false; };/* end switch */ system("pause"); } /* end while */ return 0; } /* end main */ /*-------------------------------------------------------------------- *--------Function implement------------------------------------------ *-------------------------------------------------------------------*/

#include "semifun.h"

void add_item() { FILE* item; Item_Node iNode; openfile(item, "item.dat", "ab+"); if(!fseek(item, -1*item_size, SEEK_END)) { if(fread(&iNode, item_size, 1, item) == 1) iNode.item_code += 1; else iNode.item_code = 1; }/* end if */ iNode.Y2D_sales = 0; printf("\tADD NEW ITEM DETAILS\n"); printf("Item code:%u\n", iNode.item_code); printf("Enter Item name(up to 20 chars.):"); while(1) { scanf("%s", iNode.item_name); if(isalpha(iNode.item_name[0])) break; else printf("the name can't be NULL\n"); } /* end while */ printf("Enter Targeted sales:"); scanf("%u", &(iNode.target_sales)); fseek(item, 0, SEEK_END); if(fwrite(&iNode, item_size, 1, item) == 1) { printf("Record written to Item file\n"); fclose(item); return; } /* end if */ fclose(item); written_error("item.dat"); exit(0); }; /* end function add_item() */ void add_sales() { FILE* salefp, *itemfp; PSales_Node psNode; PItem_Node piNode; bool flag = false; openfile(salefp, "sales.dat", "ab+"); if((itemfp= fopen("item.dat","rb+")) == NULL) { print_error("item.dat"); fclose(salefp); return; }/* end if */ psNode = (PSales_Node)malloc(sale_size); piNode = (PItem_Node)malloc(item_size); printf("\tADD ITEM SALE DETAILS\n"); while(1) { fseek(itemfp, 0, SEEK_SET); flag = false; printf("Enter Item code:"); scanf("%u", &(psNode->sale_code)); if(psNode->sale_code <= 0) continue; while(!feof(itemfp)) { if(fread(piNode, item_size,1,itemfp) == 1) if(piNode->item_code == psNode->sale_code) { flag = true; break; }/* end if */ } /* end while */ if(flag == true) break; else printf("Invalid code,plase try agin\n"); } /* end while */ do{ psNode->Quantity_Sold = 0; fflush(stdin); printf("Enter Quantity Sold:"); }while(scanf("%u", &(psNode->Quantity_Sold)) != 1 ||psNode->Quantity_Sold <= 0); fseek(salefp, 0, SEEK_END); if(fwrite(psNode, sale_size, 1, salefp) == 1) { fseek(itemfp, -1*item_size, SEEK_CUR); piNode->Y2D_sales += psNode->Quantity_Sold; if(fwrite(piNode, item_size, 1, itemfp) ==1) printf("Record updated in Item file\n"); else written_error("item.dat"); printf("Record written to Sales file\n"); } /* end if */ else written_error("sales.dat"); fclose(itemfp); fclose(salefp); free(psNode); free(piNode); }; /* end function add_sales() */ void del() { FILE *item1,*item2,*sales1,*sales2; Item_Node iNode; Sales_Node sNode; bool flag = true; UINT index = 0; openfile(item1, "item.dat", "rb"); if((sales1 = fopen("sales.dat","rb"))== NULL) { print_error("sales.dat"); fclose(item1); return; } /* end if */ if((item2 = fopen("item1.dat","ab+")) == NULL) { print_error("temp-file item1.dat"); fclose(item1); fclose(sales1); return; } /* end if */ if((sales2 = fopen("sales1.dat","ab+")) == NULL) { print_error("temp-file sales1.dat"); fclose(item1); fclose(sales1); fclose(item2); return; } /* end if */ printf("\tDELETE ITEM DETAILS\n"); while(1) { printf("Enter Item code:"); if(scanf("%u", &index) != 1) fflush(stdin); else break; } /* end while */ /* search in item.dat */ while(!feof(item1)) { if(fread(&iNode, item_size, 1, item1) == 1) { if(iNode.item_code != index) fwrite(&iNode, item_size, 1, item2); else { flag = false; printf("\n1 Record(s) deleted from Item file.\n"); } /* end else */ } /* end if */ } /* end while */ if(flag) printf("No record be found in item&sales file\n"); else { flag = true; /* search in sale.dat */ while(!feof(sales1)) { if(fread(&sNode, sale_size, 1, sales1) == 1) { if(iNode.item_code != index) fwrite(&sNode, sale_size, 1, sales2); else { flag = false; printf("\n1 Record(s) deleted from Sales file.\n"); } /* end else */ } /* end if */ } /* end while */ if(flag) printf("No record be found in sales file\n"); } /* end else */ fclose(item1); fclose(item2); fclose(sales1); fclose(sales2); remove("item.dat"); remove("sales.dat"); rename("item1.dat","item.dat"); rename("sales1.dat","sales.dat"); }; /* end funtion del */ void Dis_sales() { FILE* salefp; Sales_Node sNode; UINT saleNum; bool flag = false; openfile(salefp, "sales.dat", "rb"); printf("\tQUERY SALES DETAILS\n"); do{ printf("Enter Item code:"); }while(scanf("%u", &saleNum) != 1); while(!feof(salefp)) { if(fread(&sNode, sale_size, 1, salefp) == 1) { if(sNode.sale_code == saleNum) { flag = true; printf("Item %u Quantity sold:%u\n",saleNum, sNode.Quantity_Sold); } /* end if */ } /* end if */ } /* end while */ if(!flag) printf("No Record exits\n"); fclose(salefp); }; /* end function dis_sales() */ void Dis_Report() { FILE*itemfp; Item_Node iNode; bool flag = false; openfile(itemfp, "item.dat", "rb"); printf("\tREPORT ON SALES TARGET ACHIEVED ITEMS\n"); while(!feof(itemfp)) { if(fread(&iNode, item_size, 1, itemfp) == 1) if(iNode.Y2D_sales >= iNode.target_sales) { flag = true; printf("%4u%10s%5u%5u\n", iNode.item_code, iNode.item_name, iNode.target_sales, iNode.Y2D_sales); } /* end if */ } /* end if */ if(!flag) printf("No item has achieved the target sales\n"); fclose(itemfp); }; /* end function dis_report() */ /*-------------------------------------------------------------* * this is the funciton-declearation and type-define file * *-------------------------------------------------------------*/

/* start type-define */ typedef unsigned int UINT; typedef enum{false, true} bool; typedef struct { UINT item_code; char item_name[20]; UINT target_sales; UINT Y2D_sales; } Item_Node, *PItem_Node; typedef struct { int sale_code; UINT Quantity_Sold; } Sales_Node, * PSales_Node; /* end type-define */ /* start MRCO define */ #define item_size sizeof(Item_Node) #define sale_size sizeof(Sales_Node) #define print_error(x) printf("Can't open file "##x##"\nBesure the file and the progream under the same dirctory\n") #define written_error(x) printf("Written file "##x##" Failure\n") #define openfile(x,y,z) if((x = fopen(y,z)) == NULL) { print_error(y); return; } /* end MRCO define */

/* start function-declearation */ void add_itme(); void add_sales(); void del(); void Dis_sales(); void Dis_Report(); /* end function-declearation */

/*-------------------------------------------------------------* * this is the funciton-declearation and type-define file * *-------------------------------------------------------------*/

/* start type-define */ typedef unsigned int UINT; typedef enum{false, true} bool; typedef struct { UINT item_code; char item_name[20]; UINT target_sales; UINT Y2D_sales; } Item_Node, *PItem_Node; typedef struct { int sale_code; UINT Quantity_Sold; } Sales_Node, * PSales_Node; /* end type-define */ /* start MRCO define */ #define item_size sizeof(Item_Node) #define sale_size sizeof(Sales_Node) #define print_error(x) printf("Can't open file "##x##"\nBesure the file and the progream under the same dirctory\n") #define written_error(x) printf("Written file "##x##" Failure\n") #define openfile(x,y,z) if((x = fopen(y,z)) == NULL) { print_error(y); return; } /* end MRCO define */

/* start function-declearation */ void add_itme(); void add_sales(); void del(); void Dis_sales(); void Dis_Report(); /* end function-declearation */

搜索更多相关主题的帖子: function existing details include system 
2005-10-01 12:06
瑶瑶
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2005-9-27
收藏
得分:0 
哪位帮我找哈错误哈!!!谢谢啦

ωǒ喜歡? 纏綿啲ー種 感覺﹌ 彼此ニ深愛鈳苡 選擇壹輩子吥離開妳
2005-10-01 12:07
快速回复:[求助]
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017615 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved