Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #pragma warning(disable:4996)
- using namespace std;
- char ** words = new char*[101];
- char ** wordsBracket = new char*[101];
- double getResult(int wordFrom, int wordTo, int wordsCount) {
- double result = 0;
- double lastBracketResult = 0;
- for (int i = wordFrom; i < wordTo; i++)
- {
- if (strcmp(wordsBracket[i], "+") == 0) {
- if (strcmp(wordsBracket[i], ")") != 0)
- result += atof(wordsBracket[i - 1]);
- else
- result += lastBracketResult;
- }
- else if(strcmp(wordsBracket[i], "*") == 0) {
- int next = i + 1;
- double multiplyResult;
- double nextBracket = 0;
- if (strcmp(wordsBracket[i-1], ")") != 0)
- multiplyResult = atof(wordsBracket[i - 1]);
- else
- multiplyResult = lastBracketResult;
- do {
- if (strcmp(wordsBracket[next], "(") == 0) {
- int secondBracket = 0;
- while (strcmp(wordsBracket[secondBracket], ")"))
- secondBracket++;
- multiplyResult *= getResult(next + 1, secondBracket, wordsCount);
- next = secondBracket;
- }
- else if (strcmp(wordsBracket[next], "*") != 0) {
- multiplyResult *= atof(wordsBracket[next]);
- }
- next++;
- } while (next < wordTo && strcmp(wordsBracket[next], "+") != 0);
- result += multiplyResult;
- i = next;
- }
- else if (strcmp(wordsBracket[i], "(") == 0) {
- int secondBracket = 0;
- while (strcmp(wordsBracket[secondBracket], ")"))
- secondBracket++;
- if(secondBracket == wordTo - 1)
- result += getResult(i + 1, secondBracket, wordsCount);
- else
- lastBracketResult = getResult(i + 1, secondBracket, wordsCount);
- }
- else if(i == wordTo - 1){
- result += atof(wordsBracket[i]);
- }
- }
- return result;
- }
- int main(int argc, char ** argv) {
- char * str = new char[101];
- char ** wordsAfterMultyPly = new char*[101];
- char * word;
- char * plus = new char[2];
- strcpy(plus, "+");
- char * mult = new char[2];
- strcpy(mult, "*");
- int newWordsCount = 0;
- int wordsCount = 0;
- double result = 0;
- cin.getline(str, 101);
- word = strtok(str, " ");
- do {
- wordsBracket[wordsCount] = word;
- wordsCount++;
- } while ((word = strtok(NULL, " ")) != NULL);
- result = getResult(0, wordsCount, wordsCount);
- cout << result;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement