// CPP program to evaluate a given
// expression where tokens are
// separated by space.
#include <bits/stdc++.h>
using namespace std;
// Function to find precedence of
// operators.
int precedence(char op){
if(op == '+'||op == '-')
return 1;
if(op == '*'||op == '/')
return 2;
return 0;
}
// Function to perform arithmetic operations.
int applyOp(int a, int b, char op){
switch(op){
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return
a / b;
}
}
// Function that returns value of
// expression after evaluation.
int evaluate(string tokens){
int i;
// stack to store integer values.
stack <int> values;
// stack to store operators.
stack <char> ops;
for(i = 0; i < tokens.length();
i++){
// Current token is
a whitespace,
// skip it.
if(tokens[i] == '
')
continue;
// Current token is
an opening
// brace, push it to 'ops'
else
if(tokens[i] == '('){
ops.push(tokens[i]);
}
// Current token is
a number, push
// it to stack for
numbers.
else
if(isdigit(tokens[i])){
int
val = 0;
//
There may be more than one
//
digits in number.
while(i
< tokens.length() &&
isdigit(tokens[i]))
{
val
= (val*10) + (tokens[i]-'0');
i++;
}
values.push(val);
}
// Closing brace
encountered, solve
// entire
brace.
else if(tokens[i]
== ')')
{
while(!ops.empty()
&& ops.top() != '(')
{
int
val2 = values.top();
values.pop();
int
val1 = values.top();
values.pop();
char
op = ops.top();
ops.pop();
values.push(applyOp(val1,
val2, op));
}
//
pop opening brace.
ops.pop();
}
// Current token is
an operator.
else
{
//
While top of 'ops' has same or greater
//
precedence to current token, which
//
is an operator. Apply operator on top
//
of 'ops' to top two elements in values stack.
while(!ops.empty()
&& precedence(ops.top())
>=
precedence(tokens[i])){
int
val2 = values.top();
values.pop();
int
val1 = values.top();
values.pop();
char
op = ops.top();
ops.pop();
values.push(applyOp(val1,
val2, op));
}
//
Push current token to 'ops'.
ops.push(tokens[i]);
}
}
// Entire expression has been parsed at
this
// point, apply remaining ops to
remaining
// values.
while(!ops.empty()){
int val2 =
values.top();
values.pop();
int val1 =
values.top();
values.pop();
char op =
ops.top();
ops.pop();
values.push(applyOp(val1,
val2, op));
}
// Top of 'values' contains result, return
it.
return values.top();
}
int main() {
cout << evaluate("10 + 2 * 6")
<< "\n";
cout << evaluate("100 * 2 + 12")
<< "\n";
cout << evaluate("100 * ( 2 + 12 )")
<< "\n";
cout << evaluate("100 * ( 2 + 12 ) /
14");
return 0;
}
Add or Subtract. Write a function (add-sub-string s). The function consumes a Str of even length,...
Write a C function first_last that consumes a string argument and modifies it so that it only contains two characters the first, and the last. If there are less than two characters, leave the string alone. Return nothing For example: Test Result char str[]="hello"; first_last(str) printf("%s", str); ho
2. This question is about processing strings 2.1 [4 marks] Given a string str, we can calculate a checksum of the string by summing up the ASCII codes of the characters in the string. For example, the checksum of a string "apple" is 530, which equals to 'a' + 'p' + 'p' + 'l' + 'e' = 97 + 112 + 112 + 108 + 101. Write a function int calculate_checksum(char str[] that calculates and returns the checksum of the...
//Write a recursive function that, given an input string str, replaces all occurrences of a character a by a character b in str. (C++) void replace_chr(char *str, char a, char b){ }
using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...
Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...
using Dr racket language Write a function (add-mul L). The function consumes a (listof Int) It performs a series of mathematical operations. It starts with an answer of zero, then starting at the right, walks through the list L, and does the following operations: (1) If the next item in L is even, it adds the item to the answer so far. (2) If the next item on L is odd, it multiplies the answer by the item. For example,...
2) Write a function stringManip that takes in a character string and returns a character string following these rules: a) any vowel (a, e, i, o, u) is replaced by the character '&' b) any numeric character has 1 added to it (if it was 9, it becomes O instead) (str2num() is useful here) c) all lowercase characters become uppercase d) replace the final character with '!' e) append the length of the string before this step to the end...
Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...