使用C#编程语言,实现一个简单的运算器功能,要求:输入两个操作数,一个运算符,将运算结果进行输出。
- 提问者网友:人傍凄凉立暮秋
- 2021-05-07 07:14
- 五星知识达人网友:迟山
- 2021-05-07 08:38
- 1楼网友:神的生死簿
- 2021-05-07 10:02
实际上本题目根本用不着虚方法或者是抽象类的,如果你强行规定,只能这样了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
abstract class Algorithm
{
public int Number1 { get; set; }
public int Number2 { get; set; }
public Algorithm(int number1,int number2)
{
Number1 = number1;
Number2 = number2;
}
public abstract double Execute(string oper);
}
class Program:Algorithm
{
static void Main(string[] args)
{
Console.WriteLine("输入第一个数:");
int n1 =Convert.ToInt32(Console.ReadLine());
Console.WriteLine("输入第二个数:");
int n2 = Convert.ToInt32(Console.ReadLine());
Program p = new Program(n1,n2);
Console.WriteLine("结果是:"+p.Execute("+"));
}
public Program(int n1,int n2):base(n1,n2)
{
}
public override double Execute(string oper)
{
if (oper == "+")
{
return Number1 + Number2;
}
else if (oper == "-")
{
return Number1 - Number2;
}
else if (oper == "*")
{
return Number1 *Number2;
}
else if (oper == "/" && Number1!=0)
{
return Number1 /Number2;
}
return 0;
}
}
}