Patrick B. answered 05/24/21
Math and computer tutor/teacher
int WritePhoneFile(TPhoneDB phoneDB, char * filename)
{
int iReturn=0;
int iLoop;
char outbuff[255];
FILE * fptr = fopen(filename,"w");
if (fptr!=NULL)
{
fprintf(fptr,"%d %d \n",phoneDB->count,phoneDB->phoneSeqNum);
for (iLoop=0; iLoop<phoneDB->count; iLoop++)
{
SmartPhone_SerializeToCSV(&phoneDB->phones[iLoop],outbuff);
fprintf(fptr,outbuff);
}
}
else
{
iReturn=-1;
}
return(iReturn);
}
void getHighestPricePhone ( SmartPhoneType mobileList[], int size, SmartPhoneType *highestPricePhone)
{
int indexOfMaxPos=0;
int maxPrice=mobileList[0].price;
int iLoop;
for (iLoop=0; iLoop<size; iLoop++)
{
if (mobileList[iLoop].price>maxPrice)
{
indexOfMaxPos = iLoop;
maxPrice = mobileList[iLoop].price;
}
}
memcpy(highestPricePhone,&mobileList[indexOfMaxPos],SMART_PHONE_SIZE);
}
void SmartPhone_Input(TSmartPhone phone)
{
printf(" Please input the phone name :>");
scanf("%s",phone->phoneName);
printf(" Please input the phone price :>");
scanf("%d",&phone->price);
printf("Please input the ram memory :>");
scanf("%d",&phone->ramMemory);
printf("Please input the cpu speed :>");
scanf("%f",&phone->cpuSpeed);
printf("Please input the gpu memory :>");
scanf("%d",&phone->gpuMemory);
printf("Please input the username :>");
scanf("%s",phone->userName);
printf(" Please input the # :>");
scanf("%d",&phone->userPhoneNo);
printf(" Please input the country :>");
scanf("%s",phone->userCountry);
}
void My5Phones(TPhoneDB phoneDB)
{
int iLoop=0;
phoneDB->count=phoneDB->phoneSeqNum=5;
phoneDB->phones=(TSmartPhone)malloc(5*SMART_PHONE_SIZE);
for (iLoop=0; iLoop<5; iLoop++)
{
phoneDB->phones[iLoop].phoneID=(iLoop+1);
SmartPhone_Input(&phoneDB->phones[iLoop]);
SmartPhone_Output(&phoneDB->phones[iLoop]);
}
WritePhoneFile(phoneDB,"E:\\mobileInfo.txt");
}
void countryWisePhone( SmartPhoneType mobileList[], int size, char *country)
{
int iLoop;
for (iLoop=0; iLoop<size; iLoop++)
{
if (strcmp(mobileList[iLoop].userCountry,country)==0)
{
SmartPhone_Output(&mobileList[iLoop]);
printf("\n");
}
}
}
void deletePhone(SmartPhoneType mobileList[], int size, int phoneID)
{
int iIndexPos=-1;
int iLoop=0;
//locates the phone rec with the specified id
for (iLoop=0; iLoop<size; iLoop++)
{
if (mobileList[iLoop].phoneID == phoneID)
{
iIndexPos=iLoop; //found it!!!
break;
}
}
if (iIndexPos>-1) //it is there
{
//moves each phone rec one position to the left, clobbering the target record
for (iLoop=iIndexPos; iLoop<size-1; iLoop++)
{
memcpy(&mobileList[iLoop],&mobileList[iLoop+1],SMART_PHONE_SIZE);
}
memset(&mobileList[size-1],0,SMART_PHONE_SIZE); //invalidates the last record
}
size--;
/* NEXT, you have to write this array containing size-1 records to the file, whose name is
neither specified, nor passed */
}