Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void help(void);
void scan_data(char sign, float num);
void do_next_op(char sign, float num);
int main()
{
int x, y, sum;
char op, temp;
scan_data('p',9);
return 0;
}
void help(void)
{
char op[3] ;
char text[9];
FILE *help;
help = fopen("operator.txt","w");
fprintf(help,"%s\t%s\n", "+" , "add");
fprintf(help,"%s\t%s\n", "-" , "subtract");
fprintf(help,"%s\t%s\n", "*" , "multiply");
fprintf(help,"%s\t%s\n", "/" , "divide");
fprintf(help,"%s\t%s\n", "^" , "power");
fprintf(help,"%s\t%s", "q" , "quit");
fclose(help);
help = fopen("operator.txt", "r");
while(!feof(help))
{
fscanf(help, "%s\t%s", &op, &text);
printf("%s\t%s\n", op, text);
}
fclose(help);
}
void scan_data(char sign, float num)
{
char op, temp;
float num1;
scanf("%c", &temp);
printf("Enter an operator [^,*,/,+,-, q(quit), h(help)]:\n");
scanf("%c", &op);
if(op=='q'||op=='Q')
{
exit(0);
}
if(op=='h'||op=='H')
{
help();
}
printf("Enter a number to calculate for: ");
scanf("%f", &num1);
printf("%c%1.2f\n",op ,num1);
do_next_op(op, num1);
}
void do_next_op(char sign, float num)
{
float sum = 0;
switch(sign)
{
case('^'):
{
sum=pow(sum,num);
printf("The result so far is %1.2f\n", sum);
scan_data('p',9);
}
case('*'):
{
sum=sum*num;
printf("The result so far is %1.2f\n", sum);
scan_data('p',9);
}
case('/'):
{
if(sign=='/')
{
if(num<=0)
{
printf("Can not divide by 0\n");
scan_data('p',9);
}
if(num>0)
{
sum=sum/num;
printf("The result so far is %1.2f\n", sum);
scan_data('p',9);
}
}
}
case('+'):
{
sum=sum+num;
printf("The result so far is %1.2f\n", sum);
scan_data('p',9);
}
case('-'):
{
sum=sum-num;
printf("The result so far is %1.2f\n", sum);
scan_data('p',9);
}
default:
break;
}
}
Sorry if its a bit messy or confusing in certain places, I like to fix these things up after my test runs work.