+1 (315) 557-6473 

C# Program to Create Maze Assignment Solution.


Instructions

Objective
Write a program to solve math problems in C#.

Requirements and Specifications

Description: A C# console program that allows users to select different types of math problems, show the user the appropriate problem, and check whether or not they gave the correct answer. MUST BE DONE IN FUNCTIONAL PROGRAMMING.
Source Code
using System;
using System.Collections.Generic;
namespace WangApp
{
    // Create a class to represent a Math problem
    class MathProblem
    {
        private string description;
        private double answer;
        public MathProblem(string desc, double ans)
        {
            description = desc;
            answer = ans;
        }
        public string getDescription() { return description; }
        public double getAnswer() { return answer; }
        public bool checkAnswer(double ans)
        {
            return answer == ans;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Create a list of math problems
            List<MathProblem> problems = new List<MathProblem>();
            problems.Add(new MathProblem("Solve for x: x + 5 = 2x-17", 22));
            problems.Add(new MathProblem("Calculate 7^2", 49));
            problems.Add(new MathProblem("Calculate 10/(100-98) + 4^2", 21));
            problems.Add(new MathProblem("Solve for y: (8y - 4)/(4y+12) = 1", 4));
            problems.Add(new MathProblem("Calculate 1567^0", 1));
            display_menu(problems);
            // Get option from user
            int option = getMenuOption(1, 5); // between 1 and 5 because we have 5 problems
            // Display problem again:
            Console.WriteLine(problems[option - 1].getDescription());
            Console.Write("Answer = ");
            // Now, ask for answer
            double answer = Convert.ToDouble(Console.ReadLine());
            // Check if it is correct
            bool correct = problems[option - 1].checkAnswer(answer);
            if(correct)
            {
                Console.WriteLine("That is correct!");
            }
            else
            {
                Console.WriteLine("Sorry, that is not correct.");
            }
            Console.Read();
        }
        // Create a function to display the Math rpblems (Menu)
        static void display_menu(List<MathProblem> problems)
        {
            // Loop through problems and prints them
            int i = 1;
            foreach(MathProblem p in problems)
            {
                Console.WriteLine(i.ToString() + ") " + p.getDescription());
                i += 1;
            }
        }
        // Create a function that requests the user an integer that is inside a range
        static int getMenuOption(int lower, int upper)
        {
            int option;
            // Create a while-loop that will keep requesting user for an option until s/he enters a valid option
            while(true)
            {
                Console.Write("Enter option: ");
                option = Convert.ToInt32(Console.ReadLine());
                if(option >= lower && option <= upper)
                {
                    return option;
                }
                else
                {
                    Console.WriteLine("Please enter a valid option between " + lower.ToString() + " and " + upper.ToString() + ".");
                }
            }
        }
    }
}