Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int maxLenghtOfArray = 100; const int maxInputLenght = 1000;
- bool inputValidation(char input[]) {
- if (!((input[0] >= '0' && input[0] <= '9')||input[0]=='-')) {
- return false;
- }
- for (int i = 0;input[i] != '\0';i++) {
- if ((input[i] >= '0' && input[i] <= '9') || input[i] == ' ' || input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/' || input[i] == '!') {
- if ((input[i] >= '0' && input[i] <= '9') && ((input[i + 1] >= '0' && input[i + 1] <= '9') || input[i + 1] == ' ' || input[i + 1] == '!' || input[i + 1] == '\0')) {
- continue; //if the number after the symbol is correct
- }
- else if (input[i] == '!' && (input[i + 1] == ' ' || input[i + 1] == '\0')) {
- continue; //if the input after the factorial is valid
- }
- else if ((input[i] == '-')&& (input[i+1] >= '0' && input[i+1] <= '9')) {
- //if the typed number is negative
- int i_test = i+1;
- while ((input[i_test] >= '0' && input[i_test] <= '9')|| (input[i_test] == '!')) {
- //is '!' correctly used
- if (input[i_test] == '!') {
- return false;
- }
- i_test++;
- }
- continue;
- }
- else if ((input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/') && (input[i + 1] == ' ') && ((input[i + 2] >= '0' && input[i + 2] <= '9')||input[i+2]=='-') && (input[i + 1] != '\0')) {
- continue; //if the symbol after the operation is correct
- }
- else if (input[i] == ' ') {
- if (!(input[i - 1] == '+' || input[i - 1] == '-' || input[i - 1] == '*' || input[i - 1] == '/') && !(input[i + 1] == '+' || input[i + 1] == '-' || input[i + 1] == '*' || input[i + 1] == '/')) {
- return false; //if after space the input is correct
- }
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- return true;
- }
- bool exit(char input[]) {
- //exit from the console
- if (input[0] == 'e' && input[1] == 'x' && input[2] == 'i' && input[3] == 't') {
- return true;
- }
- else {
- return false;
- }
- }
- int convertDigitsIntoNumber(int digits[], int lenght, bool negativeNumber) {
- int copyOfLenght = lenght - 1;
- int number = 0;
- for (int i = 0;i < lenght;copyOfLenght--, i++) {
- number += digits[i] * pow(10, copyOfLenght);
- }
- if (negativeNumber == true) {
- number *= -1;
- return number;
- }
- return number;
- }
- int factorialCalculation(int number) {
- int factorial = 1;
- for (int i = 1;i <= number;i++) {
- factorial *= i;
- }
- return factorial;
- }
- int* convertInputNumbersFromCharToInt(char input[], int& numbersTyped) {
- numbersTyped = 0;
- bool factorial = false;
- bool negativeNumber = false;
- int intigerOfInputCharArray = 0; //input[intigerOfInputCharArray]
- int digits = 0; //how many digits a number has
- int digitsOfNumber[maxLenghtOfArray]; //saving the digits of a number when it's broken down form the char array
- int arrayOfNumbers[maxLenghtOfArray]; //save the actual numbers
- bool endOfInput = false; //when it reaches the end of the input
- while (input[intigerOfInputCharArray] != '\0') {
- digits = 0;
- factorial = false;
- if ((input[intigerOfInputCharArray] == '+' || input[intigerOfInputCharArray] == '-' || input[intigerOfInputCharArray] == '*' || input[intigerOfInputCharArray] == '/')) {
- if (input[intigerOfInputCharArray] == '-' && (input[intigerOfInputCharArray+1] >= '0' && input[intigerOfInputCharArray+1] <= '9')) {
- //is the number negative
- negativeNumber = true;
- intigerOfInputCharArray++;
- }
- else {
- //skip the operation symbols
- intigerOfInputCharArray += 2;
- }
- }
- else {
- while (input[intigerOfInputCharArray] != ' ') {
- //save the digits of the number until it reaches space
- if (input[intigerOfInputCharArray] == '\0') {
- endOfInput = true;
- break;
- }
- else if (input[intigerOfInputCharArray] == '!') {
- factorial = true;
- intigerOfInputCharArray++;
- }
- else {
- digitsOfNumber[digits] = input[intigerOfInputCharArray] - '0';
- intigerOfInputCharArray++; digits++;
- }
- }
- if (factorial == true) {
- //save the number after it's calculated as factorial
- arrayOfNumbers[numbersTyped] = factorialCalculation(convertDigitsIntoNumber(digitsOfNumber, digits,negativeNumber));
- numbersTyped++; intigerOfInputCharArray++;
- }
- else {
- //save the actual number in the array of numbers
- arrayOfNumbers[numbersTyped] = convertDigitsIntoNumber(digitsOfNumber, digits,negativeNumber);
- numbersTyped++; intigerOfInputCharArray++;
- negativeNumber = false;
- }
- }
- if (endOfInput == true) {
- break;
- }
- }
- return arrayOfNumbers;
- }
- char* operatorsFunction(char input[]) {
- char operators[maxLenghtOfArray];
- int intigerOfChar = 0; //operations [intigerOfChar]
- for (int i = 0;input[i] != '\0';i++) {
- if ((input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/')) {
- if (input[i] == '-' && (input[i + 1] >= '0' && input[i + 1] <= '9')) {
- //if the '-' is for negative number
- continue;
- }
- operators[intigerOfChar] = input[i];
- intigerOfChar++;
- }
- }
- operators[intigerOfChar] = '\0';
- return operators;
- }
- void calculationOfInput(int numbers[], char operators[], int& numbersTyped) {
- int answer = 0;
- bool error = false;
- if (numbersTyped == 1) {
- //if it's typed only one number
- cout<<"Answer: "<<numbers[0]<<endl;
- }
- else {
- for (int i = 0; i < numbersTyped - 1; i++) {
- switch (operators[i]){
- case '+':
- if (i == 0) {
- answer = numbers[i] + numbers[i + 1];
- }
- else {
- answer += numbers[i + 1];
- }
- break;
- case '-':
- if (i == 0) {
- answer = numbers[i] - numbers[i + 1];
- }
- else {
- answer -= numbers[i + 1];
- }
- break;
- case '*':
- if (i == 0) {
- answer = numbers[i] * numbers[i + 1];
- }
- else {
- answer *= numbers[i + 1];
- }
- break;
- case '/':
- if (i == 0) {
- if (numbers[i + 1] == 0) {
- error = true;
- cout << "Error! Can't divide by zero!" << endl;
- }
- else {
- answer = numbers[i] / numbers[i + 1];
- }
- }
- else {
- if (numbers[i + 1] == 0) {
- error = true;
- cout << "Error! Can't divide by zero!" << endl;
- }
- else {
- answer /= numbers[i + 1];
- }
- }
- break;
- }
- }
- if (error != true) {
- cout << "Answer: " << answer << endl;
- }
- }
- }
- int main() {
- char input[maxInputLenght];
- int numbers[maxLenghtOfArray];
- char operators[maxLenghtOfArray];
- bool stopTheCalculator = false;
- do {
- int numbersTyped = INT_MAX;
- do {
- cout << "Enter an expression to calculate or type \"exit\": " << endl;
- cin.getline(input, maxInputLenght);
- if (exit(input) == true) {
- stopTheCalculator = true;
- break;
- }
- if (inputValidation(input) == false) {
- cout << "This expression is invalid!" << endl;
- }
- } while (inputValidation(input) == false);
- if (stopTheCalculator != true) {
- for (int i = 0;i < numbersTyped;i++) {
- numbers[i] = convertInputNumbersFromCharToInt(input, numbersTyped)[i];
- }
- for (int i = 0;operatorsFunction(input)[i] != '\0';i++) {
- operators[i] = operatorsFunction(input)[i];
- }
- calculationOfInput(numbers, operators, numbersTyped);
- }
- } while (stopTheCalculator == false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement