Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
double evalFloat();
double evalInt();
int fact(int);
int perm(int);
char doQuit(char);
void evalInput();
int main()
{
evalInput();
return 0;
}
void evalInput()
{
char userInput[2048];
char optionA;
char operA, idx, decimalCount;
double opersA, opersB, answer;
int varA, varN, varK;
int result;
decimalCount = 0;
printf("Enter one of the following options: \n\
Q - Quit the program\n\
Eval(<exp>) - Evaluate a simple expression\n\
! - Evaluate an integer factorial\n\
P(n,k) - Count k-permutations of a string of n characters\n\
\n\ Enter your option: ");
fgets(userInput,2048,stdin);
for(idx = 0; idx <= strlen(userInput); idx++){
if(userInput[idx] == '.'){
decimalCount++;
}
}
if((strstr(userInput,"Q") != NULL) || (strstr(userInput,"q") != NULL)) {
optionA = doQuit(optionA);
} else if((strstr(userInput,"Eval") != NULL) ||
(strstr(userInput,"eval") != NULL) ||
(strstr(userInput,"EVAL") != NULL)) {
if((decimalCount == 1) || (decimalCount == 2)) {
sscanf(userInput,"%*c%*c%*c%*c%*c%lf %c %lf%*c",
&opersA, &operA, &opersB);
printf("%.3lf %c %.3lf = %.3lf\n",
evalFloat(opersA), evalFloat(operA),
evalFloat(opersB), evalFloat(answer));
} else if(decimalCount == 0) {
sscanf(userInput,"%*c%*c%*c%*c%*c%lf %c %lf%*c",
&opersA, &operA, &opersB);
printf("%.0lf %c %.0lf = %.0lf\n",
evalInt(opersA), evalInt(operA),
evalInt(opersB), evalInt(answer));
}
} else if(strstr(userInput,"!") != NULL) {
sscanf(userInput,"%*c%d%*c", &varN);
printf("%d! = %d\n",varN, fact(varN));;
} else if((strstr(userInput,"P") != NULL) ||
(strstr(userInput,"p") != NULL)) {
sscanf(userInput,"%*c%*c%d%*c%d%*c", &varN, &varK);
varA = fact(varN) / fact(varN - varK);
printf("A string of %d characters has %d permutations of length %d\n",
varN, varA, varK);
}
evalInput();
return;
}
char doQuit(char optionA)
{
exit(0);
}
double evalFloat(double answer, double opersA, double opersB)
{
char operA;
double index;
switch(operA) {
case '+':
answer = opersA + opersB;
//printf("%.2lf %c %.2lf = %.2lf\n",
// opers[0], operA, opers[1], answer);
break;
case '-':
answer = opersA - opersB;
//printf("%.2lf %c %.2lf = %.2lf\n",
// opers[0], operA, opers[1], answer);
break;
case '/':
answer = (float)opersA / (float)opersB;
//printf("%.2lf %c %.2lf = %.2lf\n",
// opers[0], operA, opers[1], answer);
break;
case '*':
answer = opersA * opersB;
//printf("%.2lf %c %.2lf = %.2lf\n",
// opers[0], operA, opers[1], answer);
break;
case '%':
answer = (int)opersA % (int)opersB;
//printf("%.2lf %c %.2lf = %.2lf\n",
// opers[0], operA, opers[1], answer);
break;
case '^':
for(index = 1; index <= opersB; index++)
answer = answer * opersA;
//printf("%.2lf %c %.2lf = %.2lf\n",
// opers[0], operA, opers[1], answer);
break;
default:
answer = 0;
// printf("?'%c' is not a valid operator", operA);
}
return answer;
}
double evalInt(double answer, double opersA, double opersB)
{
char operA;
int idx;
double index;
switch(operA) {
case '+':
answer = opersA + opersB;
//printf("%.0lf %c %.0lf = %.0lf\n",
// opers[0], operA, opers[1], answer);
break;
case '-':
answer = opersA - opersB;
//printf("%.0lf %c %.0lf = %.0lf\n",
// opers[0], operA, opers[1], answer);
break;
case '/':
answer = opersA / opersB;
//printf("%.0lf %c %.0lf = %.0lf\n",
// opers[0], operA, opers[1], answer);
break;
case '*':
answer = opersA * opersB;
//printf("%.0lf %c %.0lf = %.0lf\n",
// opers[0], operA, opers[1], answer);
break;
case '%':
answer = (int)opersA % (int)opersB;
//printf("%.0lf %c %.0lf = %.0lf\n",
// opers[0], operA, opers[1], answer);
break;
case '^':
for(index = 1; index <= opersB; index++)
answer = answer * opersA;
//printf("%.0lf %c %.0lf = %.0lf\n",
// opers[0], operA, opers[1], answer);
break;
default:
answer = 0;
// printf("?'%c' is not a valid operator", operA);
}
return answer;
}