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 LastWhisper1/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.
151 changes: 151 additions & 0 deletions LastWhisper1/Calculator/Calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//#include "stdafx.h"
#include <stack>
#include <vector>
#include <iostream>
#include "stdlib.h"
#include <ctime>
#include <string>
#include "Calculator.h"

#define ALLOWDIVISION 0 //是否包含除法
//#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(2, 3);
int start = 0;
int number1 = random(1, 100);
formula += to_string(number1);
while (start < count) {
//目测这里没有实现除法结果一定为整数
//先不考虑除法
#ifdef ALLOWDIVISION
int operation = random(0, 3);
int number2 = 0;
if (operation==3) {
//除法
int factor = findFactor(number1);
if (factor == 1) {
//为了题目质量不生成a/1或a/a这样的算式
operation = random(0, 2);
number2 = random(1, 100);
}
else {
number2 = random(0, 1) ? factor : (number1/factor);
}
}
else {
number2 = random(1, 100);
}
#else
int operation = random(0, 2);
int number2 = 0;
number2 = random(1, 100);
#endif // ALLOWDIVISION
formula += op[operation] + to_string(number2);
number1 = 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) {
//string.str(start, length)
tempStack->push_back(formula.substr(k, j + 1 - k));
}
if (operatorStack->empty()) {
operatorStack->push(formulaChar);
}
else {
char stackChar = operatorStack->top();
if ((stackChar == '+' || stackChar == '-')
&& (formulaChar == '*' || formulaChar == '/')) {
operatorStack->push(formulaChar);
}
else {
//char被强转为int,按字符字面值转换为string
string ts = "a";
ts[0] = operatorStack->top();
tempStack->push_back(ts);
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();
srand((unsigned int)time(NULL));
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string question = calc->MakeFormula();
cout << question << endl;
string ret = calc->Solve(question);
cout << ret << endl;
}
//getchar();
}


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

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

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

inline int findFactor(int num) {
//��һ��100���ڵ�����������Ѱ������
//����Ǻ���������2~10�����������һ������
//���������������1
int farr[9] = {0};
int fcnt = 0;
for (int i = 2; i <= 10; i++) {
if (num % i == 0 && num != i) {
farr[fcnt++] = i;
}
}
if (fcnt == 0) return 1;
else {
//srand((unsigned int)time(NULL));
return farr[random(0, fcnt-1)];
}
}
41 changes: 41 additions & 0 deletions LastWhisper1/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.vcxproj", "{10999050-754C-4FC2-B12B-A6A7068EF828}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CalculatorUnitTest", "..\UnitTest1\UnitTest1.vcxproj", "{E173A9F9-AEE4-484C-AE55-2AB452EE4D7F}"
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
{10999050-754C-4FC2-B12B-A6A7068EF828}.Debug|x64.ActiveCfg = Debug|x64
{10999050-754C-4FC2-B12B-A6A7068EF828}.Debug|x64.Build.0 = Debug|x64
{10999050-754C-4FC2-B12B-A6A7068EF828}.Debug|x86.ActiveCfg = Debug|Win32
{10999050-754C-4FC2-B12B-A6A7068EF828}.Debug|x86.Build.0 = Debug|Win32
{10999050-754C-4FC2-B12B-A6A7068EF828}.Release|x64.ActiveCfg = Release|x64
{10999050-754C-4FC2-B12B-A6A7068EF828}.Release|x64.Build.0 = Release|x64
{10999050-754C-4FC2-B12B-A6A7068EF828}.Release|x86.ActiveCfg = Release|Win32
{10999050-754C-4FC2-B12B-A6A7068EF828}.Release|x86.Build.0 = Release|Win32
{E173A9F9-AEE4-484C-AE55-2AB452EE4D7F}.Debug|x64.ActiveCfg = Debug|x64
{E173A9F9-AEE4-484C-AE55-2AB452EE4D7F}.Debug|x64.Build.0 = Debug|x64
{E173A9F9-AEE4-484C-AE55-2AB452EE4D7F}.Debug|x86.ActiveCfg = Debug|Win32
{E173A9F9-AEE4-484C-AE55-2AB452EE4D7F}.Debug|x86.Build.0 = Debug|Win32
{E173A9F9-AEE4-484C-AE55-2AB452EE4D7F}.Release|x64.ActiveCfg = Release|x64
{E173A9F9-AEE4-484C-AE55-2AB452EE4D7F}.Release|x64.Build.0 = Release|x64
{E173A9F9-AEE4-484C-AE55-2AB452EE4D7F}.Release|x86.ActiveCfg = Release|Win32
{E173A9F9-AEE4-484C-AE55-2AB452EE4D7F}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6B339595-7336-4F22-823C-CFF587CCD2A3}
EndGlobalSection
EndGlobal
158 changes: 158 additions & 0 deletions LastWhisper1/Calculator/Calculator.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{10999050-754C-4FC2-B12B-A6A7068EF828}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>Calculator</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Calculator.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Calculator.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Loading