using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simple_arithmetic //简易四则运算
{
class Program
{
public static void Ni()
{
Console.WriteLine("请输入您想做的运算: ");
Console.WriteLine("输入 【+】 表示加法");
Console.WriteLine("输入 【-】 表示减法");
Console.WriteLine("输入 【*】 表示乘法");
Console.WriteLine("输入 【/】 表示除法");
Console.WriteLine("输入【 -1】表示结束");
Console.WriteLine("====================");
string opt = Console.ReadLine();
switch (opt) //使用switch语句
{
case "+":
Opp.Add();
break;
case "-":
Opp.Sub();
break;
case "*":
Opp.Mul();
break;
case "/":
Opp.Div();
break;
}
Console.ReadLine();
}
static void Main(string[] args)
{
//首先声明
Ni();
}
}
public delegate double Num(double x, double y);//定义委托
public class SimpleMath
{
public static double Add(double x, double y)
{ return x + y; }
public static double Sub(double x, double y)
{ return x - y; }
public static double sub(double x, double y)
{ return y - x; }
public static double Mul(double x, double y)
{ return x * y; }
public static double Div(double x, double y)
{ return x / y; }
public static double div(double x, double y)
{ return y / x; }
}
public class Opp
{
public static int i = 0;
public static int fault = 0;
public static int right = 0;
public static void Add()//加法,实现加法功能
{
double sum, op;
Random p = new Random();
double x = p.Next(0, 10);
double y = p.Next(0, 10);
sum = x + y;
Console.WriteLine("{0}+{1}=", x, y);
Console.WriteLine("请输入结果:");
op = double.Parse(Console.ReadLine());
Num um = new Num(SimpleMath.Add);
Console.WriteLine("正确结果是:");
Console.WriteLine("{0}+{1}={2}", x, y, um(x, y));
if (sum == op)
{
Console.WriteLine("您的结果正确!");
i++;
fault++;
right++;
Add();
}
else if (op == -1)
{
end();
Program.Ni();
}
else
{
Console.WriteLine("您的结果错误!");
i++;
fault++;
Add();
}
}
public static void end()
{
Console.WriteLine("您已退出计算!您共做{0}道题,答对{1}道,答错{2}道", i, fault, right);
}
public static void Sub()//减法,实现减法功能
{
double jan, op;
Random p = new Random();
double x = p.Next(0, 10);
double y = p.Next(0, 10);
if (x > y)
{
jan = x - y;
Console.WriteLine("{0}-{1}=", x, y);
Console.WriteLine("请输入结果:");
op = double.Parse(Console.ReadLine());
Num um = new Num(SimpleMath.Sub);
Console.WriteLine("正确结果是:");
Console.WriteLine("{0}-{1}={2}", x, y, um(x, y));
if (jan == op)
{
Console.WriteLine("您的结果正确!");
i++;
fault++;
right++;
Add();
}
else if (op == -1)
{
end();
Program.Ni();
}
else
{
Console.WriteLine("您的结果错误!");
i++;
fault++;
Sub();
}
}
else if (x < y)
{
jan = y - x;
Console.WriteLine("{0}-{1}=", y, x);
Console.WriteLine("请输入结果:");
op = double.Parse(Console.ReadLine());
Num um = new Num(SimpleMath.sub);
Console.WriteLine("正确结果是:");
Console.WriteLine("{0}-{1}={2}", y, x, um(y, x));
if (jan == op)
{
Console.WriteLine("您的结果正确!");
i++;
fault++;
right++;
Add();
}
else if (op == -1)
{
end();
Program.Ni();
}
else
{
Console.WriteLine("您的结果错误!");
i++;
fault++;
Sub();
}
}
}
public static void Mul()//乘法,实现乘法功能
{
double cen, op;
Random p = new Random();
double x = p.Next(0, 10);
double y = p.Next(0, 10);
cen = x * y;
Console.WriteLine("{0}*{1}=", x, y);
Console.WriteLine("请输入结果:");
op = double.Parse(Console.ReadLine());
Num um = new Num(SimpleMath.Mul);
Console.WriteLine("正确结果是:");
Console.WriteLine("{0}*{1}={2}", x, y, um(x, y));
if (cen == op)
{
Console.WriteLine("您的结果正确!");
fault++;
right++;
Add();
}
else if (op == -1)
{
end();
Program.Ni();
}
else
{
Console.WriteLine("您的结果错误!");
fault++;
Mul();
}
}
public static void Div()//除法,实现除法功能
{
double chu, op;
Random p = new Random();
double x = p.Next(0, 10);
double y = p.Next(0, 10);
if (y != 0)
{
chu = x / y;
Console.WriteLine("{0}/{1}=", x, y);
Console.WriteLine("请输入结果:");
op = double.Parse(Console.ReadLine());
Num um = new Num(SimpleMath.Div);
Console.WriteLine("正确结果是:");
Console.WriteLine("{0}/{1}={2}", x, y, um(x, y));
if (chu == op)
{
Console.WriteLine("您的结果正确!");
fault++;
right++;
Add();
}
else if (op == -1)
{
end();
Program.Ni();
}
else
{
Console.WriteLine("您的结果错误!");
fault++;
Div();
}
}
else if(y==0)
{
chu = y / x;
Console.WriteLine("{0}/{1}=", y, x);
Console.WriteLine("请输入结果:");
op = double.Parse(Console.ReadLine());
Num um = new Num(SimpleMath.div);
Console.WriteLine("正确结果是:");
Console.WriteLine("{0}/{1}={2}", y, x, um(x, y));
if (chu == op)
{
Console.WriteLine("您的结果正确!");
fault++;
right++;
Add();
}
else if (op == -1)
{
end();
Program.Ni();
}
else
{
Console.WriteLine("您的结果错误!");
fault++;
Div();
}
}
}}}
感想:感觉通过这次结对编程让我感觉到了合作的重要性,比以往独自一人独自编程要有趣轻松的多,更重要的是结队编程学到的更多收获更多!