【CSharp】简易命令行计算器实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("欢迎使用计算器,请根据提示进行操作:");
            Console.WriteLine("加法运算,请按1");
            Console.WriteLine("减法运算,请按2");
            Console.WriteLine("乘法运算,请按3");
            Console.WriteLine("除法运算,请按4");
 
            int flag = Convert.ToInt32(Console.ReadLine());
 
            int a, b;
 
            Calclator c;
 
            switch(flag)
            {
                case 1 :
                    Console.WriteLine("请输入2数,并换行");
                    a = int.Parse(Console.ReadLine());
                    b = int.Parse(Console.ReadLine());
//Console.WriteLine("这是一条测试语句。");
//Console.WriteLine(a);
//Console.WriteLine(b);
 
                    c = new Calclator(a, b);
                    Console.WriteLine(c.a + " + " + c.b  + " = {0}", c.add());
 
                    break;
                case 2 :
                    Console.WriteLine("请输入2数,并换行");
                    a = int.Parse(Console.ReadLine());
                    b = int.Parse(Console.ReadLine());
 
                    c = new Calclator(a, b);
                    Console.WriteLine(c.a + " - " + c.b  + " = {0}", c.subt());
 
                    break;
                case 3 :
                    Console.WriteLine("请输入2数,并换行");
                    a = int.Parse(Console.ReadLine());
                    b = int.Parse(Console.ReadLine());
 
                    c = new Calclator(a, b);
                    Console.WriteLine(c.a + " x " + c.b  + " = {0}", c.mult());
 
                    break;
                case 4 :
                    Console.WriteLine("请输入2数,并换行");
                    a = int.Parse(Console.ReadLine());
                    b = int.Parse(Console.ReadLine());
 
                    c = new Calclator(a, b);
                    Console.WriteLine(c.a + " ÷ " + c.b  + " = {0}", c.divi());
 
                    break;
            }
        }
    }
 
    public class Calclator
    {
        public int a;
        public int b;
 
        public Calclator(int a, int b)
        {
            this.a = a;
            this.b = b;
        }
 
        public int result;
        public int add()
        {
            result = a + b;
            return result;
        }
        public int subt()  //subtraction
        {
            result = a - b;
            return result;
        }
        public int mult()  //multiplication
        {
            result = a * b;
            return result;
        }
        public int divi()  //division
        {
            result = a / b;
            return result;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值