C PROGRAM This program builds on the code below include incl
C++ PROGRAM
This program builds on the code below:
#include <iostream>
#include <fstream>
#include \"shirtType.h\"
using namespace std;
void getInput(shirtType s[],ifstream& infile, int& numShirts);
int main()
{
int numShirts;
shirtType shirts[500];
ifstream infile;
infile.open(\"h:\\\\shirtlab.txt\");
getInput(shirts,infile,numShirts);
for(int i =0; i<numShirts;i++)
shirts[i].print();
infile.close();
return 0;
}
void getInput(shirtType s[], ifstream& infile, int& numShirts)
{
int id, q,index=0;
string c,size;
double p;
infile>>id;
infile.ignore(100,\'\ \');
getline(infile,c);
infile>>size>>p>>q;
while(infile)
{
s[index].setAll(id, c, size, p, q);
index++;
infile>>id;
infile.ignore(100,\'\ \');
getline(infile,c);
infile>>size>>p>>q;
}
numShirts=index;
}
See also the description of the class shirtType below:
#include <iostream>
#include <string>
using namespace std;
class shirtType
{
private:
int shirtID;
string color;
string size;
double price;
int quantity;
public:
void setAll(int i,string c,string s, double p, int q);
void print() const;
void newQuan();
shirtType();
shirtType(int,string,string,double,int);
};
shirtType::shirtType(int i,string c,string s, double p, int q)
{
color = c;
size = s;
if(i>0)
shirtID=i;
else
shirtID=0;
if(p>0.0)
price = p;
else
price=0.0;
if (q>=0)
quantity=q;
else
quantity = 0;
}
shirtType::shirtType()
{
shirtID=0;
color=\"\";
size=\"\";
price=0.0;
quantity=0;
}
void shirtType::setAll(int i,string c,string s, double p, int q)
{
color = c;
size = s;
if(i>0)
shirtID=i;
else
shirtID=0;
if(p>0.0)
price = p;
else
price=0.0;
if (q>=0)
quantity=q;
else
quantity = 0;
}
void shirtType::newQuan()
{
cout<<\"Enter the new quantity of shirts: \"<<endl;
cin>>quantity;
}
void shirtType::print() const
{
cout<<\"Shirt ID: \"<<shirtID<<endl;
cout<<\"Color: \"<<color<<endl;
cout<<\"Size: \"<<size<<endl;
cout<<\"Price: $\"<<price<<endl;
cout<<\"Quantity: \"<<quantity<<endl;
}
WHAT NEEDS TO BE DONE:
The main program will declare an array of shirtType objects (no more than 500), get file input for some number of array objects, and determine how many array objects were input. *DO NOT USE ANY STRUCTS*
Next, the program presents the user with a menu of choices:
a – Add a shirt
s – Sell a shirt
r – Restock a shirt
p – Print a list of all shirts
q – Quit
The program reads the user’s choice, calls a function to do the user’s choice, then displays the menu again. The program loops and does the user’s choices until the user selects q (Quit).
Required functions in main( ):
getInput function to read file input into the array of shirtType objects
Menu function: displays the menu and gets input of the user’s choice
Add a shirt function: gets keyboard input from the user for shirtID, color, size, price, and number of shirts on hand; the next available shirtType object in the array accesses its member function setAll with these values as parameters, then the number of shirts in the array is incremented.
Sell a shirt function: Asks the user to input a shirt ID of the shirt to be sold, then finds the corresponding shirt object in the array (or reports “not found” to the user and exits the function). Checks that the quantity on hand is greater than 0 and if so, the shirt object accesses the class member function that modifies the quantity on hand (newQuan) to reduce the quantity on hand by the number sold.
Restock a shirt function: Asks the user to input a shirt ID of the shirt being restocked, then finds the corresponding shirt object in the array (or reports “not found” to the user and exits the function). Gets user input of the number of shits to add. The shirt object accesses the class member function that modifies the quantity on hand (newQuan) by the number of shirts coming in. It outputs the new value of quantity on hand.
Print a list of all shirts function: Output the data values of all shirts in the array.
shirtType class functions: Note that you may modify the class definition of shirtType by inserting useful class functions as needed. For example, to search for a shirt ID in the array objects, you need a public function getShirtID which returns the shirtID data value. In the header file, put the function prototype in the class definition, and add the function definition below it.
Solution
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void append(int nkkm)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=nkkm;
right=(struct node *)head;
while(right->next != NULL)
right=right->next;
right->next =temp;
right=temp;
right->next=NULL;
}
void add( int nkkm )
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=nkkm;
if (head== NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addafter(int nkkm, int loc)
{
int i;
struct node *temp,*left,*right;
right=head;
for(i=1;i<loc;i++)
{
left=right;
right=right->next;
}
temp=(struct node *)malloc(sizeof(struct node));
temp->data=nkkm;
left->next=temp;
left=temp;
left->next=right;
return;
}
void addaShirt(int nkkm)
{
int c=0;
struct node *temp;
temp=head;
if(temp==NULL)
{
add(nkkm);
}
else
{
while(temp!=NULL)
{
if(temp->data<nkkm)
c++;
temp=temp->next;
}
if(c==0)
add(nkkm);
else if(c<count())
addafter(nkkm,++c);
else
append(nkkm);
}
}
int delete(int nkkm)
{
struct node *temp, *prev;
temp=head;
while(temp!=NULL)
{
if(temp->data==nkkm)
{
if(temp==head)
{
head=temp->next;
free(temp);
return 1;
}
else
{
prev->next=temp->next;
free(temp);
return 1;
}
}
else
{
prev=temp;
temp= temp->next;
}
}
return 0;
}
void display(struct node *r)
{
r=head;
if(r==NULL)
{
return;
}
while(r!=NULL)
{
printf(\"%d \",r->data);
r=r->next;
}
printf(\"\ \");
}
int count()
{
struct node *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}
int main()
{
int i,nkkm;
struct node *n;
head=NULL;
while(1)
{
printf(\"1.Add a shirt\ \");
printf(\"2.Sell a shirt\ \");
printf(\"3.Restock a shirt\ \");
printf(\"4.Print a list of all shirts \ \");
printf(\"5.Quit\ \");
printf(\"Enter your choice : \");
if(scanf(\"%d\",&i)<=0){
printf(\"Enter only an Integer\ \");
exit(0);
} else {
switch(i)
{
case 1: printf(\"Enter the number to addaShirt : \");
scanf(\"%d\",&nkkm);
addaShirt(nkkm);
break;
case 2: if(head==NULL)
{
printf(\"List is Empty\ \");
}
else
{
printf(\"Element(s) in the list are : \");
}
display(n);
break;
case 3: printf(\"Size of the list is %d\ \",count());
break;
case 4: if(head==NULL)
printf(\"List is Empty\ \");
else{
printf(\"Enter the number to delete : \");
scanf(\"%d\",&nkkm);
if(delete(nkkm))
printf(\"%d deleted successfully\ \",nkkm);
else
printf(\"%d not found in the list\ \",nkkm);
}
break;
case 5: return 0;
default: printf(\"Invalid option\ \");
}
}
}
return 0;
}






