0

Data Structures (CS301) Assignment # 02 Semester Spring 2021

Data Structures (CS301) Assignment # 02

Problem Statement:

Due to the Coronavirus pandemic Government has asked all the major grocery stores to serve the customers as soon as possible so that the gathering of the people may be avoided. In this regard all the grocery stores have decided that there will be two queues for the customers at each grocery store. One queue is the express queue and second is the normal queue. Express queue will have the customers which have bought 5 or less than 5  grocery items. Similarly normal queue will have the customers with grocery items greater than 5. These queues will help the customer to choose the  line according to their grocery shopping requirements and they will have to wait in their queue accordingly. The idea is to develop a C++ program that could serve the purpose of FIFO (first come first served)for both express and normal service queues so that each customer may select queue according to his/her shopping requirements. The program must have the features to:
·         Record the information of the customer (name, shopping information etc.) and identify the type of customer so that he/she may select express queue or normal queue)
·         Simulate both express and normal queues i.e., count the number of customers and the serving time of each customer in each queue.
The program must have the following functional features:
1.        Record information of each customer that would be the customer name, customer id, number of grocery items purchased.
2.        Send each customer to either express queue or normal queue based on the number  of grocery items customer has purchased.
3.        Keep the count of customers in each queue. Whenever a customer enters or exits a queue display the total number of customers in that queue
4.        Print the information of the customers of both the queues.
Furthermore: You need to consider following necessary steps while developing the solution.
·         Use linked list to make queue of customers. Here CustomerNode class will be used in place of Node class.
·         Create a single CustomerQueue class to handle both express and normal queues customers. Use addCustomer(enqueue), removeCustomer(dequeue) and methods for smooth operations of queue.
·         Collect all information of customers like
o   Customer Name, Customer ID, Number of grocery items purchased,
·         After getting required information of customer, check the number of grocery items customer has purchased and then put him in the respective queue.
Whenever a customer enters into a queue print the message “Customer entered in express/normal queue” and also print the total number of customers in that queue. Use removeCustomer() method to remove customer one by one and display the information of that customer along with the remaining customers count of each queue.

CODE :

#include<iostream>
#include<string>

using namespace std;

class CustomerNode{
	public:
		int cid = 0;
		string cname = "";
		int pitem = 0;
		CustomerNode *next;
};


class CustomerQueue{
	public:
		CustomerNode *front, *rear;
		CustomerQueue(){
			front = rear = NULL;
		}
		
		void addCustomeer(int,string,int);
		void removeCustomer(char,int);
		void display(CustomerNode*,string,int);
};

void CustomerQueue::addCustomeer(int id,string name,int item){
	CustomerNode *newNode = new CustomerNode;
	
	newNode -> cid = id;
	newNode -> cname = name;
	newNode -> pitem = item;
	newNode-> next = NULL;
	
	if(front == NULL){
		front = rear = newNode;
		rear -> next = NULL;
	}else{
		while(rear->next != NULL){
			rear = rear ->next;
		}
		rear ->next = newNode;
		rear = newNode;
	}
}


void CustomerQueue::removeCustomer(char q,int count){
	CustomerNode *temp = front;
	string queue;
	if(front == NULL){
		rear = NULL;
		if(q == 'n'){
			queue = "Normal";
		}else{
			queue = "Express";
		}
		cout<<"----"<<queue<<" Queue Customer Information----"<<endl;
		cout<<queue<<"Queue is empty"<<endl;
	}else{
		
		if(q == 'n'){
			queue = "Normal";
		}else{
			queue = "Express";
		}
		cout<<"----"<<queue<<" Queue Customer Information----"<<endl;
		display(temp,queue,count);
		delete(temp);
	}
	
}


void CustomerQueue::display(CustomerNode* temp,string queue,int count){
	temp = front;
	while(temp!=NULL){
		cout<<"Customer ID = "<<temp->cid<<endl;
		cout<<"Customer Name = "<<temp->pitem<<endl;
		cout<<"Total Customer in "<<queue<<" Queue = "<<--count<<endl;
		temp=temp->next;
	}
	
}

int main(){
	CustomerQueue express,normal;
	int id = 0,item = 0,exp_count = 0,nor_count = 0;
	string name;
	bool check = true;
	char choice,exp = 'e',nor='n';
	
	while(check){
		cout<<"---Enter Customer Information---"<<endl;
		cout<<"Custometr  ID = ";
		cin>>id;
		cout<<"Custometr  Name = ";
		cin>>name;
		cout<<"Item Purchesed = ";
		cin>>item;
		if(item <= 5){
			cout<<"---------- Customer Entered in Express Queue ----------"<<endl;
			express.addCustomeer(id,name,item);
			exp_count++;
			cout<<"Total Number of Customer in Express Queue = "<<exp_count<<endl;
			
		}else{
			cout<<"------ Customer Entered in Normal Queue ---"<<endl;
			express.addCustomeer(id,name,item);
			nor_count++;
			cout<<"Total Number of Customer in Normal Queue = "<<nor_count<<endl;
		}
		
		
		cout<<"\nDo you want to enter another Customer Y for yes or N for no : ";
		cin>>choice;
		if(choice == 'n' || choice == 'N'){
			express.removeCustomer(exp,exp_count);
			normal.removeCustomer(nor,nor_count);
			check = false;
		}
	}
	
	return 0;
}
0

GUI Calculator In Python Using Tkinter

GUI Calculator In Python Using Tkinter

Learning Outcomes

  • Implement Tkinter GUI Python Application
  • Implement and utilize widgets
  • Implement Event Handlers
  • Develop logic

Program Overview

GUI Calculator In Python Using Tkinter – Create a basic five-function Calculator using the tkinter module. The calculator should allow
addition, subtraction, multiplication, and division of two numbers along with square root of a
single number (import math to access math.sqrt(value)). It should have an AC button, which
clears all the elements of the calculator as well as an “equals” and a decimal button. Include the
positive/negative which adds a – or removes it from the entered number and a backspace button.
Create your window in the below format (colors you are free to customize):

GUI Calculator In Python Using Tkinter

Look at the following online calculators to get acquainted with the 5-functions of a calculator in
the event you have not used one. It is important that you use one to gain an idea of how it works.
Calculators you use on your phone are far more sophisticated in behavior. Emulate the behavior
found in these online calculators: https://calculator-1.com/ and http://calculator-1.com/simple/

Functionality Requirements

Your Calculator should:

1) Allow users to enter numbers ONLY using buttons
•Number entry is via keyboard is not required
2) Have buttons for Add, Subtract, Multiply, and Divide
• Option1 – Be able to use the text being displayed as number 1. The text entered by
the user after the math operator should be used as number 2.
• Option2 – create a string in your display that is an equation and use eval(formula)
to determine the answer. Example: eval(‘2-1+3*5’) returns 16
3) Have a square root button. Ensure your display has an answer not a formula (perform =
first). Then do math.sqrt(answer) to get the square root and display.
4) Have a Clear button
• Clear resets all previous input
5) Offer the user the ability to use decimals.
• Allows only 1 decimal point in a number (two in a number isn’t valid)
6) Be able to show decimals in results and toggle negative or positive numbers
7) Show accurate results for all five math functions.
8) Be able to recover and not crash when a number is divided by zero.

CODE – GUI Calculator In Python Using Tkinter:

from tkinter import *
import math
root = Tk()
root.title("CALCULATOR")
root.geometry("420x530")
root.configure(bg = "lightblue")

screen = StringVar()
screen.set('')


# removes the last character of text on screen
def clearScreenByOne():
screenText = screen.get()
screen.set(screenText[:-1])

# clear all screen
def clearScreen():
screen.set(' ')

#This function will handle the button click
def buttonClickHandler(buttonText):
screenText=screen.get()
#if button ± is clicked then multiply the value will -
if buttonText == '±':
screenText = str(-(float(screenText)))
#setting result to screen
screen.set(screenText)
return
#if button sqrt is click then finds squre root of number in screen and set result back to screen
if buttonText == '√':
screenText = str(math.sqrt(float(screenText)))
screen.set(screenText)
return
#if operator is already present at last of text on screen then it will do nothing
if buttonText in ['+','-','x','÷'] and screenText[-1] in ['+','-','x','÷']:
return

#if user tried to type 2 dots
screenText = screenText.replace('..','.')

screenText = screenText + buttonText
screen.set(screenText)


def main():
#get text from screen
screenText = screen.get()
screen.set(' ')
#replaced x wit * and ÷ with / inorder to easily pass to aval function
screenText = screenText.replace('x', '*')
screenText = screenText.replace('÷', '/')
try:
#generating result
answer = round(eval(screenText),2)
except:
#if any errorr occurs
answer = "Error"
#setting text to screen
screen.set(str(answer))

def run():
#generates label on window to show output
lbl_outputScreen = Label(root,text="",bg="white",width=18,height=2,font=('Arial','30'),textvariable=screen, anchor='se')
#placing label on screen on row 0 col 0 means on top and giving it space of 4 solumns to file entire width
lbl_outputScreen.grid(row=0,column=0,columnspan=4)

#below generates buttons in form of grid which will call button_clickhandler method when clicked
btn_clearScreenByOne = Button(root,text="➜",bg="lightgrey",fg="black",width = 3,height=1,font=('Arial','25'),command=clearScreenByOne)
btn_plus_minus = Button(root,text="±",bg="lightgrey",fg="black",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('+-'))
btn_sqrt = Button(root,text="√",bg="lightgrey",fg="black",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('√'))
btn_mul = Button(root,bg="orange",fg="black",text="x",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('x'))
btn_clearScreenByOne.grid(row=1,column=1,pady=10)
btn_plus_minus.grid(row=1,column=2,pady=10)
btn_sqrt.grid(row=1,column=3,pady=10)
btn_mul.grid(row=3,column=3,pady=10)

#second row
btn7 = Button(root,bg="grey",fg="black",text="7",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('7'))
btn8 = Button(root,bg="grey",fg="black",text="8",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('8'))
btn9 = Button(root,bg="grey",fg="black",text="9",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('9'))
btn_divide = Button(root,bg="orange",fg="black",text="÷",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('÷'))
btn7.grid(row=2,column=0,pady=10)
btn8.grid(row=2,column=1,pady=10)
btn9.grid(row=2,column=2,pady=10)
btn_divide.grid(row=2,column=3,pady=10)

# The third row
btn4 = Button(root,bg="grey",fg="black",text="4",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('4'))
btn5 = Button(root,bg="grey",fg="black",text="5",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('5'))
btn6 = Button(root,bg="grey",fg="black",text="6",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('6'))
btn_minus = Button(root,bg="orange",fg="black",text="-",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('-'))
btn4.grid(row=3,column=0,pady=10)
btn5.grid(row=3,column=1,pady=10)
btn6.grid(row=3,column=2,pady=10)
btn_minus.grid(row=4,column=3,pady=10)

# Fourth row
btn1 = Button(root,bg="grey",fg="black",text="1",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('1'))
btn2 = Button(root,bg="grey",fg="black",text="2",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('2'))
btn3 = Button(root,bg="grey",fg="black",text="3",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('3'))
btn_add = Button(root,bg="orange",fg="black",text="+",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('+'))
btn1.grid(row=4,column=0,pady=10)
btn2.grid(row=4,column=1,pady=10)
btn3.grid(row=4,column=2,pady=10)
btn_add.grid(row=5,column=3,pady=10)

# The fifth row
btn0 = Button(root,bg="grey",fg="black",text="0",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('0'))
btn_dot = Button(root,bg="grey",fg="black",text=".",width = 3,height=1,font=('Arial','25'),command=lambda: buttonClickHandler('.'))
btn_equal = Button(root,bg="orange",fg="black",text="=",width = 3,height=1,font=('Arial','25'),command=main)
btn_clear = Button(root,text="C",bg = "red",fg = "black",width = 3,height=1,font=('Arial','25'),command=clearScreen)
btn_clear.grid(row=1,column=0,pady=10)
btn0.grid(row=5,column=0,pady=10)
btn_dot.grid(row=5,column=1,pady=10)
btn_equal.grid(row=5,column=2,pady=10)
#calling run method
run()
#showing window until user don't closes it
root.mainloop()

keep visiting : shaadcoding

0

Python Math Quiz Project From Easy To Hardest Mode

python math quiz project

Keywords for python math quiz project

  • python random maths quiz
  • quiz in python
  • how to make a multiple choice quiz in python
  • python quiz code with the score
  • python math example
  • how to generate random maths questions in python
  • python mathematical programming
  • python chapter 5 math quiz

Code

import random

#inut user for levels and number of question per level
levels = int(input("Enter number of levels you want to play : "))
questions = int(input("ENter number of questions per level : "))
#i to checks level
i = 1

#As single digit is between 1 and 9 so start = 1 and end = 9
start = 1
end = 9
#to save score
score = 0
#looping until levels
while i <= levels:
print("level ",i)
j = 1
#looping until questions in each level
for q in range(questions):
#generating two random numbers and random operator from + and -
num1 = random.randint(start,end)
num2 = random.randint(start,end)
operator = random.choice(['+','-'])
#making question as string
questionStatement = str(num1) + operator + str(num2)
#using eval function getting result
result = int(eval(questionStatement))
#showing statement to user and getting user answer
useranswer = int(input(f"Q#{j} {questionStatement} ? "))
#if answer matches result then showing message and giving 1 point else showing right answer
if useranswer == result:
print("You answered correctly...")
score += 1
else:
print("Sorry , you'r answer is wrong,correct is ",result)
#incrementing to increase question by 1
j += 1
print()
#for next round let's say round 2 it have to be 2 digit and round 3 3 digit and so on
#so making 1 to 10 and 9 to 99 and then 10 to 100 and 99 to 999 and so on
start = start * 10
end = (end * 10) + 9
i += 1

#finally printing score
print("\n\n")
print("Your score is : ",score)

#Any question or suggestion?
#i will like to answers your query in comments
#Thanks and have a healthy life :)

DEMO

keep visiting : shaadcoding