Download as docx
Download as docx
You are on page 1of 3

/* Fecha de Creación : 07/09/10

* Fecha de Publicación : 08/09/10


* Creado por : IAGRO
* Lenguaje Programacion : C#
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Ejercicio4
{
class Program
{
static Stack P = new Stack();
static Stack Q = new Stack();
static string []Cadena=new string[1];
static int Indice=0;
static string ValorAnteror = "+";

static void Main(string[] args)


{
int n = int.Parse(Console.ReadLine());
for (int k = 0; k < n; k++)
{
string C = "";
C = Console.ReadLine();
Cadena = new string[1];
P = new Stack();
Q = new Stack();
Indice = 0;
ValorAnteror = "+";
Cadena[0] = " ";
Cadena = C.Split(Cadena, StringSplitOptions.None);
Resolver();
}
Console.ReadKey();
}
static void Evaluar()
{
string Operando2 = P.Pop().ToString();
string Operando1 = P.Pop().ToString();
String Operador = Q.Pop().ToString();
if (Operando1.Length > 1)
{
if ((Operador == "-" && (ValorAnteror == "-")))
{}
else
{
Operando1 = "(" + Operando1 + ")";
}
}

if (EsPrecedente(Operador))
{
if(Operando2.Length==1)
Operando1 = Operando1 + Operador + Operando2;
else
Operando1 = Operando1 + Operador +"(" + Operando2 +
")";
}
else
{
Operando1 = Operando1 + Operador + Operando2;
}
ValorAnteror = Operador;
P.Push(Operando1);
}
static bool EsPrecedente(string Operador)
{
if (Operador == "-" && ValorAnteror == "-")
{
return true;
}
if (Operador == "*" && ValorAnteror == "+")
{
return true;
}
if (Operador == "*" && ValorAnteror == "-")
{
return true;
}
return false;
}
static string Invertir()
{
string s = P.Pop().ToString();
string s2 = "";
char r = '1';
for (int k = 0; k < s.Length; k++)
{
r = s[k];
if (r == '(')
r = ')';
else
{
if (r == ')')
r = '(';
}
s2 = r.ToString() + s2;
}
return s2;
}
static void Resolver()
{
while (Indice<Cadena.Length)
{
string Caracter=Cadena[Indice];
while (char.IsDigit(Caracter[0]))
{
P.Push(Caracter);
Indice++;
Caracter = Cadena[Indice];
}
Q.Push(Caracter);
Evaluar();
Indice++;
}
Console.WriteLine(P.Pop().ToString());
}
}
}

You might also like