Здравствуйте
Помогите пожалуйста доработать программу
Необходимо, чтобы из файла считывалась формула, а не постоянное выражение. То есть программа должна считывать две логические формулы и определять, эквивалентны ли они, но формулы должны быть представлены в общем виде
Код:
class Program
{
//Метод возвращает true, если проверяемый символ - разделитель ("пробел" или "равно")
static private bool IsDelimeter(char c)
{
if ((" =".IndexOf(c) != -1))
return true;
return false;
}
//Метод возвращает true, если проверяемый символ - оператор
static private bool IsOperator(char с)
{
if (("&|!→↔()".IndexOf(с) != -1))
return true;
return false;
}
//Метод возвращает приоритет оператора
static private byte GetPriority(char s)
{
switch (s)
{
case '(': return 0;
case ')': return 1;
case '|': return 3;
case '&': return 4;
case '!': return 5;
case '→': return 2;//импликация
case '↔': return 2;//эквивалентность
default: return 7;
}
}
static public double Calculate(string input)
{
string output = GetExpression(input);
double result = Counting(output);
return result;
}
static private string GetExpression(string input)
{
string output = string.Empty;
Stack<char> operStack = new Stack<char>();
for (int i = 0; i < input.Length; i++)
{
if (IsDelimeter(input[i]))
continue;
if (Char.IsDigit(input[i])) //Если цифра
{
while (!IsDelimeter(input[i]) && !IsOperator(input[i]))
{
output += input[i];
i++;
if (i == input.Length) break;
}
output += " ";
i--;
}
if (IsOperator(input[i]))
{
if (input[i] == '(')
operStack.Push(input[i]);
else if (input[i] == ')')
{
char s = operStack.Pop();
while (s != '(')
{
output += s.ToString() + ' ';
s = operStack.Pop();
}
}
else
{
if (operStack.Count > 0)
if (GetPriority(input[i]) <= GetPriority(operStack.Peek()))
output += operStack.Pop().ToString() + " ";
operStack.Push(char.Parse(input[i].ToString()));
}
}
}
while (operStack.Count > 0)
output += operStack.Pop() + " ";
return output;
}
static private double Counting(string input)
{
double result = 0;
Stack<double> temp = new Stack<double>();
for (int i = 0; i < input.Length; i++)
{
if (Char.IsDigit(input[i]))
{
string a = string.Empty;
while (!IsDelimeter(input[i]) && !IsOperator(input[i])) //Пока не разделитель
{
a += input[i];
i++;
if (i == input.Length) break;
}
temp.Push(double.Parse(a));
i--;
}
else if (IsOperator(input[i]) && (input[i]!='!'))
{
double a = temp.Pop();
double b = temp.Pop();
switch (input[i])
{
case '|':
{
if (a == 1 && b == 1) result = 1;
if (a == 1 && b == 0) result = 1;
if (a == 0 && b == 1) result = 1;
if (a == 0 && b == 0) result = 0;
}
break;
case '&':
{
if (a == 1 && b == 1) result = 1;
if (a == 1 && b == 0) result = 0;
if (a == 0 && b == 1) result = 0;
if (a == 0 && b == 0) result = 0;
}
break;
case '→'://импликация
{
if (a == 1 && b == 1) result = 1;
if (a == 1 && b == 0) result = 1;
if (a == 0 && b == 1) result = 0;
if (a == 0 && b == 0) result = 1;
}
break;
case '↔'://эквивалентность
{
if (a == 1 && b == 1) result = 1;
if (a == 1 && b == 0) result = 0;
if (a == 0 && b == 1) result = 0;
if (a == 0 && b == 0) result = 1;
}
break;
}
temp.Push(result);
}
else if (IsOperator(input[i]) && (input[i] == '!'))
{
double a = temp.Pop();
if (a == 1) result = 0;
if (a == 0) result = 1;
temp.Push(result);
}
}
return temp.Peek();
}
static void Main(string[] args)
{
string line, FileName = "1.txt";//здеcь, например, находится выражение (1&0),нужно, чтобы находилось (a&b)
using (StreamReader MyFile = new StreamReader(FileName))
{
line = MyFile.ReadLine();
}
string line1, FileName1 = "2.txt";//здеcь, например, находится выражение (1&0)v0,нужно, чтобы находилось (a&b)vс
using (StreamReader MyFile = new StreamReader(FileName1))
{
line1 = MyFile.ReadLine();
}
int i = 0;
for (i = 0; i < line.Length; i++)
{
Console.Write(line[i]);
}
Console.Write(" = ");
Console.WriteLine(Program.Calculate(line));
Console.WriteLine();
for (i = 0; i < line1.Length; i++)
{
Console.Write(line1[i]);
}
Console.Write(" = ");
Console.WriteLine(Program.Calculate(line1));
Console.WriteLine();
if (Program.Calculate(line) == Program.Calculate(line1))
{
Console.WriteLine("Формулы эквивалентны");
}
else Console.WriteLine("Формулы не эквивалентны");
Console.ReadKey();
}