Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(C++) Implement each of the functions to perform the necessary actions outlined in the `.h` files. As you are writing your functions, read the instructions

(C++) Implement each of the functions to perform the necessary actions outlined in the `.h` files. As you are writing your functions, read the instructions and think of how you would test that functions while you are writing it. Write your Test first and then implement your functions. Try running your test and then fixing your issues. You only need to support integer calculations. You may need to create auxiliary functions to complete tasks, or to avoid copy and pasting repetitive code. Do not make these class functions. These should only appear in the .cpp file - Keep track of your edge cases 

GIVEN CALCULATOR.H, Please fill the functions on CALCULATOR.CPP using the given functions-expressionstream, fifo and lifo.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
calculator.h #ifndef CMPE126S18_LABS_CALCULATOR_H #define CMPE126S18_LABS_CALCULATOR_H 
#include "lifo.h" #include "fifo.h" #include "expressionstream.h" namespace lab4{ class calculator{ lab3::fifo infix_expression; lab3::fifo postfix_expression; void parse_to_infix(std::string &input_expression); //PRIVATE function used for converting input string into void convert_to_postfix(lab3::fifo infix_expression); //PRIVATE function used for converting infix FIFO to postfix public: calculator(); //Default constructor calculator(std::string &input_expression); // Constructor that converts input_expression to infix and postfix upon creation friend std::istream& operator>>(std::istream& stream, calculator& RHS); //Store the infix and postfix expression in calculator int calculate(); //Return the calculation of the postfix expression friend std::ostream& operator<<(std::ostream& stream, calculator& RHS); //Stream out overload. Should return in the format "Infix: #,#,#,# Postfix: #,#,#,#" }; } 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

calculator.cpp

#include  #include "calculator.h" namespace lab4 { void calculator::parse_to_infix(std::string &input_expression) {} void calculator::convert_to_postfix(lab3::fifo infix_expression) {} calculator::calculator() {} calculator::calculator(std::string &input_expression) {} std::istream &operator>>(std::istream &stream, calculator &RHS) {return stream;} int lab4::calculator::calculate() {return 0;} std::ostream &operator<<(std::ostream &stream, calculator &RHS) {return stream;} // AUXILIARY FUNCTIONS bool is_number(std::string input_string); bool is_operator(std::string input_string); int get_number(std::string input_string); std::string get_operator(std::string input_string); int operator_priority(std::string operator_in); } 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
#include "expressionstream.h" namespace lab1 { bool is_number(char c); expressionstream::expressionstream(const std::string &string_in) : buffer(string_in) { current_pos = buffer.begin(); next_position = current_pos; skip_white_space(); } void expressionstream::skip_white_space() { while (*current_pos == ' ' && current_pos != buffer.end()) ++current_pos; while (*next_position == ' ' && next_position != buffer.end()) ++next_position; } std::string expressionstream::get_number() { bool is_negative = false; std::string::iterator number_start; //check if the number is negative, this is used to skip any white space after '-' if (*current_pos == '-') is_negative = true; //find beginning of number number_start = current_pos; while (number_start != buffer.end() && !is_number(*number_start))++number_start; //find ending of number next_position = number_start; while (next_position != buffer.end() && is_number(*next_position))++next_position; //create and return number using position iterators return (is_negative) ? '-' + std::string(number_start, next_position) : std::string(number_start, next_position); } bool expressionstream::is_negative() { //after finding a '-' check if it is minus or negative by checking previous character //create an iterator to the previous character std::string::iterator negative_check = next_position - 1; //move backward until reach non-whitespace while (negative_check != buffer.begin() && *negative_check == ' ') --negative_check; //if the previous character is not a number and not a close parenthesis the number is negative //EXAMPLE: the following should get negatives on the 13's but not the 5's : "-13-(-13-(5--13)-5)--13" return (!is_number(*negative_check) && *negative_check != ')'); } std::string expressionstream::get_next_token() { //move the current_position iterator forward then get the "current" token current_pos = next_position; return get_current_token(); } std::string expressionstream::get_current_token() { skip_white_space(); //check for end of buffer if (current_pos == buffer.end()) return "\0"; //reset next position each time current token is called //this should stop the 'next_position' from drifting on //consecutive "get_current_token()" calls next_position = current_pos; if (next_token_is_int()) return get_number(); //if token is not a number then it is one character long //setting the 'next_position' iterator forward one will //return one character ++next_position; return std::string(current_pos, next_position); } bool expressionstream::next_token_is_int() { skip_white_space(); if (is_number(*next_position)) return true; if (*next_position != '-') return false; return is_negative(); } bool expressionstream::next_token_is_op() { skip_white_space(); if (next_token_is_int()) return false; return (*next_position == '+' || *next_position == '-' || *next_position == '*' || *next_position == '/'); } bool expressionstream::next_token_is_paren_open() { skip_white_space(); return *next_position == '('; } bool expressionstream::next_token_is_paren_close() { skip_white_space(); return *next_position == ')'; } bool is_number(char c) { return (c >= '0' && c <= '9'); } } 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
#include "lifo.h" namespace lab3 { lifo::lifo() { lifo_storage.reserve(100); index = -1;//Reserve 100 spaces in lifo_storage } lifo::lifo(std::string input_string) { lifo_storage.reserve(100); lifo_storage.append(input_string); index = 0; } lifo::lifo(const lifo &original) { lifo_storage.reserve(100); this->index = original.index; } lifo::~lifo() { index = -1; } lifo &lifo::operator=(const lifo &right) { //return <#initializer#>; lifo_storage.reserve(right.lifo_storage.capacity()); index = right.index; for (int i = 0; i <= index; i++) { lifo_storage[i] = right.lifo_storage[i]; } return *this; } bool lifo::is_empty() const { if (index == -1 ||) { return true; } else { return false; } //return false; } unsigned lifo::size() const { int temp; if (index > -1) { for (int i = 0; i <= index; i++) { temp++; } } else { temp = 0; } return temp; } std::string lifo::top() const { return lifo_storage[index]; //return std::__cxx11::string(); } void lifo::push(std::string input) { if (index == lifo_storage.capacity() - 1) { lifo_storage.reserve(lifo_storage.capacity() + 20); } lifo_storage[++index] = input; } void lifo::pop() { if (index > -1) index--; } } 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
#include "fifo.h" namespace lab3{ fifo::fifo() { lab2::stringVector fifo_storage; fifo_storage.reserve(100); front_index=0; back_index=0; //Reserve 100 spaces in fifo_storage } fifo::fifo(std::string input_string) { fifo_storage[0]=input_string; fifo_storage.reserve(100); front_index=0; back_index=0; } fifo::fifo(const fifo &original) { fifo_storage.reserve(100); this->fifo_storage=original.fifo_storage; this->front_index=original.front_index; this->back_index=original.back_index; } fifo::~fifo() { front_index=-1; back_index=-1; } fifo &fifo::operator=(const fifo &right) { if (&right == this){ return (*this); } fifo_storage.reserve(100); this->front_index = right.front_index; this->back_index = right.back_index; } bool fifo::is_empty() const { //return false; if(front_index == -1 || front_index==back_index){ return true; } return false; //return false; } unsigned fifo::size() const { unsigned int temp; if(front_indexelse{ //If back index < front index then the list has wrapped around temp = (100-front_index)+back_index+1; //fifo_storave.size() - front index = how many until end. //Adding the back index +1 should be how many items are in that section } return (temp); } std::string fifo::top() const { //return std::__cxx11::string(); return fifo_storage[front_index]; } void fifo::enqueue(std::string input) { back_index++; fifo_storage[back_index]=input; if(fifo_storage.capacity()<=back_index){ fifo_storage.reserve(fifo_storage.capacity()+20); } } void fifo::dequeue() { if(!is_empty()) {// In queue you would always take from the front, so you need to make sure that the fifo storage isnt empty ++front_index; } else{ throw"ERROR, out of bounds"; } } } 

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

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions