using System;
class TwentyFourPoint
{
private char[] card = new char[4];
private char[] tempSortCard = new char[4];
private char[] sortCard = new char[96];
private char[] symbol = {'+','-','*','/'};
private char[] symbolSortCard = new char[448];
private char[] tempSymbolSortCard = new char[3];
private ExpressionValue expressionValue = new ExpressionValue();
public void SetCard(char[] card)
{
this.card = card;
}
public void SortCard()
{
int counter = 0;
for(int i = 0; i < 4; i++)
{
tempSortCard[0] = card[i];
for(int j = 0; j < 4; j++)
{
if( i != j)
{
tempSortCard[1] = card[j];
for(int k = 0; k < 4; k++)
{
if(k != i && k != j)
{
tempSortCard[2] = card[k];
for(int m = 0; m < 4; m++)
{
if(m != i && m != j && m!= k)
{
tempSortCard[3] = card[m];
for(int n = counter, p = 0; n < counter + 4; n++,p++)
sortCard[n] = tempSortCard[p];
counter += 4;
}
}
}
}
}
}
}
}
private void AddSymbol()
{
int counter = 1;
for(int i = 0; i < 4; i++)
{
tempSymbolSortCard[0] = symbol[i];
for(int j = 0; j < 4; j++)
{
tempSymbolSortCard[1] = symbol[j];
for(int k = 0; k < 4; k++)
{
tempSymbolSortCard[2] = symbol[k];
for(int n = counter, p = 0; n < counter + 6; n+=2,p++)
symbolSortCard[n] = tempSymbolSortCard[p];
counter += 7;
}
}
}
}
public void AddNumToSymbol()
{
int counterA = 0;
int counterB = 0;
int counterC = 0;
char[] temp = new char[4];
char[] tempExpression = new char[20];
char[] tempExpressionNoSpace = new char[20];
char[] tempExpressionII;
for(int i = 0; i < tempExpression.Length; i++)
tempExpression[i] = 'e';
tempExpression[15] = '#';
tempExpression[16] = '$';
SortCard();
AddSymbol();
for(int m = counterA; m < counterA + 4; m += 4)
{
temp[0] = sortCard[counterA];
temp[1] = sortCard[counterA + 1];
temp[2] = sortCard[counterA + 2];
temp[3] = sortCard[counterA + 3];
counterA += 4;
if(counterA == 96)
counterA = 0;
for(int i = 0; i < 64; i++)
{
for(int j = counterB,k = 0; j < counterB + 7; j += 2, k++)
symbolSortCard[j] = temp[k%4];
counterB = counterB + 7;
if(counterB == 448)
counterB = 0;
}
for(int i = 0; i < 64; i++)
{
for(int p = counterC; p < counterC + 7; p++)
{
tempExpression[2 * (p%7) +1] = symbolSortCard[p];
}
tempExpressionNoSpace = CompactSpace(tempExpression);
expressionValue.SetExpression(tempExpressionNoSpace);
if(expressionValue.Value() == 24)
PrintExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionNoSpace);
tempExpressionII = (char []) tempExpression.Clone();
tempExpressionII[0] = '(';
tempExpressionII[6] = ')';
tempExpressionII[8] = '(';
tempExpressionII[14] = ')';
tempExpressionNoSpace = CompactSpace(tempExpressionII);
expressionValue.SetExpression(tempExpressionNoSpace);
if(expressionValue.Value() == 24)
PrintExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionII);
tempExpressionII = (char []) tempExpression.Clone();
tempExpressionII[0] = '(';
tempExpressionII[6] = ')';
tempExpressionNoSpace = CompactSpace(tempExpressionII);
expressionValue.SetExpression(tempExpressionNoSpace);
if(expressionValue.Value() == 24)
PrintExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionII);
tempExpressionII = (char []) tempExpression.Clone();
tempExpressionII[0] = '(';
tempExpressionII[10] = ')';
tempExpressionNoSpace = CompactSpace(tempExpressionII);
expressionValue.SetExpression(tempExpressionNoSpace);
if(expressionValue.Value() == 24)
PrintExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionII);
tempExpressionII = (char []) tempExpression.Clone();
tempExpressionII[4] = '(';
tempExpressionII[10] = ')';
tempExpressionNoSpace = CompactSpace(tempExpressionII);
expressionValue.SetExpression(tempExpressionNoSpace);
if(expressionValue.Value() == 24)
PrintExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionII);
tempExpressionII = (char []) tempExpression.Clone();
tempExpressionII[4] = '(';
tempExpressionII[14] = ')';
tempExpressionNoSpace = CompactSpace(tempExpressionII);
expressionValue.SetExpression(tempExpressionNoSpace);
if(expressionValue.Value() == 24)
PrintExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionII);
tempExpressionII = (char []) tempExpression.Clone();
tempExpressionII[8] = '(';
tempExpressionII[14] = ')';
tempExpressionNoSpace = CompactSpace(tempExpressionII);
expressionValue.SetExpression(tempExpressionNoSpace);
if(expressionValue.Value() == 24)
PrintExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionNoSpace);
ClearExpression(tempExpressionII);
counterC = counterC + 7;
if(counterC == 448)
counterC = 0;
}
}
}
private void ClearExpression(char[] expression)
{
for(int i = 0; i < expression.Length; i++)
expression[i] = 'e';
}
private char[] CompactSpace(char[] A)
{
int j = 0;
char[] B = new char[20];
for(int i = 0; i < 17; i++)
{
if(A[i] == 'e')
continue;
else
B[j++] = A[i];
}
return B;
}
private void PrintExpression(char[] a)
{
for(int i = 0; a[i] != '#'; i++)
{
if(a[i] == '0')
Console.Write("10");
else
Console.Write(a[i]);
}
Console.Write("/n");
}
}
public class CharStack
{
private char[] list = new char[10];
private int sp = 10;
public void Push(char a)
{
if(sp != 0)
{
sp--;
list[sp] = a;
}
else
{
Console.WriteLine("The CharStack is full");
Console.Read();
}
}
public char Pop()
{
if(sp != 10)
{
sp++;
return list[sp - 1];
}
else
{
Console.WriteLine("The CharStack is empty");
return 'E';
}
}
public char CheckTop()
{
if(sp != 10)
return list[sp];
else
{
Console.WriteLine("The CCharStack is empty");
return 'E';
}
}
}
public class IntStack
{
private int[] list = new int[10];
private int sp = 10;
public void Push(int a)
{
if(sp != 0)
{
sp--;
list[sp] = a;
}
else
Console.WriteLine("The IntStack is full");
}
public int Pop()
{
if(sp != 10)
{
sp++;
return list[sp - 1];
}
else
{
Console.WriteLine("The IntStack is empty");
return -1;
}
}
public int CheckTop()
{
if(sp != 10)
return list[sp];
else
{
Console.WriteLine("The CIntStack is empty");
return -1;
}
}
}
public class ExpressionValue
{
private IntStack opd = new IntStack();
private CharStack opr = new CharStack();
private char[] expression = new char[15];
public void SetExpression(char[] expression)
{
this.expression = expression;
}
public int Value()
{
char letter;
opr.Push('#');
for(int i = 0; i != '$'; i++)
{
letter = expression[i];
if((int)letter >= (int)'0' && (int)letter <=(int)'9')
{ if((int)letter == (int)'0')
opd.Push(10);
else
opd.Push(Convert.ToInt32(letter.ToString()));
continue;
}
else if(letter == '#' && opr.CheckTop() == '#')
break;
else if(letter == '(')
{
opr.Push(letter);
continue;
}
else if(letter ==')')
{
if(opr.CheckTop() != '(')
{
int m, n;
n = opd.Pop();
m = opd.Pop();
opd.Push(Operate(m, opr.Pop(), n));
i--;
}
else
{
opr.Pop();
continue;
}
}
else if(Priority(letter) > Priority(opr.CheckTop()))
{
opr.Push(letter);
continue;
}
else if(Priority(letter) <= Priority(opr.CheckTop()))
{
int m, n;
n = opd.Pop();
m = opd.Pop();
opd.Push(Operate(m, opr.Pop(), n));
i--;
continue;
}
}
opr.Pop();
return opd.Pop();
}
private int Priority(char c)
{
int p = 0;
switch(c)
{
case '*':
case '/': p = 2;
break;
case '+':
case '-': p = 1;
break;
case '(': p = 0;
break;
case '#': p = -1;
break;
}
return p;
}
private int Operate(int opdA, char oprC, int opdB)
{
switch(oprC)
{
case '+':return (opdA + opdB);
case '-':return (opdA - opdB);
case '*':return (opdA * opdB);
case '/':
if(opdB != 0 && (opdA % opdB == 0))
return (opdA / opdB);
else
return -10000000;
default:return -20000000;
}
}
}
public class Run
{
public static void Main()
{
TwentyFourPoint A = new TwentyFourPoint();
char[] card = new char[4];
string input = "";
Console.WriteLine("<-----------------------------本程序用来做24点计算----------------------------->");
do
{
Console.WriteLine("请输入从1至10范围内的4个数字:");
for(int i = 0; i < card.Length; i++)
{
input = Console.ReadLine();
if(input == "10")
card[i] = '0';
else
card[i] = Convert.ToChar(input);
}
A.SetCard(card);
A.AddNumToSymbol();
Console.WriteLine("如想退出本程序,请输入字母E,否则按任意键继续......");
input = Console.ReadLine();
}while(input.ToUpper() != "E");
}
}