Skip to content

Commit e34f779

Browse files
committed
bugfix: multiple dots in a number now detected
1 parent 1d79bec commit e34f779

File tree

6 files changed

+71
-29
lines changed

6 files changed

+71
-29
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ obj/mainWindow.o: src/mainWindow.c src/mainWindow.h
3939
obj:
4040
mkdir $@
4141

42-
install: $(OBJS) $(EXECUTABLE)
42+
$(INSTALL_DIR):
43+
mkdir $@
44+
45+
$(LIBS_DIR):
46+
mkdir $@
47+
48+
install: $(OBJS) $(EXECUTABLE) $(INSTALL_DIR) $(LIBS_DIR)
4349
make -C libs/libvarious install
4450
make -C libs/libmaths install
4551
make -C libs/libcalc install

libs/libcalc/makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ install: $(OBJS) $(LIBNAME) $(EXECUTABLE)
5252
install -C $(LIBNAME) $(INSTALL_DIR)
5353
ln -sf $(LIBNAME) $(INSTALL_DIR)/$(SONAME)
5454
ln -sf $(SONAME) $(INSTALL_DIR)/$(LIB)
55-
install -C calc ~/.local/bin
55+
install -C $(EXECUTABLE) ~/.local/bin
5656

5757

5858
uninstall:

libs/libcalc/src/calc.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <stdio.h>
21
#include "notacion.h"
32
#include "calc.h"
43

@@ -12,14 +11,6 @@
1211
#endif
1312

1413

15-
/* BUGS ENCONTRADOS
16-
17-
(-2.5)^-1 -> double free or corruption (fasttop)
18-
Abortado (`core' generado)
19-
20-
verificar dos comas en el mismo número
21-
*/
22-
2314
void consoleCalc()
2415
{
2516
char input[256];

libs/libcalc/src/calc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ enum errores {E_NO, E_SINTAXIS, E_MATH};
88

99
#define DECIMAL_DIGITS 15
1010

11+
#include <stdio.h>
12+
1113
// Solves a complex mathematical expression.
1214
// Receives an ANS value to use when necessary (to future implementation)
1315
// and an error flag, which is ensured to be E_NO when everything is okay,

libs/libcalc/src/console.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
#include "calc.h"
2+
#include "../../libvarious/src/strings.h"
3+
#include <stdlib.h>
24

3-
int main()
5+
static void printHelp()
46
{
7+
puts("Scientific Calc v0.9");
8+
puts("Use 'q' or 'quit' to close the program");
9+
}
10+
11+
static void analyze(int argc, char *argv[])
12+
{
13+
int i;
14+
for(i=1; i<argc; i++)
15+
{
16+
if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
17+
{
18+
printHelp();
19+
}
20+
else
21+
{
22+
printf("Invalid argument '%s'\n", argv[i]);
23+
exit(1);
24+
}
25+
}
26+
exit(0);
27+
}
28+
29+
int main(int argc, char *argv[])
30+
{
31+
if(argc != 1) {
32+
analyze(argc,argv);
33+
}
34+
535
consoleCalc();
636
return 0;
737
}

libs/libcalc/src/notacion.c

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,44 @@ int checkSintax(const char* str)
88
int ops_consec = 0;
99
int parentesis = 0;
1010
int tama;
11+
int comas = 0;
12+
int esNum;
1113

1214
tama = (int) strlen(str);
1315
c = esOperacion(str[i]);
14-
if(c >= 3 && c <= 5)
15-
return 1;
16-
else while(i < tama && parentesis >= 0)
16+
if((c >= MULTIPLICACION && c <= POTENCIA) || c == PARENTESIS_C)
17+
return E_SINTAXIS;
18+
19+
while(i < tama && parentesis >= 0)
1720
{
18-
if (c >= 1 && c <=5)
21+
if (c >= SUMA && c <= POTENCIA) {
1922
ops_consec++;
20-
else
23+
comas = 0;
24+
}
25+
else {
2126
ops_consec = 0;
27+
esNum = esNumero(str[i]);
28+
if(esNum == 2) { // coma
29+
comas++;
30+
if(comas > 1)
31+
return E_SINTAXIS;
32+
}
33+
else if(!esNum && !c)
34+
return E_SINTAXIS;
35+
}
2236

23-
if(ops_consec > 1 && (c != 1 && c != 2)) // despues de un operador, solo puede haber un + o un -
24-
return 1;
37+
if(ops_consec > 1 && (c != SUMA && c != RESTA))
38+
// despues de un operador, solo puede haber un + o un -
39+
return E_SINTAXIS;
2540
if(ops_consec > 2)
26-
return 1;
27-
28-
if ( !c && !esNumero(str[i]) ) // no es operador y ni operando
29-
return 1;
41+
return E_SINTAXIS;
3042

31-
if (c == 6) // (
43+
if (c == PARENTESIS_A)
3244
parentesis++;
33-
else if (c == 7) // )
45+
else if (c == PARENTESIS_C)
3446
parentesis--;
3547

36-
i++;
37-
c = esOperacion(str[i]);
48+
c = esOperacion(str[++i]);
3849
}
3950

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

197208
int esNumero(char c)
198209
{
199-
if ((c >= '0' && c <= '9') || c == '.' || c == ',')
200-
return 1; // es numero (o coma)
210+
if (c >= '0' && c <= '9')
211+
return 1; // es numero
212+
else if (c == '.' || c == ',')
213+
return 2; // es coma
201214
else
202215
return 0; // no es numero
203216
}

0 commit comments

Comments
 (0)