Question
(C++) Use Shunting-yard Algorithm for this operation using GIVEN EXPRESSIONSTREAM, STACK & QUEUE. ONLY NEED TO FILL CALCULATOR.CPP using the rest of files. Write a
(C++) Use Shunting-yard Algorithm for this operation using GIVEN EXPRESSIONSTREAM, STACK & QUEUE.
ONLY NEED TO FILL CALCULATOR.CPP using the rest of files.
Write a simple Integer calculator using given stacks and queues and expressionstream
To read a string, convert to infix then posfix and run the calculator operations.
no need to consider negative numbers.
Only need +-/* operations along with simple () brackets:
5+20-1*(8-3*(1+0))=20.
Keep track of your edge cases
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "lifo.h"
#include "fifo.h"
#include "expressionstream.h"
namespace lab0{
class calculator{
lab3::fifo infix_expression;
lab3::fifo postfix_expression;
//PRIVATE function used for converting input string into infix
void parse_to_infix(std::string &input_expression);
//PRIVATE function used for converting infix FIFO to postfix
void convert_to_postfix(lab3::fifo infix_expression);
public:
calculator(); //Default constructor
// Constructor that converts input_expression to infix and postfix upon creation
calculator(std::string &input_expression);
//Store the infix and postfix expression in calculator
friend std::istream& operator>>(std::istream& stream, calculator& RHS);
//Return the calculation of the postfix expression
int calculate();
//Stream out overload. Should return in the format "Infix: #,#,#,# Postfix: #,#,#,#"
friend std::ostream& operator<<(std::ostream& stream, calculator& RHS);
};
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
calculator.cpp
#include
#include
#include "calculator.h"
namespace lab0 {
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 lab0::calculator::calculate() {return 0;}
std::ostream &operator<<(std::ostream &stream, calculator &RHS) {return stream;}
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) {
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; }
}
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]; }
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;
}
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;
}
unsigned fifo::size() const {
unsigned int temp;
if(front_indexelse{
temp = (100-front_index)+back_index+1;
}
return (temp);
}
std::string fifo::top() const { 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()) { ++front_index; }
else{ throw"ERROR, out of bounds"; }
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started