Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ calculator Goal Develop a program that implements a four-function calculator that performs all arithmetic with fractions. Details Your program will read a list of

c++ calculator
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Goal Develop a program that implements a four-function calculator that performs all arithmetic with fractions. Details Your program will read a list of arithmetic expressions, evaluate them and display their results. All numbers in the expression will be integers; however, the results of calculations will be fractions. Your program must be able to process any valid arithmetic expression that includes the following: Nonnegative integer numbers . The four basic arithmetic operations Parentheses Variable names, up to 100 variables, names follow C++ naming rules Assignment in the form var = expression For each expression, evaluate it. display the result and store the result in the appropriate variable. if necessary Required Objects A calculator needs two Stack objects -- one to store numbers and one to store operators. In this program, the number stack - the numStack - will store Fraction objects and the operator stack -- the opStack - Will store characters. In order to store and retrieve variable values, a Dictionary object will be necessary. The keys are strings and the values are Fractions. The exact implementation of the variable dictionary does not matter. Calculator Algorithm The program must read multiple lines from the standard input. Each line contains an arithmetic expression and possibly an assignment to a variable. An algorithm for processing such a line follows In Algorithms 1 and 2 2 3: 5: 6: A is an empty string 8: 9: 11: Algorithm 1 Main calculator algorithm 1: procedure EVALUATE(string s) Clear numStack Clear opStack Push $ onto opStack first-o dest-A 7 Scan forward for = symbol if = is found then first-position of character after = 10: dest -- first name found on line end if while first #ifndef _FRACTION_H #define _FRACTION_H #include class Fraction { public: Fraction(int32_t n=0,int32_t d=1); -Fraction() = default; Fraction operator=(Fraction rhs); Fraction operator+(Fraction rhs): Fraction operator-(Fraction rhs): Fraction operator*(Fraction rhs); Fraction operator/(Fraction rhs): bool operator==(Fraction rhs): bool operator!=(Fraction rhs): bool operator=(Fraction rhs): bool operator(Fraction rhs): [[nodiscard]] int32_t getNum() const { return num; } [[nodiscard]] int32_t getDen() const { return den; } private: int num den; std::stream &operator>>(std::stream & Fraction &); std::ostream &operator asing namespace std; const int32_t STACK SIZE - 64; template class Stack public: explicit stack(int32_t Sixe-STACK_SIZE) data-new StackType Size); capacity - Size; top - 0: > -Stack() deletedata: > void elear() { top = 0; } int size() { return top1 > bool isEmpty() { return top: void push(StackType d) if (top -- capacity) delete!) data; data temp: capacity - capacity. 2; data top++1 = d; Stacktype pop() { 1 (top - 0) throw undertlow_error("Stack is empty"); return datal--top: > StackType peek() { if (top - 0) throw underflow_error("Stack is empty"); return data top-1); private tackType data! int32 t top. capacity > fendit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago