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
62 changes: 31 additions & 31 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
42 changes: 21 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License

Copyright (c) 2018 Qian

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT License
Copyright (c) 2018 Qian
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Binary file added Mistariano/Calculator/.vs/Calculator/v16/.suo
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
41 changes: 41 additions & 0 deletions Mistariano/Calculator/Calculator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29306.81
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Calculator", "Calculator\Calculator.vcxproj", "{72B4CFE8-2DB3-4451-8DD7-64C5EBD99AC5}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CalculatorUnitTest", "CalculatorUnitTest\CalculatorUnitTest.vcxproj", "{2442C371-C497-487B-B946-C48C645DEB36}"
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
{72B4CFE8-2DB3-4451-8DD7-64C5EBD99AC5}.Debug|x64.ActiveCfg = Debug|x64
{72B4CFE8-2DB3-4451-8DD7-64C5EBD99AC5}.Debug|x64.Build.0 = Debug|x64
{72B4CFE8-2DB3-4451-8DD7-64C5EBD99AC5}.Debug|x86.ActiveCfg = Debug|Win32
{72B4CFE8-2DB3-4451-8DD7-64C5EBD99AC5}.Debug|x86.Build.0 = Debug|Win32
{72B4CFE8-2DB3-4451-8DD7-64C5EBD99AC5}.Release|x64.ActiveCfg = Release|x64
{72B4CFE8-2DB3-4451-8DD7-64C5EBD99AC5}.Release|x64.Build.0 = Release|x64
{72B4CFE8-2DB3-4451-8DD7-64C5EBD99AC5}.Release|x86.ActiveCfg = Release|Win32
{72B4CFE8-2DB3-4451-8DD7-64C5EBD99AC5}.Release|x86.Build.0 = Release|Win32
{2442C371-C497-487B-B946-C48C645DEB36}.Debug|x64.ActiveCfg = Debug|x64
{2442C371-C497-487B-B946-C48C645DEB36}.Debug|x64.Build.0 = Debug|x64
{2442C371-C497-487B-B946-C48C645DEB36}.Debug|x86.ActiveCfg = Debug|Win32
{2442C371-C497-487B-B946-C48C645DEB36}.Debug|x86.Build.0 = Debug|Win32
{2442C371-C497-487B-B946-C48C645DEB36}.Release|x64.ActiveCfg = Release|x64
{2442C371-C497-487B-B946-C48C645DEB36}.Release|x64.Build.0 = Release|x64
{2442C371-C497-487B-B946-C48C645DEB36}.Release|x86.ActiveCfg = Release|Win32
{2442C371-C497-487B-B946-C48C645DEB36}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1FCC377B-E534-46E6-87C1-01350F4E8047}
EndGlobalSection
EndGlobal
111 changes: 111 additions & 0 deletions Mistariano/Calculator/Calculator/Calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@


//#include "stdafx.h"
#include <stack>
#include <vector>
#include <iostream>
#include "stdlib.h"
#include <ctime>
#include <string>
#include "Calculator.h"

#define random(a,b) (rand()%(b-a+1)+a)

using namespace std;

Calculator::Calculator() {}

string Calculator::MakeFormula() {
string formula = "";
srand((unsigned int)time(NULL));
int count = random(1, 3);
int start = 0;
int number1 = random(1, 100);
formula += to_string(number1);
while (start <= count) {
int operation = random(0, 3);
int number2 = random(1, 100);
formula += op[operation] + to_string(number2);
start++;
}
return formula;
};

string Calculator::Solve(string formula) {
vector<string>* tempStack = new vector<string>();
stack<char>* operatorStack = new stack<char>();
int len = formula.length();
int k = 0;
for (int j = -1; j < len - 1; j++) {
char formulaChar = formula[j + 1];
if (j == len - 2 || formulaChar == '+' || formulaChar == '-' ||
formulaChar == '*' || formulaChar == '/') {
if (j == len - 2) {
tempStack->push_back(formula.substr(k));
} else {
if (k < j) {
tempStack->push_back(formula.substr(k, j + 1));
}
if (operatorStack->empty()) {
operatorStack->push(formulaChar);
} else {
char stackChar = operatorStack->top();
if ((stackChar == '+' || stackChar == '-')
&& (formulaChar == '*' || formulaChar == '/')) {
operatorStack->push(formulaChar);
} else {
tempStack->push_back(to_string(operatorStack->top()));
operatorStack->pop();
operatorStack->push(formulaChar);
}
}
}
k = j + 2;
}
}
while (!operatorStack->empty()) {
tempStack->push_back(string(1, operatorStack->top()));
operatorStack->pop();
}
stack<string>* calcStack = new stack<string>();
for (int i = 0; i < tempStack->size(); i++) {
string peekChar = tempStack->at(i);
if (peekChar != "+" && peekChar != "-"
&& peekChar != "/" && peekChar != "*") {
calcStack->push(peekChar);
} else {
int a1 = 0;
int b1 = 0;
if (!calcStack->empty()) {
b1 = stoi(calcStack->top());
calcStack->pop();
}
if (!calcStack->empty()) {
a1 = stoi(calcStack->top());
calcStack->pop();
}
if (peekChar == "+") {
calcStack->push(to_string(a1 + b1));
} else if (peekChar == "-") {
calcStack->push(to_string(a1 - b1));
} else if (peekChar == "*") {
calcStack->push(to_string(a1 * b1));
} else if (peekChar == "/") {
calcStack->push(to_string(a1 / b1));
}
}
}
return formula + "=" + calcStack->top();
}

int main() {
for (int i = 0; i < 10000000; i++) {
Calculator* calc = new Calculator();
string question = calc->MakeFormula();
cout << question << endl;
string ret = calc->Solve("11+22");
cout << ret << endl;
}
}


18 changes: 18 additions & 0 deletions Mistariano/Calculator/Calculator/Calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
//#include "stdafx.h"
#include <stack>
#include <vector>
#include <iostream>
#include "stdlib.h"
#include <ctime>
#include <string>
using namespace std;

class Calculator {
private:
string op[4] = { "+", "-", "*", "/" };
public:
Calculator();
string MakeFormula();
string Solve(string formula);
};
Loading