Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int getLength(char first[]);
- int fromCharToInt(char arr[]);
- int Pow(int num, int pow);
- int main()
- {
- char* expression = new char[100];
- char* firstSum = new char[35];
- int firstNum = 0;
- while (true)
- {
- bool isValid = true;
- cout << "Enter an expression to calculate or write 'exit' to close the program:" << endl;
- cin.getline(expression, 100);
- char operation;
- if (expression[0] == 'e' && expression[1] == 'x' && expression[2] == 'i' && expression[3] == 't'&&expression[4]=='\0')
- return 0;
- //to do validation _|_
- int sizeOfArray = getLength(expression);
- for (int i = 0; i < sizeOfArray - 1; i++)
- {
- if (expression[0] == ' ')
- {
- cout << "This expression is invalid" << endl;
- isValid = false;
- break;
- }
- while (expression[i] != ' ')
- {
- int n = 0;
- firstSum[n] = expression[i];
- n++;
- if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] == '/' || expression[i] == '!')
- {
- operation = expression[i];
- }
- }
- //to do vsichko ostanalo
- if (expression[i] == ' ')
- {
- i++;
- continue;
- }
- }
- if (!isValid)
- continue;
- break;
- }
- return 0;
- }
- int getLength(char first[])
- {
- int count = 0;
- char* p = first;
- while (*p != '\0')
- {
- count++;
- p++;
- }
- return count;
- }
- int fromCharToInt(char arr[])
- {
- int size = getLength(arr);
- int number=0;
- int pow = size-1;
- for (int i = 0; i < size; i++)
- {
- number = (arr[i] - '0') * Pow(10,pow--);
- }
- return number;
- }
- int Pow(int num, int pow)
- {
- int result = 1;
- for (int i = 0; i < pow; i++){
- result *= num;
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement