Here's the code:
Code: Select all
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
float calculate(float number1, char thingy, float number2) {
float ans;
switch (thingy) {
case '+':
ans = number1+number2;
break;
case '-':
ans = number1-number2;
break;
case '*':
ans = number1*number2;
break;
case '/':
ans = number1/number2;
break;
case '#':
ans = pow(number1, 1/(float)number2);
break;
case '^':
ans = pow(number1,number2);
break;
default:
break;
}
return ans;
}
int main()
{
int number1;
int number2;
float ans;
char thingy;
printf ("Enter the first number: ");
scanf("%d", &number1);
fflush(stdin);
printf ("Enter the operation to perform: +, -, *, / or # : ");
scanf("%c", &thingy);
fflush(stdin);
printf("Enter the second number: ");
scanf("%d", &number2);
ans = calculate(number1,thingy,number2);
FILE* fp;
fp = fopen("calc.txt", "a+");
fopen;
printf("%d%c%d=%f\n", number1, thingy, number2, ans);
fprintf(fp, "%d%c%d=%f\n", number1, thingy, number2, ans);
system("pause");
}
-Kon-Tiki-