Thread: Emergency 2

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    Emergency 2

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <string.h>
    
    #define Sup_Items_max 50   /*variables initialization */
    #define Prod_Items_max 50
    #define Stor_Items_max 50
    #define WH_Items_max 50
    
    typedef struct Prod_Rec /*products*/
    {
    	int Prod_Code;
    	char Prod_Descr[20];
    	int Prod_Vol;
    	char Prod_Storage_Option[2];
    }Prod_Rec;
    
    typedef struct Store_Rec /*Storage*/
    {
    	int Store_WHCode;
    	int Store_ShelfCode;
    	int Store_ProdCode;
    	int Store_ShelfType;
    	int Store_Quantity;
    }Store_Rec;
    
    typedef struct Sup_Rec /*Suplies*/
    {
    	int Sup_Code;
    	char Sup_Date[10];
    	int Sup_Area;
    	int Sup_Items;
    	int Sup_ProdCode;
    	int Sup_ProdUnit;
    }Sup_Rec;
    
    typedef struct Log_Rec /*Log*/
    {
    	char Log_Date[10];
    	int Log_WH;
    	int Log_Item;
    	char Log_TransactionType;
    	int Log_TransactionCode;
    	int Log_Quantity;
    	int Log_Status;
    }Log_Rec;
    
    typedef struct Sys_Rec /*System*/
    {
    	int Sys_WhNum;
    	int Sys_ProdNum;
    	char Sys_WhFN[13];
        int Sys_StorVol;
    	char Sys_ProdFN[11];
    	int Sys_SupMaxItems;
    	int Sys_OrdMaxItems;
        char Sys_VolDescr[6];
    }Sys_Rec;
    
    typedef struct WH_Rec /*WareHouse*/
    {
    	int WH_Code;
    	int WH_Area;
    	int WH_Store1Total;
    	int WH_Store1Free;
    	int WH_Store2Total;
    	int WH_Store2Free;
    	int WH_ProximityList[4];
    }WH_Rec;
    
    
    /* ************************************************************************** */
    /* global variables */
    Prod_Rec B[Prod_Items_max];
    Sup_Rec A[Sup_Items_max];
    Store_Rec S[Stor_Items_max];
    Log_Rec L[Sup_Items_max];
    WH_Rec W[WH_Items_max];
    
    
    /* ************************************************************************** */
    /* Function prototypes */
    void runF21(void);
    void runF31(void);
    void runF10(void);
    void runF11(char fname[]);
    void runF12(char fname[]);
    void runF22(int );
    int  runF23(int);
    void runF24(int ,int );
    
    /* ************************************************************************** */
    int GetUserOpt() {
        int x;
    	printf("\n\n\n\nUniversal Export Warehouse Manager\n\n");
    	printf("1. Supply into warehouse\n\n");
    	printf("2. Execute Order\n\n");
    	printf("0. Exit    ");
        scanf("%d",&x);
        
        return x;
    }
    /* ************************************************************************** */
    void ProcessUserOpt(int a) {
    
    	switch (a) {
    		case 1:  runF21(); break;
            case 2:  runF31(); break;
            default: ;
        }
    }    
    
    /* ************************************************************************** */
    int runF00() {
        int UserOpt;
    	int i;
        do {
           UserOpt=GetUserOpt();
           if (UserOpt)
              ProcessUserOpt(UserOpt);
       } while (UserOpt);
    
       system("pause");
       return 0;   
    }
    
    /* ************************************************************************** */
    int main() {
        
        return runF00();
    }
    
    /* ************************************************************************** */
    void runF31(void)
    {
    	while (getchar()!='\n');
    	printf("\n\nthis action is not implemented\n");
    	printf("press any to return to the main menu\n");
    	while (getchar()!='\n');
    	return;
    }
    
    /* ************************************************************************** */
    void runF21(void)
    {
    	FILE *p_supply,*p_sys;
    	char Msg04[]="Error reading supply file\n", line[1000];
        char supplyname[20];
    	int itemnum=0, supmax,i, apoth_code;
    	struct Sys_Rec a; struct Sup_Rec d;
    
    	printf("Universal Exports Warehouse manager\n");
    	printf("NEW SUPPLY\n");
    	runF10();
    	printf("Enter filename:");
    	scanf("%s",supplyname);               /*getting the supply file name*/
    	p_supply=fopen(supplyname,"r");
    
    	if(p_supply==NULL){                  /*checking if it's empty*/
    		fprintf(stderr,Msg04);/*we type in stderr so that we can redirect*/
    		exit(4);              /*stdout if we want to*/
    	}
        /* open sys.dat to read the max items for a supply*/
    	p_sys=fopen("sys.dat","r");
    	while(!feof(p_sys)){
    		fscanf(p_sys,"%d %d %s %d %s %d %d %s",&a.Sys_WhNum,
    	   &a.Sys_ProdNum,a.Sys_WhFN,&a.Sys_StorVol,a.Sys_ProdFN,
               &a.Sys_SupMaxItems,&a.Sys_OrdMaxItems,a.Sys_VolDescr);
    	}
        supmax=a.Sys_SupMaxItems;
    	fclose(p_sys);
    
    	/*read the supply data file and store records in an array  named A*/
    	while(fgets(line, 998, p_supply)){
    		int ret;
    		ret=sscanf(line,"%d %s %d %d %d %d",&d.Sup_Code,d.Sup_Date,
    	    &d.Sup_Area,&d.Sup_Items,&d.Sup_ProdCode,&d.Sup_ProdUnit);
        	/*if(ret!=6)continue;*/
    	    A[itemnum]=d;
    	    itemnum++;
    	    if(itemnum==supmax)break;
    	}
    
    	fclose(p_supply);
    
        /* checking if every code in array we create exist as prodcode*/
    	/*calling runF22 for each record*/
    	for(i=0;i<itemnum;i++) runF22(A[i].Sup_ProdCode);
    
    	printf("Supply contains %d items\n",itemnum);
    
    	/*run F23 to find the proper wh_code or -1 not enough space*/
    	apoth_code=runF23(itemnum);
    
    
    	if(apoth_code==-1){   /*we have no more free space*/
    		printf("Not enough space to store order in any warehouse!");
    		return;
    	}
    
    	/* we put our supply to the proper warehouse */
    	runF24(apoth_code,itemnum);
    
    	return;  /*to main menu*/
    }
    
    
    /* ************************************************************************** */
    /* with this function we check the parametrs of the system*/
    void runF10(void)
    {
    	FILE *p_sys;
    	char Msg01[]="Error opening file sys.dat\n";
    	struct Sys_Rec a;
    
    	p_sys=fopen("sys.dat","r");
        if(p_sys==NULL){
    	   fprintf(stderr, Msg01);
    	   exit(1);
    	}
    
    	while(!feof(p_sys)){
    		fscanf(p_sys,"%d %d %s %d %s %d %d %s",&a.Sys_WhNum,
    	   &a.Sys_ProdNum,a.Sys_WhFN,&a.Sys_StorVol,a.Sys_ProdFN,
               &a.Sys_SupMaxItems,&a.Sys_OrdMaxItems,a.Sys_VolDescr);
    	}
    
    
    	runF11(a.Sys_ProdFN);  /*checking the product file*/
        runF12(a.Sys_WhFN);    /* checking the warehouse file*/
    	printf("Initialising.....\n");
    
    	fclose(p_sys);
    	return;               /*return F21 to continue prosecc*/
    }
    
    /* ************************************************************************** */
    /*checking the product file*/
    void runF11(char fname[])
    {
    	FILE *p_prod;
    	char Msg02[]="Error opening file product.dat\n", line[1000];
    	struct Prod_Rec b;
    
    	p_prod=fopen(fname,"r");
        if(p_prod==NULL){
    	   fprintf(stderr, Msg02);  /*if it's null*/
    	   exit(1);
    	}
    
    	fclose(p_prod);
    	return;             /*return to F10 to continue*/
    }
    
    /* ************************************************************************** */
    /*chcking the warehouse file if exists*/
    void runF12(char fname[])
    {
    	FILE *p_warehouse;
    	char Msg03[]="Error opening file warehouse.dat\n",line[1000];
    	struct WH_Rec  c;
    
    	p_warehouse=fopen(fname,"r");
        if(p_warehouse==NULL){
    	   fprintf(stderr, Msg03);
    	   exit(2);
    	}
    	fclose(p_warehouse);
    	return;           /*return to F10 to continue*/
    }
    
    /* ************************************************************************** */
    /*checking if the products in supplyfile correspond in real products*/
    void runF22(int N)
    {
    	int i=0,found, productnummax,productnum=0;
    	char line[1000],*f;
    	char Msg05[]="The product code doesn't exist";
    	struct Sys_Rec a;
    	struct Prod_Rec b, B[Prod_Items_max];
    	FILE *p_product, *p_sys;
    
    	/* we open sys.dat to find product max number*/
    	/* and products File name to open it*/
    	p_sys=fopen("sys.dat","r");
    	while(!feof(p_sys)){
    		fscanf(p_sys,"%d %d %s %d %s %d %d %s",&a.Sys_WhNum,
    	   &a.Sys_ProdNum,a.Sys_WhFN,&a.Sys_StorVol,a.Sys_ProdFN,
               &a.Sys_SupMaxItems,&a.Sys_OrdMaxItems,a.Sys_VolDescr);
    	}
    	productnummax=a.Sys_ProdNum;
    
    	f=a.Sys_ProdFN;
    	fclose(p_sys);
    	p_product=fopen(f,"r");
    
    	/* cheking if product file is empty*/
    	if(p_product==NULL){
    		printf("error reading product file");
    		exit(5);
    	}
    
    	/*open product file and create an array B to store objects*/
    	/* and find the number of product exists*/
    	while(fgets(line,998,p_product)){
    		int ret;
    		ret=sscanf(line,"%d %s %d %s", &b.Prod_Code,b.Prod_Descr,
    		&b.Prod_Vol,b.Prod_Storage_Option);
    
    		B[productnum]=b;               /*store in array B if line is Ok*/
    		productnum++;
    		if(productnum==productnummax) break;  /*don't read too many*/
    	}
    
    
    	/*checking if all items for import exist as product*/
    	i=0;found=0;
    	while(i<productnum && found==0){
    			if(B[i].Prod_Code==N)found=1;
    			i++;
    		}
    	if(found==0){
    		fprintf(stderr,Msg05); /*not real product for import*/
    	    exit(5);
    	}
    	else
    		return;   /*if the item exist to F21 to continue*/
    }
    
    /* ************************************************************************** */
    /*this function find the proper warehouse code to store the items*/
    int runF23(int itemnum)
    {
    	int whnummax,whnum=0,i,j,l,codeapoth,done=0,warehousecode;
    	char line[1000],*f;
    	struct Sys_Rec a;struct WH_Rec w;
    	FILE *p_product, *p_sys, *p_warehouse;
    
    	struct Prod_Rec b, B[Prod_Items_max];
    	int productnum=0,productnummax;
    
    	/* we open sys.dat to find warehouse max number*/
    	/* and warehouse File name to open it*/
    	p_sys=fopen("sys.dat","r");
    	while(!feof(p_sys)){
    		fscanf(p_sys,"%d %d %s %d %s %d %d %s",&a.Sys_WhNum,
    	   &a.Sys_ProdNum,a.Sys_WhFN,&a.Sys_StorVol,a.Sys_ProdFN,
               &a.Sys_SupMaxItems,&a.Sys_OrdMaxItems,a.Sys_VolDescr);
    	}
    	whnummax=a.Sys_WhNum;
    	f=a.Sys_WhFN;
        fclose(p_sys);
    	p_warehouse=fopen(f,"r");
    
    	/* cheking if product file is empty*/
    	if(p_warehouse==NULL){
    		printf("error reading warehouse file");
    		exit(6);
    	}
    	/*open warehouse file and create an array to store objects*/
    	/* and find the number of warehouse exists*/
    	while(fgets(line,998,p_warehouse)){
    		int ret;
    		ret=sscanf(line,"%d %d %d %d %d %d %d%d%d%d", &w.WH_Code,&w.WH_Area,
    		     &w.WH_Store1Total,&w.WH_Store1Free,&w.WH_Store2Total,
    			 &w.WH_Store2Free,&w.WH_ProximityList[0],&w.WH_ProximityList[1],
    			 &w.WH_ProximityList[2],&w.WH_ProximityList[3]);
    
    		W[whnum]=w;               /*store in array W if line is Ok*/
    		whnum++;
    		if(whnum==whnummax) break;  /*don't read too many*/
    	}
    
    	/*we now find the area_code=wh_code which our product */
    	/* are to be trafficked in - (our first preference) */
        int posapoth=-1;
    	for(i=0;i<whnummax;i++)
    		{
    			if(A[1].Sup_Area==W[i].WH_Code)
    				{
    					codeapoth=W[i].WH_Code;
    					posapoth=i;  /*position in array W*/
    				}
    		}
    
        /* we have our first code with proximity list stored in */
    	/* W[posapoth].WH_ProximityList[0...3]*/
        /* if after the first search our supply can't be stored*/
    	/* we try again with next warehouse in the list*/
    
    	/*open product file and recreate an array to store objects*/
    	/* and find the number of product exists*/
    	productnummax=a.Sys_ProdNum;
        f=a.Sys_ProdFN;
        p_product=fopen(f,"r");
    	while(fgets(line,998,p_product)){
    		int ret;
    		ret=sscanf(line,"%d %s %d %s", &b.Prod_Code,b.Prod_Descr,
    		&b.Prod_Vol,b.Prod_Storage_Option);
    
    		B[productnum]=b;               /*store in array B if line is Ok*/
    		productnum++;
    		if(productnum==productnummax) break;  /*don't read too many*/
    	}
        fclose(p_product);
    	/*calculate the total volument that our supply has*/
    	int sum_vol=0;
        for(i=0;i<itemnum;i++)
    		{
    			for(l=0;l<a.Sys_ProdNum;l++)
    				if(B[l].Prod_Code==A[i].Sup_ProdCode)
    					sum_vol=sum_vol+B[l].Prod_Vol*A[i].Sup_ProdUnit;
    		}
    
    
        /*checking our first preference warehousecode same area_code*/
    	/* with any enough free space that can store items*/
    	printf("Checking warehouse **...%d\n",W[posapoth].WH_Code);
        if(sum_vol<=(W[posapoth].WH_Store1Free+W[posapoth].WH_Store2Free)*a.Sys_StorVol)
    		{
    			warehousecode=W[posapoth].WH_Code;      /*our first choise */
    			printf("that's it\n");
    			done=1;
    		}
    
    	/*if our first preference fail try with next warehouse*/
    	/*    if more than one have free space according to   */
    	/*       WH_proximityList take the first we find      */
    	for(i=0;i<=(whnum-1) && done==0;i++){
    		codeapoth=W[posapoth].WH_ProximityList[i];
    		for(j=0;j<whnum && done==0;j++){
    			if(codeapoth==W[j].WH_Code){
    				printf("checking warehouse **....%d\n",codeapoth);
    				if(sum_vol<=(W[j].WH_Store1Free+W[j].WH_Store2Free)*a.Sys_StorVol){
    					printf("that's it\n");
    					done=1;
    					warehousecode=W[j].WH_Code;
    				}
    			}
    		}
    	}
    
        /*return to F21 to continue*/
    	if(done)
    		return warehousecode;  /*the proper warehouse code*/
    	else
    		return -1;  /*there is no free space anywhere*/
    }
    
    /* ************************************************************************** */
    /*Now we have a proper code to put our supply in*/
    void runF24(int apoth_code,int supnum)
    {
    	int  i,j,l,productcode,productunit,flag=0,shelfcategory;
    	int productnum=0,productnummax,prod_vol,stornum=0;
    	int whnummax,whnum=0,item_vol;
    	char line[1000],*f1,*f2,shelftype[2],*trnstype;
    	char Msg06[]="the import of products was successful\n";
    	struct Sys_Rec a;
    	struct WH_Rec w,W[WH_Items_max];
    	struct Store_Rec s, S[Stor_Items_max];
    	struct Prod_Rec b, B[Prod_Items_max];
    	struct Log_Rec L[Sup_Items_max];
    	FILE *p_product, *p_sys, *p_warehouse,*p_stor,*p_log;
    
    	/*open system.dat*/
    	p_sys=fopen("sys.dat","r");
    	while(!feof(p_sys)){
    		fscanf(p_sys,"%d %d %s %d %s %d %d %s",&a.Sys_WhNum,
    	   &a.Sys_ProdNum,a.Sys_WhFN,&a.Sys_StorVol,a.Sys_ProdFN,
               &a.Sys_SupMaxItems,&a.Sys_OrdMaxItems,a.Sys_VolDescr);
    	}
        productnummax=a.Sys_ProdNum;
        whnummax=a.Sys_WhNum;
    
    	/*open warehouse.dat recreate an array-find the num warehouses*/
    	f1=a.Sys_WhFN;
        p_warehouse=fopen(f1,"r");
    	while(fgets(line,998,p_warehouse)){
    		int ret;
    		ret=sscanf(line,"%d %d %d %d %d %d %d%d%d%d", &w.WH_Code,&w.WH_Area,
    		     &w.WH_Store1Total,&w.WH_Store1Free,&w.WH_Store2Total,
    			 &w.WH_Store2Free,&w.WH_ProximityList[0],&w.WH_ProximityList[1],
    			 &w.WH_ProximityList[2],&w.WH_ProximityList[3]);
    
    		W[whnum]=w;               /*store in array W if line is Ok*/
    		whnum++;
    		if(whnum==whnummax) break;  /*don't read too many*/
    	}
        fclose(p_warehouse);
    
    	/* open prodact.dat recreate array B*/
    	f2=a.Sys_ProdFN;
    	p_product=fopen(f2,"r");
        while(fgets(line,998,p_product)){
    		int ret;
    		ret=sscanf(line,"%d %s %d %s", &b.Prod_Code,b.Prod_Descr,
    		&b.Prod_Vol,b.Prod_Storage_Option);
    		B[productnum]=b;
    		productnum++;
    		if(productnum==productnummax) break;
    	}
        fclose(p_product);
    
    	/*open storage.dat create array S for items that exist*/
    	p_stor=fopen("storage.dat","r");
    	while(fgets(line,998,p_stor)){
    		int ret;
    		ret=sscanf(line,"%d %d %d %d %d", &s.Store_WHCode,&s.Store_ShelfCode,
    		     &s.Store_ProdCode,&s.Store_ShelfType,&s.Store_Quantity);
            S[stornum]=s;
    		stornum++;
    		if(stornum==Stor_Items_max) break;
    	}
        fclose(p_stor);
        fclose(p_sys);
    
        /* for every one of the products that our supply has, do*/
    	for(i=0;i<supnum;i++){
    		productcode=A[i].Sup_ProdCode;
    		productunit=A[i].Sup_ProdUnit;
    
    
    
    		/*calculate total product volument and product type	*/
    		/* WHEN WE IMPORT A PRODUCT ALL QUANTITY STORED TOGETHER*/
    		for(l=0;l<productnum;l++){
    			if(B[l].Prod_Code==productcode){
    				shelftype[0]=B[l].Prod_Storage_Option[0];
    				prod_vol=productunit*B[l].Prod_Vol;
    				item_vol=B[l].Prod_Vol;
    			}
    		}
    
            /* for all items in storage if product exist in the warehouse*/
    		/* and the shelf has enough space*/
    		flag=0;
    		for(j=0;j<stornum && flag==0;j++){
    			if(apoth_code==S[j].Store_WHCode && S[j].Store_ProdCode==productcode){
    				if(prod_vol<=(a.Sys_StorVol-S[j].Store_Quantity*item_vol)){
    					S[j].Store_Quantity += productunit;   /*increase the quantity*/
    					/*shelfcategory=S[j].Store_ShelfType;*/
    					flag=1;
    				}
    			}
    		}
    
    		if(flag==1){   /*we already have a shelf with the product and enough space*/
    			p_stor=fopen("storage.dat","w");
    			for(j=0;j<stornum;j++){
    				fprintf(p_stor,"%d %d %d %d %d\n", S[j].Store_WHCode,S[j].Store_ShelfCode,
    		     S[j].Store_ProdCode,S[j].Store_ShelfType,S[j].Store_Quantity);
    			}
    			fclose(p_stor);
    		}
    		else{     /*we put the product in a new position*/
    			/* we make a new warehouse file with the new data*/
    			for(j=0;j<whnum;j++){
    				if(W[j].WH_Code==apoth_code){
    					if(shelftype=="L"){/*the L only be puted in type1*/
    						W[j].WH_Store1Total++;
    						W[j].WH_Store1Free--;
    					}
    					else{
    						if(W[j].WH_Store2Free==0){/*if we don't have any type2free*/
    							W[j].WH_Store1Total++;
    							W[j].WH_Store1Free--;
    						}
    						else{
    							W[j].WH_Store2Total++;
    						    W[j].WH_Store2Free--;
    						}
    					}
    				}
    			}
    			/* we write in warehouse file the new elements*/
    			f1=a.Sys_WhFN;
                p_warehouse=fopen(f1,"w");
    	        for(l=0;l<whnum;l++){
    				fprintf(p_warehouse,"%d %d %d %d %d %d %d %d %d %d\n",
    			 W[l].WH_Code,W[l].WH_Area,
    		     W[l].WH_Store1Total,W[l].WH_Store1Free,W[l].WH_Store2Total,
    			 W[l].WH_Store2Free,W[l].WH_ProximityList[0],W[l].WH_ProximityList[1],
    			 W[l].WH_ProximityList[2],W[l].WH_ProximityList[3]);
    			}
    			fclose(p_warehouse);
    			/*we put a new record in storage */
    			p_stor=fopen("storage.dat","w");
    			for(l=0;l<stornum;l++){
    				fprintf(p_stor,"%d %d %d %d %d\n", S[l].Store_WHCode,S[l].Store_ShelfCode,
    		     S[l].Store_ProdCode,S[l].Store_ShelfType,S[l].Store_Quantity);
    			}
                fprintf(p_stor,"%d %d %d %d %d", apoth_code,stornum+1,productcode,
    		    1,productunit);
    			fclose(p_stor);
    		}
    	}
    
    	/*we create the log FILE and a mesage for user*/
    	p_log=fopen("log.dat","w");
    	for(i=0;i<supnum;i++){
    		L[i].Log_WH=apoth_code;
    		L[i].Log_Item=A[i].Sup_ProdCode;
    		L[i].Log_TransactionCode=1;  /*1=import an item*/
    		L[i].Log_Quantity=A[i].Sup_ProdUnit;
    		L[i].Log_Status=1;           /*1 = everything be done*/
    	}
    	for(i=0;i<supnum;i++){
    		fprintf(p_log,"%s %d %d %s %d %d %d\n",A[i].Sup_Date,L[i].Log_WH,
    		L[i].Log_Item, "import",L[i].Log_TransactionCode,
    		L[i].Log_Quantity,L[i].Log_Status);
    	}
    	fclose(p_log);
    
        fprintf(stderr,Msg06);   /*messege to the user with the results*/
    	printf("The products were imported in warehouse :%d\n",apoth_code);
    
    	return;
    }
    Can someone give me numbers as import for which this code will work and for which this code will not work. The same for export

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by newuser21
    Can someone give me numbers as import for which this code will work and for which this code will not work. The same for export
    why don't you just make up your own data set? IF you wrote that code then you should be able to create a test dataset for it.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    I did not wrote the code

    I did not wrote the code. Who told you that

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by newuser21
    I did not wrote the code. Who told you that
    Nobody told me that -- we assume what you post is what you wrote, otherwise why would you post it. I doubt anyone will read or study it because it isn't work our time or effort.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. plz help me make my assigment more bulletproof
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 03-28-2008, 09:03 PM
  2. Emergency help for the problem !!!
    By chelp151 in forum C Programming
    Replies: 7
    Last Post: 04-22-2007, 12:09 PM
  3. Emergency
    By taha54 in forum C Programming
    Replies: 8
    Last Post: 03-17-2006, 08:10 AM
  4. Please help me with this !! Emergency
    By newuser21 in forum C Programming
    Replies: 1
    Last Post: 03-12-2006, 07:22 AM
  5. another novice need some emergency help
    By KarateKid in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2001, 11:09 PM