(a) Declare suitable C datatypes to represent each of the following sets of info
ID: 3905319 • Letter: #
Question
(a) Declare suitable C datatypes to represent each of the following sets of informa-
tion (as you would do in a header file):
(i) A company, described by:
• A 3-letter code.
• The current share price (a positive real number).
• The total number of shares (a positive integer).
• Total asset value (a positive real number).
• Total debts (a positive real number).
ii) A consortium of companies, including:
• A set of core member companies.
• A set of associate member companies.
FILE fopen (const char *path, const char *mode); int fclose (FILE fp); int fscanf (FILE *stream, const char *format, char *fgets(char *s, int size, FILE *stream); int feof (FILE stream); int strcmp (const char *s1, const char *s2); char *strcpy (char *dest, const char src);Explanation / Answer
here is your answer : ------------>>>>>>>>
#include<stdio.h>
#include<string.h>
/* Answer to Question A */
typedef struct Company_{
char code[4];
double currentSharePrice;
unsigned int totalNumberOfShares;
double totalAssets;
double totalDebts;
}Company;
typedef struct Consortium_{
Company *core;
//for keep track of core company
int numCore;
Company *ass;
//for keep track of Associate Company
int numAss;
}Consortium;
/* End of question A */
Consortium* readConsortium(char *file){
FILE *fp;
fp = fopen(file,"r");
if(fp == NULL){
return NULL;
}
Consortium *res = malloc(sizeof(Consortium));
int nCore,nAss;
fscanf(fp,"%d,%d",&nCore,&nAss);
res->core = malloc(sizeof(Company)*nCore);
res->ass = malloc(sizeof(Company)*nAss);
res->numCore = nCore;
res->numAss = nAss;
int i;
for(i = 0;i<nCore;i++){
if(feof(fp)){
return NULL;
}
fscanf(fp,"%s%lf%d%lf%lf",&res->core[i].code,&res->core[i].currentSharePrice,&res->core[i].totalNumberOfShares,&res->core[i].totalAssets,&res->core[i].totalDebts);
}
for(i = 0;i<nAss;i++){
if(feof(fp)){
return NULL;
}
fscanf(fp,"%s%lf%d%lf%lf",&res->ass[i].code,&res->ass[i].currentSharePrice,&res->ass[i].totalNumberOfShares,&res->ass[i].totalAssets,&res->ass[i].totalDebts);
}
return res;
}
void writeNetWorth(char *file,Consortium *cons){
FILE *fp;
fp = fopen(file,"w");
if(fp == NULL){
return;
}
double net = 0.0;
int i;
for(i = 0;i<cons->numCore;i++){
net = (cons->core[i].currentSharePrice * cons->core[i].totalNumberOfShares);
net += cons->core[i].totalAssets;
net -= cons->core[i].totalDebts;
fprintf(fp," %s : %12.2lf",cons->core[i].code,net);
}
for(i = 0;i<cons->numAss;i++){
net = (cons->ass[i].currentSharePrice * cons->ass[i].totalNumberOfShares);
net += cons->ass[i].totalAssets;
net -= cons->ass[i].totalDebts;
fprintf(fp," %s : %12.2lf",cons->ass[i].code,net);
}
}
/*
int main(){
char inFile[100];
char outFile[100];
printf("Enter Input File Name : ");
scanf("%s",&inFile);
printf(" Enter Output File Name : ");
scanf("%s",&outFile);
Consortium *cons = readConsortium(inFile);
if(cons == NULL){
printf("Error Reading File");
return -1;
}
writeNetWorth(outFile,cons);
printf("output written to file ");
free(cons->core);
free(cons->ass);
free(cons);
return 0;
}
*/
int main(int argc,char *argv[]){
if(argc < 2){
printf("please give the input arguments");
return -1;
}
int i;
char inFile[100];
char outFile[100];
Consortium *cons;
for(i = 1;i<argc;i++){
strcpy(inFile,argv[i]);
strcpy(outFile,argv[i]);
strcat(outFile,".out");
cons = readConsortium(inFile);
if(cons == NULL){
printf("Error Reading File");
return -1;
}
writeNetWorth(outFile,cons);
printf("output written to file ");
free(cons->core);
free(cons->ass);
free(cons);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.