
Convertir código c++ en lenguaje c
Publicado por Jack (1 intervención) el 10/12/2021 00:34:54
//Programa C ++ para evaluar un árbol de expresión
#include <bits/stdc++.h>
using namespace std;
//Clase para representar los nodos del árbol sintáctico
class node
{
public:
string info;
node *left = NULL, *right = NULL;
node(string x)
{
info = x;
}
};
// Función de utilidad para devolver el valor entero de una cadena dada
int toInt(string s)
{
int num = 0;
// Comprueba si el valor integral es
// negativo o no
// Si no es negativo, genera el número
// normalmente
if(s[0]!='-')
for (int i=0; i<s.length(); i++)
num = num*10 + (int(s[i])-48);
// Si es negativo, calcula el número + ve
// primero ignorando el signo e invirtiendo el
// firmar al final
else
for (int i=1; i<s.length(); i++)
{
num = num*10 + (int(s[i])-48);
num = num*-1;
}
return num;
}
// Esta función recibe un nodo del árbol de sintaxis
// y lo evalúa de forma recursiva
int eval(node* root)
{
// árbol vacío
if (!root)
return 0;
// nodo hoja, es decir, un entero
if (!root->left && !root->right)
return toInt(root->info);
// nodo hoja, es decir, un entero
int l_val = eval(root->left);
// Evaluar el subárbol derecho
int r_val = eval(root->right);
// Marque qué operador aplicar
if (root->info=="+")
return l_val+r_val;
if (root->info=="-")
return l_val-r_val;
if (root->info=="*")
return l_val*r_val;
return l_val/r_val;
}
int main()
{
// función de controlador para verificar el programa anterior
node *root = new node("+");
root->left = new node("*");
root->left->left = new node("5");
root->left->right = new node("-4");
root->right = new node("-");
root->right->left = new node("100");
root->right->right = new node("20");
cout << eval(root) << endl;
delete(root);
root = new node("+");
root->left = new node("*");
root->left->left = new node("5");
root->left->right = new node("4");
root->right = new node("-");
root->right->left = new node("100");
root->right->right = new node("/");
root->right->right->left = new node("20");
root->right->right->right = new node("2");
cout << eval(root);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
//Clase para representar los nodos del árbol sintáctico
class node
{
public:
string info;
node *left = NULL, *right = NULL;
node(string x)
{
info = x;
}
};
// Función de utilidad para devolver el valor entero de una cadena dada
int toInt(string s)
{
int num = 0;
// Comprueba si el valor integral es
// negativo o no
// Si no es negativo, genera el número
// normalmente
if(s[0]!='-')
for (int i=0; i<s.length(); i++)
num = num*10 + (int(s[i])-48);
// Si es negativo, calcula el número + ve
// primero ignorando el signo e invirtiendo el
// firmar al final
else
for (int i=1; i<s.length(); i++)
{
num = num*10 + (int(s[i])-48);
num = num*-1;
}
return num;
}
// Esta función recibe un nodo del árbol de sintaxis
// y lo evalúa de forma recursiva
int eval(node* root)
{
// árbol vacío
if (!root)
return 0;
// nodo hoja, es decir, un entero
if (!root->left && !root->right)
return toInt(root->info);
// nodo hoja, es decir, un entero
int l_val = eval(root->left);
// Evaluar el subárbol derecho
int r_val = eval(root->right);
// Marque qué operador aplicar
if (root->info=="+")
return l_val+r_val;
if (root->info=="-")
return l_val-r_val;
if (root->info=="*")
return l_val*r_val;
return l_val/r_val;
}
int main()
{
// función de controlador para verificar el programa anterior
node *root = new node("+");
root->left = new node("*");
root->left->left = new node("5");
root->left->right = new node("-4");
root->right = new node("-");
root->right->left = new node("100");
root->right->right = new node("20");
cout << eval(root) << endl;
delete(root);
root = new node("+");
root->left = new node("*");
root->left->left = new node("5");
root->left->right = new node("4");
root->right = new node("-");
root->right->left = new node("100");
root->right->right = new node("/");
root->right->right->left = new node("20");
root->right->right->right = new node("2");
cout << eval(root);
return 0;
}
Valora esta pregunta


0