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
Binary file added Junhaoo/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 Junhaoo/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.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Calculator", "Calculator\Calculator.vcxproj", "{64C7DE36-DE34-460F-968C-CB1190D878EA}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CalculatorUnitTest", "CalculatorUnitTest\CalculatorUnitTest.vcxproj", "{34628346-6539-4624-9793-59D9240663F7}"
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
{64C7DE36-DE34-460F-968C-CB1190D878EA}.Debug|x64.ActiveCfg = Debug|x64
{64C7DE36-DE34-460F-968C-CB1190D878EA}.Debug|x64.Build.0 = Debug|x64
{64C7DE36-DE34-460F-968C-CB1190D878EA}.Debug|x86.ActiveCfg = Debug|Win32
{64C7DE36-DE34-460F-968C-CB1190D878EA}.Debug|x86.Build.0 = Debug|Win32
{64C7DE36-DE34-460F-968C-CB1190D878EA}.Release|x64.ActiveCfg = Release|x64
{64C7DE36-DE34-460F-968C-CB1190D878EA}.Release|x64.Build.0 = Release|x64
{64C7DE36-DE34-460F-968C-CB1190D878EA}.Release|x86.ActiveCfg = Release|Win32
{64C7DE36-DE34-460F-968C-CB1190D878EA}.Release|x86.Build.0 = Release|Win32
{34628346-6539-4624-9793-59D9240663F7}.Debug|x64.ActiveCfg = Debug|x64
{34628346-6539-4624-9793-59D9240663F7}.Debug|x64.Build.0 = Debug|x64
{34628346-6539-4624-9793-59D9240663F7}.Debug|x86.ActiveCfg = Debug|Win32
{34628346-6539-4624-9793-59D9240663F7}.Debug|x86.Build.0 = Debug|Win32
{34628346-6539-4624-9793-59D9240663F7}.Release|x64.ActiveCfg = Release|x64
{34628346-6539-4624-9793-59D9240663F7}.Release|x64.Build.0 = Release|x64
{34628346-6539-4624-9793-59D9240663F7}.Release|x86.ActiveCfg = Release|Win32
{34628346-6539-4624-9793-59D9240663F7}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B0BACD9C-B88C-40A3-851B-CE7D5196E79B}
EndGlobalSection
EndGlobal
228 changes: 228 additions & 0 deletions Junhaoo/Calculator/Calculator/Calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
//#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(2, 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()

{

Calculator* calc = new Calculator();

string question = calc->MakeFormula();

cout << question << endl;

string ret = calc->Solve("11+22+33");

cout << ret << endl;

//getchar();

}

18 changes: 18 additions & 0 deletions Junhaoo/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