You\'re given the following files: stock.h defines the stock class stockDB.h def
ID: 647467 • Letter: Y
Question
You're given the following files:
stock.h defines the stock class
stockDB.h defines the collection of stock in doubly-linked lis
main.cpp
PE2stock.txt has a list of stocks
Without changing the above files, your job is to add all necessary files to make the main.cpp run and produce expected results as described in main.cpp
Submit screenshot of output, source code, and brief description of any changes you made to the original 4 files (if you use the code as is, say so).
hint:
1. note there is no length/size field in stockDB.
2. if some functions are not needed, you do not need to implement them.
3. minimum code is a good thing for this type of test.
4. if you have to modify the given files to make it work, note it in your submission of all the changes and why.
/*=================main.cpp==========================
#include
#include "stockDB.h"
using namespace std;
int main()
{
cout << "My name is Luis Liang, last 4 digits of id is 0166 ";
stockDB port1, port2;
port1.load(); // load PE2stock.txt
port1.print();
stockNode *sp;
sp = port1.find_middle();
cout << "middle is " << *sp << endl;
port1.delete("FRANK");
port1.recursive_print_rev(); // use recursive method to do reverse print
}
====================stockDB.h=========================
#ifndef STOCKDB_H
#define STOCKDB_H
#include
#include
#include "stock.h"
#define stockNode stockNodeIsA
using namespace std;
class stockDB
{
public:
stockDB();
bool load(); // load db from file PE2stock.txt
void print(); // print out all stocks in DB
void recursive_print_rev(); // implement with recursion
void insert_front(stockNode *);
stockNode *find_middle();
void delete(string sym); // delete symbol stock
private:
stockNode * head;
stockNode * tail;
};
#endif
=====================stock.h==========================
#ifndef STOCK_H
#define STOCK_H
#include
using namespace std;
class stock
{
friend ostream& operator<< (ostream&, const stock&);
friend istream& operator>> (istream&, stock&);
public:
stock(string sym="", int cost=0, int shares=0); // constructor with default
void setstock(string, int, int);
bool operator== (const stock&); // equal
string symbol;
private:
int cost;
int shares;
};
// stock node with Has-A relationship
class stockNodeHasA {
public:
stock stk;
private:
stockNodeHasA * prev;
stockNodeHasA * next;
};
// stock node with inheritance
class stockNodeIsA : public stock {
public:
stockNodeIsA * prev;
stockNodeIsA * next;
};
// stock node with struct
struct stockNodeStruct {
stock stk;
stockNodeStruct * prev;
stockNodeStruct * next;
};
#endif
====================PEstock.txt===================
EEM 40 8
CSCO 29 87
FRANK 12 34
HD 115 59
AAPL 129 88
TSM 24 73
GOOG 553 72
FB 80 68
T 33 48
VTI 109 39
IWM 123 03
SPY 211 38
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#define MAXNAME 50
#define MAXCODE 100
#define MAXPRICE 50
#define MAXITEMS 1000
struct video {
char name[MAXNAME];
char code[MAXCODE];
char price[MAXPRICE];
int quantity;
};
typedef struct video stk;
stk items[MAXITEMS];
int NumItems;
int Size = sizeof(stk); //display size for the test
int addItems(void);
int idelete(void);
int edit(void);
int fetch(char *buff);
void menu(void);
void view(void);
void sortRecords(void);
int main(void) {
char buff[214];
int i; //number of entries
FILE * fp_in;
if((fp_in = fopen("stockdb.txt", "r")) == NULL) {
fputs("Error! Can't find or open stockdb.txt ", stderr);
return 1;
}
i=0;
while((fgets(buff, sizeof(buff), fp_in))!=NULL) {
buff[strlen(buff)-1]='';
sscanf(buff, "%[^,], %[^,], %[^,], %d ", items[i].name,items[i].code,items[i].price,&items[i].quantity);
if(++i==MAXITEMS) {
printf("Error! The array to hold data, is too small. Please enlarge it or delete stock record ");
getchar();
break;
}
}
fclose(fp_in);
NumItems=i;
sortRecords();
//view();
menu();
printf(" ");
printf(" ");
return 0;
}
/* this function is 90% done */
int addItems(void) {
int i,j=0,ok=0,len;
char buff[80]={''};
char ch='y';
stk stk1; //variable only for this function
while(ch=='y' || ch=='Y') {
printf(" ");
printf(" Name: ");
fgets(buff, MAXNAME, stdin);
len=strlen(buff)-1;
buff[len]='';
strcpy(stk1.name, buff);
printf(" * ");
printf(" Bar code: *");
fgets(buff, MAXCODE, stdin);
len=strlen(buff)-1;
buff[len]='';
strcpy(stk1.code, buff);
printf(" * ");
printf(" Price: *");
fgets(buff, MAXPRICE, stdin);
len=strlen(buff)-1;
buff[len]='';
strcpy(stk1.price, buff);
printf(" * ");
printf(" Quantity: * ");
fgets(buff, sizeof(items[0].quantity), stdin);
sscanf(buff, "%d", &stk1.quantity);
for(j=0;j<MAXITEMS;j++) {
if(!(items[j].name[0])) {
items[j] = stk1;
ok=1;
++NumItems;
break;
}
}
if(!ok) {
printf(" Array is full, couldn't add that record");
--NumItems;
break;
}
else {
printf(" Add another item to the stock? [y/n]: ");
scanf("%1c", &ch);
getchar();
}
}
//clean up screen
for(i=12;i<22;i+=2) {
printf(" * ");
}
return ok; //successful add
}
int edit(void) {
char buff[80];
int i=0;
printf(" Enter the name of the item you want to edit: ");
fgets(buff, MAXNAME, stdin);
if(buff[strlen(buff)-1]==' ')
buff[strlen(buff)-1]='';
i = fetch(buff); //in the list?
if(!i) //no
return 0;
//video was found, i is it's index in the array
printf(" Name: %s Bar_code: %s Quantity: %2d ",
items[i].name, items[i].code, items[i].quantity);
printf(" Enter new name [hit <enter> to leave it unchanged]: ");
fgets(buff, MAXNAME, stdin);
if(strlen(buff) >1) {
if(buff[strlen(buff)-1]==' ') {
buff[strlen(buff)-1]='';
strcpy(items[i].name, buff);
}
}
printf(" Enter new barcode [<enter> to leave it unchanged]: ");
fgets(buff, MAXCODE, stdin);
if(strlen(buff) >1) {
if(buff[strlen(buff)-1]==' ') {
buff[strlen(buff)-1]='';
strcpy(items[i].code, buff);
}
}
printf(" Enter new quantity [<enter> to leave it unchanged]: ");
fgets(buff, sizeof(items[0].quantity), stdin);
if(strlen(buff) >1) {
sscanf(buff, "%d", &items[i].quantity);
}
sortRecords();
return i;
}
int fetch(char *buff) {
int lo, hi, mid;
lo=0;
hi = NumItems-1;
while(lo <= hi) {
mid=(lo + hi)/2;
if((strcmp(items[mid].name, buff)) <0) {
lo=mid+1;
}
else if((strcmp(items[mid].name, buff)) >0) {
hi=mid-1;
}
else
return mid;
}
return -1;
}
int idelete(void) {
char buff[80];
int i;
printf(" Enter the name of the item in stock you want to delete: ");
fgets(buff, MAXNAME, stdin);
buff[strlen(buff)-1]='';
i = fetch(buff); //get buff's index in the array
if(i) {
printf("Delete this record [y/n] ? ");
printf(" Name: %s Bar code: %s Quantity: %d",items[i].name,
items[i].code, items[i].quantity);
fgets(buff, 2, stdin);
if(buff[0]=='y' || buff[0]=='Y') {
printf(" Item has been deleted");
getchar();
getchar();
//*vids[i].name = "";
//*vids[i].code = "";
//*vids[i].rating = 0;
}
return i;
}
else
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.