就如圖上顯示的我移動到“=”號處按Enter 就表示輸入了“=”號就開始計算結果了。
說下我的思路:我這這些字符123456789.+*-/=保存在一個數組裏,一維二維都可,爲了方便操作我用一維數組checkchar[]來保存。但是看的時候,要把他當做二維數組來看
我這樣排着看這16個字符: 1 2 3 .
4 5 6 +
7 8 9 -
0 = * /
對應的索引
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
這樣容易發現規律。比如0 4 8 12這一排數 mod4都等於04 5 6 7
8 9 10 11
12 13 14 15
3 7 11 15加1之後mod 4等於0
這樣可以作爲邊界值的判斷依據。詳細的代碼中有。
聲明一個int 變量 index 他默認時候就就指向字符'1',當我按右方向鍵是 index就index加1 這樣就 就指向了'2' ,當然還要考慮邊界值的問題,如果我按上方向鍵不能指丟了。有了索引我就根據checkchar[index]找到我當前選中是哪個字符,按enter就告訴程序我輸入了哪個字符。然後程序對這個字符做處理。
貼下代碼,這個程序還沒實現允許使用退格鍵,如果用戶想用數字鍵輸入也兼容也沒實現。也沒做容錯機制
class Program
{
static string display = "";//用於顯示用戶的輸入
static string first = "";//保存輸入的第一個數
static string second = "";//保存輸入的第二個數
static string res = "";//用於保存結果
static char operatorChar = '0';//用於接收用戶輸入的運算符 0表示沒有運算符
static string[] charDnumber = new string[] { "(1)", "(2)", "(3)", "(.)", "(4)", "(5)", "(6)", "(+)", "(7)", "(8)", "(9)", "(-)", "(0)", "(=)", "(*)", "(/)" };
static int[] operatorchar = new int[] {7,11,14,15};//對應 +-*/
static ConsoleKey[] direction = new ConsoleKey[] { ConsoleKey.UpArrow,ConsoleKey.DownArrow,ConsoleKey.LeftArrow,ConsoleKey.RightArrow};
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("\n\t\t\t請用方向鍵,Enter鍵,退格鍵操作");
Console.ResetColor();
char[] charTable = new char[16] { '1', '2', '3', '.' , '4', '5', '6', '+' , '7', '8', '9', '-' , '0', '=', '*', '/' };//構建一維的數組。
print(display, -1);//打印一個界面
bool flog = true;
int currentIndex = -1;//當前的-1默認指向爲空
while (flog)
{
ConsoleKeyInfo cki = Console.ReadKey();
if (isdirection(cki.Key)&& currentIndex == -1)
{
currentIndex = 0;//輸入了方向鍵就默認指向0號索引
Console.Clear();
print(display, 0);
continue;
}
if (isdirection(cki.Key) && currentIndex != -1)//如果用戶輸入了方向鍵
{
switch (cki.Key)
{
case ConsoleKey.UpArrow:
currentIndex =up(currentIndex);
break;
case ConsoleKey.DownArrow:
currentIndex = down(currentIndex);
break;
case ConsoleKey.LeftArrow:
currentIndex = left(currentIndex);
break;
case ConsoleKey.RightArrow:
currentIndex = right(currentIndex);
break;
default:
break;
}
Console.Clear();
print(display, currentIndex);
}
if(cki.Key==ConsoleKey.Enter)//如果輸入了enter鍵
{
if (isOperateor(currentIndex))//此時輸入了運算符
{
operatorChar = charTable[currentIndex];
display += charTable[currentIndex].ToString();
Console.Clear();
print(operatorChar.ToString(), currentIndex);
continue;
}
if (operatorChar == '0')//此時沒有輸入運算符 ,就把輸入保存到first
{
first = first + charTable[currentIndex].ToString();
display = display + charTable[currentIndex].ToString();
//first = tempint;
Console.Clear();
print(first, currentIndex);
continue;
}
if (currentIndex == 13 && first != "" && second != "" && operatorChar != '0')//此時已經輸入運算符 ,兩個操作數也有,就根據操作數運算
{
switch (operatorChar)
{
case '+':
res = add(first, second);
break;
case '-':
res = minus(first, second);
break;
case '*':
res = multiply(first, second);
break;
case '/':
res = divide(first, second);
break;
default:
break;
}
Console.Clear();
print(res, -1);
init();//初始化一下各個變量
}
if (operatorChar != '0' && first != "")//此時已經輸入運算符 ,first也不爲空,就把輸入保存到second
{
second = second + charTable[currentIndex].ToString();
display = display + charTable[currentIndex].ToString();
Console.Clear();
print(second, currentIndex);
continue;
}
}
}
}
/// <summary>
/// 初始化程序..
/// </summary>
static void init()
{
display = "";
first = "";
second = "";
operatorChar = '0';
}
/// <summary>
/// 打印界面
/// </summary>
/// <param name="number">顯示的數據</param>
/// <param name="index">要高亮字符的索引</param>
static void print(string number, int index)
{
string[] charNumber = new string[] { " 1 ", " 2 ", " 3 ", " . ", " 4 ", " 5 ", " 6 ", " + ", " 7 ", " 8 ", " 9 ", " - ", " 0 ", " = ", " * ", " / " };
if (index > -1 && index < 16)
{
charNumber[index ] = charDnumber[index ];
}
Console.Write("\n\n\n\n\t\t\t┏");
Console.Write("━━━━━━━━━━");
Console.WriteLine("┓");
Console.WriteLine("\t\t\t┃\t\t ┃");
int count = 0;
count = number.Length;
Console.Write("\t\t\t┃");
for (int i = 0; i < 19 - count; i++)
{
Console.Write(" ");
}
Console.Write(number);
Console.WriteLine(" ┃");
Console.WriteLine("\t\t\t┃--------------------┃");
Console.WriteLine("\t\t\t┃ " + charNumber[0] + " " + charNumber[1] + " " + charNumber[2] + " " + charNumber[3] + "┃");
Console.WriteLine("\t\t\t┃\t\t ┃");
Console.WriteLine("\t\t\t┃ " + charNumber[4] + " " + charNumber[5] + " " + charNumber[6] + " " + charNumber[7] + "┃");
Console.WriteLine("\t\t\t┃\t\t ┃");
Console.WriteLine("\t\t\t┃ " + charNumber[8] + " " + charNumber[9] + " " + charNumber[10] + " " + charNumber[11] + "┃");
Console.WriteLine("\t\t\t┃\t\t ┃");
Console.WriteLine("\t\t\t┃ " + charNumber[12] + " "+charNumber[13]+" " + charNumber[14] + " " + charNumber[15] + "┃");
Console.WriteLine("\t\t\t┃\t\t ┃");
Console.WriteLine("\t\t\t┗━━━━━━━━━━┛");
}
//獲取輸入字符的索引
static int getIndex(char flog)
{
switch (flog)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '.':
return 4;
case '4':
return 5;
case '5':
return 6;
case '6':
return 7;
case '+':
return 8;
case '7':
return 9;
case '8':
return 10;
case '9':
return 11;
case '-':
return 12;
case '0':
return 13;
case '=':
return 14;
case '*':
return 15;
case '/':
return 16;
}
return -1;
}
//加運算
static string add(string a, string b)
{
double i = Double.Parse(a);
double j = Double.Parse(b);
return (j + i).ToString();
}
//減運算
static string minus(string a, string b)
{
double i = Double.Parse(a);
double j = Double.Parse(b);
return (i - j).ToString();
}
//×運算
static string multiply(string a, string b)
{
double i = Double.Parse(a);
double j = Double.Parse(b);
return (j * i).ToString();
}
//除運算
static string divide(string a, string b)
{
double i = Double.Parse(a);
double j = Double.Parse(b);
if (j == 0)
{
return "0";
}
return (i / j).ToString();
}
//按up鍵
static int up(int current)
{
if (current>=0&¤t<=3)//說明現在鍵在0-3索引出
{
return current+12;
}
else
{
return current - 4;
}
}
//按下down鍵
static int down(int current)
{
if (current>=12&¤t<=15)//說明現在鍵在12-15索引出
{
return current-12;
}
else
{
return current + 4;
}
}
//按下left鍵
static int left(int current)
{
if (current % 4 == 0)
{
return current + 3;
}
else
{
return current - 1;
}
}
//按下reght鍵
static int right(int current)
{
if ((current + 1) % 4 == 0)
{
return current - 3;
}
else
{
return current + 1;
}
}
//判斷是不是操作符
static bool isOperateor(int tempindex)
{
if (operatorchar.Contains(tempindex))
{
return true;
}
else
{
return false;
}
}
//判斷是不是輸入了方向鍵
static bool isdirection(ConsoleKey tempchar)
{
if (direction.Contains(tempchar))
{
return true;
}
else
{
return false;
}
}
}