Hi everyone
I have been doing a program about computing the remaining cost in some Store,
in accounting called ending inventory, and it is a homework from my instructor to use the Stack,List and Queue in one program, anyway
the program work without the destructor of List Class, but with it, it doesn't work.
I think my problem with the " work ( ) " member function!! but i don't know how??
Please help me...
Code:
#include <iostream>
#include "iomanip"
using namespace std;
#include "conio.h"
#include "list_stack_queue.h"
#include "process.h"
#include "start_the_program.h"
void main()
{
StartTheProgram start;
start.work();
start.PrintTable();
getch();
}
Code:
class StartTheProgram
{
private:
int it,num_of_current_items;
float pr;
char op,f;
int o;
ulist<int> Items;
ulist<float> Price;
ulist<char> Operation;
public:
float co;
StartTheProgram()
{
num_of_current_items=0;
f='a';
o=1;
}
void work()
{
while(f=='a')
{
begin:
cout<<"OPERATION "<<o<<"\n___________"<<endl;
cout<<"\nEnter the operation (p=purchase or s=sale): \n";
cin>>op;
if(op=='s')
Price.Insert(0);
if(op=='p')
{
cout<<"\nEnter the price of purchase: \n";
cin>>pr;
Price.Insert(pr);
}
cout<<"Enter the number of items: \n";
cin>>it;
//Number of current items in the stor----------
if(op=='p')
num_of_current_items+=it;
else
num_of_current_items-=it;
Items.Insert(it);
Operation.Insert(op);
cout<<"\n\t\t\t\t\t-> To do that once again press: a\n\t\t\t\t\t-> Else press : n \n";
cin>>f;
cout<<"\n-------------------------------------------------------------\n";
o++;
}
Process<float> process(Items,Price,Operation,num_of_current_items);
float final_cost=0;
process.TransferToStack();
co=process.Cost();
cout<<"The Remaining Cost In The Store Is: "<<co;
cout<<"\n-------------------------------------------------------------\n";
cout<<"\n\t\t\t\t\t-> To enter new values press : a\n\t\t\t\t\t-> To View The Table And Exit Press Any Key \n";
char m;
cin>>m;
if(m=='a')
goto begin;
cout<<"\n********************************************************************************************\n";
cout<<"\n********************************************************************************************\n";
}
void PrintTable()
{
queue<char*> Qoperation;
queue<int> Qitems;
queue<float> Qprice;
int aa=Operation.Length();
for(int i=0;i<aa;i++)
{
if(Operation.ItemValue(i) == 'p')
Qoperation.enqueue("Purchase");
else
Qoperation.enqueue("Sale");
Qprice.enqueue(Price.ItemValue(i));
Qitems.enqueue(Items.ItemValue(i));
}
cout<<"\n\n\n\t\t\t*****************\n\t\t\tTable Of Contents\n\t\t\t*****************\n\n\n";
//cout<<"\t\t IN\t OUT";
//cout<<"\n\t _ _ _ _ _ _ _ _ _ _ _ _\n";
cout<<"\nOPERATION PRICE NUMBER NUMBER REMAINING COST";
cout<<"\n\t OF OF OF ";
cout<<"\n\t PURCHASE PURCHASE SOLD ";
cout<<"\n\t ITEM ITEMS ITEMS ";
cout<<"\n_________ _________________ ______ ______________\n\n";
while(!Qoperation.IsEmpty())
{
if(Qoperation.ReturnDequeue() == "Sale")
{
if(Qoperation.LastOne())
{
cout.fill(' ');
cout.width(39);
cout<<left<<Qoperation.dequeue();
cout.width(12);
cout<<left<<Qitems.dequeue()<<co<<endl<<endl;
Qprice.dequeue();
}
else
{
cout.fill(' ');
cout.width(39);
cout<<left<<Qoperation.dequeue()<<Qitems.dequeue()<<endl<<endl;
Qprice.dequeue();
}
}
else
{
if(Qoperation.LastOne())
{
cout.fill(' ');
cout.width(15);
cout<<left<<Qoperation.dequeue();
cout.width(10);
cout<<left<<Qprice.dequeue();
cout.width(26);
cout<<Qitems.dequeue()<<co<<endl<<endl;
}
else
{
cout.fill(' ');
cout.width(15);
cout<<left<<Qoperation.dequeue();
cout.width(10);
cout<<left<<Qprice.dequeue();
cout.width(26);
cout<<Qitems.dequeue()<<endl<<endl;
}
}
}
}
};
Code:
template <class T>
class Process
{
private:
ulist<int> Items;
ulist<float> Price;
ulist<char> Operation;
float cost;
public:
stack<int> Sitem;
stack<float> Sprice;
stack<char> Soperation;
float cost_after_purchases;
int repetitions;
int num_of_store_items;
Process(ulist<int> it,ulist<float> pr,ulist<char> op,int num_of_current_items)
{
Items=it;
Price=pr;
Operation=op;
cost=0;
cost_after_purchases=0;
num_of_store_items=num_of_current_items;
}
void TransferToStack()
{
int num_of_s=Operation.FirstS();
int s=0,p=0;
int len=Items.Length();
while(p<len)
{
if(Operation.ItemValue(p) == 's')
s++;
if(Operation.ItemValue(p) =='s' && s != num_of_s)
{
p++;
continue;
}
Sitem.push(Items.ItemValue(p));
Sprice.push(Price.ItemValue(p));
Soperation.push(Operation.ItemValue(p));
p++;
}
}
float Cost()
{
if(Soperation.ReturnPop() == 'p')
{
cost_after_purchases+=(Sprice.ReturnPop()*Sitem.ReturnPop());
num_of_store_items-=(Sitem.ReturnPopPlus1());
while(Soperation.ReturnPop() == 'p')
{
cost_after_purchases+=Sitem.ReturnPop()*Sprice.ReturnPop();
num_of_store_items-=Sitem.ReturnPopPlus1();
}
}
if(!Soperation.IsEmptyR() && Soperation.ReturnPopPlus1()=='s')
{
Sitem.ReturnPop();
if(num_of_store_items <= Sitem.ReturnPop())
cost=(num_of_store_items * Sprice.ReturnPopMinus1());
else
{
int x=num_of_store_items;
ulist<int> arrItems;
ulist<float> arrPrice;
//reset
arrItems.Insert(Sitem.ReturnPopPlus1());
Sprice.ReturnPop();
arrPrice.Insert(Sprice.ReturnPop());
x-=Sitem.ReturnPopPlus1();
while(x >= Sitem.ReturnPop())
{
arrItems.Insert(Sitem.ReturnPopPlus1());
x-=Sitem.ReturnPopPlus1();
arrPrice.Insert(Sprice.ReturnPop());
}
arrItems.Insert(x);
arrPrice.Insert(Sprice.ReturnPop());
int r=0;
while(!arrItems.isempty())
{
cost+=(arrItems.ItemValue(r)*arrPrice.ItemValue(r));
r++;
}
}
}
Soperation.Clear();
Sitem.Clear();
Sprice.Clear();
float cost2=cost_after_purchases;
cost_after_purchases=0;
return cost+cost2;
}
};
Code:
template<class T>
class ulist
{
private:
int size,length,current;
T *ulistptr;
public:
ulist(int s=10)
{
size=s>0 && s<1000?s:10;
length=0;
ulistptr=new T[size];
}
/*~ulist()
{
delete[] ulistptr;
}*/
bool isfull()const{
return length==size;
}
int isempty()const{
return length==0;
}
int Insert(const T item)
{
if(!isfull())
{
ulistptr[length]=item;
current=length;
length++;
return 1;
}
else
return 0;
}
int FirstS()
{
int num_of_s=0;
for(int i=0;i<length;i++)
if(ulistptr[i] == 's')
num_of_s++;
return num_of_s;
}
T ItemValue(int i)
{
length--;
return ulistptr[i];
}
int Length()
{
return length;
}
};