0% found this document useful (0 votes)
40 views3 pages

Program Stack Stack Stack Stack

This document contains the source code for a C# program that evaluates mathematical expressions. It uses stacks to evaluate expressions by pushing operands and operators onto stacks, then popping them off to perform operations based on operator precedence. The program loops through an array of tokens from a given expression, pushes operands to one stack and operators to another. An Evaluar method pops values off the stacks to evaluate subexpressions based on operator precedence before pushing the result back to the operand stack.

Uploaded by

aigro
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

Program Stack Stack Stack Stack

This document contains the source code for a C# program that evaluates mathematical expressions. It uses stacks to evaluate expressions by pushing operands and operators onto stacks, then popping them off to perform operations based on operator precedence. The program loops through an array of tokens from a given expression, pushes operands to one stack and operators to another. An Evaluar method pops values off the stacks to evaluate subexpressions based on operator precedence before pushing the result back to the operand stack.

Uploaded by

aigro
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX or read online on Scribd
You are on page 1/ 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