2011. 6. 2. 11:07

C#으로 만든 간단한 콘솔 계산기

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: 
  6: namespace ConsoleCalc
  7: {
  8:     class Program
  9:     {
 10:         public double add(double num1, double num2)
 11:         {
 12:             double result = num1 + num2;
 13:             return result;
 14:         }
 15: 
 16:         public static double sub(double num1, double num2)
 17:         {
 18:             double result = num1 - num2;
 19:             return result;
 20:         }
 21: 
 22:         public static double mul(double num1, double num2)
 23:         {
 24:             double result = num1 * num2;
 25:             return result;
 26:         }
 27: 
 28:         public static double div(double num1, double num2)
 29:         {
 30:             double result = num1 / num2;
 31:             return result;
 32:         }
 33: 
 34:         public static double mod(double num1, double num2)
 35:         {
 36:             double result = num1 % num2;
 37:             return result;
 38:         }
 39: 
 40:         static void Main(string[] args)
 41:         {
 42:             Program calc = new Program();
 43: 
 44:             double num1, num2, result;
 45:             string op, again;
 46: 
 47:             Console.WriteLine("두 숫자와 연산기호로 계산하는 프로그램");       
 48: 
 49:             do
 50:             {
 51:                 Console.Write("\n숫자 1 : ");
 52:                 num1 = double.Parse(Console.ReadLine());
 53:                 Console.Write("숫자 2 : ");
 54:                 num2 = double.Parse(Console.ReadLine());
 55: 
 56:                 Console.Write("연산 기호 선택 (+, -, x, /, %) : ");
 57:                 op = Console.ReadLine();
 58: 
 59:                 if (op == "+")
 60:                 {
 61:                     result = calc.add(num1, num2);
 62:                 }
 63: 
 64:                 else if (op == "-")
 65:                 {
 66:                     result = sub(num1, num2);
 67:                 }
 68: 
 69:                 else if (op == "*")
 70:                 {
 71:                     result = mul(num1, num2); 
 72:                 }
 73: 
 74:                 else if (op == "%")
 75:                 {
 76:                     result = mod(num1, num2); 
 77:                 }
 78: 
 79:                 else
 80:                 {
 81:                     result = div(num1, num2);
 82:                 }
 83: 
 84:                 Console.WriteLine("연산결과 : {0} {1} {2} = {3:0.00}", 
 85:                                                       num1, op, num2, result);
 86: 
 87:                 Console.Write("\n계산 계속? (y/n) : ");
 88:                 again = Console.ReadLine();
 89:             } while (again == "y" && again == "Y");
 90:             
 91:         } //main
 92:     }
 93: }