Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/**************************************************************/
/*Declaration of a Structure of Type PART*/
typedef struct {
int ID;
char desc[50];
int quant;
float price;
}PART;
/*************************************************************/
PART part[1000];
int count=0;
int parts_sold=0;
float total=0;
FILE*in;
/******************************************************************************/
/*Declaration of Functions*/
void insertinorder(int);
void getdescription(char[]);
void addnewpart();
void changeprice();
void addquant();
void sellpart();
void printparts();
int binarysearch(int);
/******************************************************************************/
/*
The Main Function Opens the file "parts.dat" and reads the required data
And assigns the data to the Structure based on its Field.
It Then Calls The Various Funtions Based On The "Code" Received.
*/
int main ()
{
int tempid,execute=1;
char word[100],ch;
char chs[3];
in=fopen("parts.dat","r");
if (in==NULL){
printf("Error!!!File \"parts.dat\" Not Found\n");
system("pause");
return 1;
}
fscanf(in,"%d",&tempid);
while(tempid!=0)
{
insertinorder(tempid);
count++;
fscanf(in,"%d",&tempid);
}
while (execute==1)
{
ch=getc(in);
while (ch == ' ')
ch=getc(in);
chs[0]=getc(in);
if (chs[0] != EOF)
{
chs[1]=getc(in);
chs[2]='\0';
if (strcmp(chs,"ap")==0)
addnewpart();
else if (strcmp(chs,"sp")==0)
sellpart();
else if (strcmp(chs,"cp")==0)
changeprice();
else if (strcmp(chs,"cq")==0)
addquant();
else if (strcmp(chs,"ls")==0)
printparts();
}
else
execute=0;
}
printf("\nThe Total Number Of Parts Sold Was:%d\n",parts_sold);
printf("The Total Sales Amounted to:$%5.2f\n",total);
printf("\n");
fclose(in);
system("pause");
return 0;
}
/******************************************************************************/
/*
The Following Function Is Responsible For Inserting A Part In Order.
*/
void insertinorder(int tempid)
{
int k=count-1;
while (k>=0 && tempid<part[k].ID){
part[k+1]=part[k];
k--;
}
part[k+1].ID=tempid;
getdescription(part[k+1].desc);
fscanf(in,"%d",&part[k+1].quant);
fscanf(in,"%f",&part[k+1].price);
}
/******************************************************************************/
/*
The Following Function Is Responsible For Capturing The Description
Whilst Rejecting The Character "*" Between Which The Description Is
Encased.
*/
void getdescription(char word[])
{
char c;
int n=0;
while ((c=getc(in))== ' ' && c!='*') ;
c=getc(in);
word[n]=c;
while ((c=getc(in))!='*')
{
n++;
word[n]=c;
}
word[++n]='\0';
}
/******************************************************************************/
/*
The Following Function Adds A New Part To The List Of Existing Parts
In Order.
*/
void addnewpart()
{
int tempid;
fscanf(in,"%d",&tempid);
insertinorder(tempid);
count++;
}
/*****************************************************************************/
/*
The Following Function Updates The Price Field Of A Part
*/
void changeprice()
{
int id,ID;
float newprice;
fscanf(in,"%d",&id);
fscanf(in,"%f",&newprice);
ID=binarysearch(id);
if (ID==-1)
printf("\nThe Part %d Price Cannot Be Altered. Part Entry Not Found!\n",id);
else
{
part[ID].price=newprice;
}
}
/*****************************************************************************/
/*
The Following Function Updates The Quantity Field Of
A Part(Adds To The Quantity.)
*/
void addquant()
{
int id,ID,quant;
fscanf(in,"%d",&id);
fscanf(in,"%d",&quant);
ID=binarysearch(id);
if (ID==-1)
printf("\nThe Part %d Quantity Cannot be Added. Part Entry Not Found!\n",id);
else
{
part[ID].quant=part[ID].quant+quant;
}
}
/*****************************************************************************/
/*
The Following Function Checks If There Is Sufficient
Quantity Of A Part To Sell, Then Modifies The Quantity
After Subtracting The Amount To Be Sold.
*/
void sellpart()
{
int id,ID,quant;
fscanf(in,"%d",&id);
fscanf(in,"%d",&quant);
ID=binarysearch(id);
if (ID==-1)
printf("\nThe Part %d Cannot Be Sold. Part Entry Not Found!\n",id);
else
{
if(part[ID].quant<quant)
printf("\nThe Part %d Cannot be Sold.Not Enough Stock!\n",id);
else
{
part[ID].quant=part[ID].quant-quant;
parts_sold=parts_sold+quant;
total=total+(part[ID].price * quant);
}
}
}
/*****************************************************************************/
/*
When Called, This Function Prints The Entire List Of Parts
Along With The Respective Description,Price And Quantity.
*/
void printparts()
{
int i;
printf("\nPart ID.\tDescription\t\tCurrent Quantity\tPrice\n\n");
for (i=0; i<count; i++)
printf("%d\t\t%-30s\t%d\t\t%5.2f\n",part[i].ID, part[i].desc, part[i].quant, part[i].price);
}
/*****************************************************************************/
/*
This Function Searches For A Given Key.It Returns The Location Of
The Key If Found and -1 If It Isn't Found.
*/
int binarysearch(int id)
{
int mid,lo=0,hi=count;
while(lo<=hi)
{
mid=(lo+hi)/2;
if(part[mid].ID==id)
return mid;
else if(id<part[mid].ID)
hi=mid-1;
else
lo=mid+1;
}
return -1;
}
/*****************************************************************************/