Update cv3/program.c
This commit is contained in:
parent
e19a7ed823
commit
50804be2f7
@ -63,12 +63,17 @@ int read(struct stack *s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
float value;
|
float value;
|
||||||
int scan = sscanf(temp, "%f", &value);
|
char extra;
|
||||||
|
int scan = sscanf(temp, "%f %c", &value, &extra);
|
||||||
|
|
||||||
// If it's a number
|
// If it's a properly formatted number
|
||||||
if (scan == 1) {
|
if (scan == 1) {
|
||||||
push_stack(s, value);
|
push_stack(s, value);
|
||||||
print_stack(s);
|
print_stack(s);
|
||||||
|
} else if (scan > 1) {
|
||||||
|
// If extra characters were found, it's an invalid input like .23.4
|
||||||
|
printf("bad input\n");
|
||||||
|
exit(0);
|
||||||
} else {
|
} else {
|
||||||
switch (temp[0]) {
|
switch (temp[0]) {
|
||||||
case '+':
|
case '+':
|
||||||
@ -102,13 +107,14 @@ int read(struct stack *s) {
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
printf("bad input\n");
|
printf("bad input\n");
|
||||||
exit(0); // Continue on invalid input
|
exit(0); // Exit on invalid input
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1; // Continue the loop
|
return 1; // Continue the loop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
struct stack my_stack;
|
struct stack my_stack;
|
||||||
init(&my_stack);
|
init(&my_stack);
|
||||||
|
Loading…
Reference in New Issue
Block a user