Question: write a psuedo code for the following c++ code, the code should show m
ID: 3583685 • Letter: Q
Question
Question: write a psuedo code for the following c++ code, the code should show major functions showing its input return value if any etc.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int SIZE=1000;
int realSize;
string fname="books.txt", headLine="ISBN|Title|Author|Copies|Year|Publisher|Price| ";
bool loadBooks(string[], string[], string[], string[], int[], int[], double[]);
void writeBooks(string[], string[], string[], string[], int[], int[], double[]);
void addBook(string[], string[], string[], string[], int[], int[], double[]);
void dispStock(string[], string[], string[], string[], int[], int[], double[]);
void dispPublisher(string[], string[], string[], string[], string, int[], int[], double[]);
void dispPrice(string[], string[], string[], string[], double, int[], int[], double[]);
void removeBook(string[], string[]);
int isbnExists(string,string[]);
int updateBook(string, int, string[], int[]);
double calcAvg(double[]);
void dispStatistics(string[], int[], int[], double[]);
int main()
{
//instantiating book instances
string isbn[SIZE], title[SIZE], author[SIZE], publisher[SIZE], str;
int copies[SIZE], year[SIZE], choice, temp;
double price[SIZE], dbl;
while(true){
realSize=0;
if(!loadBooks(isbn,title,author,publisher,copies,year,price)){
system("PAUSE");
return -1;
}
cout << "Bookstore Information System [1] Add book details [2] Update a book [3] Remove a book [4] List books stock [5] List books by a given publisher [6] List books above a given price [7] List books above the average price [8] Display Stock statistics [9] Exit Enter your choice: ";
cin >> choice;
switch(choice){
case 1:
addBook(isbn,title,author,publisher,copies,year,price);
break;
case 2:
cin.ignore();
cout << "Enter ISBN: ";
getline(cin,str);
do{
cout << "Do you want to remove or add copies? [1] Remove [2] Add Choice: ";
cin >> choice;
if(choice<1||choice>2)
cout <<"Invalid option! ";
}while(choice<1||choice>2);
do{
cout << "Enter number of copies: ";
cin >> temp;
if(temp<=0)
cout <<"Enter a number greater than 0! ";
}while(temp<=0);
if(choice==1)
temp=temp*(-1);
temp=updateBook(str,temp,isbn,copies);
if(temp==-2)
cout << "ISBN doesn't exist! ";
else if(temp==-1)
cout << "Not enough stock! ";
else{
cout << "Copies ";
choice==1?
cout << "removed successfully! ":
cout << "added successfully! ";
}
break;
case 3:
removeBook(isbn,title);
break;
case 4:
dispStock(isbn,title,author,publisher,copies,year,price);
break;
case 5:
cin.ignore();
cout << "Enter Publisher Name: ";
getline(cin,str);
dispPublisher(isbn,title,author,publisher,str,copies,year,price);
break;
case 6:
do{
cout << "Enter a price: ";
cin >> dbl;
if(dbl<=0)
cout <<"Enter a number greater than 0! ";
}while(dbl<=0);
dispPrice(isbn,title,author,publisher,dbl,copies,year,price);
break;
case 7:
dispPrice(isbn,title,author,publisher,calcAvg(price),copies,year,price);
cout << "Average Price: " << calcAvg(price) << endl;
break;
case 8:
dispStatistics(title,copies,year,price);
break;
case 9:
cout << "Thank you for using The Bookstore Information System. Bye. ";
system("PAUSE");
return 0;
default:
cout << "Invalid choice! ";
}
writeBooks(isbn,title,author,publisher,copies,year,price);
}
return 0;
}
bool loadBooks(string isbn[], string title[], string author[], string publisher[], int copies[], int year[], double price[]){
ifstream inFile;
inFile.open(fname.c_str());
string temp;
if(inFile.is_open()){
getline(inFile,temp);
while(!inFile.eof()){
getline(inFile,isbn[realSize],'|');
getline(inFile,title[realSize],'|');
getline(inFile,author[realSize],'|');
inFile >> copies[realSize];
inFile.ignore(10,'|');
inFile >> year[realSize];
inFile.ignore(10,'|');
getline(inFile,publisher[realSize],'|');
inFile >> price[realSize];
getline(inFile,temp);
realSize++;
}
inFile.close();
return true;
}
else
cout << "File does not exist! ";
return false;
}
void writeBooks(string isbn[], string title[], string author[], string publisher[], int copies[], int year[], double price[]){
ofstream outFile(fname.c_str());
outFile << headLine;
for(int i=0; i<realSize; i++){
if(isbn[i]!="")
outFile << endl << isbn[i] << "|" << title[i] << "|" << author[i] << "|" << copies[i] << "|" << year[i] << "|" << publisher[i] << "|" << price[i] << "| ";
}
outFile.close();
}
int isbnExists(string userIsbn, string isbn[]){
for(int i=0; i<realSize; i++){
if(userIsbn==isbn[i])
return i;
}
return -1;
}
void addBook(string isbn[], string title[], string author[], string publisher[], int copies[], int year[], double price[]){
string uIsbn, uTitle, uAuthor, uPublisher;
int uCopies, uYear;
double uPrice;
bool flag=false;
cout <<"Bookstore Information System - Add book details: ================================================ ";
cin.ignore();
do{
cout << "Enter the ISBN: ";
getline(cin,uIsbn);
if(uIsbn=="")
cout << "Invalid ISBN ";
else if(isbnExists(uIsbn,isbn)!=-1){
cout << "ISBN exists! ";
flag=true;
}
else flag=false;
}while(uIsbn==""||flag);
cout << "Enter the title: ";
getline(cin,uTitle);
cout << "Enter author name: ";
getline(cin,uAuthor);
do{
cout << "Enter number of copies: ";
cin >> uCopies;
if(uCopies<0)
cout << "Number of copies can't be less than 0! ";
}while(uCopies<0);
do{
cout << "Enter year: ";
cin >> uYear;
if(uYear<1900 || uYear>2017)
cout << "Year must be between 1900 and 2017! ";
}while(uYear<1900 || uYear>2017);
cout << "Enter publisher name: ";
cin.ignore();
getline(cin,uPublisher);
do{
cout << "Enter the price: ";
cin >> uPrice;
if(uPrice<0)
cout << "Price can't be less than 0! ";
}while(uPrice<0);
isbn[realSize]=uIsbn;
title[realSize]=uTitle;
author[realSize]=uAuthor;
publisher[realSize]=uPublisher;
copies[realSize]=uCopies;
year[realSize]=uYear;
price[realSize]=uPrice;
realSize++;
cout << ".....Record added.";
}
int updateBook(string uIsbn, int uCopies, string isbn[], int copies[]){
int index=isbnExists(uIsbn,isbn);
if(index==-1)
return -2;
else if(uCopies+copies[index]<0)
return -1;
copies[index]+=uCopies;
return 0;
}
void removeBook(string isbn[], string title[]){
string uIsbn;
int index;
char choice;
cout << "Please enter ISBN: ";
cin.ignore();
getline(cin,uIsbn);
index=isbnExists(uIsbn,isbn);
if(index!=-1){
cout << "Are you sure you want to remove "" << title[index] << ""? (Enter Y to remove) ";
cin >> choice;
if(choice=='y'||choice=='Y'){
isbn[index]="";
cout << "Removed successfully. ";
}
else
cout << "Operation cancelled. ";
}
else
cout << "Invalid ISBN! ";
}
void dispStock(string isbn[], string title[], string author[], string publisher[], int copies[], int year[], double price[]){
cout << setw(20) << left << "ISBN";
printf("%s %s %s ", "Title ","Author(s) ","Publisher ");
cout << setw(8) << left << "Copies";
cout << setw(8) << left << "Year";
cout << setw(8) << left << "Price";
cout << setw(15) << left << "Total Price" << endl << endl;
for(int i=0; i<realSize; i++){
cout << setw(20) << left << isbn[i];
printf("%.8s.. %.8s.. %.8s.. ", title[i].c_str(),author[i].c_str(),publisher[i].c_str());
cout << setw(8) << left << copies[i];
cout << setw(8) << left << year[i];
cout << fixed << setw(8) << left << setprecision(2) << price[i];
cout << fixed << setw(15) << left << setprecision(2) << price[i]*copies[i] << endl << endl;
}
}
void dispPublisher(string isbn[], string title[], string author[], string publisher[], string upublisher, int copies[], int year[], double price[]){
cout << setw(20) << left << "ISBN";
printf("%s %s ", "Title ","Author(s) ");
cout << setw(8) << left << "Copies";
cout << setw(8) << left << "Year";
cout << setw(8) << left << "Price";
cout << " ======================================================================== ";
for(int i=0; i<realSize; i++){
if(upublisher==publisher[i]){
cout << setw(20) << left << isbn[i];
printf("%.8s.. %.8s.. ", title[i].c_str(),author[i].c_str());
cout << setw(8) << left << copies[i];
cout << setw(8) << left << year[i];
cout << fixed << setw(8) << left << setprecision(2) << price[i] << endl << endl;
}
}
cout << "======================================================================== ";
}
void dispPrice(string isbn[], string title[], string author[], string publisher[], double uprice, int copies[], int year[], double price[]){
cout << setw(20) << left << "ISBN";
printf("%s %s ", "Title ","Author(s) ");
cout << setw(8) << left << "Copies";
cout << setw(8) << left << "Year";
cout << setw(8) << left << "Price";
cout << " ======================================================================== ";
for(int i=0; i<realSize; i++){
if(uprice<price[i]){
cout << setw(20) << left << isbn[i];
printf("%.8s.. %.8s.. ", title[i].c_str(),author[i].c_str());
cout << setw(8) << left << copies[i];
cout << setw(8) << left << year[i];
cout << fixed << setw(8) << left << setprecision(2) << price[i] << endl << endl;
}
}
cout << "======================================================================== ";
}
double calcAvg(double price[]){
double sum=0;
for(int i=0; i<realSize; i++)
sum+=price[i];
return sum/realSize;
}
double stockVal(double price[], int copies[]){
double sum=0;
for(int i=0; i<realSize; i++)
sum+=price[i]*copies[i];
return sum;
}
double mostExpensive(double price[]){
double mprice=price[0];
for(int i=0; i<realSize; i++)
if(price[i]>mprice)
mprice=price[i];
return mprice;
}
double leastExpensive(double price[]){
double lprice=price[0];
for(int i=0; i<realSize; i++)
if(price[i]<lprice)
lprice=price[i];
return lprice;
}
int leastStocked(int copies[]){
int lstock=copies[0];
for(int i=0; i<realSize; i++)
if(lstock>copies[i])
lstock=copies[i];
return lstock;
}
int oldestYear(int year[]){
int oyear=year[0];
for(int i=0; i<realSize; i++)
if(oyear>year[i])
oyear=year[i];
return oyear;
}
int newestYear(int year[]){
int nyear=year[0];
for(int i=0; i<realSize; i++)
if(nyear<year[i])
nyear=year[i];
return nyear;
}
void dispStatistics(string title[], int copies[], int year[], double price[]){
double mprice=mostExpensive(price), lprice=leastExpensive(price);
int lstock=leastStocked(copies), oyear=oldestYear(year), nyear=newestYear(year);
cout << "Current Stock Value: " << stockVal(price,copies) << endl << endl;
cout << "Average Book Price: " << calcAvg(price) << endl << endl;
cout << "Most expensive books (" << mprice << "): ";
for(int i=0; i<realSize; i++)
if(price[i]==mprice)
cout << title[i] << endl;
cout << " Least expensive books (" << lprice << "): ";
for(int i=0; i<realSize; i++)
if(price[i]==lprice)
cout << title[i] << endl;
cout << " Least stocked books (" << lstock << "): ";
for(int i=0; i<realSize; i++)
if(copies[i]==lstock)
cout << title[i] << endl;
cout << " Oldest books (" <<oyear<<"): ";
for(int i=0; i<realSize; i++)
if(year[i]==oyear)
cout << title[i] << endl;
cout << " Newest books (" << nyear << "): ";
for(int i=0; i<realSize; i++)
if(year[i]==nyear)
cout << title[i] << endl;
cout << endl;
}
Explanation / Answer
Answer:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.