I am trying to use strtok
to break a simple equation like 5 + 4
down and store each part into an array, and once I have done this, perform the operation indicated.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//#include "stdafx.h"
int main() {
char uin[10], op[10], error = 'e';
char *token;
const char s[2] = " ";
double num1, num2, fNum1, res;
int i = 0;
int x, y, z, a, b, c, op1;
printf("Welcome to the calculator, please enter an equation\n");
while (error == 'e') { // & assigns address and * gives access to what it points to
a = &uin[0];
b = &uin[2];
c = &uin[4];
gets(uin);
rewind(stdin);
printf("Is this what you entered? %s\n", uin);
token = strtok(uin, s);
//x = &token;
//printf("The element in the array currently assigned to token is: %s\n", token);
while (token != NULL) {
if (isdigit(token[0])) {
num1 = atof(token);
printf("token is now: %1.2f\n", num1);
} else
strcpy(op, token);
token = strtok(NULL, s);
if (isdigit(token[0])) {
num1 = atof(token);
} else
strcpy(op, token);
//token = strtok(NULL, s);
//y = &token;
//printf("The element in the array currently assigned to token is: %s\n", token);
}
//token = strtok(NULL, s);
//y = &token;
//printf("The element in the array currently assigned to token is: %s\n", token);
//token = strtok(NULL, s);
//z = &token;
//printf("The element in the array currently assigned to token is: %s\n", token);
}
system("pause");
}
I am really having a hard time with this. I think that I am using strtok
correctly to take the first part of the gets(uin)
and storing it, but I don't understand how to take the middle part (+
-
*
or /
) and store it.