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
2 changes: 1 addition & 1 deletion Calculator/Calculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public decimal Calc(decimal arg1, decimal arg2, string oper)
{
return _calculatorOperations[oper](arg1, arg2);
}
public decimal GetNumber(string s)
private static decimal GetNumber(string s)
{
const NumberStyles styles = NumberStyles.AllowDecimalPoint;
var provider = new CultureInfo("en-US");
Expand Down
35 changes: 35 additions & 0 deletions CalculatorASP/Calculator/Calculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Globalization;

namespace Calculator
{
public class Calculator
{
private readonly Dictionary<string, Func<decimal, decimal, decimal>> _calculatorOperations =
new Dictionary<string, Func<decimal, decimal, decimal>>()
{
{"+", (a, b) => a + b },
{"-", (a, b) => a - b },
{"*", (a, b) => a * b },
{"/", (a, b) => a / b }
};
public decimal Calc(decimal arg1, decimal arg2, string oper)
{
return _calculatorOperations[oper](arg1, arg2);
}
public decimal GetNumber(string s)
{
const NumberStyles styles = NumberStyles.AllowDecimalPoint;
var provider = new CultureInfo("en-US");
return decimal.Parse(s, styles, provider);
}
public decimal Calc(string s)
{
var str = s.Split();
var arg1 = GetNumber(str[0]);
var arg2 = GetNumber(str[2]);
return Calc(arg1, arg2, str[1]);
}
}
}
19 changes: 19 additions & 0 deletions CalculatorASP/Calculator/Calculator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="2.9.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions CalculatorASP/Calculator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Diagnostics.CodeAnalysis;
namespace Calculator
{
[ExcludeFromCodeCoverage]
internal static class Program
{
private static void Main()
{
try
{
var calculator = new Calculator();
var str = Console.ReadLine();
var value = calculator.Calc(str);
Console.WriteLine(value);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
37 changes: 37 additions & 0 deletions CalculatorASP/CalculatorASP.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30523.141
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CalculatorASP", "CalculatorASP\CalculatorASP.csproj", "{11D5E5E8-8456-4A3D-9038-496437BF33D0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Calculator", "..\Calculator\Calculator.csproj", "{84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}"
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "ProxyF", "ProxyF\ProxyF.fsproj", "{69E6A6AB-C19A-4243-8C9C-9B9EAB919092}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{11D5E5E8-8456-4A3D-9038-496437BF33D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11D5E5E8-8456-4A3D-9038-496437BF33D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11D5E5E8-8456-4A3D-9038-496437BF33D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11D5E5E8-8456-4A3D-9038-496437BF33D0}.Release|Any CPU.Build.0 = Release|Any CPU
{84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84C2F09B-E7D2-4214-AAE0-6E3F20F103F3}.Release|Any CPU.Build.0 = Release|Any CPU
{69E6A6AB-C19A-4243-8C9C-9B9EAB919092}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69E6A6AB-C19A-4243-8C9C-9B9EAB919092}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69E6A6AB-C19A-4243-8C9C-9B9EAB919092}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69E6A6AB-C19A-4243-8C9C-9B9EAB919092}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B882847A-B94B-491F-BB5A-5E7A7C064526}
EndGlobalSection
EndGlobal
11 changes: 11 additions & 0 deletions CalculatorASP/CalculatorASP/CalculatorASP.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Calculator\Calculator.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions CalculatorASP/CalculatorASP/CalculatorMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;

namespace CalculatorASP
{
public class CalculatorMiddleware
{
private readonly RequestDelegate _next;

public CalculatorMiddleware(RequestDelegate next) => _next = next;

public async Task InvokeAsync(HttpContext context)
{
var value = context.Request.Query["value"];
var calclulator = new Calculator.Calculator();
var ans = calclulator.Calc(value);
await context.Response.WriteAsync(ans.ToString());
await _next.Invoke(context);
}
}
}
26 changes: 26 additions & 0 deletions CalculatorASP/CalculatorASP/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace CalculatorASP
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
27 changes: 27 additions & 0 deletions CalculatorASP/CalculatorASP/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:51963",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"CalculatorASP": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
25 changes: 25 additions & 0 deletions CalculatorASP/CalculatorASP/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace CalculatorASP
{

public class Startup
{
public Startup()
{

}
public void Configure(IApplicationBuilder app)
{
app.UseMiddleware<CalculatorMiddleware>();
}
}
}
9 changes: 9 additions & 0 deletions CalculatorASP/CalculatorASP/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
10 changes: 10 additions & 0 deletions CalculatorASP/CalculatorASP/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
58 changes: 58 additions & 0 deletions CalculatorASP/ProxyF/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
open System
open System.Text.RegularExpressions;
open System.Net.Http

type AsyncMaybeBuilder () =
member this.Bind(x, f) =
async {
let! x' = x
match x' with
| Some s -> return! f s
| None -> return None
}

member this.Return x =
async{return x}

let asyncMaybe = new AsyncMaybeBuilder()

let checkAccess input =
Regex.IsMatch(input, @"(-?\d+(?:\.\d+)?)\s*([-]?[*]?[%2B]?[%2F]?)\s(-?\d+(?:\.\d+)?)")

let GetOper op =
match op with
| "+" -> "%2B"
| "*" -> "*"
| "/" -> "%2F"
| "-" -> "-"
| _ -> ""

let GiveRequest expression = async{

let client = new HttpClient()
let! response = client.GetAsync("http://localhost:51963?value=" + expression) |> Async.AwaitTask
let! content = response.Content.ReadAsStringAsync() |> Async.AwaitTask
let result =
match response.IsSuccessStatusCode with
|true -> Some(content)
|false -> None
return result
}

let output (result : string option) =
match result with
| None -> Console.WriteLine("Bad Request")
| Some result -> Console.WriteLine(result)

[<EntryPoint>]
let main argv =
let a = Console.ReadLine()
let op = GetOper(Console.ReadLine())
let b = Console.ReadLine()
let expression = a + " " + op + " " + b;
let result =
match checkAccess expression with
| true -> Async.RunSynchronously(GiveRequest expression) |> Some
| false -> None
output result.Value
0
12 changes: 12 additions & 0 deletions CalculatorASP/ProxyF/ProxyF.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>

</Project>
16 changes: 0 additions & 16 deletions CalculatorF/CalculatorF.sln

This file was deleted.

Loading