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 Cuogeihong/Calculator/.vs/Calculator/v15/.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.
41 changes: 41 additions & 0 deletions Cuogeihong/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 15
VisualStudioVersion = 15.0.28307.421
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Calculator", "Calculator\Calculator.vcxproj", "{FEF1EAB2-AA5E-4060-82D6-067820651E2D}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CalculatorUnitTest", "CalculatorUnitTest\CalculatorUnitTest.vcxproj", "{A5F81C3A-5DFB-463E-8C29-9F62507C1ADE}"
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
{FEF1EAB2-AA5E-4060-82D6-067820651E2D}.Debug|x64.ActiveCfg = Debug|x64
{FEF1EAB2-AA5E-4060-82D6-067820651E2D}.Debug|x64.Build.0 = Debug|x64
{FEF1EAB2-AA5E-4060-82D6-067820651E2D}.Debug|x86.ActiveCfg = Debug|Win32
{FEF1EAB2-AA5E-4060-82D6-067820651E2D}.Debug|x86.Build.0 = Debug|Win32
{FEF1EAB2-AA5E-4060-82D6-067820651E2D}.Release|x64.ActiveCfg = Release|x64
{FEF1EAB2-AA5E-4060-82D6-067820651E2D}.Release|x64.Build.0 = Release|x64
{FEF1EAB2-AA5E-4060-82D6-067820651E2D}.Release|x86.ActiveCfg = Release|Win32
{FEF1EAB2-AA5E-4060-82D6-067820651E2D}.Release|x86.Build.0 = Release|Win32
{A5F81C3A-5DFB-463E-8C29-9F62507C1ADE}.Debug|x64.ActiveCfg = Debug|x64
{A5F81C3A-5DFB-463E-8C29-9F62507C1ADE}.Debug|x64.Build.0 = Debug|x64
{A5F81C3A-5DFB-463E-8C29-9F62507C1ADE}.Debug|x86.ActiveCfg = Debug|Win32
{A5F81C3A-5DFB-463E-8C29-9F62507C1ADE}.Debug|x86.Build.0 = Debug|Win32
{A5F81C3A-5DFB-463E-8C29-9F62507C1ADE}.Release|x64.ActiveCfg = Release|x64
{A5F81C3A-5DFB-463E-8C29-9F62507C1ADE}.Release|x64.Build.0 = Release|x64
{A5F81C3A-5DFB-463E-8C29-9F62507C1ADE}.Release|x86.ActiveCfg = Release|Win32
{A5F81C3A-5DFB-463E-8C29-9F62507C1ADE}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EBAEF9CF-A961-4E51-BE7C-5E103AC34F9C}
EndGlobalSection
EndGlobal
3 changes: 3 additions & 0 deletions Cuogeihong/Calculator/Calculator.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpAutoNaming/IsNamingAutoDetectionCompleted/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpAutoNaming/IsNotificationDisabled/@EntryValue">True</s:Boolean></wpf:ResourceDictionary>
199 changes: 199 additions & 0 deletions Cuogeihong/Calculator/Calculator/Calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@


#include "pch.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() {
srand((unsigned int)time(NULL));
while (true) {
string formula = "";
int count = random(1, 2);
int start = 0;
int number1 = random(0, 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++;
}
if (Adjust(formula)) {
return formula; //采用暴力方法判断整除,存在很大问题,效率不好且答案很可能没有除法
}
}
};

bool Calculator::Adjust(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 - k + 1));
}
if (operatorStack->empty()) {
operatorStack->push(formulaChar);
}
else {
char stackChar = operatorStack->top();
if ((stackChar == '+' || stackChar == '-')
&& (formulaChar == '*' || formulaChar == '/')) {
operatorStack->push(formulaChar);
}
else {
tempStack->push_back(string(1, 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));
if (a1 % b1 != 0) {
return false;
}
}
}
}
return true;
}

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 - k + 1));
}
if (operatorStack->empty()) {
operatorStack->push(formulaChar);
}
else {
char stackChar = operatorStack->top();
if ((stackChar == '+' || stackChar == '-')
&& (formulaChar == '*' || formulaChar == '/')) {
operatorStack->push(formulaChar);
}
else {
tempStack->push_back(string(1, 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");
cout << ret << endl;
getchar();
}


19 changes: 19 additions & 0 deletions Cuogeihong/Calculator/Calculator/Calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include "pch.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);
bool Adjust(string formula);
};
Loading