Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ please I am not allowed to use or any math libraries for this assignment. /////////////////////////////// calculator.cpp #include calculator.h unsigned long long factorial(int n) {

C++ please

I am not allowed to use or any math libraries for this assignment.

image text in transcribed

/////////////////////////////// calculator.cpp

#include "calculator.h"

unsigned long long factorial(int n) { ///fix me return 0; }

double factExp(double x, int n) { ///fix me return 0; }

double sin(double x, int nTerms) { ///fix me return 0; }

double sqrt(double x, int nTerms) { ///fix me return 0; }

double primary() // Number or ( Expression ) { Token t = ts.get(); switch (t.kind) { case '(': // handle (expression ) { double d = expression(); t = ts.get(); if (t.kind != ')') error("')' expected");

///Look ahead for a fact Token here before returning d ///don't forget to put the Token back if it is not fact ///fix me return d; // return the numbers value

}

case '-': return -primary();

case number: // rather than case '8': { ///Look ahead for a fact Token here before returning t's value ///don't forget to put the Token back if it is not fact ///fix me return t.value; // return the numbers value

}

///if the token is of type sine, then ///call for a new primary ///pass that into the sin function ///use nTerms = 1000 case sine: ///fix me

///if the token is of type root, then ///call for a new primary ///pass that into the sqrt function ///use nTerms = 1000 case root: ///fix me

default: error("primary expected"); } }

///The code below this line does not need to be changed at all

double expression() // read and evaluate: 1 1+2.5 1+2+3.14 etc. { double left = term(); // get the Term while (true) { Token t = ts.get(); // get the next token switch (t.kind) // and do the right thing with it { case '+': left += term(); break; case '-': left -= term(); break; default: ts.putback(t);

return left; // return the value of the expression } } } double term() // exactly like expression(), but for * and / { double left = primary(); // get the Primary while (true) { Token t = ts.get(); // get the next Token switch (t.kind) { case '*': left *= primary(); break; case '/': { double d = primary(); if (d==0) error("divide by zero"); left /= d; break; } default: ts.putback(t); return left; // return the value } } }

void clean_up_mess() { ts.ignore(print); }

void calculate() { while (cin) try { cout

///////////////////////// main.cpp

#include "../std_lib_facilities.h" #include "calculator.h" #include "Token_stream.h"

Token_stream ts;

///This file does not need to be changed at all int main() { try { calculate(); keep_window_open("~~"); // cope with Windows console mode return 0; } catch (...) // other errors (dont try to recover) { cerr

keep_window_open("~~"); }

////////////////////////////////////// Token_stream.cpp

#include "Token_stream.h"

Token Token_stream::get() // read a Token from the Token_stream { if (full) { full=false; // check if we already have a Token ready return buffer; }

char ch; cin >> ch; // note that >> skips whitespace (space, newline, tab, etc.)

switch (ch) { case '(': case ')': case ';': case 'q': case '+': case '-': case '*': case '/': case fact: return Token{ch}; // let each character represent itself case '.': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { cin.putback(ch); // put digit back into the input stream double val; cin >> val; // read a floating-point number return Token{number,val}; // rather than Token{'8',val} }

///look for an 's' ///if the stream contains 's''i''n', return a sine token ///if the stream contains 's''q''r''t', return a root token ///else throw a Bad token exception (use error function) case 's': ///fix me

default: error("Bad token"); } }

void Token_stream::ignore(char c) // skip characters until we find a c; also discard that c { // first look in buffer: if (full && c==buffer.kind) // && means and { full = false; return; } full = false; // discard the contents of buffer // now search input: char ch = 0; while (cin>>ch) if (ch==c) return; }

void Token_stream::putback(Token t) { if (full) error("putback() into a full buffer"); buffer=t; full=true; }

/////////////////////////////////////// Token_stream.h

#ifndef TOKEN_STREAM_H_INCLUDED #define TOKEN_STREAM_H_INCLUDED

#include "../std_lib_facilities.h"

///This file does not need to be changed at all ///Note the new constants below

struct Token // user-defined type called Token { char kind; // what kind of token double value; // used for numbers (only): a value };

class Token_stream { public: // user interface: Token get(); // get a Token void putback(Token); // put a Token back into the Token_stream void ignore(char c); // discard tokens up to and including a c

private: // representation: not directly accessible to users: bool full {false}; // is there a Token in the buffer? Token buffer; // here is where we keep a Token put back using putback()

};

// Token kind values: const char number = '8'; // a floating-point number const char quit = 'q'; // an exit command const char print = ';'; // a print command

///constant for factorial operator const char fact = '!'; // a print command

///constants for "sin" and "sqrt" inputs const char sine = 21; ///arbitrary number const char root = 22;

// User interaction strings: const string prompt = "> "; const string result = "= "; // indicate that a result follows

#endif // TOKEN_STREAM_H_INCLUDED

Instructions Use the given starter code and makefile from the Canvas section labeled Completed Calculator to begin. (1 pt) Implement a factorial function: unsigned long long factorial(int n); o returns n! eg 5! = 120 (1 pt) For later use, implement an exponential divided by factorial function: double factExp(double x, int n); o returns o The factorial in the denominator will overflow after about 20!, so we will not be able to use our factorial function here o Instead, use a for loop to keep the terms small as they are being multiplied: . .= 1. (1). (1).(1)...() (1 pt) Implement a sine function: double sin(double x, int nTerms); Sine can be approximated with a Taylor Series: 2 sin z = x - x + - + -... o The more terms added together the more accurate the approximation is o The parameter n Terms will determine how many terms to generate o Use your factExp function to generate each term

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 Processing Fundamentals Design And Implementation

Authors: David M. Kroenke

5th Edition

B000CSIH5A, 978-0023668814

More Books

Students also viewed these Databases questions