function (programming C++)?

2009-08-03 2:59 pm
Write a simple program that can perform arithmetic operations (+, -, * and \) of two integers and display the result of each operation. Ask user to select which arithmetic operration to perform. Each arithmetic operation has to be performed in a function.


here is my answer:

#include<stdio.h>
#include<conio.h>
#include<math.h>
float calculate(float x,float y);
int main()
{
float x,y;
char operation;
printf("please key in value of x and y: \n");
scanf("%f%f",&x,&y);
printf("please enter the operation you want to perform (+, -, *, /):\n");
scanf("%s",&operation);
calculate(x,y);
getch();
return 0;
}
float calculate(float x,float y)
{
if (char operation = '+')
{
float add=x+y;
printf("the sum is %f+%f=%f\n",x,y,add);
}
else if (char operation = '-')
{
float subt=x-y;
printf("the subtraction is %f-%f=%f\n",x,y,subt);
}
else if (char operation = '*')
{
float mult=x*y;
printf("the multiplication is %f*%f=%f\n",x,y,mult);
}
else if (char operation = '/')
{
float div=x/y;
printf("the division is %f/%f=%f\n",x,y,div);
}
return 0;
}


but yet.. i still can't get what i want... =(
Anyone can help me???

回答 (3)

2009-08-03 3:22 pm
✔ 最佳答案
There are a number of things you need to correct:
1) the variable char operation have been declared inside the main function, so it cannot be accessed from any other function. The scope of a local variable is limited to the function in which it is declared. So you cannot access the operation variable from the calculate function.
soln:
There are three solution.
a) Make the char operation a global variable by declaring the variable outside any function, (just after the #include statements)
b) Pass the operation variable as a parameter to the function calculate, so the prototype of function becomes:
float calculate (float x, float y, char operation);
c) Have separate functions each for addition, subtraction, division and multiplication and call the appropriate function from main() based on the operation input.
switch (operation) {
case '+':
result = addition (x,y);
break;
case '-':
result = subtraction (x,y);
break;
... ... ... and so on. You can use if-else instead of switch-case also.

2) in the function calculate, the statement
if (char operation = '+')
has two problems:
the operator '=' s assignment operator, what you need here is comparator, i.e '=='.
and the keyword char is not required inside the if condition.
so it should be:
if (operation == '+'),
Same for other if statements in the function

3) scanf("%s",&operation);
%s is a format specifier for string, use %c for a character
scanf("%c",&operation);

4) in
else if (operation == '/')
{
float div=x/y;
printf("the division is %f/%f=%f\n",x,y,div);
}
In a division operation It is always better to check if the divisor is not 0.
i.e y should not be 0, or your program will crash.

else if (char operation = '/')
{
if (y==0)
{
printf("ERROR: Division by Zero\n");
exit (1);
}
float div=x/y;
printf("the division is %f/%f=%f\n",x,y,div);
}
2009-08-03 3:12 pm
#include<stdio.h>
#include<conio.h>
#include<math.h>
float calculate(float x,float y, char operation); /*operation parameter*/
int main()
{
float x,y;
char operation;
printf("please key in value of x and y: \n");
scanf("%f%f",&x,&y);
printf("please enter the operation you want to perform (+, -, *, /):\n");
scanf("%s",&operation);
calculate(x,y,operation); /*operation parameter*/
getch();
return 0;
}
float calculate(float x,float y, char operation) /*you must include operation as a parameter*/
{
if (operation == '+') /* "==" not "=" because you are comparing*/
{
float add=x+y;
printf("the sum is %f+%f=%f\n",x,y,add);
}
else if (operation == '-')
{
float subt=x-y;
printf("the subtraction is %f-%f=%f\n",x,y,subt);
}
else if (operation == '*')
{
float mult=x*y;
printf("the multiplication is %f*%f=%f\n",x,y,mult);
}
else if (operation == '/')
{
float div=x/y;
printf("the division is %f/%f=%f\n",x,y,div);
}
return 0;
}
2009-08-03 3:19 pm
I didn't test your code but i think this fix some problems :
2 problems i can see :
the first one you have to pass the operator as a parameter to the calculate function :
float calculate(float x,float y, char operation)
and use it to do the test

the second problem :
you used the assign symbol '=' and you should use the '=='

so the test would be : if (operation == '+') ....
.....
else if (operation == '-')
....
else if (operation == '*')
....
else if (operation == '/')
...


收錄日期: 2021-04-25 13:43:13
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090803065929AAOWIQA

檢視 Wayback Machine 備份