Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void calculadora_polaca(char** comandos){
- pila_t* pila = pila_crear();
- if(!pila){
- fprintf(stdout, "%s", "ERROR\n");
- return;
- }
- bool ok = true;
- for(int i = 0; comandos[i]; i++){
- int* n = atoi(comandos[i]);
- if(*n || strcmp(comandos[i], "0") == 0){
- pila_apilar(pila, n);
- }
- else{
- free(n);
- calcular(pila, comandos[i], &ok);
- if(!ok){
- break;
- }
- }
- }
- if(!ok){
- fprintf(stdout, "%s\n", "ERROR");
- }
- else{
- int* respuesta = pila_desapilar(pila);
- if(pila_esta_vacia(pila)){
- fprintf(stdout, "%d\n", *respuesta);
- }
- else{
- fprintf(stdout, "%s\n", "ERROR");
- free(respuesta);
- }
- }
- free_strv(comandos);
- pila_destruir(pila)
- }
- int main(int argc, char* argv[]){
- size_t tam = 0;
- FILE* archivo;
- if(argc == 1){
- archivo = stdin;
- }
- else if(argc == 2){
- char* entrada = argv[1];
- archivo = fopen(entrada, "r");
- if(!archivo){
- fprintf(stderr, "%s", "No se pudo leer el archivo\n");
- return -1;
- }
- }
- else{
- fprintf(stderr, "%s", "Cantidad de parametros erroneo\n");
- return -1;
- }
- char* linea;
- while(getline(&linea, &tam, archivo) != EOF){
- char** linea_act = split(linea, ' ')
- if(!linea_act){
- fclose(archivo);
- fprintf(archivo, "%s", "ERROR\n");
- return -1;
- }
- calculadora_polaca(linea_act);
- free_strv(linea_act);
- }
- free(linea);
- fclose(archivo);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement