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
34 changes: 34 additions & 0 deletions NSun-S/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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

*.vs
Binary file added NSun-S/Project1/Debug/Project1.ilk
Binary file not shown.
Binary file added NSun-S/Project1/Debug/Project1.pdb
Binary file not shown.
Binary file added NSun-S/Project1/Debug/UnitTest1.exp
Binary file not shown.
Binary file added NSun-S/Project1/Debug/UnitTest1.ilk
Binary file not shown.
Binary file added NSun-S/Project1/Debug/UnitTest1.pdb
Binary file not shown.
41 changes: 41 additions & 0 deletions NSun-S/Project1/Project1.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.29326.143
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Project1", "Project1\Project1.vcxproj", "{1F525612-0B6E-45C1-BCE5-06A9346E26CA}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UnitTest1", "UnitTest1\UnitTest1.vcxproj", "{4BA9AF09-E866-4BBF-85A1-803BA0067796}"
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
{1F525612-0B6E-45C1-BCE5-06A9346E26CA}.Debug|x64.ActiveCfg = Debug|x64
{1F525612-0B6E-45C1-BCE5-06A9346E26CA}.Debug|x64.Build.0 = Debug|x64
{1F525612-0B6E-45C1-BCE5-06A9346E26CA}.Debug|x86.ActiveCfg = Debug|Win32
{1F525612-0B6E-45C1-BCE5-06A9346E26CA}.Debug|x86.Build.0 = Debug|Win32
{1F525612-0B6E-45C1-BCE5-06A9346E26CA}.Release|x64.ActiveCfg = Release|x64
{1F525612-0B6E-45C1-BCE5-06A9346E26CA}.Release|x64.Build.0 = Release|x64
{1F525612-0B6E-45C1-BCE5-06A9346E26CA}.Release|x86.ActiveCfg = Release|Win32
{1F525612-0B6E-45C1-BCE5-06A9346E26CA}.Release|x86.Build.0 = Release|Win32
{4BA9AF09-E866-4BBF-85A1-803BA0067796}.Debug|x64.ActiveCfg = Debug|x64
{4BA9AF09-E866-4BBF-85A1-803BA0067796}.Debug|x64.Build.0 = Debug|x64
{4BA9AF09-E866-4BBF-85A1-803BA0067796}.Debug|x86.ActiveCfg = Debug|Win32
{4BA9AF09-E866-4BBF-85A1-803BA0067796}.Debug|x86.Build.0 = Debug|Win32
{4BA9AF09-E866-4BBF-85A1-803BA0067796}.Release|x64.ActiveCfg = Release|x64
{4BA9AF09-E866-4BBF-85A1-803BA0067796}.Release|x64.Build.0 = Release|x64
{4BA9AF09-E866-4BBF-85A1-803BA0067796}.Release|x86.ActiveCfg = Release|Win32
{4BA9AF09-E866-4BBF-85A1-803BA0067796}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BD6CF3B2-2C8B-4EF1-8A88-BB781BC881C8}
EndGlobalSection
EndGlobal
183 changes: 183 additions & 0 deletions NSun-S/Project1/Project1/Calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@


#include "stdafx.h"
#include <stack>
#include <vector>
#include <iostream>
#include <fstream>
#include <ios>
#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 = "";

int count = random(2, 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);
if (operation == 3)
{
while (!(number1 % number2 == 0))
{
number2 = random(1, number1);
}
number1 = number1 / number2;
}
else
{
number1 = number2;
}
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();
//cout << formula.substr(2, 3) << endl;
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 + 1)//////////////////////////////
{
tempStack->push_back(formula.substr(k, j + 1 - k));
//cout << formula.substr(k, j + 1) << k << j+1<< endl;
}
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()));
if ((formulaChar == '+' || formulaChar == '-'))
{
while (!operatorStack->empty())
{
tempStack->push_back(string(1, operatorStack->top()));
operatorStack->pop();

}
}
else
{
while (!operatorStack->empty()&&!(operatorStack->top()=='+'|| operatorStack->top() == '-'))
{
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(int argc, char* argv[])
{
int n;
n = stoi(argv[1]);
//cin >> n;
std::fstream output("subject.txt", ios::out);
Calculator* calc = new Calculator();
srand((unsigned int)time(NULL));
for(int i = 0; i < n; i++){
string question = calc->MakeFormula();
//cout << question << endl;
string ret = calc->Solve(question);
output << ret << endl;
//cout << ret << endl;
//getchar();
}
output.close();
}


19 changes: 19 additions & 0 deletions NSun-S/Project1/Project1/Calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#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);
};
3 changes: 3 additions & 0 deletions NSun-S/Project1/Project1/Debug/Project1.Build.CppClean.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
d:\study\软件工程\lab0\calculator\nsun-s\project1\project1\debug\vc142.idb
d:\study\软件工程\lab0\calculator\nsun-s\project1\project1\debug\vc142.pdb
d:\study\软件工程\lab0\calculator\nsun-s\project1\project1\debug\project1.tlog\cl.command.1.tlog
3 changes: 3 additions & 0 deletions NSun-S/Project1/Project1/Debug/Project1.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
 Calculator.cpp
D:\study\软件工程\lab0\Calculator\NSun-S\Project1\Project1\Calculator.cpp(121,20): warning C4018: “<”: 有符号/无符号不匹配
Project1.vcxproj -> D:\study\软件工程\lab0\Calculator\NSun-S\Project1\Debug\Project1.exe
Binary file added NSun-S/Project1/Project1/Debug/Project1.stdafx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#TargetFrameworkVersion=v4.0:PlatformToolSet=v142:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=10.0
Debug|Win32|D:\study\软件工程\lab0\Calculator\NSun-S\Project1\|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file added NSun-S/Project1/Project1/Debug/vc142.idb
Binary file not shown.
Binary file added NSun-S/Project1/Project1/Debug/vc142.pdb
Binary file not shown.
Loading