
c语言:设一个函数,调用它时,每次实现不同的功能:(1)求两个数之和;(2)求两个数之差;(3)求
实验步骤与要求:(1)在主函数中输入2个数a,b,并输出a,b的和、差和乘积。(2)分别编写函数add()、sub()、mul()计算两个数的和、差、积。(3)编写函数p...
实验步骤与要求:
(1)在主函数中输入2个数a,b,并输出a,b的和、差和乘积。
(2)分别编写函数add()、sub()、mul()计算两个数的和、差、积。
(3) 编写函数process(),分别调用函数add()、sub()、mul()。 展开
(1)在主函数中输入2个数a,b,并输出a,b的和、差和乘积。
(2)分别编写函数add()、sub()、mul()计算两个数的和、差、积。
(3) 编写函数process(),分别调用函数add()、sub()、mul()。 展开
4个回答
展开全部
#include <stdio.h>
void add(int a, int b)
{
int c = a + b;
printf(" %d + %d = %d\n", a,b,c);
}
void sub(int a, int b)
{
int c = a - b;
printf(" %d - %d = %d\n", a,b,c);
}
void mul(int a, int b)
{
int c = a * b;
printf(" %d * %d = %d\n", a,b,c);
}
void process(int a, int b)
{
add(a, b);
sub(a, b);
mul(a, b);
}
int main()
{
int a, b;
printf("请输入两个数: ");
scanf("%d%d", &a, &b);
printf("他们的和是: %d\n", a + b);
printf("他们的差是: %d\n", a - b);
printf("他们的积是: %d\n", a * b);
process(a,b);
}
void add(int a, int b)
{
int c = a + b;
printf(" %d + %d = %d\n", a,b,c);
}
void sub(int a, int b)
{
int c = a - b;
printf(" %d - %d = %d\n", a,b,c);
}
void mul(int a, int b)
{
int c = a * b;
printf(" %d * %d = %d\n", a,b,c);
}
void process(int a, int b)
{
add(a, b);
sub(a, b);
mul(a, b);
}
int main()
{
int a, b;
printf("请输入两个数: ");
scanf("%d%d", &a, &b);
printf("他们的和是: %d\n", a + b);
printf("他们的差是: %d\n", a - b);
printf("他们的积是: %d\n", a * b);
process(a,b);
}
展开全部
代码如下:
#include <iostream>
using namespace std;
// 加法的模板函数
template <typename T>
T Add(T a, T b)
{
return (a + b);
}
// 测试函数
int main()
{
// 显式
cout<<"显式调用:"<<endl;
int ia = 1, ib = 2, ic = 0;
ic = Add<int>(ia, ib);
cout<<ia<<" + "<<ib<<" = "<<ic<<endl;
// 隐式
cout<<"隐式调用:"<<endl;
float fa = 1.1f, fb = 2.2f, fc = 0.0f;
fc = Add(fa, fb);
cout<<fa<<" + "<<fb<<" = "<<fc<<endl;
cout<<endl;
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
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' );
}
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' );
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你只要在主函数处设置一个计数变量记录调用次数。根据次数决定在process函数调用哪个功能函数。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询