C# 普通的計算器
阿新 • • 發佈:2019-01-10
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalculatorA
{
class Calculator
{
public double GetNum()
{
//資料格式為 數字.數字
Regex reg = new Regex(@"^-?\d+\.\d+$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
//資料格式為 數字
Regex reg1 =new Regex(@"^-?\d+\d+$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
//資料格式為 .數字
Regex reg2 = new Regex(@"^-?\.\d+$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
System.Console.WriteLine("請輸入一個數字");
string number = Console.ReadLine();
System.Console.WriteLine("----------------------------");
//System.Console.WriteLine(reg.Match(number1).Success); //成功返回true,否則返回false
double Num;
//只有同時不滿足上面3個正則表示式才進入迴圈,直到使用者輸入正確,滿足上述3個正則表示式,才能退出迴圈
while ((reg.Match(number).Success) != true && (reg1.Match(number).Success) != true && (reg2.Match(number).Success) != true)
{
System.Console.WriteLine("輸入有誤,請重新輸入");
number = Console.ReadLine();
System.Console.WriteLine("----------------------------");
//如果滿足上述3個正則表示式,將得到的字串轉換成double型別,並返回
if (reg.Match(number).Success || reg1.Match(number).Success || reg2.Match(number).Success)
{
System.Console.WriteLine("輸入正確!!!");
Num = Convert.ToDouble(number);
return Num;
}
}
System.Console.WriteLine("輸入正確!!!");
Num = Convert.ToDouble(number);
return Num;
}
public int GetSelector()
{
//資料格式為 數字1-5
Regex reg3 = new Regex(@"^[1-5]$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
System.Console.WriteLine("***Flora計算器***");
System.Console.WriteLine("請輸入以下數字表達你想要選擇的計算方式:");
System.Console.WriteLine("1表示加法計算");
System.Console.WriteLine("2表示減法計算");
System.Console.WriteLine("3表示乘法計算");
System.Console.WriteLine("4表示除法計算");
System.Console.WriteLine("5表示取餘計算");
string selector =Console.ReadLine();
System.Console.WriteLine("*****************");
int Sel;
//只有同時不滿足正則表示式才進入迴圈,直到使用者輸入正確,滿足正則表示式,才能退出迴圈
while ((reg3.Match(selector).Success) != true)
{
System.Console.WriteLine("您輸入的不是數字1-5,請重新輸入!");
selector = Console.ReadLine();
System.Console.WriteLine("----------------------------");
//如果滿足上述正則表示式,將得到的字串轉換成int型別,並返回
if (reg3.Match(selector).Success)
{
System.Console.WriteLine("輸入正確!!!");
Sel = int.Parse(selector);
return Sel;
}
}
System.Console.WriteLine("輸入正確!!!");
Sel = int.Parse(selector);
return Sel;
}
public void Add() //加法計算
{
double Num1 = GetNum();
double Num2 = GetNum();
System.Console.WriteLine("****加法計算****");
double Result = Num1 + Num2;
System.Console.WriteLine("{0} + {1} = {2}", Num1, Num2, Result);
}
public void Sub() //減法計算
{
double Num1 = GetNum();
double Num2 = GetNum();
System.Console.WriteLine("~~~~減法計算~~~~");
double Result = Num1 - Num2;
System.Console.WriteLine("{0} - {1} = {2}", Num1, Num2, Result);
}
public void Multi() //乘法計算
{
double Num1 = GetNum();
double Num2 = GetNum();
System.Console.WriteLine("~~~~乘法計算~~~~");
double Result = Num1 * Num2;
System.Console.WriteLine("{0} * {1} = {2}", Num1, Num2, Result);
}
public void Division() //除法計算
{
double Num1 = GetNum();
double Num2 = GetNum();
System.Console.WriteLine("~~~~除法計算~~~~");
double Result = Num1 / Num2;
System.Console.WriteLine("{0} / {1} = {2}", Num1, Num2, Result);
}
public void Remainder() //取餘計算
{
double Num1 = GetNum();
double Num2 = GetNum();
System.Console.WriteLine("~~~~取餘計算~~~~");
double Result = Num1 % Num2;
System.Console.WriteLine("{0} % {1} = {2}", Num1, Num2, Result);
}
public void Cal()
{
int Sel = GetSelector();
switch (Sel)
{
case 1:
System.Console.WriteLine("~~~~將進行加法運算~~~~");
Add();
break;
case 2:
System.Console.WriteLine("~~~~將進行減法運算~~~~");
Sub();
break;
case 3:
System.Console.WriteLine("~~~~將進行乘法運算~~~~");
Multi();
break;
case 4:
System.Console.WriteLine("~~~~將進行除法運算~~~~");
Division();
break;
case 5:
System.Console.WriteLine("~~~~將進行取餘運算~~~~");
Remainder();
break;
default:
System.Console.WriteLine("~~~~輸入有誤,程式退出!~~~~");
break;
}
}
}
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalculatorA
{
class Calculator
{
public double GetNum()
{
//資料格式為 數字.數字
Regex reg = new Regex(@"^-?\d+\.\d+$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
//資料格式為 數字
Regex reg1 =new Regex(@"^-?\d+\d+$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
//資料格式為 .數字
Regex reg2 = new Regex(@"^-?\.\d+$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
System.Console.WriteLine("請輸入一個數字");
string number = Console.ReadLine();
System.Console.WriteLine("----------------------------");
//System.Console.WriteLine(reg.Match(number1).Success); //成功返回true,否則返回false
double Num;
//只有同時不滿足上面3個正則表示式才進入迴圈,直到使用者輸入正確,滿足上述3個正則表示式,才能退出迴圈
while ((reg.Match(number).Success) != true && (reg1.Match(number).Success) != true && (reg2.Match(number).Success) != true)
{
System.Console.WriteLine("輸入有誤,請重新輸入");
number = Console.ReadLine();
System.Console.WriteLine("----------------------------");
//如果滿足上述3個正則表示式,將得到的字串轉換成double型別,並返回
if (reg.Match(number).Success || reg1.Match(number).Success || reg2.Match(number).Success)
{
System.Console.WriteLine("輸入正確!!!");
Num = Convert.ToDouble(number);
return Num;
}
}
System.Console.WriteLine("輸入正確!!!");
Num = Convert.ToDouble(number);
return Num;
}
public int GetSelector()
{
//資料格式為 數字1-5
Regex reg3 = new Regex(@"^[1-5]$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
System.Console.WriteLine("***Flora計算器***");
System.Console.WriteLine("請輸入以下數字表達你想要選擇的計算方式:");
System.Console.WriteLine("1表示加法計算");
System.Console.WriteLine("2表示減法計算");
System.Console.WriteLine("3表示乘法計算");
System.Console.WriteLine("4表示除法計算");
System.Console.WriteLine("5表示取餘計算");
string selector =Console.ReadLine();
System.Console.WriteLine("*****************");
int Sel;
//只有同時不滿足正則表示式才進入迴圈,直到使用者輸入正確,滿足正則表示式,才能退出迴圈
while ((reg3.Match(selector).Success) != true)
{
System.Console.WriteLine("您輸入的不是數字1-5,請重新輸入!");
selector = Console.ReadLine();
System.Console.WriteLine("----------------------------");
//如果滿足上述正則表示式,將得到的字串轉換成int型別,並返回
if (reg3.Match(selector).Success)
{
System.Console.WriteLine("輸入正確!!!");
Sel = int.Parse(selector);
return Sel;
}
}
System.Console.WriteLine("輸入正確!!!");
Sel = int.Parse(selector);
return Sel;
}
public void Add() //加法計算
{
double Num1 = GetNum();
double Num2 = GetNum();
System.Console.WriteLine("****加法計算****");
double Result = Num1 + Num2;
System.Console.WriteLine("{0} + {1} = {2}", Num1, Num2, Result);
}
public void Sub() //減法計算
{
double Num1 = GetNum();
double Num2 = GetNum();
System.Console.WriteLine("~~~~減法計算~~~~");
double Result = Num1 - Num2;
System.Console.WriteLine("{0} - {1} = {2}", Num1, Num2, Result);
}
public void Multi() //乘法計算
{
double Num1 = GetNum();
double Num2 = GetNum();
System.Console.WriteLine("~~~~乘法計算~~~~");
double Result = Num1 * Num2;
System.Console.WriteLine("{0} * {1} = {2}", Num1, Num2, Result);
}
public void Division() //除法計算
{
double Num1 = GetNum();
double Num2 = GetNum();
System.Console.WriteLine("~~~~除法計算~~~~");
double Result = Num1 / Num2;
System.Console.WriteLine("{0} / {1} = {2}", Num1, Num2, Result);
}
public void Remainder() //取餘計算
{
double Num1 = GetNum();
double Num2 = GetNum();
System.Console.WriteLine("~~~~取餘計算~~~~");
double Result = Num1 % Num2;
System.Console.WriteLine("{0} % {1} = {2}", Num1, Num2, Result);
}
public void Cal()
{
int Sel = GetSelector();
switch (Sel)
{
case 1:
System.Console.WriteLine("~~~~將進行加法運算~~~~");
Add();
break;
case 2:
System.Console.WriteLine("~~~~將進行減法運算~~~~");
Sub();
break;
case 3:
System.Console.WriteLine("~~~~將進行乘法運算~~~~");
Multi();
break;
case 4:
System.Console.WriteLine("~~~~將進行除法運算~~~~");
Division();
break;
case 5:
System.Console.WriteLine("~~~~將進行取餘運算~~~~");
Remainder();
break;
default:
System.Console.WriteLine("~~~~輸入有誤,程式退出!~~~~");
break;
}
}
}