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
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
83 changes: 83 additions & 0 deletions CPE200Lab1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C2B44135-F1AC-48A9-BD70-3BE2C38C2A15}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>CPE200Lab1</RootNamespace>
<AssemblyName>CPE200Lab1</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CalculatorEngine.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="MainForm.resx">
<DependentUpon>MainForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
223 changes: 223 additions & 0 deletions CalculatorEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPE200Lab1
{
class CalculatorEngine
{
private bool hasDot;
private bool isAllowBack;
private bool isAfterOperater;
private bool isAfterEqual;
private string firstOperand;
private string operate;



private string display = "0";

public string Display()
{
return display;
}



public string Calculator(string operate, string firstOperand, string secondOperand, int maxOutputSize = 8)
{
switch (operate)
{
case "+":
return (Convert.ToDouble(firstOperand) + Convert.ToDouble(secondOperand)).ToString();
case "-":
return (Convert.ToDouble(firstOperand) - Convert.ToDouble(secondOperand)).ToString();
case "X":
return (Convert.ToDouble(firstOperand) * Convert.ToDouble(secondOperand)).ToString();
case "÷":
// Not allow devide be zero
if (secondOperand != "0")
{
double result;
string[] parts;
int remainLength;

result = (Convert.ToDouble(firstOperand) / Convert.ToDouble(secondOperand));
// split between integer part and fractional part
parts = result.ToString().Split('.');
// if integer part length is already break max output, return error
if (parts[0].Length > maxOutputSize)
{
return "E";
}
// calculate remaining space for fractional part.
remainLength = maxOutputSize - parts[0].Length - 1;
// trim the fractional part gracefully. =
return result.ToString("N" + remainLength);
}
break;
case "%":
//your code here
break;
}
return "E";
}
public void resetAll()
{

display = "0";
isAllowBack = true;
hasDot = false;
isAfterOperater = false;
isAfterEqual = false;
}

public void Number(string h)
{


if (isAfterEqual)
{
resetAll();
}
if (isAfterOperater)
{
display = "0";
}
if (display.Length is 8)
{
return;
}
isAllowBack = true;
string digit = h;
if (display is "0")
{
display = "";
}
display += digit;
isAfterOperater = false;


}

public void handleOperater()
{
if (isAfterEqual)
{
resetAll();
}
if(display.Length is 8)
{
return;
}
if (display[0]is '-')
{
display = display.Substring(1, display.Length - 1);
}
else
{
display = "-" + display;
}




}
public void sum(string y)
{
if(display is "0")
{
display = "";
}
if (!isAfterOperater)
{
isAfterOperater = true;
isAfterEqual = false;
}
display += y;
isAllowBack = true;
}


public void Dot()
{
if (display is "Error")
{
return;
}
if (isAfterEqual)
{
resetAll();
}
if (display.Length is 8)
{
return;
}
if (!hasDot)
{
display += ".";
hasDot = true;
}
}

public void Sign()
{
if (display is "Error")
{
return;
}
if (isAfterEqual)
{
resetAll();
}
// already contain negative sign
if (display.Length is 8)
{
return;
}
if (display[0] is '-')
{
display = display.Substring(1, display.Length - 1);
}
else
{
display = "-" + display;
}
}

public void Back()
{
if (display is "Error")
{
return;
}
if (isAfterEqual)
{
return;
}
if (!isAllowBack)
{
return;
}
if (display != "0")
{
string current = display;
char rightMost = current[current.Length - 1];
if (rightMost is '.')
{
hasDot = false;
}
display = current.Substring(0, current.Length - 1);
if (display is "" || display is "-")
{
display = "0";
}
}
}


}

}
Loading