F
F
flash_back2016-02-10 11:39:12
C++ / C#
flash_back, 2016-02-10 11:39:12

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.

Exercise 4 done, here is the code:
#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;
    }

I can't figure out what it means:
digits written in numeric and string format

Like in numerical and so we enter. And in string it's just string, but why? And if completely in words, it is somehow difficult to exercise one of the introductory chapters of a textbook for beginners. And what do you think?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Mironov, 2016-02-10
@flash_back

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.

R
res2001, 2016-02-10
@res2001

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 question

Ask a Question

731 491 924 answers to any question