1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <stdio.h> // C standard input & output library
int main() // declare the main function
{ // start the main function
float num1, num2; // declare the 2 numbers to handle for our calculation
char op, sum[256]; // declare the operator & whole input string
gets(sum); // get the user input
sscanf(sum, "%f %c %f", &num1, &op, &num2); // scan the input for a number, a character, then a number
if(op == '+') // if the operator is + (addition)
{
printf("%f + %f = %f", num1, num2, num1 + num2); // do the sum & output it
}
else if(op == '-') // or if the operator is - (subtraction)
{
printf("%f - %f = %f", num1, num2, num1 - num2); // do the sum & output it
}
else if(op == '*') // or if the operator is * (multiplication)
{
printf("%f * %f = %f", num1, num2, num1 * num2); // do the sum & output it
}
else if(op == '/') // or if the operator is / (division)
{
printf("%f / %f = %f", num1, num2, num1 / num2); // do the sum & output it
}
else // if none of the above
{
printf("There was an error calculating your request"); // tell the user there was an error
}
getchar(); // wait for the user to press enter before the console closes
return 0; // return 0 to the main function
} // end the main function
|