作业帮 > 综合 > 作业

c语言:设一个函数,调用它时,每次实现不同的功能:(1)求两个数之和;(2)求两个数之差;(3)求

来源:学生作业帮 编辑:搜狗做题网作业帮 分类:综合作业 时间:2024/05/13 13:15:18
c语言:设一个函数,调用它时,每次实现不同的功能:(1)求两个数之和;(2)求两个数之差;(3)求
实验步骤与要求:
(1)在主函数中输入2个数a,b,并输出a,b的和、差和乘积.
(2)分别编写函数add()、sub()、mul()计算两个数的和、差、积.
(3) 编写函数process(),分别调用函数add()、sub()、mul().
c语言:设一个函数,调用它时,每次实现不同的功能:(1)求两个数之和;(2)求两个数之差;(3)求
#include
float add( float x, float y );
float sub( float x, float y );
float mul( float x, float y );
float process( float x, float y, char mode );
void clear();
int main( )
{
do{
printf( " Enter mode[+、- or * , 0 to exit ]: " );
char mode;
scanf( " %c", &mode );
if( '0' == mode ) break;
clear();
printf( " Enter x and y: " );
float x , y;
scanf( " %f %f", &x, &y );
printf( " %f %c %f = %f\n\n", x, mode, y, process( x, y, mode ) );
clear();
}while( 1 );
return 0;
}
float add( float x, float y )
{
return x + y;
}
float sub( float x, float y )
{
return x - y;
}
float mul( float x, float y )
{
return x * y;
}
float process( float x, float y, char mode )
{
switch mode :
case '+' : return add( x, y );
case '-' : return sub( x, y );
case '*' : return mul( x, y );
default : return -1;
}
void clear()
{
while( getchar() != '\n' );
}