简体   繁体   中英

Pseudocode for switch statement in c#

How do I write the pseudocode for a switch (case) statement in C#?

switch (option)
{
    case 1:
        Console.Write("Enter First Number: ");
        num1 = Convert.ToDouble(Console.ReadLine());

        Console.Write("Enter Second Number: ");
        num2 = Convert.ToDouble(Console.ReadLine());

        result = num1 + num2;
        Console.WriteLine(result);
        break;

    case 2:
        Console.Write("Enter First Number: ");
        num1 = Convert.ToDouble(Console.ReadLine());

        Console.Write("Enter Second Number: ");
        num2 = Convert.ToDouble(Console.ReadLine());

        result = num1 - num2;
        Console.WriteLine(result);
        break;

    case 3:
        Console.Write("Enter First Number: ");
        num1 = Convert.ToDouble(Console.ReadLine());

        Console.Write("Enter Second Number: ");
        num2 = Convert.ToDouble(Console.ReadLine());

        result = num1 * num2;
        Console.WriteLine(result);
        break;

    default:
        Console.WriteLine("\n Next time follow instructions. You can only choose numbers 1 - 4");
        break;
}

So, if I was going to write this, I'd start with an enumerated type for the operations:

public enum ArithmeticOperation
{
    Add,
    Subtract,
    Multiply,
    Divide,
}

I'd write a little helper function:

 private static string ShowEnumOptions<T>() where T : struct
 {
     if (!typeof(T).IsEnum)
     {
         throw new ArgumentException($"Type: {typeof(T).ToString()} must be an enumerated type");
     }

     var options = Enum.GetNames(typeof(T));
     return string.Join("/", options);
 }

(the newest version of C# (which I don't use yet) allows a System.Enum constraint on a generic type parameter which would simplify this)

Then I'd write my main program to look like this:

static void Main(string[] args)
{
    while (true)
    {
        ArithmeticOperation operation = default(ArithmeticOperation);
        var goodOperation = false;
        while (!goodOperation)
        {
            Console.Write(
                $"Enter operation (one of [{ShowEnumOptions<ArithmeticOperation>()}] or \"Quit\"): ");
            var response = Console.ReadLine();
            if (string.Equals(response, "Quit", StringComparison.InvariantCultureIgnoreCase))
            {
                return; //quit the app
            }

            if (Enum.TryParse<ArithmeticOperation>(response, true, out operation))
            {
                goodOperation = true;
            }
        }

        double value1 = 0.0;
        double value2 = 0.0;        //initialize them to keep the compiler happy
        var goodDouble = false;
        while (!goodDouble)
        {
            Console.Write("Enter the first number: ");
            var response = Console.ReadLine();
            if (double.TryParse(response, out value1))
            {
                goodDouble = true;
            }
        }
        goodDouble = false;
        while (!goodDouble)
        {
            Console.Write("Enter the second number: ");
            var response = Console.ReadLine();
            if (double.TryParse(response, out value2))
            {
                goodDouble = true;
            }
        }

        //ok, got an operation and two numbers

        double result = 0.0;
        switch (operation)
        {
            case ArithmeticOperation.Add:
                result = value1 + value2;
                break;
            case ArithmeticOperation.Subtract:
                result = value1 - value2;
                break;
            case ArithmeticOperation.Multiply:
                result = value1 * value2;
                break;
            case ArithmeticOperation.Divide:
                if (value2 == 0.0)
                {
                    Console.WriteLine("Division by zero is invalid");
                    result = double.NaN;   //NaN means "not a number"
                    break;
                }
                result = value1 / value2;
                break;
        }

        Console.WriteLine($"Result is {result}");
    }
}

Note that I check all input for validity. Always assume your users will enter bad data. Also note that I check my double for equality with zero. Checking for floating point equality is usually a bad idea, but it's the right thing to do here.

Then, as pseudo code, all I'd write would be:

 // Get the operation (one of add/subtract/multiply or divide) - or allow the user to quit
 // Get each of value1 and value2 as doubles
 // Based on the operation, calculate the result (pay attention to division by zero)
 // Show the result
 // Loop back and let the user try again (or quit)

Pseudocode is basically writing what you're trying to do in comments. What your professor was probably trying to teach you is to make a bunch of comments to plan out the structure of your code, and then write your code. What you have above is already functional code. At the risk of answering your homework question, I'd say it goes something like this:

switch(option)
{
    case 1:
        //do something
        break;
    case 2:
        //do something else
        break;
    default:
        //what to do if none of the cases are met
        break;
} 

I don't know what you mean with pseudocode but this code is less repetitive:

double result = 0;
Console.Write("Enter First Number: ");
double num1 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter Second Number: ");
double num2 = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter a number from 1 to 3");
string input = Console.ReadLine();

switch (input) {

    case "1" : 
        result = num1 + num2;
        break;
    case "2":
        result = num1 - num2;
        break;
    case "3":
        result = num1 * num2;
        break;  
    default:
    Console.WriteLine("\n Next time follow instructions. You can only choose numbers 1 - 4");
    break;

}
Console.WriteLine("Result = " + result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM