Skip to content

Commit

Permalink
#3 add validations
Browse files Browse the repository at this point in the history
  • Loading branch information
BawinileM committed Jun 14, 2022
1 parent 30d8115 commit 465e361
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 30 deletions.
3 changes: 2 additions & 1 deletion CalculatorAPI/Controllers/TrigFunctionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
namespace CalculatorAPI.Controllers
{
public class TrigFunctionController
{
{ [Authorise]
[HttpPost("TrigFunctions")]
public async Task<IActionResult> getInputs([FromBody] TrigPostRequest trig)
{


TrigPostRequest d = new TrigPostRequest();
List<Trig> ls= trig.Tags;
Expand Down
36 changes: 27 additions & 9 deletions CalculatorAPI/Repositories/TrigFunctionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,50 @@
using System.Threading.Tasks;
using CalculatorAPI.Models;
using CalculatorAPI.utils;
using System.Text.RegularExpressions;


namespace CalculatorAPI.Repositories
{
public class TrigFunctionRepository
{ public static double TrigImplement( List<Trig> ls){
{
public static double TrigImplement( List<Trig> ls){
Validations.Validate(ls);
//assume the inputs are already validated:
string answer = null;
foreach (Trig obj in ls){
if(obj.trigFunction == "sin"){
answer = answer + obj.sign + Math.Round(Math.Sin(obj.degreeValue*Math.PI / 180.0), 2);
if(getStr(obj.trigFunction) == "sin"){
answer = answer + obj.sign + Math.Round(getNum(obj.trigFunction)* Math.Sin(obj.degreeValue*Math.PI / 180.0), 2);
}

if(obj.trigFunction == "cos"){
answer = answer + obj.sign + Math.Round(Math.Cos(obj.degreeValue*Math.PI / 180.0), 2);
if(getStr(obj.trigFunction)=="cos"){
answer = answer + obj.sign + Math.Round(getNum(obj.trigFunction)* Math.Cos(obj.degreeValue*Math.PI / 180.0), 2);
}

if(obj.trigFunction == "tan"){
answer = answer +obj.sign +Math.Round(Math.Tan(obj.degreeValue*Math.PI / 180.0), 2);
if(getStr(obj.trigFunction) =="tan" ){
answer = answer +obj.sign +Math.Round(getNum(obj.trigFunction)* Math.Tan(obj.degreeValue*Math.PI / 180.0), 2);
}
}

// eval will be replaced by that method that evaluates bodmas
return Eval(answer.Replace(",", "."));

}


static Double Eval(String expression)
public static String getStr(String var){
return Regex.Replace(var, @"\d", "");
}
public static Double getNum(String var){
if(var.Any(char.IsDigit)){
return Convert.ToDouble(Regex.Replace(var, @"\D", ""));
}
else {
return 1;
}
}

//this method will need to be deleted when the bodmas implementation is working
public static Double Eval(String expression)
{
System.Data.DataTable table = new System.Data.DataTable();
return Convert.ToDouble(table.Compute(expression, String.Empty));
Expand Down
Binary file modified CalculatorAPI/bin/Debug/net6.0/CalculatorAPI.dll
Binary file not shown.
Binary file modified CalculatorAPI/bin/Debug/net6.0/CalculatorAPI.pdb
Binary file not shown.
Binary file modified CalculatorAPI/obj/Debug/net6.0/CalculatorAPI.dll
Binary file not shown.
Binary file modified CalculatorAPI/obj/Debug/net6.0/CalculatorAPI.pdb
Binary file not shown.
Binary file modified CalculatorAPI/obj/Debug/net6.0/ref/CalculatorAPI.dll
Binary file not shown.
Binary file modified CalculatorAPI/obj/Debug/net6.0/refint/CalculatorAPI.dll
Binary file not shown.
7 changes: 7 additions & 0 deletions CalculatorAPI/obj/staticwebassets.pack.sentinel
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
37 changes: 17 additions & 20 deletions CalculatorAPI/utils/Validations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,37 @@
using System.Linq;
using System.Threading.Tasks;
using CalculatorAPI.Models;
using System.Text.RegularExpressions;


namespace CalculatorAPI.utils
{
public class Validations
{

string [] signs = {"+", "-", "*", "/"};
string [] trigfun = {"sin", "cos", "tan"};

double [] undefinedAngle= {-270,-90,90,270};
public static void Validate(List<Trig> ls){
foreach (Trig obj in ls){
if(obj.trigFunction=="sin" || obj.trigFunction=="cos" || obj.trigFunction=="tan" ){
if(obj.sign=="+" || obj.sign=="-" || obj.sign=="*" || obj.sign=="/"){
foreach (Trig obj in ls){
if( Regex.Replace(obj.trigFunction, @"\d", "") == "sin" || Regex.Replace(obj.trigFunction, @"\d", "") == "cos" || Regex.Replace(obj.trigFunction, @"\d", "") == "tan" ){
if(obj.sign == "+" || obj.sign == "-" || obj.sign == "*" || obj.sign == "/"){

}else{
throw new Exception("Please enter a valid sign");
}

}else{
throw new Exception("Please enter a valid trig function");
throw new Exception("Please enter a valid sign");
}

}else{
throw new Exception("Please enter a valid trig function choose between cos, sin & tan");
}

if(obj.trigFunction == "tan" ){
if(obj.degreeValue==-270 || obj.degreeValue==-90 || obj.degreeValue==90 ||obj.degreeValue==270)
throw new Exception("Tan is undefined for this degree value");
}
}
if(ls[0].sign=="*" || ls[0].sign=="/"){
if(Regex.Replace(obj.trigFunction, @"\d", "") == "tan" ){
if(obj.degreeValue ==-270 || obj.degreeValue ==-90 || obj.degreeValue ==90 ||obj.degreeValue == 270)
throw new Exception("Tan is undefined for this degree value");
}
}
if(ls[0].sign == "*" || ls[0].sign == "/"){
throw new Exception("Cannot start with division or multiplication");
}
}

}
}

}
}

0 comments on commit 465e361

Please sign in to comment.