Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Calculator v0.1/Calculator/Calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

#include <iostream>
#include <stack>
#include "calcul.h"
using namespace std;

int main()
{
calcul cal;
char c;
while (c=cin.peek())
{
if (c == '#') break;//��������� ���� ������
cal.readthread();
cal.maths();
cin.ignore();
}


}
160 changes: 160 additions & 0 deletions Calculator v0.1/Calculator/calcul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#include "calcul.h"

int priority(char c)//���������� ��������
{
if ((c == '+') || (c == '-')) return 1;
if ((c == '*') || (c == '/')) return 2;
return 0;
}

void calcul::processing_oper(char c)//��������� ���������� ��������
{
if ((operation.size() == 0) || (c == '('))//����������� ������ � ����� ������ ���� � ������ �����, ���� ����� � ���� ������ ������ ��������
{
operation.push(c);
cin.ignore();//��������� ������������� ������ �� ������
return;
}

if (priority(c) <= priority(operation.top()))
//���� � ��������������� �������� ��������� ������ ��� ����� ��� ������� �� ����� ��������, �� ������� ��������� ��������, ������� ���� �� �����, � ����� ������� �����
{
oper();
operation.push(c);
cin.ignore();//��������� ������������� ������ �� ������
return;
}
//���� � ���������� �� ���� � ��������� ��������������� �������� �� ������, ��� ��� ���������� �� ����
operation.push(c);
cin.ignore();//��������� ������������� ������ �� ������
return;
}

bool calcul::check_neg(char c)//��������� ������������� �����, ���� ��� ������ ����� � ��������� ��� ��� ����� ����������� ��������
{
if (c == '-')
{
if (number.size() == 0)
return true;
if ((operation.top() == '(') && (operation.size() > number.size()))//����� - ����� ����� ������, �� ���������� �������� ��������� ���������� ��������� �����
{
return true;
}
return false;//c='-' �� �� ���� �� ������� �� ���������
}
return false;
}

void calcul::readthread()
{
char c;
while (c = cin.peek())
{
if (c == '\n') break;
if (c == ' ')
{
cin.ignore();
continue;
};
if (((c >= '0') && (c <= '9')) || check_neg(c))//������� ����� �� 0 �� 9 +
{
double value;
cin >> value;//��������� ����� �� ������
number.push(value);
continue;
}
if ((c == '+') || (c == '-') || (c == '/') || (c == '*') || (c == '('))
{
processing_oper(c);
continue;
}
if (c == ')')
{
while (operation.top() != '(')//��������� ������� � ���������
{
oper();
}
operation.pop();//������� ������������� ������
cin.ignore();//��������� ������������� ������ �� ������
continue;
}

cout << "Expression entered incorrectly" << endl;
break;

}
}

calcul::calcul()
{

}


void calcul::maths()//����� ���������� ��� �������������� �������� � �������
{
while (operation.size() != 0)
{
oper();
}
if (number.size() != 0) cout << "Result=" << number.top() << endl;
number.pop();//������� ��������� ����� � �����
}

void calcul::oper()//������������ ������ ���� �������� � �������
{
double a, b, c;
a = number.top();//����� �������� ����� �� �����
number.pop();//������� ������ ����� �� �����
b=number.top();//����� �������� ����� �� �����
number.pop();//������� ������ ����� �� �����
//���� �������� �� ����� � ����������
switch (operation.top())
{
case '+': sum(a, b); break;
case '-': substraction(b, a); break;//������ �������. ��� ��� ������� � ����� ���������� �� ������������ ������� � �������
case '*': multiplication(a, b); break;
case '/': division(b, a); break;//������ �������. ��� ��� ������� � ����� ���������� �� ������������ ������� � �������
}
if (operation.size()!=0) operation.pop();//������� �������� �� ����� ��� �������, ��� �� �� ����, ����� ������� ������� �� 0
}

void calcul::sum(double a, double b)
{
double c = a + b;
number.push(c);//����� ��������� � ���� � �������
}
void calcul::substraction(double a, double b)
{
double c = a - b;
number.push(c);//����� ��������� � ���� � �������
}


template <typename T>//������, ��� ��� � ��� ��� ����� ������ �����
void clearStack(stack <T>& s)
{
while (s.size() != 0) {
s.pop();
}
return;
}

void calcul::division(double a, double b)
{
if (b == 0)
{
cout<<"Error. You can't divide by zero";
//����� ������ �� ������������ ��������, ������� ����� ����� � ��������
clearStack(operation);
clearStack(number);
return;
}
double c = a / b;
number.push(c);//����� ��������� � ���� � �������
}
void calcul::multiplication(double a, double b)
{
double c = a * b;
number.push(c);//����� ��������� � ���� � �������
}
33 changes: 33 additions & 0 deletions Calculator v0.1/Calculator/calcul.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#ifndef calcul_h
#define calcul_h

#include <iostream>
#include <stack>
#include <string>
#include <sstream>

using namespace std;

class calcul
{
private:
stack <double> number;
stack <char> operation;

public:
calcul();
void readthread();
bool check_neg(char c);
void maths();
void oper();
void processing_oper(char c);
void sum(double a, double b);
void substraction(double a, double b);
void division(double a, double b);
void multiplication(double a, double b);



};
#endif
31 changes: 31 additions & 0 deletions Calculator/Calculator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32819.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Calculator", "Calculator\Calculator.vcxproj", "{EB43910F-C3D2-44CF-A1FF-855A2AA6EE51}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EB43910F-C3D2-44CF-A1FF-855A2AA6EE51}.Debug|x64.ActiveCfg = Debug|x64
{EB43910F-C3D2-44CF-A1FF-855A2AA6EE51}.Debug|x64.Build.0 = Debug|x64
{EB43910F-C3D2-44CF-A1FF-855A2AA6EE51}.Debug|x86.ActiveCfg = Debug|Win32
{EB43910F-C3D2-44CF-A1FF-855A2AA6EE51}.Debug|x86.Build.0 = Debug|Win32
{EB43910F-C3D2-44CF-A1FF-855A2AA6EE51}.Release|x64.ActiveCfg = Release|x64
{EB43910F-C3D2-44CF-A1FF-855A2AA6EE51}.Release|x64.Build.0 = Release|x64
{EB43910F-C3D2-44CF-A1FF-855A2AA6EE51}.Release|x86.ActiveCfg = Release|Win32
{EB43910F-C3D2-44CF-A1FF-855A2AA6EE51}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BE3AC232-DA28-4578-827B-9660B65419AC}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions Calculator/Calculator/Calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

#include <iostream>
#include <stack>
#include "calcul.h"
using namespace std;

int main()
{
calcul cal;

cal.maths();

}
Loading