I wrote a program, I want a dot or comma to appear as a separator and an integer separator (at the moment when entering fractions separated by commas, for example, 2.1 the program closes CODE

#include <stdio.h> #include <math.h> #include <assert.h> #include <windows.h> #include <iostream> #include <locale> int tok; double tokval; int next() { for (;;) { int c = getchar(); if (c == EOF || strchr("+-*/^()\n", c) != NULL) return tok = c; if (isspace(c)) continue; if (isdigit(c) || c == '.') { ungetc(c, stdin); scanf(" %lf", &tokval); return tok = 'n'; } fprintf(stderr, "Bad character: %c\n", c); abort(); } } void skip(int t) { assert(tok == t); next(); } double expr(); double numpar() { if (tok == 'n') { double x = tokval; skip('n'); return x; } skip('('); double x = expr(); skip(')'); return x; } double factor() { double x = numpar(); if (tok == '^') { skip('^'); x = pow(x, factor()); } return x; } void exit() { } double term() { double x = factor(); for (;;) { if (tok == '*') { skip('*'); x *= factor(); } else if (tok == '/') { skip('/'); x /= factor(); } else return x; } } double expr() { double x = term(); for (;;) { if (tok == '+') { skip('+'); x += term(); } else if (tok == '-') { skip('-'); x -= term(); } else return x; } } int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); printf("Π˜Π½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡ:\n(*)ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅, (+)слоТСниС, (-)Π²Ρ‹Ρ‡ΠΈΡ‚Π°Π½ΠΈΠ΅, (/)Π΄Π΅Π»Π΅Π½ΠΈΠ΅, (^)Π²ΠΎΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅ Π² ΡΡ‚Π΅ΠΏΠ΅Π½ΡŒ\nΠŸΡ€ΠΈ использовании Π½Π΅ Ρ†Π΅Π»Ρ‹Ρ… чисСл ΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉΡ‚Π΅ΡΡŒ символом (.)\n"); printf("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅:\n"); next(); while (tok != EOF) { if (tok == '\n') { skip('\n'); continue; } printf("Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚:%.9g\n", expr()); } return 0; } 
  • And if you enter 2.1? - Alone_Fox
  • you need to be entered through a period and a comma - Vladislav Maslov
  • Well, in the case of a point works? - Alone_Fox
  • Yes, but it is necessary to provide a comma as a separator - Vladislav Maslov
  • If you need to work at the same time as a dot, and a comma, then for your code you need to make your scanf() - avp

3 answers 3

You can try to read a number in a string, change commas to points. And convert the string to double type.

To replace the comma with a point

  #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string s; getline(cin, s); replace( s.begin(), s.end(), ',', '.' ); cout << s; } 

If you do not translate into double, you can also make calculations with a string. For example:

  #include <iostream> #include <string> #include <cstdlib> int main() { string s; getline(cin, s); char *ptrEnd; cout << strtod(s.c_str(), &ptrEnd) + 1.0; } 

    Maybe it's in the locale, try std::setlocale( LC_ALL,"Russian" ); #include <locale> library #include <locale>

    • It will still be either a period or a comma (but not both at the same time) - avp
    • unfortunately will be OR OR .... - Vladislav Maslov

    Try to connect the cstring file. It worked in my online compiler (proof: https://repl.it/IVPi/1 )

    • entered through a comma in your compiler, still nothing works - Vladislav Maslov