Skip to content

Commit

Permalink
bugfix: multiple dots in a number now detected
Browse files Browse the repository at this point in the history
  • Loading branch information
feer9 committed Mar 12, 2019
1 parent 1d79bec commit e34f779
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 29 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ obj/mainWindow.o: src/mainWindow.c src/mainWindow.h
obj:
mkdir $@

install: $(OBJS) $(EXECUTABLE)
$(INSTALL_DIR):
mkdir $@

$(LIBS_DIR):
mkdir $@

install: $(OBJS) $(EXECUTABLE) $(INSTALL_DIR) $(LIBS_DIR)
make -C libs/libvarious install
make -C libs/libmaths install
make -C libs/libcalc install
Expand Down
2 changes: 1 addition & 1 deletion libs/libcalc/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ install: $(OBJS) $(LIBNAME) $(EXECUTABLE)
install -C $(LIBNAME) $(INSTALL_DIR)
ln -sf $(LIBNAME) $(INSTALL_DIR)/$(SONAME)
ln -sf $(SONAME) $(INSTALL_DIR)/$(LIB)
install -C calc ~/.local/bin
install -C $(EXECUTABLE) ~/.local/bin


uninstall:
Expand Down
9 changes: 0 additions & 9 deletions libs/libcalc/src/calc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <stdio.h>
#include "notacion.h"
#include "calc.h"

Expand All @@ -12,14 +11,6 @@
#endif


/* BUGS ENCONTRADOS
(-2.5)^-1 -> double free or corruption (fasttop)
Abortado (`core' generado)
verificar dos comas en el mismo número
*/

void consoleCalc()
{
char input[256];
Expand Down
2 changes: 2 additions & 0 deletions libs/libcalc/src/calc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ enum errores {E_NO, E_SINTAXIS, E_MATH};

#define DECIMAL_DIGITS 15

#include <stdio.h>

// Solves a complex mathematical expression.
// Receives an ANS value to use when necessary (to future implementation)
// and an error flag, which is ensured to be E_NO when everything is okay,
Expand Down
32 changes: 31 additions & 1 deletion libs/libcalc/src/console.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
#include "calc.h"
#include "../../libvarious/src/strings.h"
#include <stdlib.h>

int main()
static void printHelp()
{
puts("Scientific Calc v0.9");
puts("Use 'q' or 'quit' to close the program");
}

static void analyze(int argc, char *argv[])
{
int i;
for(i=1; i<argc; i++)
{
if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
{
printHelp();
}
else
{
printf("Invalid argument '%s'\n", argv[i]);
exit(1);
}
}
exit(0);
}

int main(int argc, char *argv[])
{
if(argc != 1) {
analyze(argc,argv);
}

consoleCalc();
return 0;
}
47 changes: 30 additions & 17 deletions libs/libcalc/src/notacion.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,44 @@ int checkSintax(const char* str)
int ops_consec = 0;
int parentesis = 0;
int tama;
int comas = 0;
int esNum;

tama = (int) strlen(str);
c = esOperacion(str[i]);
if(c >= 3 && c <= 5)
return 1;
else while(i < tama && parentesis >= 0)
if((c >= MULTIPLICACION && c <= POTENCIA) || c == PARENTESIS_C)
return E_SINTAXIS;

while(i < tama && parentesis >= 0)
{
if (c >= 1 && c <=5)
if (c >= SUMA && c <= POTENCIA) {
ops_consec++;
else
comas = 0;
}
else {
ops_consec = 0;
esNum = esNumero(str[i]);
if(esNum == 2) { // coma
comas++;
if(comas > 1)
return E_SINTAXIS;
}
else if(!esNum && !c)
return E_SINTAXIS;
}

if(ops_consec > 1 && (c != 1 && c != 2)) // despues de un operador, solo puede haber un + o un -
return 1;
if(ops_consec > 1 && (c != SUMA && c != RESTA))
// despues de un operador, solo puede haber un + o un -
return E_SINTAXIS;
if(ops_consec > 2)
return 1;

if ( !c && !esNumero(str[i]) ) // no es operador y ni operando
return 1;
return E_SINTAXIS;

if (c == 6) // (
if (c == PARENTESIS_A)
parentesis++;
else if (c == 7) // )
else if (c == PARENTESIS_C)
parentesis--;

i++;
c = esOperacion(str[i]);
c = esOperacion(str[++i]);
}

return (parentesis + ops_consec == 0) ? E_NO : E_SINTAXIS;
Expand Down Expand Up @@ -196,8 +207,10 @@ nodo* infijaAPostfija(const char* inf, double ans)

int esNumero(char c)
{
if ((c >= '0' && c <= '9') || c == '.' || c == ',')
return 1; // es numero (o coma)
if (c >= '0' && c <= '9')
return 1; // es numero
else if (c == '.' || c == ',')
return 2; // es coma
else
return 0; // no es numero
}
Expand Down

0 comments on commit e34f779

Please sign in to comment.