TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
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
Forums
Monthly Leaders
Forum guidelines
Ant 6729
NA
87
2.2k
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