- #include <iostream>
- #include <cstring>
-
- using namespace std;
-
- void chang_opmu (char*); // Change les signes - unaire par un 'n' pour ne pas les confondres avec les - binaire (de soustraction)
- int eval (char*); // Evalue l'expression passée en paramètre
- int str_to_int (char*); // Convertie un nombre sous forme chaînée, en type int ( "123"(str) -> 123(int) )
-
- int main() {
- char expr[201];
- cin >> expr;
- chang_opmu(expr);
- cout << eval(expr);
-
- return 0;
- }
-
- void chang_opmu (char* str) {
- if (str[0] == '-') str[0]='n';
- for (int i=1; i<255; i++)
- if ( (str[i] == '-') && (!isdigit(str[i-1])) ) str[i]='n';
- }
-
- int eval (char *str) {
- char* p=NULL;
- int total = 0;
- char *a=strrchr(str, '+'), *b=strrchr(str, '-'), *c=strrchr(str, '*'), *d=strrchr(str, '/'); // Pour éviter d'appeller plusieurs fois les mêmes fonctions
- if (a || b) {
- p = (a>b)?a:b;
- if (*p=='+') {
- *p = '\0';
- total = eval(str);
- total += eval(++p);
- }
- else if (*p=='-') {
- *p = '\0';
- total = eval(str);
- total -= eval(++p);
- }
- return total;
- }
- else if (c || d) {
- p = (c>d)?c:d;
- if (*p=='*') {
- *p = '\0';
- total = eval(str);
- total *= eval(++p);
- }
- else if (*p=='/') {
- *p = '\0';
- total = eval(str);
- total /= eval(++p);
- }
- return total;
- }
- else return str_to_int(str);
- }
-
- int str_to_int (char* str) {
- int total=0;
- int i = (str[0]=='n')?1:0;
- while (('0' <= str[i]) && (str[i] <= '9')) {
- total *= 10;
- total += (int(str[i]) -48);
- i++;
- }
- if (str[0]=='n') return -total;
- else return total;
- }
#include <iostream>
#include <cstring>
using namespace std;
void chang_opmu (char*); // Change les signes - unaire par un 'n' pour ne pas les confondres avec les - binaire (de soustraction)
int eval (char*); // Evalue l'expression passée en paramètre
int str_to_int (char*); // Convertie un nombre sous forme chaînée, en type int ( "123"(str) -> 123(int) )
int main() {
char expr[201];
cin >> expr;
chang_opmu(expr);
cout << eval(expr);
return 0;
}
void chang_opmu (char* str) {
if (str[0] == '-') str[0]='n';
for (int i=1; i<255; i++)
if ( (str[i] == '-') && (!isdigit(str[i-1])) ) str[i]='n';
}
int eval (char *str) {
char* p=NULL;
int total = 0;
char *a=strrchr(str, '+'), *b=strrchr(str, '-'), *c=strrchr(str, '*'), *d=strrchr(str, '/'); // Pour éviter d'appeller plusieurs fois les mêmes fonctions
if (a || b) {
p = (a>b)?a:b;
if (*p=='+') {
*p = '\0';
total = eval(str);
total += eval(++p);
}
else if (*p=='-') {
*p = '\0';
total = eval(str);
total -= eval(++p);
}
return total;
}
else if (c || d) {
p = (c>d)?c:d;
if (*p=='*') {
*p = '\0';
total = eval(str);
total *= eval(++p);
}
else if (*p=='/') {
*p = '\0';
total = eval(str);
total /= eval(++p);
}
return total;
}
else return str_to_int(str);
}
int str_to_int (char* str) {
int total=0;
int i = (str[0]=='n')?1:0;
while (('0' <= str[i]) && (str[i] <= '9')) {
total *= 10;
total += (int(str[i]) -48);
i++;
}
if (str[0]=='n') return -total;
else return total;
}