Share your repls and programming experiences

← Back to all posts
c++ code
MUsman022 (2)

#include <iostream> //adding Libraries
#include <stack>
#include <queue>

using namespace std;
void stacknqueuesum(stack <int> s, queue <int> q, int* sum); //declaration of function
int main()
{
int select = 0;
int data = 0;
int a = 1; // declaration of variable
int size[2];
queue <int> q; //Queue Declaration
stack <int> s;
s.push(30); //sum up some values in stack
s.push(40);
s.push(50);

q.push(15);     //sum up some values in queue  
q.push(45);
q.push(25);
while (!(select == 5))
{
	a = 1;     //Asking user to select an option 
	cout << endl << "Please chose any option  given as under" << endl;
	cout << "Press 1 to add number of masks in a new order " << endl;
	cout << "Press 2 to add new batch of masks to Inventory (number of masks) " << endl;
	cout << "Press 3 Process order in queue " << endl;
	cout << "Press 4 Check how many masks are ordered and available " << endl;
	cout << "Press 5  Quit the program " << endl;
	cin >> select;    // user is providing input  
	if (select == 1) {

		do {
			cout << endl << "Enter number of mask to order" << endl;
			cin >> data;  // user  is providing input 

			size[0] = 0; size[1] = 0;
			stacknqueuesum(s, q, size); //checking size of stack 

			if (((size[0]) - (size[1])) >= data)  //Checking if order available or not 
			{
				q.push(data); //Adding mask to order  
			}
			else
			{
				cout << "out of range or lengthy" << endl;
			}
			cout << "Enter 1 to add more or enter 2 to continue ";
			cin >> a;   //Taking user's input   
			cout << endl;
		} while (a == 1);
	}   if (select == 2)
	{
		do
		{
			cout << "Enter number of mask to inventory" << endl;
			cin >> data;  //Taking user's input  
			s.push(data); //Adding masks to inventory  
			cout << "Enter 1 to add more or press 2 to continue " << endl;
			cin >> a;   //Taking user's input  
		} while (a == 1);
	}
	else if (select == 3)
	{
		if (a == 1 & q.empty())  //If orders are empty display no pending orders
		{
			cout << endl << "No Pending Orders" << endl;
		}
		while (a == 1 & (!q.empty()))
		{

			int order = 0, deliever = 0;
			order = q.front();  //Store value of order      
			while (order > deliever)
			{
				deliever = deliever + s.top();
				s.pop();  //remove that value from stack 
			}
			if (order < deliever)
			{
				s.push(deliever - order);

			}
			q.pop();   //remove order 

			cout << "Enter 1 to process next order or press 2 to continue " << endl;
			cin >> a;    //Taking user's input    
		}
	}
	else if (select == 4)
	{
		size[0] = 0; size[1] = 0;
		stacknqueuesum(s, q, size);   //calling function of size checking 
		cout << "Number of inventories are " << size[0] << endl; //display number of inventories 
		cout << "Number of orders are " << size[1] << endl; //display number of orders 

	}
}

system("pause");

}

void stacknqueuesum(stack <int> s, queue <int> q, int* size) //function to check
{ //function to check size of stack & queue

if ((!s.empty()) | (!q.empty())) //checking if stack and queue both are not empty 
{
	if (!s.empty())   //checking if stack is not empty 
	{
		size[0] = size[0] + s.top(); //adding top to previous value 

		s.pop();   //removing top value 
	}
	if (!q.empty())    //checking if queue is not empty
	{
		size[1] = size[1] + q.front(); //adding front to previous value 
		q.pop();   //removing front value 
	}
	stacknqueuesum(s, q, size); //recursively calling this function again
}

}