?? calculator.cs
字號(hào):
}
/// <summary>
/// Elimina elementos vacios
/// </summary>
/// <param name="toRemove">Cadena para determinar si el nodo que la contiene debe ser borrado</param>
/// <returns>Verdadero si debe ser borrado, de otro modo falso</returns>
static bool RemoveEmpty(String toRemove)
{
if (toRemove == "")
return true;
return false;
}
/// <summary>
/// Determina la prioridad de un operador
/// </summary>
/// <param name="oper">El operador a evaluar</param>
/// <returns>El peso o prioridad del operador</returns>
static int Priority(char oper)
{
if (oper == '*' || oper == '/' || oper == '%')
return 1;
else if (oper == '+' || oper == '-')
return 0;
return -1;
}
/// <summary>
/// Determina si una cadena es un operador no suma ni resta
/// </summary>
/// <param name="elem">La cadena a evaluar</param>
/// <returns>Verdadero si es un operador no suma ni resta, falso de otro modo</returns>
static bool IsOperator(String elem)
{
if (elem == "*" || elem == "/" || elem == "%")
return true;
return false;
}
/// <summary>
/// Identifica los bloques de operaciones a realizar
/// </summary>
/// <param name="postOrder">La expresión en formato postOrden</param>
/// <returns>La lista de operaciones a realizar</returns>
static List<R> IdentifyOperations(List<String> postOrder)
{
List<R> ListaR = new List<R>();
if (postOrder.Count > 1)
{
ListaR = new List<R>(); //Contiene la lista de R (resultados)
int contadorR = 0; //Cuenta el numero de expresiones
int indiceOperador = -1;
while (postOrder.Count > 1)
{
indiceOperador = BuscaOperador(postOrder, ref contadorR);
Identifica(ref postOrder, indiceOperador, ref ListaR, contadorR);
}
}
else
{
R errecita = new R();
errecita.getFirst = postOrder[0];
errecita.getSubindice = 1;
ListaR.Add(errecita);
}
return ListaR;
}
/// <summary>
/// Regresa el operador de la expresión
/// </summary>
/// <param name="Lista">La lista de los elementos en postorden</param>
/// <param name="contadorR">El índice de las operaciones</param>
/// <returns>El índice del operador</returns>
static int BuscaOperador(List<String> Lista, ref int contadorR)
{
int indice = -1;
for (int i = 0; i < Lista.Count; i++)
if (Lista[i].Equals("+") || Lista[i].Equals("-") || Lista[i].Equals("*") || Lista[i].Equals("/") || Lista[i].Equals("%"))
{
indice = i;
contadorR++;
break;
}
return indice;
}
/// <summary>
/// Identifica las expresiones (operandos y operador)
/// </summary>
/// <param name="Lista">La lista de los elementos en postorden</param>
/// <param name="indice">índice del operador</param>
/// <param name="ListaR">La lista de relaciones</param>
/// <param name="contadorR">>El índice de las operaciones</param>
static void Identifica(ref List<String> Lista, int indice, ref List<R> ListaR, int contadorR)
{
indice = indice - 2;
//el inicio de la expresion
int indiceprincipal = indice;
String first = null;
String second = null;
String op = null;
first = Lista[indice];
op = Lista[indice + 2];
second = Lista[indice + 1];
//Buscar R's en la expresion
//Si hay R estadoR = true, sino false.
bool estadoR = (first.Contains("R") || second.Contains("R"));
//Si existe una R la expresion es dependiente
//Removemos los nodos de la Lista, los que estan a la derecha de indiceprincipal
for (int contador = 0; contador < 3; contador++)
Lista.RemoveAt(indiceprincipal);
//Sustituir la expresion por una R
Lista.Insert(indiceprincipal, "R" + contadorR);
//a?adir un nodo a la ListaR: expresion, número y estado
AgregaR(first, second, op, ref ListaR, contadorR, estadoR);
}
/// <summary>
/// Agrega un elemento a la lista de operaciones
/// </summary>
/// <param name="first">Primer operando</param>
/// <param name="second">Segundo operando</param>
/// <param name="op">Operador</param>
/// <param name="ListaR">Lista de operaciones a rellenar</param>
/// <param name="contadorR">índice de la operación</param>
static void AgregaR(String first, String second, String op, ref List<R> ListaR, int contadorR, bool estadorR)
{
R objeto = new R();
objeto.getSubindice = contadorR;
objeto.getFirst = first;
objeto.getSecond = second;
objeto.getOp = op;
ListaR.Add(objeto);
}
/// <summary>
/// Regresa una lista de operaciones en una matriz de acuerdo a tiempos y procesos
/// </summary>
/// <param name="operations">La lista de operaciones</param>
/// <param name="process">El número de procesos permisibles</param>
/// <returns></returns>
static String[,] Organize(List<R> operations, int process)
{
List<String>[] table = new List<String>[process];
for (int i = 0; i < process; i++)
table[i] = new List<String>();
List<R> helpList = new List<R>();
while (operations.Count != 0)
{
helpList.Clear();
for (int i = 0; i < process; i++)
{
int j = 0;
foreach (R errecita in operations)
{
if (errecita.getOp != null)
{
if (errecita.getFirst[0] != 'R' && errecita.getSecond[0] != 'R')
{
table[i].Add("R" + errecita.getSubindice + "= " + errecita.getFirst + errecita.getOp + errecita.getSecond);
helpList.Add(errecita);
operations.RemoveAt(j);
break;
}
else
{
bool flag = false;
if (errecita.getFirst[0] == 'R' && (Independent(ref helpList, ref operations, int.Parse(errecita.getFirst.Substring(1, errecita.getFirst.Length - 1)))))
flag = true;
if (errecita.getFirst[0] != 'R')
flag = true;
if ((errecita.getSecond[0] != 'R' && flag) || (flag && errecita.getSecond[0] == 'R' && (Independent(ref helpList, ref operations, int.Parse(errecita.getSecond.Substring(1, errecita.getSecond.Length - 1))))))
{
table[i].Add("R" + errecita.getSubindice + "= " + errecita.getFirst + errecita.getOp + errecita.getSecond);
helpList.Add(errecita);
operations.RemoveAt(j);
break;
}
}
}
else
{
table[i].Add("R" + errecita.getSubindice + "= " + errecita.getFirst);
operations.RemoveAt(0);
break;
}
j++;
}
}
}
#region Conversión de arreglo de listas a matriz
int max = 0;
for (int i = 0; i < process; i++)
if (table[i].Count > max)
max = table[i].Count;
String[,] result = new String[process, max];
for (int i = 0; i < process; i++)
{
String[] preTable = table[i].ToArray();
for (int j = 0; j < max; j++)
{
if (j < preTable.Length)
result[i, j] = preTable[j];
else
result[i, j] = "";
}
}
#endregion
return result;
}
/// <summary>
/// Determina si un operando deja de ser dependiente
/// </summary>
/// <param name="helpList">Los elementos que se a?aden en la misma columna del tiempo</param>
/// <param name="relations">La lista de relaciones que no han sido usadas</param>
/// <param name="indexOfR">El índice del cual se solía ser dependiente</param>
/// <returns>Verdadero si el operando es libre de salir, falso de otro modo</returns>
static bool Independent(ref List<R> helpList, ref List<R> relations, int indexOfR)
{
foreach (R relation in relations)
if (relation.getSubindice == indexOfR)
return false;
foreach (R relation in helpList)
if (relation.getSubindice == indexOfR)
return false;
return true;
}
}
class R
{
int subindice = 0;
String First = null;
String Op = null;
String Second = null;
public int getSubindice
{
get { return this.subindice; }
set { this.subindice = value; }
}
public String getFirst
{
get { return this.First; }
set { this.First = value; }
}
public String getOp
{
get { return this.Op; }
set { this.Op = value; }
}
public String getSecond
{
get { return this.Second; }
set { this.Second = value; }
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -