Skip to content

Commit 62049d6

Browse files
committed
上传最近的作业
1 parent 57fe0c0 commit 62049d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+3358
-8
lines changed
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
# 3-7
1+
# 2024_10_28
2+
3+
## 3-7
4+
25
完成函数,参数为两个 unsigned short int型数,返回值为第一个参数除以第二个参数的结果,数据类型为 short int;如果第二个参数为0,则返回值为一1。在主程序中实现输入输出。
36

4-
# 3-13
7+
## 3-13
8+
59
递归的方法编写函数求 Fibonacci级数,公式为
610
$$ F_n =F_{n-1} +F_{n-2},(n>2),F_1=F_2=1 $$
711
观察递归调用的过程。
812

9-
# 3-14
13+
## 3-14
14+
1015
用递归的方法编写函数求n阶勒让德多项式的值,在主程序中实现输入输出。递归公式为
1116
$$ P_n(x)=\left\{\begin{array}{l}1 , n=0 \\
1217
x , n=1 \\
1318
[(2n-1)xP_{n-1}(x)-(n-1)P_{n-2}(x)]/n , n>1\end{array}\right. $$
1419

20+
## 3-15
1521

16-
# 3-15
1722
写递归函数 getPower 计算$x^y$,在同一个程序中针对整型和实型实现两个重载的
1823
函数:
24+
1925
```c++
2026
int getPower(int x, int y); //整型版本,当y<0时,返回0
2127
double getPower(double x,int y); //实型版本
2228
```
23-
在主程序中实现输入输出,分别输入一个整数a 和一个实数b作为底数,再输入一个
24-
整数m作为指数,输出$a^m$和$b^m$。另外请读者思考,如果在调用getPower 函数计算
25-
$a^m$时希望得到一个实型结果(实型结果表示范围更大,而且可以准确表示m<0时
26-
的结果),该如何调用?
29+
30+
在主程序中实现输入输出,分别输入一个整数a 和一个实数b作为底数,再输入一个整数m作为指数,输出$a^m$和$b^m$。另外请读者思考,如果在调用getPower 函数计算$a^m$时希望得到一个实型结果(实型结果表示范围更大,而且可以准确表示m<0时的结果),该如何调用?
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "5-13.h"
2+
3+
int X::get(){
4+
return i;
5+
}
6+
7+
void Y::g(X *x) {
8+
x->i++;
9+
}
10+
11+
void Z::f(X *x) {
12+
x->i += 5;
13+
}
14+
15+
void h(X *x) {
16+
x->i += 10;
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include "5-13.h"
3+
#include <windows.h>
4+
5+
int main() {
6+
X x;
7+
Y y;
8+
Z z;
9+
SetConsoleOutputCP(CP_UTF8);
10+
11+
std::cout << "初始值: " << x.get() << std::endl;
12+
13+
y.g(&x);
14+
std::cout << "调用Y的g函数后: " << x.get() << std::endl;
15+
16+
z.f(&x);
17+
std::cout << "调用Z的f函数后: " << x.get() << std::endl;
18+
19+
h(&x);
20+
std::cout << "调用h函数后: " << x.get() << std::endl;
21+
22+
return 0;
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class X;
2+
3+
class Y {
4+
public:
5+
void g(X *x);
6+
};
7+
8+
class Z {
9+
public:
10+
void f(X *x);
11+
};
12+
13+
14+
void h(X *x);
15+
16+
class X {
17+
private:
18+
int i = 0;
19+
friend void Y::g(X *x);
20+
friend void Z::f(X *x);
21+
friend void h(X *);
22+
public:
23+
int get();
24+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int x = 5, y = 7;
5+
6+
void myFunction()
7+
{
8+
int y = 10;
9+
cout <<"x from myFunction" << x <<"\n";
10+
cout <<"y from myFunction" << y <<"\n\n";
11+
}
12+
13+
int main()
14+
{
15+
cout <<"x from main" << x <<"\n";
16+
cout <<"y from main" << y <<"\n";
17+
myFunction();
18+
cout <<"Back from myFunction !\n\n";
19+
cout <<"x from main" << x <<"\n";
20+
cout <<"y from main" << y <<"\n";
21+
return 0;
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <windows.h>
3+
4+
class Cat {
5+
public:
6+
Cat() {
7+
numOfCats++;
8+
}
9+
10+
~Cat() {
11+
numOfCats--;
12+
}
13+
14+
static int getNumOfCats() {
15+
return numOfCats;
16+
}
17+
18+
private:
19+
static int numOfCats;
20+
};
21+
22+
int Cat::numOfCats = 0;
23+
24+
int main() {
25+
SetConsoleOutputCP(CP_UTF8);
26+
Cat cat1;
27+
Cat cat2;
28+
Cat cat3;
29+
30+
std::cout << "当前有 " << Cat::getNumOfCats() << " 只猫。" << std::endl;
31+
32+
return 0;
33+
}
Binary file not shown.

0 commit comments

Comments
 (0)