# Can anyone solve this c# calculator example ?

11 Reputation points
2023-06-26T21:55:56.82+00:00
``````using System;

public class MyCalculator {

int Adding(int x, int y) {
// TODO: Write your code here
}

int Subtracting(int x, int y) {
// TODO: Write your code here
}

int Multiplying(int x, int y) {
// NB: you can ONLY use the addition and subtraction operators
// TODO: Write your code here
}

int Divideing(int x, int y) {
// NB:you can ONLY use the addition and subtraction operators
// TODO: Write your code here
}

static public string Run(string operation, int x, int y) {
var MyCalculator = new MyCalculator();

switch (operation) {
case "subtracting":
return MyCalulator.Subtracting(x, y).ToString();
case "multiplying":
return MyCalculator.Multiplying(x, y).ToString();
case "dividing":
return MyCalculator.Divideing(x, y).ToString();
default:
throw new ArgumentException("Invalid operation");
}
}
}
------------
using NUnit.Framework;

[TestFixture]
public class MyCalculatorTest {
//
// First Example
//
//[Test]
//public void ExampleTest() {
//	Assert.AreEqual("this is an example", "this is an example");
//}

//
// Second Example
//
//[Test]
//public void RunTest() {
//	Assert.AreEqual("expected_output", Solution.Run(parameters));
//}
}
//==================
//Integer Calculator
//The task is to create an integer calculator, however you may use only the addition and subtraction arithmetic operators.

//You need to implement solutions for the following:

//Adding - The addition of two integer numbers x + y = ? (example: 3 + 5 = 8)
//Subtracting - The subtraction of two integer numbers x − y = ? (example: 10 − 2 = 8)
//Multiplying - The multiplication of two integer numbers x ∗ y = ? (example: 4 ∗ 2 = 8)
//Dividing - The division of two integer numbers, disregarding any remainder x ÷ y = ? (example: 20 ÷ 2 = 10)
//The solution needs to satisfy the following interface:

public interface ICalculator {
int Subtracting(int x, int y);
int Multiplying(int x, int y);
int Dividing(int x, int y);
}
//The Run method should accept as its operation input one of "adding", "subtracting", "multiplying", "dividing" and then invoke the relevant method with the remaining two parameters.
//The Run method should return the string "exception" if the answer could not be computed from the operation, otherwise it should return the string form of the result of the operation as returned by the corresponding method.
//============
using NUnit.Framework;

[TestFixture]
public class MyCalculatorTest {
//
// First Example
//
//[Test]
//public void ExampleTest() {
//	Assert.AreEqual("this is an example", "this is an example");
//}

//
// Second Example
//
//[Test]
//public void RunTest() {
//	Assert.AreEqual("expected_output", MyCalculator.Run(parameters));
//}
}

//===============
``````
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,705 questions

1. 43,376 Reputation points Microsoft Vendor
2023-06-27T03:01:02.1966667+00:00

Hi @Bill Johnson ， Welcome to Microsoft Q&A.

Check out the code sample below:

``````using System;
using static _6_xx_1.Program;

namespace _6_xx_1
{
internal class Program
{
public interface ICalculator
{
int Subtracting(int x, int y);
int Multiplying(int x, int y);
int Dividing(int x, int y);
}

public class MyCalculator : ICalculator
{

public int Adding(int x, int y)
{
return x + y;
}

public int Subtracting(int x, int y)
{
return x - y;
}

public int Multiplying(int x, int y)
{
int result = 0;
bool isNegative = false;

if (y < 0)
{
y = -y;
isNegative = true;
}

for (int i = 0; i < y; i++)
{
result += x;
}

return isNegative ? -result : result;
}

public int Dividing(int x, int y)
{
if (y == 0)
{
throw new ArgumentException("Cannot divide by zero");
}

int quotient = 0;
bool isNegative = false;

if (x < 0)
{
x = -x;
isNegative = !isNegative;
}

if (y < 0)
{
y = -y;
isNegative = !isNegative;
}

while (x >= y)
{
x -= y;
quotient++;
}

return isNegative ? -quotient : quotient;
}
}

static void Main(string[] args)
{
Console.WriteLine(Solution.Run("subtracting", 10, 2));
Console.WriteLine(Solution.Run("multiplying", 4, 2));
Console.WriteLine(Solution.Run("dividing", 20, 2));
//Pause
}

}
public class Solution
{
public static string Run(string operation, int x, int y)
{
ICalculator calculator = new MyCalculator();

switch (operation)
{
case "subtracting":
return calculator.Subtracting(x, y).ToString();
case "multiplying":
return calculator.Multiplying(x, y).ToString();
case "dividing":
return calculator.Dividing(x, y).ToString();
default:
throw new ArgumentException("Invalid operation");
}
}
}
}

``````
``````using System;

namespace _6_xx_2
{
internal class Program
{
public class MyCalculator
{
public int Adding(int x, int y)
{
return x + y;
}

public int Subtracting(int x, int y)
{
return x - y;
}

public int Multiplying(int x, int y)
{
int result = 0;
int sign = (x < 0 ^ y < 0) ? -1 : 1; // Handle negative values

x = Math.Abs(x);
y = Math.Abs(y);

while (y > 0)
{
y = Subtracting(y, 1);
}

return result * sign;
}

public int Divideing(int x, int y)
{
if (y == 0)
{
throw new DivideByZeroException("Cannot divide by zero");
}

int sign = (x < 0 ^ y < 0) ? -1 : 1; // Handle negative values

x = Math.Abs(x);
y = Math.Abs(y);

int quotient = 0;

while (x >= y)
{
x = Subtracting(x, y);
}

return quotient * sign;
}

public static string Run(string operation, int x, int y)
{
var myCalculator = new MyCalculator();

switch (operation.ToLower())
{
case "subtracting":
return myCalculator.Subtracting(x, y).ToString();
case "multiplying":
return myCalculator.Multiplying(x, y).ToString();
case "dividing":
return myCalculator.Divideing(x, y).ToString();
default:
throw new ArgumentException("Invalid operation");
}
}
}
static void Main(string[] args)
{
Console.WriteLine(MyCalculator.Run("subtracting", 10, 2));
Console.WriteLine(MyCalculator.Run("multiplying", 4, 2));
Console.WriteLine(MyCalculator.Run("dividing", 20, 2));
//Pause