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 Simple Calculator/Debug/Simple Calculator.exe
Binary file not shown.
Binary file added Simple Calculator/Debug/Simple Calculator.ilk
Binary file not shown.
Binary file added Simple Calculator/Debug/Simple Calculator.pdb
Binary file not shown.
Binary file added Simple Calculator/Simple Calculator.sdf
Binary file not shown.
28 changes: 28 additions & 0 deletions Simple Calculator/Simple Calculator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Simple Calculator", "Simple Calculator\Simple Calculator.vcxproj", "{4D2C7175-4FBD-42C8-A84D-72AF9C818A84}"
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
{4D2C7175-4FBD-42C8-A84D-72AF9C818A84}.Debug|x64.ActiveCfg = Debug|x64
{4D2C7175-4FBD-42C8-A84D-72AF9C818A84}.Debug|x64.Build.0 = Debug|x64
{4D2C7175-4FBD-42C8-A84D-72AF9C818A84}.Debug|x86.ActiveCfg = Debug|Win32
{4D2C7175-4FBD-42C8-A84D-72AF9C818A84}.Debug|x86.Build.0 = Debug|Win32
{4D2C7175-4FBD-42C8-A84D-72AF9C818A84}.Release|x64.ActiveCfg = Release|x64
{4D2C7175-4FBD-42C8-A84D-72AF9C818A84}.Release|x64.Build.0 = Release|x64
{4D2C7175-4FBD-42C8-A84D-72AF9C818A84}.Release|x86.ActiveCfg = Release|Win32
{4D2C7175-4FBD-42C8-A84D-72AF9C818A84}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
57 changes: 57 additions & 0 deletions Simple Calculator/Simple Calculator/Calculator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Calculator.hpp
// Simple Calculator
//
// Created by Nicholas.L.Y on 2016/4/29.
// Copyright © 2016年 廖颖泓. All rights reserved.
//
#include <iostream>
#include <string>
using namespace std;
bool isNumber(char a) {
return (a >= '0' && a <= '9');
}
bool isOperator(char a) {
return (a == '+' || a == '-');
}
class calculator {
public:
calculator() {}
~calculator() {}
int calculate(string a) throw(ExpressionException,
IllegalSymbolException,
MissingOperatorException,
MissingOperandException,
EmptyExpressionException) {
if (a.empty()) throw EmptyExpressionException();
int len = a.length();
int amount = 0;
char _operator;
for (int i = 0; i < len; i++) {
if (!isNumber(a[i]) && !isOperator(a[i])) {
throw IllegalSymbolException(i);
}
else if (isOperator(a[i]) && i % 2 == 0) {
throw MissingOperandException(i);
}
else if (isNumber(a[i]) && i % 2 == 0) {
if (i == 0) {
amount = a[i] - '0';
} else {
if (_operator == '+') amount += a[i] - '0';
else if (_operator == '-') amount -= a[i] - '0';
}
}
else if (isOperator(a[i]) && i % 2 == 1) {
_operator = a[i];
}
else if (isNumber(a[i]) && i % 2 == 1) {
throw MissingOperatorException(i);
}
}
if (len % 2 == 0 && isOperator(a[len - 1])) {
throw MissingOperandException(len);
}
return amount;
}
};
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=v140:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=8.1
Debug|Win32|C:\Users\nicholasly\documents\visual studio 2015\Projects\Simple Calculator\|
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,4 @@
 Source.cpp
c:\users\nicholasly\documents\visual studio 2015\projects\simple calculator\simple calculator\calculator.hpp(25): warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
Simple Calculator.vcxproj -> C:\Users\nicholasly\documents\visual studio 2015\Projects\Simple Calculator\Debug\Simple Calculator.exe
Simple Calculator.vcxproj -> C:\Users\nicholasly\documents\visual studio 2015\Projects\Simple Calculator\Debug\Simple Calculator.pdb (Partial PDB)
Binary file not shown.
Binary file not shown.
Binary file not shown.
68 changes: 68 additions & 0 deletions Simple Calculator/Simple Calculator/Exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#ifndef EXCEPTION_HPP_
#define EXCEPTION_HPP_
#include <sstream>
#include <string>

class Utils {
public:
static std::string int2String(int num) {
std::stringstream ss;
ss << num;
return ss.str();
}
};

class Exception {
public:
virtual const std::string what() const throw() { return "Exception occors"; };
};

class ExpressionException : public Exception {
public:
virtual const std::string what() const throw() {
return "ExpressionException";
}
};

class IllegalSymbolException : public ExpressionException {
private:
int position;

public:
IllegalSymbolException(int position) : position(position) {}
virtual const std::string what() const throw() {
return "IllegalSymbolException at position:" + Utils::int2String(position);
}
};

class MissingOperatorException : public ExpressionException {
private:
int position;

public:
MissingOperatorException(int position) : position(position) {}
virtual const std::string what() const throw() {
return "Expected operator at position:" + Utils::int2String(position);
}
};

class MissingOperandException : public ExpressionException {
private:
int position;

public:
MissingOperandException(int position) : position(position) {}
virtual const std::string what() const throw() {
return "Expected operand at position:" + Utils::int2String(position);
}
};

class EmptyExpressionException : public ExpressionException {
public:
EmptyExpressionException() {}
virtual const std::string what() const throw() {
return "The expression is empty";
}
};

#endif
122 changes: 122 additions & 0 deletions Simple Calculator/Simple Calculator/Simple Calculator.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" 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">
<ProjectGuid>{4D2C7175-4FBD-42C8-A84D-72AF9C818A84}</ProjectGuid>
<RootNamespace>SimpleCalculator</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</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 />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Calculator.hpp" />
<ClInclude Include="Exception.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Source.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Exception.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Calculator.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Source.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
51 changes: 51 additions & 0 deletions Simple Calculator/Simple Calculator/Source.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "Exception.hpp"
#include "Calculator.hpp"
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main() {
calculator c;
std::string str;
bool flag;

while (cin >> str) {
flag = false;
try {
cout << c.calculate(str) << endl;
}
catch (EmptyExpressionException e) {
cout << e.what() << endl;
flag = true;
}
catch (MissingOperatorException e) {
cout << e.what() << endl;
flag = true;
}
catch (MissingOperandException e) {
cout << e.what() << endl;
flag = true;
}
catch (IllegalSymbolException e) {
cout << e.what() << endl;
flag = true;
}
catch (ExpressionException e) {
cout << e.what() << endl;
flag = true;
}
catch (Exception e) {
// unhandled exception
throw e;
}
if (!flag) {
std::cout << "No exception happened!" << std::endl;
}
}

return 0;
}