c++编程问题,初学者跪求答案
定义如下函数:
void compute(float a, float b, float & sum, float & sub, float & pro);
(1)在main函数中调用此函数,输入:两个浮点数;输出:浮点数的和、差、乘积。
(2)再根据类似的函数形式,利用引用实现,输入:两个整形数;输出:这两个数的最大公约数和最小公倍数。
C++ç¨åºï¼
#include <iostream>
using namespace std;
void compute(float a, float b, float *sum, float *sub, float *pro)
{
*sum = a + b;
*sub = a - b;
*pro = a * b;
}
void gcd_gcm(int n, int m, int *gcd, int *gcm)
{
int temp;
int n1, m1;
n1 = n;
m1 = m;
if(n < m)
{
temp = n;
n = m;
m = temp;
}
//è¾è½¬ç¸é¤æ³æ±æå¤§å ¬çº¦æ°
while(m != 0)
{
temp = n % m;
n = m;
m = temp;
}
*gcd = n;
*gcm = n1 * m1 / *gcd;
}
void main()
{
float a = 1.2;
float b = 2.5;
float sum, sub, pro;
int n = 12;
int m = 16;
int gcd;
int gcm;
compute(a, b, &sum, &sub, &pro);
cout<<"sum = "<<sum<<endl;
cout<<"sub = "<<sub<<endl;
cout<<"pro = "<<pro<<endl;
gcd_gcm(n, m, &gcd, &gcm);
cout<<"æå¤§å ¬çº¦æ° = "<<gcd<<endl;
cout<<"æå°å ¬åæ° = "<<gcm<<endl;
}
è¿è¡ææï¼
追é®é¢ããè¦æ±æ¯å¨çæçç¨åºä¸è¾å ¥æ°æ®è¿è¡è¿ç®çãæ¯ä¸æ¯æéé¢çfloat a = 1.2æ¹æint float aå°±å¯ä»¥äºï¼
æå»äºï¼å·²ç»å¼æç½äºï¼è°¢è°¢
追çè®°å¾é纳å
追é®æ±æï¼æäºä¸ç¹ï¼å·²é纳
using namespace std;
void compute(float a, float b, float &sum, float &sub, float &pro);
void commonnum(int a, int b, int &large, int &least);
int main() {
float a = 1.125f;
float b = 8.725f;
float sum, sub, pro;
compute(a, b, sum, sub, pro);
printf("sum=%.3f, sub=%.3f, pro=%.3f\n", sum, sub, pro);
int m=27, n=15, la, le;
commonnum(m, n, la, le);
printf("最大公约数:%d, 最小公倍数:%d\n", la, le);
return 0;
}
void compute(float a, float b, float &sum, float &sub, float &pro) {
sum = a+b;
sub = a-b;
pro = a*b;
}
void commonnum(int m, int n, int &large, int &least) {
int a=m, b=n;
while (a!=b) {
if (a>b) a=a-b;
else b=b-a;
}
large=a;
least=m*n/a;
}