用C#的控制台语句实现加减乘除 简单的…
4个回答
展开全部
using System;
namespace ConsoleApplication1
{
class Program
{
delegate double PD(double p1, double p2);
static double Multiply(double p1, double p2)
{
return p1 * p2;
}
static double Divide(double p1, double p2)
{
return p1 / p2;
}
static double Plus(double p1,double p2)
{
return p1+p2;
}
static double Minus(double p1, double p2)
{
return p1 - p2;
}
static void Main(string[] args)
{
PD pro;
Console.WriteLine("Enter 2 numbers separated withed with args comma:");
string input = Console.ReadLine(); int commaPos = input.IndexOf(',');
double p1 = Convert.ToDouble(input.Substring(0, commaPos));
double p2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - 1 - commaPos));
Console.WriteLine("Enter Mto mutiply or D to divide:");
input = Console.ReadLine();
if (input == "M")
pro = new PD(Multiply);
else if(input=="D")
pro = new PD(Divide);
else if (input == "P") pro = new PD(Plus);
else pro = new PD(Minus);
Console.WriteLine("Result:{0}", pro(p1, p2));
Console.ReadKey();
}
}
}
namespace ConsoleApplication1
{
class Program
{
delegate double PD(double p1, double p2);
static double Multiply(double p1, double p2)
{
return p1 * p2;
}
static double Divide(double p1, double p2)
{
return p1 / p2;
}
static double Plus(double p1,double p2)
{
return p1+p2;
}
static double Minus(double p1, double p2)
{
return p1 - p2;
}
static void Main(string[] args)
{
PD pro;
Console.WriteLine("Enter 2 numbers separated withed with args comma:");
string input = Console.ReadLine(); int commaPos = input.IndexOf(',');
double p1 = Convert.ToDouble(input.Substring(0, commaPos));
double p2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - 1 - commaPos));
Console.WriteLine("Enter Mto mutiply or D to divide:");
input = Console.ReadLine();
if (input == "M")
pro = new PD(Multiply);
else if(input=="D")
pro = new PD(Divide);
else if (input == "P") pro = new PD(Plus);
else pro = new PD(Minus);
Console.WriteLine("Result:{0}", pro(p1, p2));
Console.ReadKey();
}
}
}
展开全部
输入的是判断一下是不是数字不就行 了。如果是数字再调用相应函数。不是就不执行,提示再次输入。
public double addition(double i, double j)
{
return i + j;
}
public double subtract(double i, double j)
{
return i - j;
}
public double multiplication(double i, double j)
{
return i * j;
}
public double Division(double i, double j)
{
if (j != 0)
{
return i / j;
}
else
{
return 0;
}
}
public double addition(double i, double j)
{
return i + j;
}
public double subtract(double i, double j)
{
return i - j;
}
public double multiplication(double i, double j)
{
return i * j;
}
public double Division(double i, double j)
{
if (j != 0)
{
return i / j;
}
else
{
return 0;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int i=32+99;
System.Console.WriteLine(i);
System.Console.WriteLine(i);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1楼真简单。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询