Answered step by step
Verified Expert Solution
Question
1 Approved Answer
( C + + assignment ) a calculator program. very important college assignment I'll fail my class if I dont get a good grade for
Cassignment a calculator program. very important college assignment I'll fail my class if I dont get a good grade for this. it has starting codes. please dont change the starting codes and just complete them. Below is the claculator.cpp and the pictures are main.cpp
#include
THIS IS A HELPER FUNCTION. YOU AREN'T REQUIRED TO USE IT AND YOU CAN
MODIFY IT IF YOU WANT, BUT I STRONGLY RECOMMEND THAT YOU USE IT ASIS
YOU MAY FIND IT VERY HELPFUL.
Function: isnumber
Description: Determines whether a given string holds a valid numeric value
Returns: True if the given string holds a valid numeric value, or false
otherwise. If this function returns true, it's safe to use std::stod
on the string to convert it to a number double afterwards. Otherwise,
the given string does not contain a valid numeric value, and attempting
to use std::stod on the string may crash your program.
bool isnumberstd::string str
A valid number must contain at least one digit and at most one
decimal point
int numdigits ;
int numpoints ;
for int i ; i strlength; i
bool ispoint strati;
A negative sign is a dash at the beginning of the string
bool isnegativesign strati && i ;
bool isnumber strati && strati;
if ispoint
If the character is a decimal point, increment
the number of points found, and return false if
it's greater than
numpoints;
if numpoints
return false;
if isnumber
If the character is a digit, increment the number of
digits found
numdigits;
If the character isn't any of the three valid possibilities,
return false, immediately ending the function
if ispoint && isnegativesign && isnumber
return false;
Return true only if at least one digit was found
return numdigits ;
TODO Write other functions as you see fit.
THIS IS A REQUIRED FUNCTION. YOU MAY NOT ALTER ITS HEADER NAME PARAMETERS,
OR RETURN TYPE IN ANY WAY.
Function: isvalidexpression
Description: Determines whether a given string holds a valid arithmetic
expression, as defined in the assignment document.
Parameters:
expression std::string: A string holding the expression to be checked
for validity.
Returns: True if the given string holds a valid arithmetic expression, or
false otherwise.
For example, isvalidexpression should return true, but
isvalidexpression should return false notice the lack of spaces;
see the assignment document for an explanation of what constitutes a valid
arithmetic expression for this assignment
bool isvalidexpressionstd::string expression
TODO Complete this function. You may create other functions above and have
this function call them, if you'd like you probably should for the sake
of the course's style guidelines
TODO Remove the below return statement. It's a placeholder just to get
the starter code to compile without warnings and run without undefined
behavior.
return false;
THIS IS A REQUIRED FUNCTION. YOU MAY NOT ALTER ITS HEADER NAME PARAMETERS,
OR RETURN TYPE IN ANY WAY.
Function: computevalue
Description: Computes and returns the value of the given arithmetic
expression. This function should ASSUME that the given string is indeed
a valid arithmetic expression. That is your program should check whether
a string contains a valid arithmetic expression eg using your
isvalidexpression function BEFORE passing it into this function.
Parameters:
expression std::string: A string holding the arithmetic expression
to be evaluated.
Returns double: The value of the given arithmetic expression, computed
according to the rules described in the assignment document
For example: computevalue should return assuming you don't
complete the orderofoperations extra credit.
double computevaluestd::string expression
TODO Complete this function. You may create other functions above and have
this function call them, if you'd like you probably should for the sake
of the course's style guidelines
TODO Remove the below return statement. It's a placeholder just to get
the starter code to compile without warnings and run without undefined
behavior.
return ;
Also I wasnt allowed to use AI
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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