Im studying for a midterm and am trying to create a program in C + that diminstr
ID: 3733457 • Letter: I
Question
Im studying for a midterm and am trying to create a program in C + that diminstrates file organization, the structure ADT, and function arguments with structure. here are the parameters.
a. Create a text file of 15+ products including product Names, unit
prices, quantities sold, and sales of each product in your computer file. Then input the file into an array of structure ADT to your program.
b. You need to print out the unsorted file.
c. Sort the above data by sorting on the product Names. You may use either bubble sort or selection sort in ascending or descending order on product Names. The arguments of call function must include arrays of structure.
d. You may use C-String function Strcmp (,) to compare two product Names and use Strcpy (,) together to do swapping for sorting.
e. In the program algorithm, you must use “->” structure pointer operator.
f. Print out the sorted array: product Names, unit prices, quantities sold, and the sales of each product including the total units sold and the total sales at the bottom of the sorted data..
Please include output so that i can study it thanks you
Explanation / Answer
/*INPUT FILE:
product price(per1) quantitiesSold sales of product(sales of product = quantities sold * price per unit)
kite 3 8 24
map 2 6 12
ball 5 3 15
lamp 23 5 115
doll 30 16 480
fan 50 5 250
car 100 2 200
gun 120 2 240
jug 11 10 110
orange 11 20 220
pen 1 30 30
stand 25 3 75
apple 10 5 50
hat 2 101 202
ice 3 22 66
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
typedef struct{
string name;
double price,sold,sales;
}ADT;
vector<ADT> ADTarr;
void read_file(){
ifstream data;
string line,word;
data.open("store.txt",ios::in);
string name,price,sold,sales;
getline(data,line);
while(getline(data,line)){
stringstream strStrm(line);
ADT adt;
if(strStrm>>adt.name){
if(strStrm>>adt.price){
if(strStrm>>adt.sold){
if(strStrm>>adt.sales){
ADTarr.push_back(adt);
}
}
}
}
}
}
void write_file(){
for(int i = 0; i<ADTarr.size();++i){
cout<<ADTarr[i].name<<" "<<ADTarr[i].price<<" "<<ADTarr[i].sold<<" "<<ADTarr[i].sales<<endl;
}
}
void sort(){
int i, j;
for (i = 0; i < ADTarr.size()-1; i++)
for (j = 0; j < ADTarr.size()-i-1; j++)
if ( ADTarr[j].name.compare(ADTarr[j+1].name) >0 ){
ADT temp = ADTarr[j];
ADTarr[j] = ADTarr[j+1];
ADTarr[j+1] = temp;
}
}
void display_total(){
double priceTotal = 0.00,soldTotal= 0.00,salesTotal= 0.00;
for(int i = 0; i<ADTarr.size();++i){
priceTotal += ADTarr[i].price;
soldTotal+=ADTarr[i].sold;
salesTotal +=ADTarr[i].sales;
}
cout<<"Total price = "<<priceTotal<<endl;
cout<<"Total sold items = "<<soldTotal<<endl;
cout<<"Total sales items = "<<salesTotal<<endl;
}
int main(int argc, char** argv) {
read_file();
write_file();
sort();
cout<<endl<<endl;
write_file();
display_total();
return 0;
}
/*OUTPUT:
kite 3 8 24
map 2 6 12
ball 5 3 15
lamp 23 5 115
doll 30 16 480
fan 50 5 250
car 100 2 200
gun 120 2 240
jug 11 10 110
orange 11 20 220
pen 1 30 30
stand 25 3 75
apple 10 5 50
hat 2 101 202
ice 3 22 66
apple 10 5 50
ball 5 3 15
car 100 2 200
doll 30 16 480
fan 50 5 250
gun 120 2 240
hat 2 101 202
ice 3 22 66
jug 11 10 110
kite 3 8 24
lamp 23 5 115
map 2 6 12
orange 11 20 220
pen 1 30 30
stand 25 3 75
Total price = 396
Total sold items = 238
Total sales items = 2089
*/