Design, implement and test a Ct+ program that reads a series of data records fro
ID: 3876072 • Letter: D
Question
Design, implement and test a Ct+ program that reads a series of data records from a file and stores each record in a linked list (in ascending order by items number). After creating the linked list the program should traverse the list and print the report below. Record Description: ltem_ Number //unsigned integer Item_category // alpha_numeric ...4 chars ex. ELEC Item_Purchase Date II mm dd yyyy Item Cost Item Count Items SOLD // Float Il integer // integer Inventory Report Date: Jan. 16, 2018 Item Cost 200.50 100.00 Item Purchase Items e Item Item Number 232 345 Date Jan. 1, 2012 Dec. 1, 2016 Sold Age Revenue 6 201.00 2 $500.00 Total revenue 701.00 Age to the nearest year 2Explanation / Answer
data.txt : ------------------>>>>>>>>>>>>.
232 ELEC 01 01 2012 100.50 34 2
345 ELEC 12 01 2016 100.01 12 5
program : --------------->>>>>>>>>>>>.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct Date{
int mm;
int dd;
int yy;
};
typedef struct Date date;
struct Item{
unsigned int item_no;
char item_cat[5];
date purchase_date;
float cost;
int count;
int sold;
struct Item *next;
};
typedef struct Item item;
string month_name[12] = {"jan.","feb.","mar.","apr.","may","jun.","jul.","aug.","sep.","oct.","nov.","dec."};
item* addNode(item *head,item *data,int count){
item *temp = head;
item *prev = NULL;
if(head == NULL){
head = data;
return head;
}
for(int i = 0;i<count;i++){
if(temp->item_no >= data->item_no){
if(prev == NULL){
data->next = temp;
head = data;
return head;
}
else{
data->next = prev->next;
prev->next = data;
return head;
}
}
prev = temp;
temp = temp->next;
}
prev->next = data;
return head;
}
void displayAllNode(item *head,int count){
cout<<" Item Item purchase Item Items Item Item ";
cout<<" Number Date Cost Sold Age Revenue ";
item *temp = head;
for(int i = 0;i<count;i++){
cout<<" "<<temp->item_no<<" "<<month_name[temp->purchase_date.mm - 1]
<<" "<<temp->purchase_date.dd<<","<<temp->purchase_date.yy<<" $"<<temp->cost
<<" "<<temp->sold<<" "<<2018-temp->purchase_date.yy<<" "<<temp->cost*temp->sold<<" ";
temp = temp->next;
}
}
item* loadFile(item *head,int &count){
ifstream file;
file.open("data.txt");
if(file.is_open()){
while(!file.eof()){
item *temp = new item;
temp->next = NULL;
file>>temp->item_no>>temp->item_cat>>temp->purchase_date.mm>>temp->purchase_date.dd>>temp->purchase_date.yy;
file>>temp->cost>>temp->count>>temp->sold;
head = addNode(head,temp,count);
count = count + 1;
}
count--;
file.close();
return head;
}
else{
cout<"check the data.txt file";
file.close();
return NULL;
}
}
int main(){
item *head = NULL;
int count = 0;
head = loadFile(head,count);
displayAllNode(head,count);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.