Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Convert the C program to C++. Replace ALL of the calls to the standard C I/O functions printf(), scanf(), fprintf() and fscanf() to the

1.Convert the C program to C++. Replace ALL of the calls to the standard C I/O functions printf(), scanf(), fprintf() and fscanf() to the C++ standard cin, cout, and fstream operators. Your C++ program can not include ANY calls to printf(), scanf(), fprintf() or fscanf() and can reference only the C++ header files, not any of the standard C header files. You must also remove any other C-specific syntax and replace it with C++ syntax. For example, all of the #define statements should be replaced by C++ consts, all of the character arrays should be replace with C++ strings, etc

2.

image text in transcribed

hw1_functions file

#include

#include

#include "hw1.h"

/* function to swap two part structures - passed by reference */

swap_parts(struct part *part_a, struct part *part_b)

{

struct part temp_part;

temp_part.part_no = part_a->part_no;

temp_part.price = part_a->price;

strcpy(temp_part.description,part_a->description);

temp_part.source.part_no = part_a->source.part_no;

strcpy(temp_part.source.dealer,part_a->source.dealer);

part_a->part_no = part_b->part_no;

part_a->price = part_b->price;

strcpy(part_a->description,part_b->description);

part_a->source.part_no = part_b->source.part_no;

strcpy(part_a->source.dealer,part_b->source.dealer);

part_b->part_no = temp_part.part_no;

part_b->price = temp_part.price;

strcpy(part_b->description,temp_part.description);

part_b->source.part_no = temp_part.source.part_no;

strcpy(part_b->source.dealer,temp_part.source.dealer);

}

print_catalog(struct part list[], int num_parts)

{

int i;

printf("\tCatalog ");

printf("Part no.\tDescription\tPrice ");

printf("---------------------------------------- ");

for(i=0;i

printf("%d\t%s\t%.2f ",list[i].part_no, list[i].description,

list[i].price);

printf(" ");

}

print_full_catalog(struct part list[], int num_parts)

{

int i;

printf("\tFull Catalog including supplier ");

printf("Part no.\tDescription\tPrice\tSupplier\tManu. Part No. ");

printf("------------------------------------------------------------------------- ");

for(i=0;i

printf("%d\t%s\t%.2f\t%s\t\t%d ",list[i].part_no, list[i].description,

list[i].price, list[i].source.dealer, list[i].source.part_no);

printf(" ");

}

sort_catalog(struct part list[], int num_parts, int price_or_parts)

{

int i,j, tempx,tempy, size;

int smallest, smallest_index;

float cheapest;

struct part temp_part;

if(price_or_parts == BY_PARTS) {

/* insertion sort */

/* outer loop - go through loop once */

for(i=0;i

/* inner loop - go through from i until end */

/* find smallest element in that section */

smallest = list[i].part_no;

smallest_index = i;

for(j=i;j

if(list[j].part_no

smallest = list[j].part_no;

smallest_index = j;

}

/* found smallest, swap */

swap_parts(&list[i], &list[smallest_index]);

} /* end for */

} /* end if */

else if(price_or_parts == BY_PRICE) {

/* insertion sort */

/* outer loop - go through loop once */

for(i=0;i

/* inner loop - go through from i until end */

/* find smallest element in that section */

cheapest = list[i].price;

smallest_index = i;

for(j=i;j

if(list[j].price

cheapest = list[j].price;

smallest_index = j;

}

/* found smallest, swap */

swap_parts(&list[i], &list[smallest_index]);

} /* end for */

} /* end else if */

}

---------------hw1.h file ------------------------------

#define DESC_SIZE 40 // maximum size of part description

#define NO_OF_PARTS 10 // maximum number of parts in our extensive catalog

#define MAXCHAR 256 // maximum characters in the file name

#define BY_PRICE 2

#define BY_PARTS 3

#ifndef EX1_H

#define EX1_H

struct supplier {

int part_no;

char dealer[DESC_SIZE];

};

struct part {

int part_no;

float price;

char description[DESC_SIZE];

struct supplier source;

};

#endif

--------Hw1.c file---------

#include

#include

#include "hw1.h"

void print_full_catalog(struct part list[], int num_parts);

void print_catalog(struct part list[], int num_parts);

void sort_catalog(struct part list[], int num_parts, int price_or_parts);

void main(void)

{

FILE *infile = NULL;

char infilename[MAXCHAR];

char line[MAXCHAR];

struct part part_list[NO_OF_PARTS];

int num_of_parts, i = 0;

printf("Program to read in a part catalog from a file ");

/* input file name to read and try and open it */

while(infile == NULL) {

printf("Input catalog filename: ");

scanf("%s",infilename);

printf(" ");

if((infile = fopen(infilename, "r")) == NULL) {

printf("ERROR: file %s can not be opened! ",infilename);

}

}

while(fscanf(infile,"%d %s %f %s %d ", &part_list[i].part_no, part_list[i].description,

&part_list[i].price, part_list[i].source.dealer,

&part_list[i].source.part_no) != EOF) {

i++;

if(i > NO_OF_PARTS) {

printf("ERROR, number of parts in catalog is greater than list size! ");

exit(0);

}

}

num_of_parts = i;

printf("\tOriginal Catalog ");

print_full_catalog(part_list, num_of_parts); // print as read

sort_catalog(part_list, num_of_parts, BY_PARTS); // sort by part number

printf("\tCatalog sorted by Parts ");

print_full_catalog(part_list, num_of_parts); // print sorted by part number

sort_catalog(part_list, num_of_parts, BY_PRICE); // sort by price

printf("\tCatalog sorted by Price ");

print_full_catalog(part_list, num_of_parts); // print sorted by price number

} /* end main */

-----catalog.txt file-----

1254 Retro_Encabulator 24500.99 Digikey 24530 1342 Panametric_Fam___ 234.99 Digikey 659832 2349 Cardinal_Gram_Meter 3400.95 Newark 00231839 453 Sperving_Bearing_ 0.02 Digikey 23453 48753 Hydro_Coptic_M_Vane 12.75 Walmart 489274 83746 Lunar_Wayne_Shaft 34.55 Newark 00345698

Once you have converted the program to C++, modify the program to implement the following features (using C++ features and syntax): 1. Modify the program to take command line arguments using argc and argv[] to input the name of the input file the command line. If the user runs the program incorrectly from the command line by failing to enter the filename, OR enters a first argument of /?, the program should print the following message: Program to keep a list of parts from a catalog. Usage: [input_file_name] Where comes from argv[0]. If the user enters the name of an input file that cannot be opened correctly for reading, the program should print out the following message and exit: ERROR: file cannot be opened! Usage: [input_file_name] Where is the name of the input file that the user input on the command line. 2. Modify the program to add a function to sort the list of parts by manufacturer part number. 3. Modify the main routine to implement a main menu to allow the user to select the operation you want the program to perform. The main menu should print out like below: Enter option: (0) Sort and print the Catalog by part number (1) Sort and print the Catalog by price (2) Sort and print the Catalog by manufacturer's part number (3) Exit the program Choice ? The program should perform the requested operation and then print out the menu again. This process should repeat until the user selects option 3 to exit the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

Students also viewed these Databases questions

Question

Be familiar with the basic ways to manage capacity.

Answered: 1 week ago