Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cctype>
- enum { MAX_SIZE = 1000 };
- char *x;
- void inc_exp(){
- std::cout << "ERROR: incorrect expression.\n";
- std::exit(1);
- }
- int parseFactor() {
- while (isspace(*x)) {
- x++;
- }
- int value = 0, sign = 1;
- if (*x == '+' || *x == '-') {
- if (*x == '-') {
- sign = -1;
- }
- x++;
- }
- while (isspace(*x)) {
- x++;
- }
- if (isdigit(*x)) {
- while (isdigit(*x)) {
- value = value * 10 + *x - '0';
- x++;
- }
- return sign * value;
- }
- else {
- inc_exp();
- }
- }
- int parseMultDiv() {
- int fac1 = parseFactor();
- while (*x == '*' || *x == '/') {
- char temp = *x;
- x++;
- int fac2 = parseFactor();
- if (temp == '*') {
- fac1 *= fac2;
- }
- else {
- if (fac2 == 0) inc_exp();
- fac1 /= fac2;
- }
- }
- return fac1;
- }
- int parseSumSub() {
- int pro1 = parseMultDiv();
- while (*x == '+' || *x == '-') {
- int sign = 1;
- if (*x == '-') sign = -1;
- ++x;
- int pro2 = parseMultDiv();
- pro1 = pro1 + sign * pro2;
- }
- if (*x != '\0') {
- inc_exp();
- }
- return pro1;
- }
- int main(int argc, char **argv) {
- char norm_exp[MAX_SIZE];
- x = norm_exp;
- if (argc == 1) {
- inc_exp();
- }
- for (int i = 1; i < argc; i++) {
- for (int j = 0; j < len(argc[i]); j++) {
- *x++ = argc[j];
- }
- }
- if (*(x - 1) != '\"' || norm_exp != '\"') {
- inc_exp();
- }
- *(x - 1) = '\0';
- x = norm_exp + 1;
- std::cout << parseSumSub();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement