HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Calculating the equivalent value of 2 resistors or capacitors

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theequivalentcapacitorsresistorsvaluecalculating

Problem

I started learning C# and this is my first "project". Its main purpose is to calculate the equivalent value of two resistors or capacitors connected in either series or parallel.

I just finished it and was wondering if you could review it for me. I'm open to learning what's bad, what's good (if it even has any in it) and how I can improve it.

```
using System;

class Program
{
static void Main()
{
bool goAgain = true;
while (goAgain)
{

Console.WriteLine("Hello! This is my app for calculating the equivalent of RESISTORS or CAPACITORS \nconnected in SERIES or in PARALLEL: ");
Console.WriteLine("Press Enter when you are ready to start!");
Console.ReadLine();
Console.Clear();

Console.WriteLine("First tell me are we using RESISTORS or CAPACITOR ? \nType R or C :");
string type = Console.ReadLine();
Console.WriteLine("Okay whats the connection? In SERIES or in PARALLEL? Type S or P: ");
string connection = Console.ReadLine();

Console.WriteLine("Okay! Whats the value of the first one?");
decimal valueOfTheFirstOne = Decimal.Parse(Console.ReadLine());
Console.WriteLine("What is its factor?\n1.Pico\n2.Nano\n3.Micro\n4.Mili\n5.Normal\n6.Kilo\n7.Mega\n8.Giga\n9.Tera");
decimal factorOfTheFirstOne = Decimal.Parse(Console.ReadLine());

Console.WriteLine("Okay! And the second?");
decimal valueOfTheSecondOne = Decimal.Parse(Console.ReadLine());

Console.WriteLine("What is its factor?\n1.Mili\n2.Micro\n3.Nano\n4.Pico\n5.Normal\n6.Kilo\n7.Mega\n8.Giga\n9.Tera");
decimal secondFactor = Decimal.Parse(Console.ReadLine());
switch ((int)factorOfTheFirstOne)
{
case 1:
factorOfTheFirstOne = 0.001m;
break;
case 2:
factorOfTheFirstOne = 0.000001m;

Solution

The major problem in the code is the absence of the verification of the user's input. For example, Decimal.Parse may throw FormatException, or OverflowException. Perhaps, you should also verify that the values are positive (you are going to accept only the passive components, aren't you?)

Huge switches on the factors can be replaced by simple formulas.

Switches on the component types and serial/parallel connections can be greatly simplified if you recall what is the impedance (~R for the resistor and ~1/C for the capacitor)

Minor issues with the code style like inconsistent variables naming (e.g. factorOfTheFirstOne and secondFactor

Context

StackExchange Code Review Q#124635, answer score: 3

Revisions (0)

No revisions yet.