Skip to content

Commit 0b84460

Browse files
committed
upload
1 parent 146b8d5 commit 0b84460

File tree

10 files changed

+774
-0
lines changed

10 files changed

+774
-0
lines changed
919 KB
Binary file not shown.
528 KB
Binary file not shown.
594 KB
Binary file not shown.
675 KB
Binary file not shown.
356 KB
Binary file not shown.

3.C++程序设计/week7/README.md

Lines changed: 574 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# 编程作业: 编程作业—文件操作与模板
2+
3+
## 编程题#1
4+
5+
[来源: POJ](http://cxsjsxmooc.openjudge.cn/test/7w6/) (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
6+
7+
**注意: 总时间限制: 1000ms 内存限制: 65536kB**
8+
9+
### 描述
10+
11+
实现一个三维数组模版CArray3D,可以用来生成元素为任意类型变量的三维数组,使得下面程序输出结果是:
12+
13+
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,
14+
15+
注意,只能写一个类模版,不能写多个。
16+
17+
```c++
18+
#include <iostream>
19+
using namespace std;
20+
// 在此处补充你的代码
21+
int main()
22+
{
23+
CArray3D<int> a(3,4,5);
24+
int No = 0;
25+
for( int i = 0; i < 3; ++ i )
26+
for( int j = 0; j < 4; ++j )
27+
for( int k = 0; k < 5; ++k )
28+
a[i][j][k] = No ++;
29+
for( int i = 0; i < 3; ++ i )
30+
for( int j = 0; j < 4; ++j )
31+
for( int k = 0; k < 5; ++k )
32+
cout << a[i][j][k] << ",";
33+
return 0;
34+
}
35+
```
36+
37+
### 样例输入
38+
39+
```
40+
41+
```
42+
43+
### 样例输出
44+
45+
```
46+
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,
47+
```
48+
49+
### 提示
50+
51+
提示:类里面可以定义类,类模版里面也可以定义类模版。例如:
52+
53+
```
54+
class A
55+
{
56+
class B {
57+
58+
};
59+
};
60+
61+
template
62+
class S
63+
{
64+
T x;
65+
class K {
66+
T a;
67+
};
68+
};
69+
```
70+
71+
### 解题思路
72+
73+
之前写过一个类Array2D,稍作修改一下就好了,比如把`**ptr` 改成 `***ptr`
74+
75+
76+
77+
## 编程题#2: 实数的输出格式
78+
79+
[来源: POJ](http://cxsjsxmooc.openjudge.cn/test/F/) (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
80+
81+
**注意: 总时间限制: 1000ms 内存限制: 1000kB**
82+
83+
### 描述
84+
85+
利用流操纵算子实现: 输入一个实数,先以非科学计数法输出,小数点后面保留5位有效数字;再以科学计数法输出,小数点后面保留7位有效数字。
86+
87+
**注意:在不同系统、编译器上的输出格式略有不同,但保证在程序中采用默认格式设置一定能在OJ平台上得到正确结果。**
88+
89+
### 输入
90+
91+
以非科学计数法表示的一个正实数,保证可以用double类型存储。
92+
93+
### 输出
94+
95+
第一行:以非科学计数法输出该实数,小数点后面保留5位有效数字;
96+
97+
第二行:以科学计数法输出该实数,小数点后面保留7位有效数字。
98+
99+
### 样例输入
100+
101+
```
102+
12.34
103+
```
104+
105+
### 样例输出
106+
107+
```
108+
12.34000
109+
1.2340000e+01
110+
```
111+
112+
### 解题思路
113+
114+
没啥好说的,就是运用各种格式输出符。
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
using namespace std;
4+
5+
int main()
6+
{
7+
double real;
8+
cin >> real;
9+
10+
cout << fixed << setprecision(5) << real << endl;
11+
cout << scientific << setprecision(7) << real << endl;
12+
return 0;
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
using namespace std;
4+
5+
int main()
6+
{
7+
int a;
8+
cin >> a;
9+
cout << hex << a << endl;
10+
cout << dec << setw(10) << setfill('0') << a << endl;
11+
return 0;
12+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
template <class T>
5+
class CArray3D
6+
{
7+
private:
8+
T ***ptr;
9+
int size1, size2, size3;
10+
public:
11+
CArray3D(int s1, int s2, int s3) :size1(s1), size2(s2), size3(s3)
12+
{
13+
if (size1 == 0 || size2 == 0 || size3 == 0)
14+
{
15+
ptr = NULL;
16+
}
17+
else
18+
{
19+
ptr = new T**[size1];
20+
for (int i = 0; i < size1; i++)
21+
{
22+
ptr[i] = new T*[size2];
23+
for (int j = 0; j < size2; j++)
24+
{
25+
ptr[i][j] = new T[size3];
26+
}
27+
}
28+
}
29+
}
30+
~CArray3D()
31+
{
32+
if (ptr)
33+
{
34+
for (int i = 0; i < size1; i++)
35+
{
36+
for (int j = 0; j < size2; j++)
37+
{
38+
delete[] ptr[i][j];
39+
}
40+
delete[] ptr[i];
41+
}
42+
delete ptr;
43+
}
44+
}
45+
T** operator[](int i) { return ptr[i]; }
46+
};
47+
48+
int main()
49+
{
50+
CArray3D<int> a(3, 4, 5);
51+
int No = 0;
52+
for (int i = 0; i < 3; ++i)
53+
for (int j = 0; j < 4; ++j)
54+
for (int k = 0; k < 5; ++k)
55+
a[i][j][k] = No++;
56+
for (int i = 0; i < 3; ++i)
57+
for (int j = 0; j < 4; ++j)
58+
for (int k = 0; k < 5; ++k)
59+
cout << a[i][j][k] << ",";
60+
return 0;
61+
}

0 commit comments

Comments
 (0)