using hnn0731.Domain; //包含在同一命名空间里。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hnn0731
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入用户名:");
String userName = Console.ReadLine();
int count = 0;
while (userName != "admin")
{
count++;
if (count == 3)
{
Console.WriteLine("输入用户名错误超过三次,将重新进入!");
System.Threading.Thread.Sleep(1000);
}
Console.WriteLine("用户名错误,请重新输入!!!");
Console.WriteLine("请输入用户名:");
userName = Console.ReadLine();
}
Console.WriteLine("请输入密码:");
String password = Console.ReadLine();
//int countpass = 0;
while (password != "password")
{
Console.WriteLine("密码错误,请重新输入!!!");
Console.WriteLine("请输入密码:");
password = Console.ReadLine();
}
Console.WriteLine("登陆成功!!!");
int select = 1;
while (select == 1)
{
Console.WriteLine("欢迎进入计算器空间!!!");
Console.WriteLine("请输入要计算的数字类型:整型选择1,浮点型选择2");
int type = Convert.ToInt32(Console.ReadLine());
if (type == 1)
{
Console.WriteLine("请输入要计算的第一个数字:");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入要计算的符号:");
string sigh = Convert.ToString(Console.ReadLine());
Console.WriteLine("请输入要计算的第二个数字:除数不能为0");
int num2 = Convert.ToInt32(Console.ReadLine());
Test ti = new Test();
switch (sigh)
{
case "+": Console.WriteLine("int:{0}{1}{2}={3}", num1, sigh, num2, ti.add(num1, num2));
break;
case "-": Console.WriteLine("int:{0}{1}{2}={3}", num1, sigh, num2, ti.subdu(num1, num2));
break;
case "*": Console.WriteLine("int:{0}{1}{2}={3}", num1, sigh, num2, ti.multi(num1, num2));
break;
case "/": Console.WriteLine("int:{0}{1}{2}={3}", num1, sigh, num2, ti.divi(num1, num2));
break;
}
//object a = 1;
//Console.WriteLine(a);
//Console.WriteLine(a.GetType());
//Console.WriteLine(a.ToString());
}
else
{
Console.WriteLine("请输入要计算的第一个数字:");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("请输入要计算的符号:");
string sigh = Convert.ToString(Console.ReadLine());
Console.WriteLine("请输入要计算的第二个数字:");
double num2 = Convert.ToDouble(Console.ReadLine());
Test ti = new Test();
switch (sigh)
{
case "+": Console.WriteLine("double:{0}{1}{2}={3}", num1, sigh, num2, ti.add(num1, num2));
break;
case "-": Console.WriteLine("double:{0}{1}{2}={3}", num1, sigh, num2, ti.subdu(num1, num2));
break;
case "*": Console.WriteLine("double:{0}{1}{2}={3}", num1, sigh, num2, ti.multi(num1, num2));
break;
case "/": Console.WriteLine("double:{0}{1}{2}={3}", num1, sigh, num2, ti.divi(num1, num2));
break;
}
}
Console.WriteLine("是否要继续使用计算机,是请输入1,否输入0");
select = Convert.ToInt32(Console.ReadLine());
}
//Console.ReadKey();
}
}
}
引用的函数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hnn0731.Domain
{
class Test
{
//加法
public T add(T t1, T t2)
{
//object tem1=t1,tem2=t2;
if (t1 is int)
{
object ret = Convert.ToInt32(t1) + Convert.ToInt32(t2);
return (T)ret;
}
else if (t1 is double)
{
object ret = Convert.ToDouble(t1) + Convert.ToDouble(t2);
return (T)ret;
}
return t1;
}
//减法
public T subdu(T t1, T t2)
{
if (t1 is int)
{
object ret = Convert.ToInt32(t1) - Convert.ToInt32(t2);
return (T)ret;
}
else if (t1 is double)
{
object ret = Convert.ToDouble(t1) - Convert.ToDouble(t2);
return (T)ret;
}
return t1;
}
//乘法
public T multi(T t1, T t2)
{
if (t1 is int)
{
object ret = Convert.ToInt32(t1) * Convert.ToInt32(t2);
return (T)ret;
}
else if (t1 is double)
{
object ret = Convert.ToDouble(t1) * Convert.ToDouble(t2);
return (T)ret;
}
return t1;
}
//除法
public T divi(T t1, T t2)
{
if (Convert.ToInt32(t2) != 0)
{
if (t1 is int)
{
object ret = Convert.ToInt32(t1) / Convert.ToInt32(t2);
return (T)ret;
}
// 在 C# 的统一类型系统中,所有类型(预定义类型、用户定义类型、引用类型和值类型)
//都是直接或间接从 Object 继承的。可以将任何类型的值赋给 object 类型的变量。
//将值类型的变量转换为对象的过程称为“装箱”。
//将对象类型的变量转换为值类型的过程称为“取消装箱”。
else if (t1 is double) //判断t1是否为double类型
{
object ret = Convert.ToDouble(t1) / Convert.ToDouble(t2);
return (T)ret;
}
}
else
{
Console.WriteLine("输入错误!!!");
object ret = 0;
return (T)ret;
}
return t1;
}
}
}