I am trying to read in an equation, then take each part separately, however then user can enter as big or small equation as they like (for example. 3+7, 2+-9+8 or even 2). I have this however it doesn't seem to be working.
printf("Please enter an equation\n");
scanf("%f", &num);
//printf("%f", num);
while ( num != '\n'){
scanf("%f", &num);
scanf("%c", &op);
//printf("%c %f \n", op, num);
}
when i output what i have got it is not the same as the input.
You may wish to read How to read a line from the console in C? for the full details, but basically you just do this:
char * getline(void) {
char * line = malloc(100), * linep = line;
size_t lenmax = 100, len = lenmax;
int c;
if(line == NULL)
return NULL;
for(;;) {
c = fgetc(stdin);
if(c == EOF)
break;
if(--len == 0) {
len = lenmax;
char * linen = realloc(linep, lenmax *= 2);
if(linen == NULL) {
free(linep);
return NULL;
}
line = linen + (line - linep);
linep = linen;
}
if((*line++ = c) == '\n')
break;
}
*line = '\0';
return linep;
}
You are trying to take the complete expression in a float variable (num, in your code). If you do a scanf("%f", &num);
in while loop then you are just overwriting the values in num. You need to take the expression in a char array or char*. Then you need to have an algrithm to seperate the operators and numbers, convert the numbers to desired type and solve the euation.
If you want to read an expression and have your program understand that, you need severely heavier machinery. Either this is an XY problem, i.e., you need to rethink the problem and find another approach; or you should look into the whole parsing/compiling area.
Scripting languages (like Python or Perl) have some sort of eval
builtin, so you can take a snippet of code as text and get it evaluated (run). Perhaps using one of those is a better match to your problem? But take care, blindly running anything the user inputs is a huge risk...
To read an arbitrary-length line with scanf, you can use
scanf("%[^\n]", equation);
This regular expression means "read everything until you find the '\n' character".
Keep in mind that this is not secure though, since the user can easily overflow the "equation" buffer. If you want to avoid that, I would suggest reading char by char in a loop, like so:
for(i=0; i < MAX_EQ_SIZE; i++)
{
char tmp;
scanf("%c", &tmp);
if(tmp == '\n')
break;
equation[i] = tmp;
}
Since you are asking just how to read, I'm not going into parsing the read equation.
Here is a code example:
#include <stdio.h>
#define MAX_EQ_SIZE 1024
void parse(char * eq)
{
// Do the processing
}
int main()
{
char equation[MAX_EQ_SIZE];
scanf("%[^\n]", equation); // Read a whole line
scanf("%*c"); // Read and ignore the \n
puts(equation);
parse(equation);
}
Use fgets()
to read the line of input.
Use the return value from sscanf()
to determine if number or operator.
int Eval(void) {
char buffer[100];
char *p = buffer;
printf("Please enter an equation\n");
if (fgets(buffer, sizeof buffer, stdin) == NULL)
return -1; // no more input
float num;
char op;
int n;
while (*p) {
if (sscanf(p, "%f %n", &num, &n) == 1) {
printf("num %f\n", num);
p += n;
}
if (sscanf(p, " %c %n", &op, &n) == 1) {
printf("op %c\n", op);
p += n;
} else if (*p) {
printf("Error '%s'\n", p);
return 0;
}
}
return 1;
}