Answer the question
In order to leave comments, you need to log in
An exercise from Stroustrup's book. Mini calculator program. Numbers written in string format. What is meant in this context?
Hi all.
I can not understand how it is necessary to change the program based on the conditions of the exercise.
The exercise is worded like this:
Modify the mini-calculator described in Exercise 4 so that it accepts numbers as input, written in numerical and string format.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
double first;
double second;
double result;
char operation;
while (cin >> first >> second >> operation)
{
switch (operation){
case '+': result=first+second;
cout << "Сумма " << first << " и " << second << " равна " << result << endl;
break;
case '-': result=first-second;
cout << "Разность " << first << " и " << second << " равна " << result << endl;
break;
case '*': result=first*second;
cout << "Произведение " << first << " и " << second << " равно " << result << endl;
break;
case '/': result=first/second;
cout << "Частное " << first << " и " << second << " равно " << result << endl;
break;
case '%': result= fmod(first,second);
cout << "Остаток от деления " << first << " и " << second << " равен " << result << endl;
break;
default: cout << "Неизвестная операция" << endl; break;
}
digits written in numeric and string format
Answer the question
In order to leave comments, you need to log in
Oh, this translation. Read the original (stroustrup principles and practice using c++ 2014):
"Modify the “mini calculator” from exercise 5 to accept (just) single-digit numbers written as either digits or spelled out."
Numbers with only one digit written as text.
Numbers in string format - yes, just string. To perform arithmetic operations on them, you need to convert them to a number - see std::atoi, etc.
Looking ahead a little, the advantage of entering in a string format is that you can write the entire expression on one line, and then parse this one line. Those. you can make a universal calculator that takes not two arguments and an operation, but complex expressions. In addition to simple actions in this case, calculations of various mathematical functions, etc. can be screwed into the calculator.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question