Pagini recente » Cod sursa (job #1162545) | Cod sursa (job #2952660) | Cod sursa (job #3264618) | Cod sursa (job #2937446) | Cod sursa (job #3260813)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Testare
{
class PrecedentaOperator
{
public static int Precedenta(char op)
{
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
else
return 0;
}
}
class Operatie
{
public static int Operatii(int n, int m, char c)
{
if (c == '+')
return n + m;
if (c == '-')
return n - m;
if (c == '*')
return n * m;
if (c == '/')
return n / m;
throw new InvalidOperationException("Invalid operator");
}
}
class Evaluari
{
public static int Evaluare(string expresie)
{
int i;
Stack<int> valoare = new Stack<int>();
Stack<char> operanzi = new Stack<char>();
int x = expresie.Length;
for (i = 0; i < x; i++)
{
if (expresie[i] == ' ')
continue;
if (expresie[i] == '(')
{
operanzi.Push(expresie[i]);
}
else
if (char.IsDigit(expresie[i]))
{
int val = 0;
while (i < x && char.IsDigit(expresie[i]))
{
val = (val * 10) + (expresie[i] - '0');
i++;
}
valoare.Push(val);
i--;
}
else if (expresie[i] == ')')
{
while (operanzi.Count > 0 && operanzi.Peek() != '(')
{
int val2 = valoare.Pop();
int val1 = valoare.Pop();
char op = operanzi.Pop();
valoare.Push(Operatie.Operatii(val1, val2, op));
}
if (operanzi.Count > 0)
operanzi.Pop();
}
else
{
while (operanzi.Count > 0 && PrecedentaOperator.Precedenta(operanzi.Peek()) >= PrecedentaOperator.Precedenta(expresie[i]))
{
int val2 = valoare.Pop();
int val1 = valoare.Pop();
char op = operanzi.Pop();
valoare.Push(Operatie.Operatii(val1, val2, op));
}
operanzi.Push(expresie[i]);
}
}
while (operanzi.Count > 0)
{
int val2 = valoare.Pop();
int val1 = valoare.Pop();
char op = operanzi.Pop();
valoare.Push(Operatie.Operatii(val1, val2, op));
}
return valoare.Peek();
}
}
class CitireAfis
{
public static string Citire(StreamReader fin)
{
return fin.ReadLine();
}
public static void afisare(StreamWriter fout, string sir)
{
fout.WriteLine(Evaluari.Evaluare(sir));
fout.Close();
}
}
class Program
{
public static void Main()
{
foreach (string e in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory))
if (e.EndsWith(".in"))
{
Console.WriteLine(e);
StreamReader fin = new StreamReader(e);
StreamWriter fout = new StreamWriter(e.Replace(".in", ".out"));
CitireAfis.afisare(fout, CitireAfis.Citire(fin));
fin.Close();
fout.Close();
}
int score = 0;
foreach (string e in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory))
if (e.EndsWith(".ok"))
{
StreamReader fout1 = new StreamReader(e);
StreamReader fout2 = new StreamReader(e.Replace(".ok", ".out"));
if (fout1.ReadLine().Equals(fout2.ReadLine()))
score += 10;
fout1.Close();
fout2.Close();
}
Console.WriteLine(score);
}
}
}