Question

In: Computer Science

handwriten code for calculate button be able to delcare varables and name constants,use parse to capture...

handwriten code for calculate button be able to delcare varables and name constants,use parse to capture input use formulas in assignment statments,and display formated output on a form

Solutions

Expert Solution

Note :- This code is written in C++ program.

1)It will take arguments through command line, two operands

2) It will parse the arguments stored in argv to get the operation ( + , - , * , / ) that is to be performed.

3) And then it will convert the String to Integer (stoi function)  to do the arithmetic operation.

4) Final Result will be displayed.

--------------------------------------------------------------------------------------------------------------------------------------------------------

#include <iostream>
#include <string>
using namespace std;

main (int argc, char **argv)
{
std::string token;
size_t pos = 0;
std::string inputStr;
std::string delimiterPlus = "+";
std::string delimiterSub = "-";
std::string delimiterMul = "*";
std::string delimiterDiv = "/";
int FinalResult = 0;
int opr1,opr2=0;
bool operatorFlag = false;
  
cout << "You have entered " << argc << " arguments:" << "\n";

for (int i = 0; i < argc; ++i)
{
cout << "argv= "<<argv[i] << "\n";
inputStr = argv[i];
}


//Addition of 2 numbers
while ((pos = inputStr.find (delimiterPlus)) != std::string::npos)
{
//int opr1,opr2;
token = inputStr.substr (0, pos);
//std::cout << "token Plus=" <<token << std::endl;
opr1=stoi(token);
operatorFlag = true;
inputStr.erase (0, pos + delimiterPlus.length ());
}
//cout << "LastInput Plus=" << inputStr << std::endl;
opr2=stoi(inputStr);
  
if(operatorFlag){
FinalResult = opr1 + opr2;
operatorFlag = false;
}
  
//Substraction of two numbers
while ((pos = inputStr.find (delimiterSub)) != std::string::npos)
{
token = inputStr.substr (0, pos);
//std::cout << "token Sub=" <<token << std::endl;
opr1=stoi(token);
operatorFlag = true;
inputStr.erase (0, pos + delimiterSub.length ());
}
  
// cout << "Opr1 Sub=" << opr1 << std::endl;
  
// cout << "LastInput Sub=" << inputStr << std::endl;
opr2=stoi(inputStr);
  
if(operatorFlag){
FinalResult = opr1 - opr2;
operatorFlag = false;
}
      
//Multiplication of two numbers
while ((pos = inputStr.find (delimiterMul)) != std::string::npos)
{
token = inputStr.substr (0, pos);
// std::cout << "token mul=" <<token << std::endl;
opr1=stoi(token);
operatorFlag = true;
inputStr.erase (0, pos + delimiterMul.length ());
}
  
//cout << "Opr1 mul=" << opr1 << std::endl;
  
//cout << "LastInput mul=" << inputStr << std::endl;
opr2=stoi(inputStr);
  
if(operatorFlag){
FinalResult = opr1 * opr2;
operatorFlag = false;
}
  
//Division of two numbers
while ((pos = inputStr.find (delimiterDiv)) != std::string::npos)
{
token = inputStr.substr (0, pos);
//std::cout << "token Div=" <<token << std::endl;
opr1=stoi(token);
operatorFlag = true;
inputStr.erase (0, pos + delimiterDiv.length ());
}

// cout << "Opr1 Div=" << opr1 << std::endl;
  
//cout << "LastInput Div=" << inputStr << std::endl;
opr2=stoi(inputStr);
  
if(operatorFlag){
FinalResult = opr1/opr2;
operatorFlag = false;
}

  
cout<<"Final Result= " << FinalResult <<std::endl;
  
return 0;
}


Related Solutions

Parse string java code Write a recursive program that can calculate the value of a given...
Parse string java code Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x Using index of for -/+
My Javascript code isn't working (when i press calculate button) - what's wrong with it ?...
My Javascript code isn't working (when i press calculate button) - what's wrong with it ? Car Rental Enter Number of Rental Days: Select Car Type: onclick= "priceofcar = 50;"/> Compact onclick= "priceofcar = 60;"/> Economy onclick= "priceofcar = 70;"/> Intermediate Select Loss Damage Waiver onclick= "damagewaiver='yes'"/> Yes onclick= "damagewaiver='no'"/> No damagewaiver = boxDays.value * 25;} else if (damagewaiver == No) { damagewaiver = 0; }/> Select Roadside Issues Coverage: onclick= "issuescoverage='yes'"/> Yes onclick= "issuescoverage='no'"/> No issuescoverage = boxDays.value *...
Use data from the table below to calculate the equilibrium constants at 25 ∘C for each...
Use data from the table below to calculate the equilibrium constants at 25 ∘C for each reaction. Standard Thermodynamic Quantities for Selected Substances at 25∘C Substance ΔH∘f(kJ/mol) ΔG∘f(kJ/mol) S∘(J/mol⋅K) H2(g) 0 0 130.7 N2(g) 0 0 191.6 Br2(g) 30.9 3.1 245.5 Cl2(g) 0 0 223.1 NO2(g) 33.2 51.3 240.1 N2O4(g) 9.16 99.8 304.4 NH3(g) -45.9 -16.4 192.8 P4(g) 58.9 24.4 280.0 PH3(g) 5.4 13.5 210.2 Part A 2NO2(g)⇌N2O4(g) Find K Part B Br2(g)+Cl2(g)⇌2BrCl(g) ΔG∘f for BrCl(g) is -1.0 kJ/mol Find...
Use ?G?f values from Appendix IIB to calculate the equilibrium constants at 25 ?C for each...
Use ?G?f values from Appendix IIB to calculate the equilibrium constants at 25 ?C for each of the following reactions. a. 2NO2(g)?N2O4(g) b. 3/2H2(g)+1/4P4(g)?PH3(g)
Be able to identify the ASA Code of Ethics principles.
Be able to identify the ASA Code of Ethics principles.
Click the block of code below and hit the Run button above. In [6]: from statsmodels.formula.api...
Click the block of code below and hit the Run button above. In [6]: from statsmodels.formula.api import ols # create the multiple regression model with mpg as the response variable; weight and horsepower as predictor variables. model = ols('mpg ~ wt+hp', data=cars_df).fit() print(model.summary()) OLS Regression Results ============================================================================== Dep. Variable: mpg R-squared: 0.825 Model: OLS Adj. R-squared: 0.813 Method: Least Squares F-statistic: 63.86 Date: Thu, 13 Feb 2020 Prob (F-statistic): 5.81e-11 Time: 23:12:29 Log-Likelihood: -70.042 No. Observations: 30 AIC: 146.1 Df...
Using the aqueous equilibrium constants, calculate the concentration of OH- in M and pH for the...
Using the aqueous equilibrium constants, calculate the concentration of OH- in M and pH for the following solutions (given that all are at 25 degrees celsius): 0.15 M NaBrO 8.2×10−2 M NaHS. A mixture that is 0.13 M in NaNO2 and 0.25 M in Ca(NO2)2. Thank you! :)
Use BASH to make a code that takes any list of numbers and calculate the Median...
Use BASH to make a code that takes any list of numbers and calculate the Median and Mode.
How can I configure the button in this tip calculator to calculate the total using the...
How can I configure the button in this tip calculator to calculate the total using the entires for the bill and tip percentage? I'm using Visual Studio 2019 Xamarin.Forms Main.Page.xaml <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Accomplish_2.MainPage" BackgroundColor="Gray" Padding="5"> <StackLayout> <!-- Place new controls here --> <Label Text="Tip Calculator" HorizontalOptions="Center" VerticalOptions="Center" FontSize="Title" FontAttributes="Bold"/> <BoxView BackgroundColor="LightPink" HeightRequest="3"></BoxView>    <Entry Placeholder="Bill Total" Keyboard="Numeric" x:Name="billTotal"></Entry>    <Entry Placeholder="Tip Percentage" Keyboard="Numeric" x:Name="tipPercent"></Entry> <Button Text="Calculate" Clicked="Button_Clicked"></Button>    </StackLayout> </ContentPage> Everything above...
complete the code to determine the highest score value in python import random #variables and constants...
complete the code to determine the highest score value in python import random #variables and constants MAX_ROLLS = 5 MAX_DICE_VAL = 6 #declare a list of roll types ROLL_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "4 of a kind" ] pScore = 0 cScore = 0 num_Score = int( input ("Enter a number of round: ") ) print ("\n") count = 0 while count < num_Score: #set this to the value MAX_ROLLS pdice = [0,0,0,0,0]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT