Why Join
Become a member
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
C# Corner Home
Technologies
MonthlyLeaders
ASK A QUESTION
Forumguidelines
Ant 6729
1.9k
83
925
Deliver a duplicate section of code into a separate part.
Jul 2 2019 7:10 AM
Hello everyone
I see there is duplication in my cases
Could you help me to optimize it?
using
System;
namespace
_3_??
{
public
delegate
int
BinaryOp(
int
x,
int
y);
public
class
SimpleMath
{
public
static
int
Add(
int
x,
int
y)
{
return
x + y; }
public
static
int
Subtract(
int
x,
int
y)
{
return
x - y; }
}
class
Program
{
static
void
Main(
string
[] args)
{
BinaryOp b =
new
BinaryOp(SimpleMath.Add);
BinaryOp w =
new
BinaryOp(SimpleMath.Subtract);
string
operationRes;
Console.WriteLine(
"What operations you wonna checkup? (addition/subtraction, write a/s to choose): "
);
operationRes = Console.ReadLine();
Console.WriteLine(
"Insert first number"
);
int
x = (Convert.ToInt16(Console.ReadLine()));
Console.WriteLine(
"Insert second number"
);
int
y = (Convert.ToInt16(Console.ReadLine()));
switch
(operationRes)
{
case
"a"
:
Console.WriteLine(
"Insert sum of x and y"
);
int
userresult = (Convert.ToInt16(Console.ReadLine()));
int
pcresult = b(x, y);
if
(userresult > pcresult)
Console.WriteLine(
"Something wrong with your calculations, man..."
+
"\n"
+
"Your result should be less..."
);
else
if
(userresult < pcresult)
Console.WriteLine(
"Something wrong with your calculations, man..."
+
"\n"
+
"Your result should be more..."
);
else
Console.WriteLine(
"Y've inserted right result!"
);
break
;
case
"s"
:
Console.WriteLine(
"Insert substraction of x and y"
);
int
userresult2 = (Convert.ToInt16(Console.ReadLine()));
int
pcresult2 = w(x, y);
if
(userresult2 > pcresult2)
Console.WriteLine(
"Something wrong with your calculations, man..."
+
"\n"
+
"Your result should be less..."
);
else
if
(userresult2 < pcresult2)
Console.WriteLine(
"Something wrong with your calculations, man..."
+
"\n"
+
"Your result should be more..."
);
else
Console.WriteLine(
"Y've inserted right result!"
);
break
;
default
:
break
;
}
}
}
}
Reply
Answers (
1
)
Looking for Neural Network Framework based on .NET C#
How to learn algorithm