实现简单四则运算小程序
面向过程式的
using System;
using System.Collections.Generic;
using System.Text;
namespace huangjin
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入数字A:");
string A = Console.ReadLine();
Console.Write("请选择运算符号(+,-,*,/):");
string B = Console.ReadLine();
Console.Write("请输入数字B:");
string C=Console.ReadLine();
string D="";
if(B=="+")
D=Convert.ToString(Convert.ToDouble(A)+Convert.ToDouble(C));
if(B=="-")
D=Convert.ToString(Convert.ToDouble(A)-Convert.ToDouble(C));
if(B=="*")
D=Convert.ToString(Convert.ToDouble(A)*Convert.ToDouble(C));
if(B=="/")
D=Convert.ToString(Convert.ToDouble(A)/Convert.ToDouble(C));
Console.WriteLine("结果是:"+D);
Console.ReadKey();
}
}
}
面向对象的编程,不能提示除数为零的情况
using System;
using System.Collections.Generic;
using System.Text;
namespace DaHua1
{
public class Operation
{
public static double GetResult(double numberA, double numberB, string operate)
{
double Result = 0d;
switch (operate)
{
case"+":
Result = numberA + numberB;
break;
case "-":
Result = numberA - numberB;
break;
case "*":
Result = numberA * numberB;
break;
case "/":
Result = numberA / numberB;
break;
}
return Result;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DaHua1
{
class Program
{
static void Main(string[] args)
{
try
{
Console.Write("请输入数字A:");
string strNumberA = Console.ReadLine();
Console.Write("请选择运算符号(+,-,*,/):");
string strOperate = Console.ReadLine();
Console.Write("请输入数字B:");
string strNumberB = Console.ReadLine();
string strResult = "";
strResult = Convert.ToString(Operation.GetResult(Convert.ToDouble(strNumberA),
Convert.ToDouble(strNumberB), strOperate));
Console.WriteLine("结果是:"+strResult);
}
catch (Exception ex)
{
Console.WriteLine("您的输入有错:" + ex.Message);
}
Console.ReadKey();
}
}
}
面对象的编程,能提示除数为零并且要求重新输入
using System;
using System.Collections.Generic;
using System.Text;
namespace DaHua1
{
public class Operation
{
public static double GetResult(double numberA, double numberB, string operate)
{
double Result = 0d;
switch (operate)
{
case "+":
Result = numberA + numberB;
break;
case "-":
Result = numberA - numberB;
break;
case "*":
Result = numberA * numberB;
break;
case "/":
Result = numberA / numberB;
break;
}
return Result;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DaHua1
{
class Program
{
static void Main(string[] args)
{
try
{
Console.Write("请输入数字A:");
string strNumberA = Console.ReadLine();
Console.Write("请选择运算符号(+,-,*,/):");
string strOperate = Console.ReadLine();
while (true)
{
Console.Write("请输入数字B:");
string strNumberB = Console.ReadLine();
string strResult = "";
if (strOperate != "/")
{
strResult = Convert.ToString(Operation.GetResult(Convert.ToDouble(strNumberA),
Convert.ToDouble(strNumberB), strOperate));
Console.WriteLine("结果是:{0}" + strResult);
break;
}
else if (strNumberB != "0")
{
strResult = Convert.ToString(Operation.GetResult(Convert.ToDouble(strNumberA),
Convert.ToDouble(strNumberB), strOperate));
Console.WriteLine("结果是:" + strResult);
break;
}
else
{
Console.WriteLine("请检查除数的非零性,并再次输入");
}
}
}
catch (Exception ex)
{
Console.WriteLine("您的输入有错:" + ex.Message);
}
Console.ReadKey();
}
}
}