分析下列程序,并显示结果:#include "iostream.h" class CTest {public: int x,y; CTest ( int a=1,int b=1 ) :x(a), y(b) { } int fun1() { int k; if ( x<y ) { k=x; x=y; y=k; } if( x%y ==0 ) return y; else { k=x%y; x=y; y=k; cout<<k<<endl; return fun1(); } } int fun2( ) { return ( x * y ) / fun1(); } }; void main() { CTest t1(35,15); cout<<t1.fun1()<<endl; CTest t2(12,32); cout<<t2.fun2()<<endl; }
时间: 2023-06-18 11:07:50 浏览: 45
这是一个 C++ 程序,功能是定义了一个类 CTest,其中包含两个成员变量 x 和 y,以及两个成员函数 fun1 和 fun2。
在主函数 main 中,创建了两个 CTest 类的对象 t1 和 t2,并分别调用了 fun1 和 fun2 函数。
fun1 函数的作用是求 x 和 y 的最大公约数。如果 x < y,则交换 x 和 y。然后判断 x 和 y 是否互质,若是则返回 y,否则将 x 取模 y 的结果赋值给 k,然后将 y 赋值为 k,x 赋值为原来的 y,再递归调用 fun1 函数,直到 x 和 y 互质为止。在递归过程中,每次求出的 k 值都会输出到屏幕上。
fun2 函数的作用是求 x 和 y 的最小公倍数,即 x 和 y 的乘积除以它们的最大公约数。
程序运行结果为:
10
96
其中,第一行输出的是 t1.fun1() 的返回值,即 10;第二行输出的是 t2.fun2() 的返回值,即 96。
相关问题
补全程序,并写出程序运行结果。 #include <iostream> using namespace std; class Class { public : Class ( int...
补全程序如下:
```
#include <iostream>
using namespace std;
class Class {
public:
Class(int a, int b, int c) {
x = a;
y = b;
z = c;
}
void print() {
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "z: " << z << endl;
}
private:
int x;
int y;
int z;
};
int main() {
Class obj(1, 2, 3);
obj.print();
return 0;
}
```
程序运行结果为:
```
x: 1
y: 2
z: 3
```
程序定义了一个名为 Class 的类,该类包括有参构造函数和一个 print() 函数,构造函数用于对类的私有成员变量进行初始化,print() 函数用于输出类的私有成员变量。在主函数 main() 中,创建了一个名为 obj 的 Class 类对象,构造函数传入三个整数参数 1、2 和 3。最后调用 obj 对象的 print() 函数,输出 obj 对象的私有成员变量 x、y 和 z 的值。
逐句分析下列程序:#include "iostream.h" class CTest {public: int x,y; CTest ( int a=1,int b=1 ) :x(a), y(b) { } int fun1() { int k; if ( x<y ) { k=x; x=y; y=k; } if( x%y ==0 ) return y; else { k=x%y; x=y; y=k; cout<<k<<endl; return fun1(); } } int fun2( ) { return ( x * y ) / fun1(); } }; void main() { CTest t1(35,15); cout<<t1.fun1()<<endl; CTest t2(12,32); cout<<t2.fun2()<<endl; }
这是一个 C++ 程序,它包含了一个名为 CTest 的类和一个 main 函数。下面逐句分析:
```
#include "iostream.h"
```
这是一个预处理指令,将 iostream.h 头文件包含进来,以便在程序中使用输入输出流。
```
class CTest {
public:
int x, y;
CTest(int a = 1, int b = 1) : x(a), y(b) { }
int fun1()
{
int k;
if (x < y)
{
k = x;
x = y;
y = k;
}
if (x % y == 0) return y;
else
{
k = x % y;
x = y;
y = k;
cout << k << endl;
return fun1();
}
}
int fun2()
{
return (x * y) / fun1();
}
};
```
这是定义 CTest 类的部分。CTest 类包含有两个成员变量 x 和 y,在构造函数中可以指定其默认值为 1。CTest 类还包含有两个成员函数 fun1 和 fun2。fun1 函数是递归函数,用于计算 x 和 y 的最大公约数,它返回 y 的值,同时在每次递归调用时输出 k 的值。fun2 函数用于计算 x 和 y 的最小公倍数,它调用了 fun1 函数。
```
void main()
{
CTest t1(35, 15);
cout << t1.fun1() << endl;
CTest t2(12, 32);
cout << t2.fun2() << endl;
}
```
这是主函数的部分,它创建了两个 CTest 类的对象 t1 和 t2,并分别调用了它们的 fun1 和 fun2 函数,将结果输出到标准输出流。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)