support bazel complie this project and format code.
This commit is contained in:
Binary file not shown.
23
practical_exercises/10_day_practice/day1/BUILD
Normal file
23
practical_exercises/10_day_practice/day1/BUILD
Normal file
@@ -0,0 +1,23 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day1:union`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day1:print`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day1:annotate`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day1:runnian`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "union",
|
||||
srcs = ["union.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "print",
|
||||
srcs = ["print.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "annotate",
|
||||
srcs = ["annotate.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "runnian",
|
||||
srcs = ["runnian.cpp"],
|
||||
)
|
@@ -1,8 +1,5 @@
|
||||
#include<iostream>
|
||||
|
||||
|
||||
一种条件编译指令注释
|
||||
|
||||
/* 注释.cpp */
|
||||
#include <iostream>
|
||||
|
||||
//另一种注释方法
|
||||
#if 0
|
||||
@@ -12,5 +9,4 @@ asd
|
||||
//打开注释
|
||||
//条件编译指令
|
||||
#if 1
|
||||
asData
|
||||
#endif
|
24
practical_exercises/10_day_practice/day1/print.cpp
Normal file
24
practical_exercises/10_day_practice/day1/print.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
/* 打印练习.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[]) {
|
||||
int i, j, k, f;
|
||||
for (i = 1; i <= 4; i++) {
|
||||
for (j = 1; j <= 30; j++)
|
||||
cout << " ";
|
||||
for (k = 1; k <= 8 - 2 * i; k++)
|
||||
cout << " ";
|
||||
for (f = 1; f <= 2 * i; f++)
|
||||
cout << '*';
|
||||
cout << endl;
|
||||
}
|
||||
for (i = 1; i <= 3; i++) {
|
||||
for (j = 1; j <= 30; j++)
|
||||
cout << " ";
|
||||
for (f = 1; f <= 7 - 2 * i; f++)
|
||||
cout << '*';
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
17
practical_exercises/10_day_practice/day1/runnian.cpp
Normal file
17
practical_exercises/10_day_practice/day1/runnian.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
/* 是否闰年.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[]) {
|
||||
int year;
|
||||
bool isLeapYear;
|
||||
cout << "Enter the year: ";
|
||||
cin >> year;
|
||||
isLeapYear = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
|
||||
if (isLeapYear) {
|
||||
cout << year << " is a leap year" << endl;
|
||||
} else {
|
||||
cout << year << " is not a leap year" << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
21
practical_exercises/10_day_practice/day1/union.cpp
Normal file
21
practical_exercises/10_day_practice/day1/union.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
/* 联合体学习.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
//相同的内存地址
|
||||
union myun {
|
||||
struct {
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
} u;
|
||||
int k;
|
||||
} a;
|
||||
int main() {
|
||||
a.u.x = 4;
|
||||
a.u.y = 5;
|
||||
a.u.z = 6;
|
||||
a.k = 0; //覆盖掉第一个int空间值
|
||||
printf("%d %d %d %d\n", a.u.x, a.u.y, a.u.z, a.k);
|
||||
|
||||
return 0;
|
||||
}
|
@@ -1,24 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int i,j,k,f;
|
||||
for (i=1;i<=4;i++){
|
||||
for (j=1;j<=30;j++)
|
||||
cout<<" ";
|
||||
for (k=1;k<=8-2*i;k++)
|
||||
cout<<" ";
|
||||
for (f=1;f<=2*i;f++)
|
||||
cout<<'*';
|
||||
cout<<endl;
|
||||
}
|
||||
for(i=1;i<=3;i++){
|
||||
for (j=1;j<=30;j++)
|
||||
cout<<" ";
|
||||
for (f=1;f<=7-2*i;f++)
|
||||
cout<<'*';
|
||||
cout<<endl;
|
||||
}
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int year;
|
||||
bool isLeapYear;
|
||||
cout<<"Enter the year: ";
|
||||
cin>>year;
|
||||
isLeapYear = (((year%4==0)&&(year%100!=0))||(year%400==0));
|
||||
if(isLeapYear)
|
||||
{
|
||||
cout<<year<<" is a leap year"<<endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<year<<" is not a leap year"<<endl;
|
||||
}
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
//相同的内存地址
|
||||
union myun
|
||||
{
|
||||
struct { int x; int y; int z; }u;
|
||||
int k;
|
||||
}a;
|
||||
int main()
|
||||
{
|
||||
a.u.x =4;
|
||||
a.u.y =5;
|
||||
a.u.z =6;
|
||||
a.k = 0; //覆盖掉第一个int空间值
|
||||
printf("%d %d %d %d\n",a.u.x,a.u.y,a.u.z,a.k);
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
30
practical_exercises/10_day_practice/day10/file/10-4.cpp
Normal file
30
practical_exercises/10_day_practice/day10/file/10-4.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
char c[30] = "this is string";
|
||||
double d = -1231.232;
|
||||
cout.width(30);
|
||||
cout.fill('*');
|
||||
cout.setf(ios::left);
|
||||
cout << c << "----L1" << endl;
|
||||
cout.width(30);
|
||||
cout.fill('-');
|
||||
cout.setf(ios::right);
|
||||
cout << c << "----L2" << endl;
|
||||
cout.setf(ios::dec | ios::showbase | ios::showpoint);
|
||||
cout.width(30);
|
||||
cout << d << "----L3"
|
||||
<< "\n";
|
||||
cout.setf(ios::showpoint);
|
||||
cout.precision(10);
|
||||
cout.width(30);
|
||||
cout << d << "----L4"
|
||||
<< "\n";
|
||||
cout.width(30);
|
||||
cout.setf(ios::oct, ios::basefield);
|
||||
cout << 100 << "----L5"
|
||||
<< "\n";
|
||||
|
||||
return 0;
|
||||
}
|
@@ -1,18 +1,17 @@
|
||||
//用cin输入字符串数据时,如果字符串中含有空白就不能完整输入。因为遇到空白字符时,cin就认为字符串结束了。
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char a[50];
|
||||
cout<<"please input a string:";
|
||||
cin>>a;
|
||||
cout<<a<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
int main(int argc, char const *argv[]) {
|
||||
char a[50];
|
||||
cout << "please input a string:";
|
||||
cin >> a;
|
||||
cout << a << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
若a的内容是:
|
||||
this is a string!
|
||||
this is a string!
|
||||
就难以输入啦!
|
||||
这样的数据应用输入流类的成员函数输入
|
||||
*/
|
12
practical_exercises/10_day_practice/day10/file/12-2.cpp
Normal file
12
practical_exercises/10_day_practice/day10/file/12-2.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[]) {
|
||||
char stu[5][10];
|
||||
int i;
|
||||
for (i = 0; i < 5; i++)
|
||||
cin.getline(stu[i], 10, ',');
|
||||
for (i = 0; i < 5; i++)
|
||||
cout << stu[i] << endl;
|
||||
|
||||
return 0;
|
||||
}
|
19
practical_exercises/10_day_practice/day10/file/12-3.cpp
Normal file
19
practical_exercises/10_day_practice/day10/file/12-3.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
//函数原型
|
||||
// put(char c)
|
||||
// write(const char*c, int n)
|
||||
int main() {
|
||||
char c;
|
||||
char a[50] = "this is a string...";
|
||||
cout << "use get() input char:";
|
||||
while ((c = cin.get()) != '\n') {
|
||||
cout.put(c);
|
||||
cout.put('\n');
|
||||
cout.put('t').put('h').put('i').put('s').put('\n');
|
||||
cout.write(a, 12).put('\n');
|
||||
cout << "look"
|
||||
<< "\t here!" << endl;
|
||||
}
|
||||
|
||||
}
|
20
practical_exercises/10_day_practice/day10/file/12-5.cpp
Normal file
20
practical_exercises/10_day_practice/day10/file/12-5.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Eg12-5.cpp
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main() {
|
||||
char c[30] = "this is string";
|
||||
double d = -1234.8976;
|
||||
cout << setw(30) << left << setfill('*') << c << "----L1" << endl;
|
||||
cout << setw(30) << right << setfill('*') << c << "----L2" << endl;
|
||||
// showbase显示数值的基数前缀
|
||||
cout << dec << showbase << showpoint << setw(30) << d << "----L3"
|
||||
<< "\n";
|
||||
// showpoint显示小数点
|
||||
cout << setw(30) << showpoint << setprecision(10) << d << "----L4"
|
||||
<< "\n";
|
||||
// setbase(8)设置八进制
|
||||
cout << setw(30) << setbase(16) << 100 << "----L5"
|
||||
<< "\n";
|
||||
|
||||
}
|
37
practical_exercises/10_day_practice/day10/file/12-6.cpp
Normal file
37
practical_exercises/10_day_practice/day10/file/12-6.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
// 12-6.cpp
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[]) {
|
||||
fstream ioFile;
|
||||
ioFile.open("./a.dat", ios::out);
|
||||
ioFile << "张三"
|
||||
<< " " << 76 << " " << 98 << " " << 67 << endl; // L3
|
||||
ioFile << "李四"
|
||||
<< " " << 89 << " " << 70 << " " << 60 << endl;
|
||||
ioFile << "王十"
|
||||
<< " " << 91 << " " << 88 << " " << 77 << endl;
|
||||
ioFile << "黄二"
|
||||
<< " " << 62 << " " << 81 << " " << 75 << endl;
|
||||
ioFile << "刘六"
|
||||
<< " " << 90 << " " << 78 << " " << 67 << endl;
|
||||
ioFile.close();
|
||||
ioFile.open("./a.dat", ios::in | ios::binary);
|
||||
char name[10];
|
||||
int chinese, math, computer;
|
||||
cout << "姓名\t"
|
||||
<< "英语\t"
|
||||
<< "数学\t"
|
||||
<< "计算机\t"
|
||||
<< "总分" << endl;
|
||||
ioFile >> name;
|
||||
while (!ioFile.eof()) {
|
||||
ioFile >> chinese >> math >> computer;
|
||||
cout << name << "\t" << chinese << "\t" << math << "\t" << computer << "\t"
|
||||
<< chinese + math + computer << endl;
|
||||
ioFile >> name;
|
||||
}
|
||||
ioFile.close();
|
||||
|
||||
return 0;
|
||||
}
|
20
practical_exercises/10_day_practice/day10/file/12-7.cpp
Normal file
20
practical_exercises/10_day_practice/day10/file/12-7.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Eg12-7.cpp
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main() {
|
||||
char ch;
|
||||
ofstream out("/test.dat", ios::out | ios::binary); // L1
|
||||
for (int i = 0; i < 90; i++) {
|
||||
if (i > 0 && (i % 30) == 0)
|
||||
out.put('\n');
|
||||
out.put(i);
|
||||
out.put(' ');
|
||||
}
|
||||
out.close();
|
||||
ifstream in("/test.dat", ios::in | ios::binary);
|
||||
while (in.get(ch))
|
||||
cout << ch;
|
||||
in.close();
|
||||
|
||||
}
|
45
practical_exercises/10_day_practice/day10/file/12-9.cpp
Normal file
45
practical_exercises/10_day_practice/day10/file/12-9.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
// Eg12-12.cpp
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Employee {
|
||||
private:
|
||||
int number, age;
|
||||
char name[20];
|
||||
double sal;
|
||||
|
||||
public:
|
||||
Employee() {}
|
||||
Employee(int num, char *Name, int Age, double Salary) {
|
||||
number = num;
|
||||
strcpy(name, Name);
|
||||
age = Age;
|
||||
sal = Salary;
|
||||
}
|
||||
void display() {
|
||||
cout << number << "\t" << name << "\t" << age << "\t" << sal << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
ofstream out("D:/Employee.dat", ios::out); //定义随机输出文件
|
||||
Employee e1(1, "张三", 23, 2320);
|
||||
Employee e2(2, "李四", 32, 3210);
|
||||
Employee e3(3, "王五", 34, 2220);
|
||||
Employee e4(4, "刘六", 27, 1220);
|
||||
out.write((char *)&e1, sizeof(e1)); //按e1,e2,e3,e4顺序写入文件
|
||||
out.write((char *)&e2, sizeof(e2));
|
||||
out.write((char *)&e3, sizeof(e3));
|
||||
out.write((char *)&e4, sizeof(e4));
|
||||
|
||||
//下面的代码将e3(即王五)的年龄改为40岁
|
||||
Employee e5(3, "王五", 40, 2220);
|
||||
out.seekp(
|
||||
2 *
|
||||
sizeof(
|
||||
e1)); //指针定位到第3(起始为0)个数据块,这里写错了应该是2*sizeof(e1)
|
||||
out.write((char *)&e5, sizeof(e5)); //将e5写到第3个数据块位置,覆盖e3
|
||||
out.close(); //关闭文件
|
||||
|
||||
}
|
48
practical_exercises/10_day_practice/day10/file/BUILD
Normal file
48
practical_exercises/10_day_practice/day10/file/BUILD
Normal file
@@ -0,0 +1,48 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-9`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-6`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:10-4`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-2`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:practice`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-1`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-5`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-3`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-7`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "12-9",
|
||||
srcs = ["12-9.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-6",
|
||||
srcs = ["12-6.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "10-4",
|
||||
srcs = ["10-4.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-2",
|
||||
srcs = ["12-2.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "practice",
|
||||
srcs = ["practice.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-1",
|
||||
srcs = ["12-1.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-5",
|
||||
srcs = ["12-5.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-3",
|
||||
srcs = ["12-3.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-7",
|
||||
srcs = ["12-7.cpp"],
|
||||
)
|
18
practical_exercises/10_day_practice/day10/file/input/BUILD
Normal file
18
practical_exercises/10_day_practice/day10/file/input/BUILD
Normal file
@@ -0,0 +1,18 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file/input:getline`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file/input:get2`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file/input:get`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "getline",
|
||||
srcs = ["getline.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "get2",
|
||||
srcs = ["get2.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "get",
|
||||
srcs = ["get.cpp"],
|
||||
)
|
@@ -1,16 +1,15 @@
|
||||
//【例12-2】 用函数get和getline读取数据。
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
char a,b,c,d;
|
||||
cin.get(a);
|
||||
cin.get(b);
|
||||
c = cin.get();
|
||||
d = cin.get();
|
||||
cout<<int(a)<<','<<int(b)<<','<<int(c)<<','<<int(d)<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
int main() {
|
||||
char a, b, c, d;
|
||||
cin.get(a);
|
||||
cin.get(b);
|
||||
c = cin.get();
|
||||
d = cin.get();
|
||||
cout << int(a) << ',' << int(b) << ',' << int(c) << ',' << int(d) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
@@ -0,0 +1,44 @@
|
||||
|
||||
//【例12-2】 用函数get和getline读取数据。
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
// cin.get(arrayname,size) 把字符输入到arrayname中,长度不超过size
|
||||
int main() {
|
||||
// get()两个参数
|
||||
|
||||
// 1.输入串长<size,输入串长>arraylength,会自动扩张arrayname大小,使能保存所有数据
|
||||
// char a[10];
|
||||
// cin.get(a,20);
|
||||
// cout<<a<<endl;
|
||||
// cout<<sizeof(a)<<endl;
|
||||
// 2.输入串长<size,输入串长<arraylength,把串全部输入,后面补‘\0’
|
||||
// char b[10];
|
||||
// cin.get(b,20);
|
||||
// cout<<b<<endl;//12345,此时数组内数据为‘12345'\0’
|
||||
// cout<<sizeof(b)<<endl;
|
||||
// 3.输入串长>size,先截取size个字符,若还是大于arraylength,则自动扩张arrayname大小,使能保存所有数据
|
||||
// char c[5];
|
||||
// cin.get(c,10);
|
||||
// cout<<c<<endl;
|
||||
// cout<<sizeof(c)<<endl;
|
||||
// 4.输入串长>size,先截取size个字符,若小于arraylength,则把截取串放入数组中,最后补充‘\0’
|
||||
// char d[10];
|
||||
// cin.get(d,5);
|
||||
// cout<<d<<endl;
|
||||
// cout<<sizeof(d)<<endl;
|
||||
|
||||
// get()三个参数
|
||||
/*
|
||||
用法:cin.get(arrayname,size,s)
|
||||
?把数据输入到arrayname字符数组中,当到达长度size时结束或者遇到字符s时结束
|
||||
注释:a必须是字符数组,即char
|
||||
a[]l类型,不可为string类型;size为最大的输入长度;s为控制,遇到s则当前输入结束缓存区里的s不会被舍弃
|
||||
|
||||
*/
|
||||
int i;
|
||||
char e[10];
|
||||
cin.get(e, 8, ',');
|
||||
cout << e;
|
||||
|
||||
return 0;
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
/*
|
||||
(1)cin.getline(arrayname,size)与cin.get(arrayname,size)的区别
|
||||
cin.get(arrayname,size)当遇到[enter]时会结束目前输入,他不会删除缓冲区中的[enter]
|
||||
cin.getline(arrayname,size)当遇到[enter]时会结束当前输入,但是会删除缓冲区中的[enter]
|
||||
*/
|
||||
int main() {
|
||||
/*
|
||||
char a[10];
|
||||
char b;
|
||||
cin.get(a,10);
|
||||
cin.get(b);
|
||||
cout<<a<<endl<<int(b);//输入:12345[enter] 输出:12345 【换行】 10*/
|
||||
/*char c[10];
|
||||
char d;
|
||||
cin.getline(c,10);
|
||||
cin.get(d);
|
||||
cout<<c<<endl<<int(d);//输入:12345[enter]a[enter] 输出:12345【换行】97*/
|
||||
// cin.getline(arrayname,size,s)与cin.gei(arrayname,size,s)的区别
|
||||
/*
|
||||
cin.getline(arrayname,size,s)当遇到s时会结束输入,并把s从缓冲区中删除
|
||||
cin.get(arrayname,size,s)当遇到s时会结束输入,但不会删除缓冲区中的s
|
||||
*/
|
||||
/*
|
||||
char e[10];
|
||||
char f;
|
||||
cin.get(e,10,',');
|
||||
cin.get(f);
|
||||
cout<<e<<endl<<f;//输入:12345,[enter]
|
||||
输出:12345【换行】,说明:cin,get不会删除缓冲区的,*/
|
||||
char e1[10];
|
||||
char f1;
|
||||
cin.getline(e1, 10, ',');
|
||||
cin.get(f1);
|
||||
cout << e1 << endl << f1; //输入:asd,wqe 输出:asd【换行】w
|
||||
|
||||
}
|
64
practical_exercises/10_day_practice/day10/file/practice.cpp
Normal file
64
practical_exercises/10_day_practice/day10/file/practice.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
//向量是一个能够存放任意类型的动态数组
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
class Person {
|
||||
private:
|
||||
char name[20];
|
||||
char id[18];
|
||||
int age;
|
||||
char addr[20];
|
||||
|
||||
public:
|
||||
Person() {}
|
||||
Person(char *n, char *pid, int Age, char *Addr) {
|
||||
strcpy(name, n);
|
||||
strcpy(id, pid);
|
||||
age = Age;
|
||||
strcpy(addr, Addr);
|
||||
}
|
||||
void display() {
|
||||
cout << name << "\t" << id << "\t" << age << "\t" << addr << endl;
|
||||
}
|
||||
};
|
||||
int main(int argc, char const *argv[]) {
|
||||
vector<Person> v;
|
||||
vector<Person>::iterator
|
||||
pos; //声明一个迭代器,来访问vector容器,作用:遍历或者指向vector容器的元素
|
||||
char ch;
|
||||
ofstream out("d:/person.dat", ios::out | ios::app | ios::binary);
|
||||
char Name[20], ID[18], Addr[20];
|
||||
int Age;
|
||||
cout << "------输入个人档案------" << endl << endl;
|
||||
do {
|
||||
cout << "姓名: ";
|
||||
cin >> Name;
|
||||
cout << "身份证号: ";
|
||||
cin >> ID;
|
||||
cout << "年龄: ";
|
||||
cin >> Age;
|
||||
cout << "地址: ";
|
||||
cin >> Addr;
|
||||
Person per(Name, ID, Age, Addr);
|
||||
out.write((char *)&per, sizeof(per));
|
||||
cout << "Enter another Person(y/n)?";
|
||||
cin >> ch;
|
||||
} while (ch == 'y');
|
||||
out.close();
|
||||
ifstream in("d:/person.dat", ios::in | ios::binary); // L9
|
||||
Person s;
|
||||
in.read((char *)&s, sizeof(s));
|
||||
while (!in.eof()) {
|
||||
v.push_back(s);
|
||||
in.read((char *)&s, sizeof(s));
|
||||
}
|
||||
cout << "\n---------从文件中读出的数据--------" << endl << endl; // L15
|
||||
pos = v.begin();
|
||||
for (pos = v.begin(); pos != v.end(); pos++)
|
||||
(*pos).display();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char stu[5][10];
|
||||
int i;
|
||||
for(i=0;i<5;i++)
|
||||
cin.getline(stu[i],10,',');
|
||||
for(i=0;i<5;i++)
|
||||
cout<<stu[i]<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
//函数原型
|
||||
//put(char c)
|
||||
//write(const char*c, int n)
|
||||
int main(){
|
||||
char c;
|
||||
char a[50]="this is a string...";
|
||||
cout<<"use get() input char:";
|
||||
while((c=cin.get())!='\n'){
|
||||
cout.put(c);
|
||||
cout.put('\n');
|
||||
cout.put('t').put('h').put('i').put('s').put('\n');
|
||||
cout.write(a,12).put('\n');
|
||||
cout<<"look"<<"\t here!"<<endl;
|
||||
}
|
||||
system("pause");
|
||||
}
|
@@ -1,17 +0,0 @@
|
||||
//Eg12-5.cpp
|
||||
#include<iostream>
|
||||
#include<iomanip>
|
||||
using namespace std;
|
||||
int main(){
|
||||
char c[30]="this is string";
|
||||
double d=-1234.8976;
|
||||
cout<<setw(30)<<left<<setfill('*')<<c<<"----L1"<<endl;
|
||||
cout<<setw(30)<<right<<setfill('*')<<c<<"----L2"<<endl;
|
||||
//showbase显示数值的基数前缀
|
||||
cout<<dec<<showbase<<showpoint<<setw(30)<<d<<"----L3"<<"\n";
|
||||
//showpoint显示小数点
|
||||
cout<<setw(30)<<showpoint<<setprecision(10)<<d<<"----L4"<<"\n";
|
||||
//setbase(8)设置八进制
|
||||
cout<<setw(30)<<setbase(16)<<100<<"----L5"<<"\n";
|
||||
system("pause");
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
//12-6.cpp
|
||||
#include<iostream>
|
||||
#include<fstream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
fstream ioFile;
|
||||
ioFile.open("./a.dat",ios::out);
|
||||
ioFile<<"张三"<<" "<<76<<" "<<98<<" "<<67<<endl; //L3
|
||||
ioFile<<"李四"<<" "<<89<<" "<<70<<" "<<60<<endl;
|
||||
ioFile<<"王十"<<" "<<91<<" "<<88<<" "<<77<<endl;
|
||||
ioFile<<"黄二"<<" "<<62<<" "<<81<<" "<<75<<endl;
|
||||
ioFile<<"刘六"<<" "<<90<<" "<<78<<" "<<67<<endl;
|
||||
ioFile.close();
|
||||
ioFile.open("./a.dat",ios::in|ios::binary);
|
||||
char name[10];
|
||||
int chinese,math,computer;
|
||||
cout<<"姓名\t"<<"英语\t"<<"数学\t"<<"计算机\t"<<"总分"<<endl;
|
||||
ioFile>>name;
|
||||
while(!ioFile.eof()) {
|
||||
ioFile>>chinese>>math>>computer;
|
||||
cout<<name<<"\t"<<chinese<<"\t"<<math<<"\t"<<computer<<"\t"<<chinese+math+computer<<endl;
|
||||
ioFile>>name;
|
||||
}
|
||||
ioFile.close();
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
//Eg12-7.cpp
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
char ch;
|
||||
ofstream out("/test.dat",ios::out|ios::binary); //L1
|
||||
for(int i=0;i<90;i++){
|
||||
if(i>0 && (i % 30)==0)
|
||||
out.put('\n');
|
||||
out.put(i);
|
||||
out.put(' ');
|
||||
|
||||
}
|
||||
out.close();
|
||||
ifstream in("/test.dat",ios::in|ios::binary);
|
||||
while(in.get(ch))
|
||||
cout<<ch;
|
||||
in.close();
|
||||
system("pause");
|
||||
}
|
@@ -1,41 +0,0 @@
|
||||
//Eg12-12.cpp
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
class Employee{
|
||||
private:
|
||||
int number ,age;
|
||||
char name[20];
|
||||
double sal;
|
||||
public:
|
||||
Employee(){}
|
||||
Employee(int num,char* Name,int Age, double Salary){
|
||||
number=num;
|
||||
strcpy(name,Name);
|
||||
age=Age;
|
||||
sal=Salary;
|
||||
}
|
||||
void display(){
|
||||
cout<<number<<"\t"<<name<<"\t"<<age<<"\t"<<sal<<endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
ofstream out("D:/Employee.dat",ios::out); //定义随机输出文件
|
||||
Employee e1(1,"张三",23,2320);
|
||||
Employee e2(2,"李四",32,3210);
|
||||
Employee e3(3,"王五",34,2220);
|
||||
Employee e4(4,"刘六",27,1220);
|
||||
out.write((char*)&e1,sizeof(e1)); //按e1,e2,e3,e4顺序写入文件
|
||||
out.write((char*)&e2,sizeof(e2));
|
||||
out.write((char*)&e3,sizeof(e3));
|
||||
out.write((char*)&e4,sizeof(e4));
|
||||
|
||||
//下面的代码将e3(即王五)的年龄改为40岁
|
||||
Employee e5(3,"王五",40,2220);
|
||||
out.seekp(2*sizeof(e1)); //指针定位到第3(起始为0)个数据块,这里写错了应该是2*sizeof(e1)
|
||||
out.write((char*)&e5,sizeof(e5)); //将e5写到第3个数据块位置,覆盖e3
|
||||
out.close(); //关闭文件
|
||||
system("pause");
|
||||
}
|
@@ -1,44 +0,0 @@
|
||||
|
||||
//【例12-2】 用函数get和getline读取数据。
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
//cin.get(arrayname,size) 把字符输入到arrayname中,长度不超过size
|
||||
int main()
|
||||
{
|
||||
//get()两个参数
|
||||
|
||||
//1.输入串长<size,输入串长>arraylength,会自动扩张arrayname大小,使能保存所有数据
|
||||
// char a[10];
|
||||
// cin.get(a,20);
|
||||
// cout<<a<<endl;
|
||||
// cout<<sizeof(a)<<endl;
|
||||
//2.输入串长<size,输入串长<arraylength,把串全部输入,后面补‘\0’
|
||||
// char b[10];
|
||||
// cin.get(b,20);
|
||||
// cout<<b<<endl;//12345,此时数组内数据为‘12345'\0’
|
||||
// cout<<sizeof(b)<<endl;
|
||||
//3.输入串长>size,先截取size个字符,若还是大于arraylength,则自动扩张arrayname大小,使能保存所有数据
|
||||
// char c[5];
|
||||
// cin.get(c,10);
|
||||
// cout<<c<<endl;
|
||||
// cout<<sizeof(c)<<endl;
|
||||
//4.输入串长>size,先截取size个字符,若小于arraylength,则把截取串放入数组中,最后补充‘\0’
|
||||
// char d[10];
|
||||
// cin.get(d,5);
|
||||
// cout<<d<<endl;
|
||||
// cout<<sizeof(d)<<endl;
|
||||
|
||||
//get()三个参数
|
||||
/*
|
||||
用法:cin.get(arrayname,size,s) ?把数据输入到arrayname字符数组中,当到达长度size时结束或者遇到字符s时结束
|
||||
注释:a必须是字符数组,即char a[]l类型,不可为string类型;size为最大的输入长度;s为控制,遇到s则当前输入结束缓存区里的s不会被舍弃
|
||||
|
||||
*/
|
||||
int i;
|
||||
char e[10];
|
||||
cin.get(e,8,',');
|
||||
cout<<e;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
@@ -1,38 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
/*
|
||||
(1)cin.getline(arrayname,size)与cin.get(arrayname,size)的区别
|
||||
cin.get(arrayname,size)当遇到[enter]时会结束目前输入,他不会删除缓冲区中的[enter]
|
||||
cin.getline(arrayname,size)当遇到[enter]时会结束当前输入,但是会删除缓冲区中的[enter]
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
/*
|
||||
char a[10];
|
||||
char b;
|
||||
cin.get(a,10);
|
||||
cin.get(b);
|
||||
cout<<a<<endl<<int(b);//输入:12345[enter] 输出:12345 【换行】 10*/
|
||||
/*char c[10];
|
||||
char d;
|
||||
cin.getline(c,10);
|
||||
cin.get(d);
|
||||
cout<<c<<endl<<int(d);//输入:12345[enter]a[enter] 输出:12345【换行】97*/
|
||||
//cin.getline(arrayname,size,s)与cin.gei(arrayname,size,s)的区别
|
||||
/*
|
||||
cin.getline(arrayname,size,s)当遇到s时会结束输入,并把s从缓冲区中删除
|
||||
cin.get(arrayname,size,s)当遇到s时会结束输入,但不会删除缓冲区中的s
|
||||
*/
|
||||
/*
|
||||
char e[10];
|
||||
char f;
|
||||
cin.get(e,10,',');
|
||||
cin.get(f);
|
||||
cout<<e<<endl<<f;//输入:12345,[enter] 输出:12345【换行】,说明:cin,get不会删除缓冲区的,*/
|
||||
char e1[10];
|
||||
char f1;
|
||||
cin.getline(e1,10,',');
|
||||
cin.get(f1);
|
||||
cout<<e1<<endl<<f1;//输入:asd,wqe 输出:asd【换行】w
|
||||
system("pause");
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char c[30]="this is string";
|
||||
double d = -1231.232;
|
||||
cout.width(30);
|
||||
cout.fill('*');
|
||||
cout.setf(ios::left);
|
||||
cout<<c<<"----L1"<<endl;
|
||||
cout.width(30);
|
||||
cout.fill('-');
|
||||
cout.setf(ios::right);
|
||||
cout<<c<<"----L2"<<endl;
|
||||
cout.setf(ios::dec|ios::showbase|ios::showpoint);
|
||||
cout.width(30);
|
||||
cout<<d<<"----L3"<<"\n";
|
||||
cout.setf(ios::showpoint);
|
||||
cout.precision(10);
|
||||
cout.width(30);
|
||||
cout<<d<<"----L4"<<"\n";
|
||||
cout.width(30);
|
||||
cout.setf(ios::oct,ios::basefield);
|
||||
cout<<100<<"----L5"<<"\n";
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@@ -1,63 +0,0 @@
|
||||
#include<iostream>
|
||||
#include<fstream>
|
||||
//向量是一个能够存放任意类型的动态数组
|
||||
#include<vector>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
class Person{
|
||||
private:
|
||||
char name[20];
|
||||
char id[18];
|
||||
int age;
|
||||
char addr[20];
|
||||
public:
|
||||
Person(){}
|
||||
Person(char *n,char *pid,int Age,char* Addr){
|
||||
strcpy(name,n);
|
||||
strcpy(id,pid);
|
||||
age=Age;
|
||||
strcpy(addr,Addr);
|
||||
}
|
||||
void display(){
|
||||
cout<<name<<"\t"<<id<<"\t"<<age<<"\t"<<addr<<endl;
|
||||
}
|
||||
};
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
vector<Person> v;
|
||||
vector<Person>::iterator pos;//声明一个迭代器,来访问vector容器,作用:遍历或者指向vector容器的元素
|
||||
char ch;
|
||||
ofstream out("d:/person.dat",ios::out|ios::app|ios::binary);
|
||||
char Name[20],ID[18],Addr[20];
|
||||
int Age;
|
||||
cout<<"------输入个人档案------"<<endl<<endl;
|
||||
do{
|
||||
cout<<"姓名: ";
|
||||
cin>>Name;
|
||||
cout<<"身份证号: ";
|
||||
cin>>ID;
|
||||
cout<<"年龄: ";
|
||||
cin>>Age;
|
||||
cout<<"地址: ";
|
||||
cin>>Addr;
|
||||
Person per(Name,ID,Age,Addr);
|
||||
out.write((char*)&per,sizeof(per));
|
||||
cout<<"Enter another Person(y/n)?";
|
||||
cin>>ch;
|
||||
}while(ch=='y');
|
||||
out.close();
|
||||
ifstream in("d:/person.dat",ios::in|ios::binary); //L9
|
||||
Person s;
|
||||
in.read((char*)&s,sizeof(s));
|
||||
while(!in.eof()){
|
||||
v.push_back(s);
|
||||
in.read((char*)&s,sizeof(s));
|
||||
}
|
||||
cout<<"\n---------从文件中读出的数据--------"<<endl<<endl;//L15
|
||||
pos=v.begin();
|
||||
for(pos=v.begin();pos!=v.end();pos++)
|
||||
(*pos).display();
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
48
practical_exercises/10_day_practice/day2/BUILD
Normal file
48
practical_exercises/10_day_practice/day2/BUILD
Normal file
@@ -0,0 +1,48 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day2:rec1`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day2:rec2`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day2:hanoi`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day2:st`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day2:static`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day2:shaizi`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day2:pow`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day2:compute`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day2:enum`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "rec1",
|
||||
srcs = ["rec1.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "rec2",
|
||||
srcs = ["rec2.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "hanoi",
|
||||
srcs = ["hanoi.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "st",
|
||||
srcs = ["st.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "static",
|
||||
srcs = ["static.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "shaizi",
|
||||
srcs = ["shaizi.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "pow",
|
||||
srcs = ["pow.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "compute",
|
||||
srcs = ["compute.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "enum",
|
||||
srcs = ["enum.cpp"],
|
||||
)
|
26
practical_exercises/10_day_practice/day2/compute.cpp
Normal file
26
practical_exercises/10_day_practice/day2/compute.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
/* 求π.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
double arctan(double x);
|
||||
int main(int argc, char const *argv[]) {
|
||||
double a = 16.0 * arctan(1.0 / 5.0);
|
||||
double b = 4.0 * arctan(1.0 / 239.0);
|
||||
double pi = a - b;
|
||||
cout << pi << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
double arctan(double x) {
|
||||
double head = x;
|
||||
int tail = 1;
|
||||
double art = 0;
|
||||
while (double(head / tail) > 1e-15) {
|
||||
art = (tail % 4 == 1) ? art + head / tail : art - head / tail;
|
||||
head *= x * x;
|
||||
tail += 2;
|
||||
cout << "---------------" << endl;
|
||||
cout << tail << endl;
|
||||
cout << "---------------" << endl;
|
||||
}
|
||||
return art;
|
||||
}
|
17
practical_exercises/10_day_practice/day2/enum.cpp
Normal file
17
practical_exercises/10_day_practice/day2/enum.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
/* 枚举类型.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
enum weekday { s, m, t, w, thu, f, s1 };
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
enum weekday wek = s;
|
||||
// weekday wek=s;
|
||||
for (int i = wek; i != f; i++) {
|
||||
cout << i << endl;
|
||||
cout << wek + s << endl;
|
||||
cout << "-------哈哈-------" << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
25
practical_exercises/10_day_practice/day2/hanoi.cpp
Normal file
25
practical_exercises/10_day_practice/day2/hanoi.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
/* 汉诺塔.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
void move(char A, char B);
|
||||
void hanoi(int n, char A, char B, char C);
|
||||
int main(int argc, char const *argv[]) {
|
||||
cout << "请输入盘子数量:";
|
||||
int disks;
|
||||
cin >> disks;
|
||||
hanoi(disks, 'A', 'B', 'C');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void move(char A, char B) { cout << A << "->" << B << endl; }
|
||||
|
||||
void hanoi(int n, char A, char B, char C) {
|
||||
if (n == 1) {
|
||||
move(A, C);
|
||||
} else {
|
||||
hanoi(n - 1, A, C, B);
|
||||
move(A, C);
|
||||
hanoi(n - 1, B, A, C);
|
||||
}
|
||||
}
|
29
practical_exercises/10_day_practice/day2/pow.cpp
Normal file
29
practical_exercises/10_day_practice/day2/pow.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
/* x的n次方.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
double power(double x, int n);
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
int x;
|
||||
cin >> x;
|
||||
int wei = 0;
|
||||
int sum = 0;
|
||||
int each, chu;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
each = x % 10;
|
||||
chu = x / 10;
|
||||
sum += each * power(2, wei);
|
||||
x = chu;
|
||||
wei++;
|
||||
}
|
||||
cout << sum << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
double power(double x, int n) {
|
||||
double val = 1.0;
|
||||
while (n--) {
|
||||
val *= x;
|
||||
}
|
||||
return val;
|
||||
}
|
21
practical_exercises/10_day_practice/day2/rec1.cpp
Normal file
21
practical_exercises/10_day_practice/day2/rec1.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
/* 递归1.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int f(int n);
|
||||
int main(int argc, char const *argv[]) {
|
||||
cout << "input x:";
|
||||
int x;
|
||||
cin >> x;
|
||||
cout << f(x) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int f(int n) {
|
||||
if (n == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
return n * f(n - 1);
|
||||
}
|
||||
}
|
21
practical_exercises/10_day_practice/day2/rec2.cpp
Normal file
21
practical_exercises/10_day_practice/day2/rec2.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
/* 递归2.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int f(int n, int k);
|
||||
int main(int argc, char const *argv[]) {
|
||||
cout << "请输入n与k" << endl;
|
||||
int n, k;
|
||||
cin >> n;
|
||||
cin >> k;
|
||||
cout << f(n, k) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int f(int n, int k) {
|
||||
if ((n == k) || (k == 0)) {
|
||||
return 1;
|
||||
} else {
|
||||
return f(n - 1, k - 1) + f(n - 1, k);
|
||||
}
|
||||
}
|
54
practical_exercises/10_day_practice/day2/shaizi.cpp
Normal file
54
practical_exercises/10_day_practice/day2/shaizi.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/* 掷骰子.cpp */
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int rolldice();
|
||||
int main(int argc, char const *argv[]) {
|
||||
int flag;
|
||||
unsigned seed;
|
||||
cout << "请输入无符号整数:" << endl;
|
||||
cin >> seed;
|
||||
srand(seed);
|
||||
int sum = rolldice();
|
||||
int selfdim;
|
||||
switch (sum) {
|
||||
case 7:
|
||||
case 11:
|
||||
flag = 1;
|
||||
break;
|
||||
case 2:
|
||||
case 3:
|
||||
case 12:
|
||||
flag = 2;
|
||||
break;
|
||||
default:
|
||||
flag = 0;
|
||||
selfdim = sum;
|
||||
break;
|
||||
}
|
||||
while (flag == 0) {
|
||||
sum = rolldice();
|
||||
if (sum == selfdim) {
|
||||
flag = 1;
|
||||
} else if (sum == 7) {
|
||||
flag = 2;
|
||||
}
|
||||
}
|
||||
if (flag == 1) {
|
||||
cout << "player win\n";
|
||||
} else {
|
||||
cout << "player loses\n";
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rolldice() {
|
||||
int sum = 0;
|
||||
int dim1 = rand() % 6 + 1;
|
||||
int dim2 = rand() % 6 + 1;
|
||||
sum = dim1 + dim2;
|
||||
cout << "sum=" << dim1 << "+" << dim2 << endl;
|
||||
return sum;
|
||||
}
|
19
practical_exercises/10_day_practice/day2/st.cpp
Normal file
19
practical_exercises/10_day_practice/day2/st.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
/* 结构体.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
struct student {
|
||||
int num;
|
||||
char name[20];
|
||||
char gender;
|
||||
};
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
student s = {10, "asd", 'M'};
|
||||
cout << s.num << endl;
|
||||
cout << sizeof(s.num) << endl;
|
||||
cout << sizeof(s.name) << endl;
|
||||
cout << sizeof(s.gender) << endl;
|
||||
cout << sizeof(s) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
37
practical_exercises/10_day_practice/day2/static.cpp
Normal file
37
practical_exercises/10_day_practice/day2/static.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
/* 静态变量.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int i = 1; // i 为全局变量,具有静态生存期。
|
||||
int main(void) {
|
||||
static int a; // 静态局部变量,有全局寿命,局部可见。
|
||||
int b = -10; // b, c为局部变量,具有动态生存期。
|
||||
int c = 0;
|
||||
void other(void);
|
||||
cout << "---MAIN---\n";
|
||||
cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c
|
||||
<< endl; // 1 0 -10 0
|
||||
c = c + 8;
|
||||
other(); // 33 4 0 15
|
||||
cout << "---MAIN---\n";
|
||||
cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c
|
||||
<< endl; // 33 0 -10 8
|
||||
i = i + 10;
|
||||
other(); // 75 6 4 15
|
||||
other(); // 107 8 6 15
|
||||
|
||||
return 0;
|
||||
}
|
||||
void other(void) {
|
||||
static int a = 2;
|
||||
static int b;
|
||||
// a,b为静态局部变量,具有全局寿命,局部可见。
|
||||
//只第一次进入函数时被初始化。
|
||||
int c = 10; // C为局部变量,具有动态生存期
|
||||
//每次进入函数时都初始化。
|
||||
a = a + 2;
|
||||
i = i + 32;
|
||||
c = c + 5;
|
||||
cout << "---OTHER---\n";
|
||||
cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c << endl;
|
||||
b = a;
|
||||
}
|
@@ -1,32 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
double power(double x, int n);
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int x;
|
||||
cin>>x;
|
||||
int wei=0;
|
||||
int sum=0;
|
||||
int each,chu;
|
||||
for(int i=0;i<8;i++)
|
||||
{
|
||||
each=x%10;
|
||||
chu=x/10;
|
||||
sum+=each*power(2,wei);
|
||||
x=chu;
|
||||
wei++;
|
||||
}
|
||||
cout<<sum<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
double power(double x, int n)
|
||||
{
|
||||
double val = 1.0;
|
||||
while(n--)
|
||||
{
|
||||
val*=x;
|
||||
}
|
||||
return val;
|
||||
}
|
@@ -1,63 +0,0 @@
|
||||
#include<iostream>
|
||||
#include<cstdlib>
|
||||
using namespace std;
|
||||
int rolldice();
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int flag;
|
||||
unsigned seed;
|
||||
cout<<"请输入无符号整数:"<<endl;
|
||||
cin>>seed;
|
||||
srand(seed);
|
||||
int sum = rolldice();
|
||||
int selfdim;
|
||||
switch(sum)
|
||||
{
|
||||
case 7:
|
||||
case 11:
|
||||
flag=1;
|
||||
break;
|
||||
case 2:
|
||||
case 3:
|
||||
case 12:
|
||||
flag=2;
|
||||
break;
|
||||
default:
|
||||
flag=0;
|
||||
selfdim=sum;
|
||||
break;
|
||||
}
|
||||
while(flag==0)
|
||||
{
|
||||
sum=rolldice();
|
||||
if(sum==selfdim)
|
||||
{
|
||||
flag=1;
|
||||
}
|
||||
else if(sum==7)
|
||||
{
|
||||
flag=2;
|
||||
}
|
||||
}
|
||||
if(flag==1)
|
||||
{
|
||||
cout<<"player win\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"player loses\n";
|
||||
}
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rolldice()
|
||||
{
|
||||
int sum=0;
|
||||
int dim1 = rand()%6+1;
|
||||
int dim2 = rand()%6+1;
|
||||
sum = dim1+dim2;
|
||||
cout<<"sum="<<dim1<<"+"<<dim2<<endl;
|
||||
return sum;
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
enum weekday
|
||||
{
|
||||
s,m,t,w,thu,f,s1
|
||||
};
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
enum weekday wek=s;
|
||||
// weekday wek=s;
|
||||
for(int i=wek;i!=f;i++)
|
||||
{
|
||||
cout<<i<<endl;
|
||||
cout<<wek+s<<endl;
|
||||
cout<<"-------哈哈-------"<<endl;
|
||||
}
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
double arctan(double x);
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
double a = 16.0*arctan(1.0/5.0);
|
||||
double b = 4.0*arctan(1.0/239.0);
|
||||
double pi = a-b;
|
||||
cout<<pi<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
double arctan(double x)
|
||||
{
|
||||
double head=x;
|
||||
int tail=1;
|
||||
double art=0;
|
||||
while(double(head/tail)>1e-15)
|
||||
{
|
||||
art=(tail%4==1)? art+head/tail: art-head/tail;
|
||||
head*=x*x;
|
||||
tail+=2;
|
||||
cout<<"---------------"<<endl;
|
||||
cout<<tail<<endl;
|
||||
cout<<"---------------"<<endl;
|
||||
}
|
||||
return art;
|
||||
}
|
@@ -1,33 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
void move(char A, char B);
|
||||
void hanoi(int n,char A, char B, char C);
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
cout<<"请输入盘子数量:";
|
||||
int disks;
|
||||
cin>>disks;
|
||||
hanoi(disks,'A','B','C');
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void move(char A, char B)
|
||||
{
|
||||
cout<<A<<"->"<<B<<endl;
|
||||
}
|
||||
|
||||
void hanoi(int n, char A, char B, char C)
|
||||
{
|
||||
if (n==1)
|
||||
{
|
||||
move(A,C);
|
||||
}
|
||||
else
|
||||
{
|
||||
hanoi(n-1,A,C,B);
|
||||
move(A,C);
|
||||
hanoi(n-1,B,A,C);
|
||||
}
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
struct student
|
||||
{
|
||||
int num;
|
||||
char name[20];
|
||||
char gender;
|
||||
};
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
student s={10,"asd",'M'};
|
||||
cout<<s.num<<endl;
|
||||
cout<<sizeof(s.num)<<endl;
|
||||
cout<<sizeof(s.name)<<endl;
|
||||
cout<<sizeof(s.gender)<<endl;
|
||||
cout<<sizeof(s)<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
@@ -1,25 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int f(int n);
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
cout<<"input x:";
|
||||
int x;
|
||||
cin>>x;
|
||||
cout<<f(x)<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int f(int n)
|
||||
{
|
||||
if(n==0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return n*f(n-1);
|
||||
}
|
||||
}
|
@@ -1,25 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int f(int n, int k);
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
cout<<"请输入n与k"<<endl;
|
||||
int n,k;
|
||||
cin>>n;
|
||||
cin>>k;
|
||||
cout<<f(n,k)<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int f(int n, int k)
|
||||
{
|
||||
if ((n==k)||(k==0))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return f(n-1,k-1)+f(n-1,k);
|
||||
}
|
||||
}
|
@@ -1,32 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int i=1; // i 为全局变量,具有静态生存期。
|
||||
int main(void)
|
||||
{
|
||||
static int a; // 静态局部变量,有全局寿命,局部可见。
|
||||
int b=-10; // b, c为局部变量,具有动态生存期。
|
||||
int c=0;
|
||||
void other(void);
|
||||
cout<<"---MAIN---\n";
|
||||
cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl;//1 0 -10 0
|
||||
c=c+8; other();// 33 4 0 15
|
||||
cout<<"---MAIN---\n";
|
||||
cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl;//33 0 -10 8
|
||||
i=i+10; other(); //75 6 4 15
|
||||
other(); //107 8 6 15
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
void other(void)
|
||||
{
|
||||
static int a=2;
|
||||
static int b;
|
||||
// a,b为静态局部变量,具有全局寿命,局部可见。
|
||||
//只第一次进入函数时被初始化。
|
||||
int c=10; // C为局部变量,具有动态生存期
|
||||
//每次进入函数时都初始化。
|
||||
a=a+2; i=i+32; c=c+5;
|
||||
cout<<"---OTHER---\n";
|
||||
cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl;
|
||||
b=a;
|
||||
}
|
38
practical_exercises/10_day_practice/day3/BUILD
Normal file
38
practical_exercises/10_day_practice/day3/BUILD
Normal file
@@ -0,0 +1,38 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day3:static_member1`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day3:swap`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day3:pratice`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day3:static_member2`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day3:predeclare`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day3:inline`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day3:static_data`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "static_member1",
|
||||
srcs = ["static_member1.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "swap",
|
||||
srcs = ["swap.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "pratice",
|
||||
srcs = ["pratice.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "static_member2",
|
||||
srcs = ["static_member2.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "predeclare",
|
||||
srcs = ["predeclare.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "inline",
|
||||
srcs = ["inline.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "static_data",
|
||||
srcs = ["static_data.cpp"],
|
||||
)
|
15
practical_exercises/10_day_practice/day3/inline.cpp
Normal file
15
practical_exercises/10_day_practice/day3/inline.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
/* 内联函数.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
//函数声明
|
||||
inline double CalArea(double radius);
|
||||
int main(int argc, char const *argv[]) {
|
||||
double r(3.0);
|
||||
double area;
|
||||
area = CalArea(r);
|
||||
cout << area << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
//加关键字inline
|
||||
inline double CalArea(double radius) { return 3.14 * radius * radius; }
|
52
practical_exercises/10_day_practice/day3/pratice.cpp
Normal file
52
practical_exercises/10_day_practice/day3/pratice.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
/* 函数综合练习题.cpp */
|
||||
/*
|
||||
一圆型游泳池如图所示,现在需在其周围建一圆型过道,并在其四周围上栅栏。栅栏价格为35元/米,过道造价为20元/平方米。
|
||||
过道宽度为3米,游泳池半径由键盘输入。要求编程计算并输出过道和栅栏的造价。
|
||||
|
||||
图形描述:大圆嵌套小圆:
|
||||
小圆在大圆中间,小圆为游泳池,大圆与小圆间隔为过道。
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
const float PI = 3.14159;
|
||||
const float FencePrice = 35;
|
||||
const float ConcretePrice = 20;
|
||||
|
||||
class Circle {
|
||||
private:
|
||||
float radius;
|
||||
|
||||
public:
|
||||
Circle(float r);
|
||||
float Circumference() const;
|
||||
float Area() const;
|
||||
};
|
||||
Circle::Circle(float r) { radius = r; }
|
||||
|
||||
// 计算圆的周长
|
||||
float Circle::Circumference() const { return 2 * PI * radius; }
|
||||
|
||||
// 计算圆的面积
|
||||
float Circle::Area() const { return PI * radius * radius; }
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
float radius;
|
||||
float FenceCost, ConcreteCost;
|
||||
|
||||
// 提示用户输入半径
|
||||
cout << "Enter the radius of the pool: ";
|
||||
cin >> radius;
|
||||
|
||||
// 声明 Circle 对象
|
||||
Circle Pool(radius);
|
||||
Circle PoolRim(radius + 3);
|
||||
// 计算栅栏造价并输出
|
||||
FenceCost = PoolRim.Circumference() * FencePrice;
|
||||
cout << "Fencing Cost is ¥" << FenceCost << endl;
|
||||
|
||||
// 计算过道造价并输出
|
||||
ConcreteCost = (PoolRim.Area() - Pool.Area()) * ConcretePrice;
|
||||
cout << "Concrete Cost is ¥" << ConcreteCost << endl;
|
||||
|
||||
return 0;
|
||||
}
|
@@ -1,3 +1,4 @@
|
||||
/* 类前向声明.cpp */
|
||||
/*
|
||||
使用前向引用声明虽然可以解决一些问题,但它并不是万能的。需要注意的是,
|
||||
尽管使用了前向引用声明,但是在提供一个完整的类声明之前,不能声明该类的对象,
|
||||
@@ -5,35 +6,35 @@
|
||||
*/
|
||||
|
||||
//第一种
|
||||
#include<iostream>
|
||||
class Fred; //前向引用声明
|
||||
#include <iostream>
|
||||
class Fred; //前向引用声明
|
||||
class Barney {
|
||||
Fred x; //错误:类Fred的声明尚不完善
|
||||
Fred x; //错误:类Fred的声明尚不完善
|
||||
};
|
||||
class Fred {
|
||||
Barney y;
|
||||
Barney y;
|
||||
};
|
||||
|
||||
|
||||
//第二种
|
||||
class Fred; //前向引用声明
|
||||
|
||||
class Fred; //前向引用声明
|
||||
|
||||
class Barney {
|
||||
public:
|
||||
void method()
|
||||
{
|
||||
x->yabbaDabbaDo(); //错误:Fred类的对象在定义之前被使用
|
||||
}
|
||||
private:
|
||||
Fred* x; //正确,经过前向引用声明,可以声明Fred类的对象指针
|
||||
};
|
||||
|
||||
public:
|
||||
void method() {
|
||||
x->yabbaDabbaDo(); //错误:Fred类的对象在定义之前被使用
|
||||
}
|
||||
|
||||
private:
|
||||
Fred *x; //正确,经过前向引用声明,可以声明Fred类的对象指针
|
||||
};
|
||||
|
||||
class Fred {
|
||||
public:
|
||||
void yabbaDabbaDo();
|
||||
private:
|
||||
Barney* y;
|
||||
};
|
||||
public:
|
||||
void yabbaDabbaDo();
|
||||
|
||||
private:
|
||||
Barney *y;
|
||||
};
|
||||
|
||||
/*
|
||||
总结:当使用前向引用声明时,只能使用被声明的符号,而不能涉及类的任何细节。
|
44
practical_exercises/10_day_practice/day3/static_data.cpp
Normal file
44
practical_exercises/10_day_practice/day3/static_data.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/* 静态数据成员.cpp */
|
||||
/*
|
||||
学习知识:
|
||||
静态数据成员
|
||||
用关键字static声明
|
||||
该类的所有对象维护该成员的同一个拷贝
|
||||
必须在类外定义和初始化,用(::)来指明所属的类。
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Point {
|
||||
public:
|
||||
Point(int xx = 0, int yy = 0) {
|
||||
X = xx;
|
||||
Y = yy;
|
||||
countP++;
|
||||
}
|
||||
Point(Point &p);
|
||||
int GetX() { return X; }
|
||||
int GetY() { return Y; }
|
||||
void GetC() { cout << " Object id=" << countP << endl; }
|
||||
|
||||
private:
|
||||
int X, Y;
|
||||
//静态数据成员,必须在外部定义和初始化,内部不能直接初始化!
|
||||
static int countP;
|
||||
};
|
||||
Point::Point(Point &p) {
|
||||
X = p.X;
|
||||
Y = p.Y;
|
||||
countP++;
|
||||
}
|
||||
//必须在类外定义和初始化,用(::)来指明所属的类。
|
||||
int Point::countP = 0;
|
||||
int main() {
|
||||
Point A(4, 5);
|
||||
cout << "Point A," << A.GetX() << "," << A.GetY();
|
||||
A.GetC();
|
||||
Point B(A);
|
||||
cout << "Point B," << B.GetX() << "," << B.GetY();
|
||||
B.GetC();
|
||||
|
||||
return 0;
|
||||
}
|
27
practical_exercises/10_day_practice/day3/static_member1.cpp
Normal file
27
practical_exercises/10_day_practice/day3/static_member1.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
/* 静态成员函数1.cpp */
|
||||
/*
|
||||
知识点:
|
||||
静态成员函数
|
||||
类外代码可以使用类名和作用域操作符来调用静态成员函数。
|
||||
静态成员函数只能引用属于该类的静态数据成员或静态成员函数。
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Application {
|
||||
public:
|
||||
static void f();
|
||||
static void g();
|
||||
|
||||
private:
|
||||
static int global;
|
||||
};
|
||||
int Application::global = 0;
|
||||
void Application::f() { global = 5; }
|
||||
void Application::g() { cout << global << endl; }
|
||||
|
||||
int main() {
|
||||
Application::f();
|
||||
Application::g();
|
||||
|
||||
return 0;
|
||||
}
|
23
practical_exercises/10_day_practice/day3/static_member2.cpp
Normal file
23
practical_exercises/10_day_practice/day3/static_member2.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
/* 静态成员函数2.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class A {
|
||||
public:
|
||||
static void f(A a);
|
||||
|
||||
private:
|
||||
int x;
|
||||
};
|
||||
void A::f(A a) {
|
||||
|
||||
//静态成员函数只能引用属于该类的静态数据成员或静态成员函数。
|
||||
// cout<<x; //对x的引用是错误的
|
||||
cout << a.x; //正确
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
A a;
|
||||
a.f(A());
|
||||
|
||||
return 0;
|
||||
}
|
19
practical_exercises/10_day_practice/day3/swap.cpp
Normal file
19
practical_exercises/10_day_practice/day3/swap.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
/* 两数交换.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
void swap(int &a, int &b);
|
||||
int main(int argc, char const *argv[]) {
|
||||
int x1(5);
|
||||
int x2(7);
|
||||
swap(x1, x2);
|
||||
cout << x1 << " " << x2 << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void swap(int &a, int &b) {
|
||||
int t;
|
||||
t = a;
|
||||
a = b;
|
||||
b = t;
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
void swap(int & a, int & b);
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int x1(5);
|
||||
int x2(7);
|
||||
swap(x1,x2);
|
||||
cout<<x1<<" "<<x2<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void swap(int & a, int & b)
|
||||
{
|
||||
int t;
|
||||
t = a;
|
||||
a = b;
|
||||
b = t;
|
||||
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
//函数声明
|
||||
inline double CalArea(double radius);
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
double r(3.0);
|
||||
double area;
|
||||
area = CalArea(r);
|
||||
cout<<area<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
//加关键字inline
|
||||
inline double CalArea(double radius)
|
||||
{
|
||||
return 3.14*radius*radius;
|
||||
}
|
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
一圆型游泳池如图所示,现在需在其周围建一圆型过道,并在其四周围上栅栏。栅栏价格为35元/米,过道造价为20元/平方米。
|
||||
过道宽度为3米,游泳池半径由键盘输入。要求编程计算并输出过道和栅栏的造价。
|
||||
|
||||
图形描述:大圆嵌套小圆:
|
||||
小圆在大圆中间,小圆为游泳池,大圆与小圆间隔为过道。
|
||||
*/
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
const float PI=3.14159;
|
||||
const float FencePrice=35;
|
||||
const float ConcretePrice=20;
|
||||
|
||||
class Circle
|
||||
{
|
||||
private:
|
||||
float radius;
|
||||
public:
|
||||
Circle(float r);
|
||||
float Circumference() const;
|
||||
float Area() const;
|
||||
};
|
||||
Circle::Circle(float r)
|
||||
{
|
||||
radius=r;
|
||||
}
|
||||
|
||||
// 计算圆的周长
|
||||
float Circle::Circumference() const
|
||||
{
|
||||
return 2 * PI * radius;
|
||||
}
|
||||
|
||||
// 计算圆的面积
|
||||
float Circle::Area() const
|
||||
{
|
||||
return PI * radius * radius;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
float radius;
|
||||
float FenceCost, ConcreteCost;
|
||||
|
||||
// 提示用户输入半径
|
||||
cout<<"Enter the radius of the pool: ";
|
||||
cin>>radius;
|
||||
|
||||
// 声明 Circle 对象
|
||||
Circle Pool(radius);
|
||||
Circle PoolRim(radius + 3);
|
||||
// 计算栅栏造价并输出
|
||||
FenceCost = PoolRim.Circumference() * FencePrice;
|
||||
cout << "Fencing Cost is ¥" << FenceCost << endl;
|
||||
|
||||
// 计算过道造价并输出
|
||||
ConcreteCost = (PoolRim.Area() - Pool.Area())*ConcretePrice;
|
||||
cout << "Concrete Cost is ¥" << ConcreteCost << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
知识点:
|
||||
静态成员函数
|
||||
类外代码可以使用类名和作用域操作符来调用静态成员函数。
|
||||
静态成员函数只能引用属于该类的静态数据成员或静态成员函数。
|
||||
*/
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
class Application
|
||||
{
|
||||
public:
|
||||
static void f();
|
||||
static void g();
|
||||
private:
|
||||
static int global;
|
||||
};
|
||||
int Application::global=0;
|
||||
void Application::f()
|
||||
{ global=5;}
|
||||
void Application::g()
|
||||
{ cout<<global<<endl;}
|
||||
|
||||
int main()
|
||||
{
|
||||
Application::f();
|
||||
Application::g();
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
@@ -1,24 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
class A
|
||||
{
|
||||
public:
|
||||
static void f(A a);
|
||||
private:
|
||||
int x;
|
||||
};
|
||||
void A::f(A a)
|
||||
{
|
||||
|
||||
//静态成员函数只能引用属于该类的静态数据成员或静态成员函数。
|
||||
// cout<<x; //对x的引用是错误的
|
||||
cout<<a.x; //正确
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
A a;
|
||||
a.f(A());
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
学习知识:
|
||||
静态数据成员
|
||||
用关键字static声明
|
||||
该类的所有对象维护该成员的同一个拷贝
|
||||
必须在类外定义和初始化,用(::)来指明所属的类。
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Point
|
||||
{
|
||||
public:
|
||||
Point(int xx=0, int yy=0) {X=xx; Y=yy; countP++; }
|
||||
Point(Point &p);
|
||||
int GetX() {return X;}
|
||||
int GetY() {return Y;}
|
||||
void GetC() {cout<<" Object id="<<countP<<endl;}
|
||||
private:
|
||||
int X,Y;
|
||||
//静态数据成员,必须在外部定义和初始化,内部不能直接初始化!
|
||||
static int countP;
|
||||
};
|
||||
Point::Point(Point &p)
|
||||
{ X=p.X;
|
||||
Y=p.Y;
|
||||
countP++;
|
||||
}
|
||||
//必须在类外定义和初始化,用(::)来指明所属的类。
|
||||
int Point::countP=0;
|
||||
int main()
|
||||
{ Point A(4,5);
|
||||
cout<<"Point A,"<<A.GetX()<<","<<A.GetY();
|
||||
A.GetC();
|
||||
Point B(A);
|
||||
cout<<"Point B,"<<B.GetX()<<","<<B.GetY();
|
||||
B.GetC();
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
13
practical_exercises/10_day_practice/day4/clock/BUILD
Normal file
13
practical_exercises/10_day_practice/day4/clock/BUILD
Normal file
@@ -0,0 +1,13 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day4/clock:operator`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day4/clock:operator_plus`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "operator",
|
||||
srcs = ["operator.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "operator_plus",
|
||||
srcs = ["operator_plus.cpp"],
|
||||
)
|
25
practical_exercises/10_day_practice/day4/clock/operator.cpp
Normal file
25
practical_exercises/10_day_practice/day4/clock/operator.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
/* 重载()的时钟.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Time {
|
||||
private:
|
||||
int hh, mm, ss;
|
||||
|
||||
public:
|
||||
Time(int h = 0, int m = 0, int s = 0) : hh(h), mm(m), ss(s) {}
|
||||
void operator()(int h, int m, int s) {
|
||||
hh = h;
|
||||
mm = m;
|
||||
ss = s;
|
||||
}
|
||||
void ShowTime() { cout << hh << ":" << mm << ":" << ss << endl; }
|
||||
};
|
||||
int main() {
|
||||
Time t1(12, 10, 11);
|
||||
t1.ShowTime();
|
||||
t1.operator()(23, 20, 34);
|
||||
t1.ShowTime();
|
||||
t1(10, 10, 10);
|
||||
t1.ShowTime();
|
||||
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
/* 重载++的时钟.cpp */
|
||||
/*
|
||||
设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算,加时1秒,但要使计时过程能够自动进位。
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Time {
|
||||
public:
|
||||
Time(int h = 0, int m = 0, int s = 0) {
|
||||
hour = h;
|
||||
minute = m;
|
||||
second = s;
|
||||
}
|
||||
Time operator++();
|
||||
Time operator++(int);
|
||||
void showTime() {
|
||||
cout << "当前时间为:" << hour << ":" << minute << ":" << second << endl;
|
||||
}
|
||||
|
||||
private:
|
||||
int hour, minute, second;
|
||||
};
|
||||
Time Time::operator++(int n) {
|
||||
Time tmp = *this;
|
||||
++(*this);
|
||||
return tmp;
|
||||
}
|
||||
Time Time::operator++() {
|
||||
++second;
|
||||
if (second == 60) {
|
||||
second = 0;
|
||||
++minute;
|
||||
if (minute == 60) {
|
||||
minute = 0;
|
||||
hour++;
|
||||
if (hour == 24) {
|
||||
hour = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
Time t(23, 59, 59);
|
||||
++t;
|
||||
t.showTime();
|
||||
(t++).showTime();
|
||||
t.showTime();
|
||||
|
||||
return 0;
|
||||
}
|
13
practical_exercises/10_day_practice/day4/const/BUILD
Normal file
13
practical_exercises/10_day_practice/day4/const/BUILD
Normal file
@@ -0,0 +1,13 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day4/const:obj_func`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day4/const:obj_ref`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "obj_func",
|
||||
srcs = ["obj_func.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "obj_ref",
|
||||
srcs = ["obj_ref.cpp"],
|
||||
)
|
40
practical_exercises/10_day_practice/day4/const/obj_func.cpp
Normal file
40
practical_exercises/10_day_practice/day4/const/obj_func.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class R {
|
||||
public:
|
||||
R(int r1, int r2) {
|
||||
R1 = r1;
|
||||
R2 = r2;
|
||||
}
|
||||
// const区分成员重载函数
|
||||
void print();
|
||||
void print() const;
|
||||
|
||||
private:
|
||||
int R1, R2;
|
||||
};
|
||||
/*
|
||||
常成员函数说明格式:类型说明符 函数名(参数表)const;
|
||||
这里,const是函数类型的一个组成部分,因此在实现部分也要带const关键字。
|
||||
const关键字可以被用于参与对重载函数的区分
|
||||
通过常对象只能调用它的常成员函数
|
||||
*/
|
||||
|
||||
void R::print() {
|
||||
cout << "普通调用" << endl;
|
||||
cout << R1 << ":" << R2 << endl;
|
||||
}
|
||||
//实例化也需要带上
|
||||
void R::print() const {
|
||||
cout << "常对象调用" << endl;
|
||||
cout << R1 << ";" << R2 << endl;
|
||||
}
|
||||
int main() {
|
||||
R a(5, 4);
|
||||
a.print(); //调用void print()
|
||||
//通过常对象只能调用它的常成员函数
|
||||
const R b(20, 52);
|
||||
b.print(); //调用void print() const
|
||||
|
||||
return 0;
|
||||
}
|
27
practical_exercises/10_day_practice/day4/const/obj_ref.cpp
Normal file
27
practical_exercises/10_day_practice/day4/const/obj_ref.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
void display(const double &r);
|
||||
|
||||
class A {
|
||||
public:
|
||||
A(int i, int j) {
|
||||
x = i;
|
||||
y = j;
|
||||
}
|
||||
|
||||
private:
|
||||
int x, y;
|
||||
};
|
||||
int main() {
|
||||
double d(9.5);
|
||||
display(d);
|
||||
A const a(3, 4); // a是常对象,不能被更新
|
||||
|
||||
return 0;
|
||||
}
|
||||
void display(const double &r)
|
||||
//常引用做形参,在函数中不能更新 r所引用的对象。
|
||||
{
|
||||
cout << r << endl;
|
||||
}
|
@@ -1,24 +0,0 @@
|
||||
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
void display(const double& r);
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
A(int i,int j) {x=i; y=j;}
|
||||
private:
|
||||
int x,y;
|
||||
};
|
||||
int main()
|
||||
{ double d(9.5);
|
||||
display(d);
|
||||
A const a(3,4); //a是常对象,不能被更新
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
void display(const double& r)
|
||||
//常引用做形参,在函数中不能更新 r所引用的对象。
|
||||
{ cout<<r<<endl; }
|
||||
|
||||
|
@@ -1,41 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
class R
|
||||
{ public:
|
||||
R(int r1, int r2){R1=r1;R2=r2;}
|
||||
//const区分成员重载函数
|
||||
void print();
|
||||
void print() const;
|
||||
private:
|
||||
int R1,R2;
|
||||
};
|
||||
/*
|
||||
常成员函数说明格式:类型说明符 函数名(参数表)const;
|
||||
这里,const是函数类型的一个组成部分,因此在实现部分也要带const关键字。
|
||||
const关键字可以被用于参与对重载函数的区分
|
||||
通过常对象只能调用它的常成员函数
|
||||
*/
|
||||
|
||||
void R::print()
|
||||
{
|
||||
cout<<"普通调用"<<endl;
|
||||
cout<<R1<<":"<<R2<<endl;
|
||||
}
|
||||
//实例化也需要带上
|
||||
void R::print() const
|
||||
{
|
||||
cout<<"常对象调用"<<endl;
|
||||
cout<<R1<<";"<<R2<<endl;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
R a(5,4);
|
||||
a.print(); //调用void print()
|
||||
//通过常对象只能调用它的常成员函数
|
||||
const R b(20,52);
|
||||
b.print(); //调用void print() const
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
16
practical_exercises/10_day_practice/day4/copy_ctor/BUILD
Normal file
16
practical_exercises/10_day_practice/day4/copy_ctor/BUILD
Normal file
@@ -0,0 +1,16 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day4/copy_ctor:clock`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
|
||||
|
||||
cc_library(
|
||||
name = "clock",
|
||||
hdrs = ["clock.h"],
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "main",
|
||||
srcs = ["clock.cpp"],
|
||||
deps = [
|
||||
":clock",
|
||||
],
|
||||
)
|
38
practical_exercises/10_day_practice/day4/copy_ctor/clock.cpp
Normal file
38
practical_exercises/10_day_practice/day4/copy_ctor/clock.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "clock.h"
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
Clock::Clock(int NewH, int NewM, int NewS) {
|
||||
this->Hour = NewH;
|
||||
this->Minute = NewM;
|
||||
this->Second = NewS;
|
||||
}
|
||||
Clock::Clock(Clock &c) {
|
||||
this->Hour = c.Hour;
|
||||
this->Minute = c.Minute;
|
||||
this->Second = c.Second;
|
||||
}
|
||||
void Clock::SetTime(int NewH, int NewM, int NewS) {
|
||||
//加不加this指针都一样
|
||||
this->Hour = NewH;
|
||||
this->Minute = NewM;
|
||||
this->Second = NewS;
|
||||
}
|
||||
void Clock::ShowTime() {
|
||||
cout << this->Hour << endl;
|
||||
cout << this->Minute << endl;
|
||||
cout << this->Second << endl;
|
||||
}
|
||||
//析构函数
|
||||
Clock::~Clock() {}
|
||||
int main(int argc, char const *argv[]) {
|
||||
Clock c(0, 0, 0);
|
||||
|
||||
c.SetTime(10, 20, 30);
|
||||
c.ShowTime();
|
||||
//拷贝构造函数调用
|
||||
Clock c1(c);
|
||||
c1.ShowTime();
|
||||
c1.SetTime(90, 98, 99);
|
||||
c1.ShowTime();
|
||||
return 0;
|
||||
}
|
26
practical_exercises/10_day_practice/day4/copy_ctor/clock.h
Normal file
26
practical_exercises/10_day_practice/day4/copy_ctor/clock.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef CLOCK
|
||||
#define CLOCK
|
||||
class Clock {
|
||||
public:
|
||||
Clock(int NewH, int NewM, int NewS);
|
||||
Clock(
|
||||
Clock &
|
||||
c); //拷贝构造函数,如果不加,编译器会自动生成一个拷贝构造函数,因此加不加都可以直接使用。
|
||||
void SetTime(int NewH, int NewM, int NewS);
|
||||
void ShowTime();
|
||||
~Clock(); //析构函数,编译器会自动产生一个默认的析构函数。
|
||||
private:
|
||||
int Hour, Minute, Second;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
#ifndef 标识符
|
||||
程序段1
|
||||
#else
|
||||
程序段2
|
||||
#endif
|
||||
如果“标识符”未被定义过,则编译程序段1,否则编译程序段2。
|
||||
|
||||
*/
|
13
practical_exercises/10_day_practice/day4/friend/BUILD
Normal file
13
practical_exercises/10_day_practice/day4/friend/BUILD
Normal file
@@ -0,0 +1,13 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day4/friend:class`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day4/friend:func`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "class",
|
||||
srcs = ["class.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "func",
|
||||
srcs = ["func.cpp"],
|
||||
)
|
@@ -1,4 +1,4 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
/*
|
||||
若一个类为另一个类的友元,则此类的所有成员都能访问对方类的私有成员。
|
||||
@@ -9,39 +9,33 @@ using namespace std;
|
||||
如果声明B类是A类的友元,B类的成员函数就可以访问A类的私有和保护数据,
|
||||
但A类的成员函数却不能访问B类的私有、保护数据。
|
||||
*/
|
||||
class A{
|
||||
class A {
|
||||
friend class B;
|
||||
public:
|
||||
void Display(){
|
||||
cout<<x<<endl;
|
||||
}
|
||||
private:
|
||||
int x;
|
||||
};
|
||||
class B
|
||||
{ public:
|
||||
void Set(int i);
|
||||
void Display();
|
||||
private:
|
||||
A a;
|
||||
};
|
||||
void B::Set(int i)
|
||||
{
|
||||
a.x=i;
|
||||
}
|
||||
void B::Display()
|
||||
{
|
||||
a.Display();
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
B b;
|
||||
b.Set(10);
|
||||
b.Display();
|
||||
public:
|
||||
void Display() { cout << x << endl; }
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
private:
|
||||
int x;
|
||||
};
|
||||
class B {
|
||||
public:
|
||||
void Set(int i);
|
||||
void Display();
|
||||
|
||||
private:
|
||||
A a;
|
||||
};
|
||||
void B::Set(int i) { a.x = i; }
|
||||
void B::Display() { a.Display(); }
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
B b;
|
||||
b.Set(10);
|
||||
b.Display();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
27
practical_exercises/10_day_practice/day4/friend/func.cpp
Normal file
27
practical_exercises/10_day_practice/day4/friend/func.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
//使用友元函数计算两点间距离
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Point {
|
||||
public:
|
||||
Point(int x = 0, int y = 0) : X(x), Y(y) {}
|
||||
int GetX() { return X; }
|
||||
int GetY() { return Y; }
|
||||
friend float Distance(Point &a, Point &b);
|
||||
|
||||
private:
|
||||
int X, Y; //私有数据成员
|
||||
};
|
||||
//通过将一个模块声明为另一个模块的友元,一个模块能够引用到另一个模块中本是被隐藏的信息。
|
||||
float Distance(Point &a, Point &b) {
|
||||
double dx = a.X - b.X;
|
||||
double dy = a.Y - b.Y;
|
||||
return sqrt(dx * dx + dy * dy);
|
||||
}
|
||||
|
||||
int main() {
|
||||
Point p1(3.0, 5.0), p2(4.0, 6.0);
|
||||
cout << "两点距离为:" << Distance(p1, p2) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
@@ -1,31 +0,0 @@
|
||||
//使用友元函数计算两点间距离
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
class Point{
|
||||
public:
|
||||
Point(int x=0,int y=0):X(x),Y(y){}
|
||||
int GetX(){
|
||||
return X;
|
||||
}
|
||||
int GetY(){
|
||||
return Y;
|
||||
}
|
||||
friend float Distance(Point &a,Point &b);
|
||||
private:
|
||||
int X,Y;//私有数据成员
|
||||
};
|
||||
//通过将一个模块声明为另一个模块的友元,一个模块能够引用到另一个模块中本是被隐藏的信息。
|
||||
float Distance(Point &a, Point &b){
|
||||
double dx = a.X-b.X;
|
||||
double dy = a.Y-b.Y;
|
||||
return sqrt(dx*dx+dy*dy);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Point p1(3.0,5.0),p2(4.0,6.0);
|
||||
cout<<"两点距离为:"<<Distance(p1,p2)<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@@ -1,47 +0,0 @@
|
||||
#include<iostream>
|
||||
#include"clock.h"
|
||||
using namespace std;
|
||||
Clock::Clock(int NewH,int NewM,int NewS)
|
||||
{
|
||||
this->Hour=NewH;
|
||||
this->Minute=NewM;
|
||||
this->Second=NewS;
|
||||
}
|
||||
Clock::Clock(Clock &c)
|
||||
{
|
||||
this->Hour=c.Hour;
|
||||
this->Minute=c.Minute;
|
||||
this->Second=c.Second;
|
||||
}
|
||||
void Clock::SetTime(int NewH,int NewM,int NewS)
|
||||
{
|
||||
//加不加this指针都一样
|
||||
this->Hour=NewH;
|
||||
this->Minute=NewM;
|
||||
this->Second=NewS;
|
||||
}
|
||||
void Clock::ShowTime()
|
||||
{
|
||||
cout<<this->Hour<<endl;
|
||||
cout<<this->Minute<<endl;
|
||||
cout<<this->Second<<endl;
|
||||
}
|
||||
//析构函数
|
||||
Clock::~Clock()
|
||||
{
|
||||
|
||||
}
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
Clock c(0,0,0);
|
||||
|
||||
c.SetTime(10,20,30);
|
||||
c.ShowTime();
|
||||
//拷贝构造函数调用
|
||||
Clock c1(c);
|
||||
c1.ShowTime();
|
||||
c1.SetTime(90,98,99);
|
||||
c1.ShowTime();
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
#ifndef CLOCK
|
||||
# define CLOCK
|
||||
class Clock
|
||||
{
|
||||
public:
|
||||
Clock(int NewH,int NewM,int NewS);
|
||||
Clock(Clock &c);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><ECBAAF>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˼Ӳ<CBBC><D3B2>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ʹ<EFBFBD>á<EFBFBD>
|
||||
void SetTime(int NewH,int NewM,int NewS);
|
||||
void ShowTime();
|
||||
~Clock();//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ĭ<EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
private:
|
||||
int Hour,Minute,Second;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
#ifndef <20><>ʶ<EFBFBD><CAB6>
|
||||
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1
|
||||
#else
|
||||
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2
|
||||
#endif
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD>
|
||||
|
||||
*/
|
@@ -1,25 +0,0 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Time{
|
||||
private:
|
||||
int hh,mm,ss;
|
||||
public:
|
||||
Time(int h=0,int m=0,int s=0):hh(h),mm(m),ss(s){}
|
||||
void operator()(int h,int m,int s) {
|
||||
hh=h;
|
||||
mm=m;
|
||||
ss=s;
|
||||
}
|
||||
void ShowTime(){
|
||||
cout<<hh<<":"<<mm<<":"<<ss<<endl;
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
Time t1(12,10,11);
|
||||
t1.ShowTime();
|
||||
t1.operator()(23,20,34);
|
||||
t1.ShowTime();
|
||||
t1(10,10,10);
|
||||
t1.ShowTime();
|
||||
system("pause");
|
||||
}
|
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算,加时1秒,但要使计时过程能够自动进位。
|
||||
*/
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
class Time{
|
||||
public:
|
||||
Time(int h=0,int m=0,int s=0){
|
||||
hour = h;
|
||||
minute = m;
|
||||
second = s;
|
||||
}
|
||||
Time operator++();
|
||||
Time operator++(int);
|
||||
void showTime(){
|
||||
cout<<"当前时间为:"<<hour<<":"<<minute<<":"<<second<<endl;
|
||||
}
|
||||
|
||||
private:
|
||||
int hour,minute,second;
|
||||
|
||||
};
|
||||
Time Time::operator++(int n){
|
||||
Time tmp=*this;
|
||||
++(*this);
|
||||
return tmp;
|
||||
}
|
||||
Time Time::operator++(){
|
||||
++second;
|
||||
if(second==60){
|
||||
second=0;
|
||||
++minute;
|
||||
if(minute==60){
|
||||
minute=0;
|
||||
hour++;
|
||||
if(hour==24){
|
||||
hour=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
Time t(23,59,59);
|
||||
++t;
|
||||
t.showTime();
|
||||
(t++).showTime();
|
||||
t.showTime();
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
33
practical_exercises/10_day_practice/day5/ctor_dtor/BUILD
Normal file
33
practical_exercises/10_day_practice/day5/ctor_dtor/BUILD
Normal file
@@ -0,0 +1,33 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:param`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:noctor`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:cseq`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:ctor`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:ctor_d`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:seq`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "param",
|
||||
srcs = ["param.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "noctor",
|
||||
srcs = ["noctor.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "cseq",
|
||||
srcs = ["cseq.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "ctor",
|
||||
srcs = ["ctor.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "ctor_d",
|
||||
srcs = ["ctor_d.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "seq",
|
||||
srcs = ["seq.cpp"],
|
||||
)
|
41
practical_exercises/10_day_practice/day5/ctor_dtor/cseq.cpp
Normal file
41
practical_exercises/10_day_practice/day5/ctor_dtor/cseq.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
先构造成员
|
||||
再构造自身(调用构造函数)
|
||||
*/
|
||||
|
||||
class A {
|
||||
public:
|
||||
A() { cout << "Constructing A" << endl; }
|
||||
~A() { cout << "Destructing A" << endl; }
|
||||
};
|
||||
class B {
|
||||
public:
|
||||
B() { cout << "Constructing B" << endl; }
|
||||
~B() { cout << "Destructing B" << endl; }
|
||||
};
|
||||
|
||||
class C {
|
||||
public:
|
||||
C() { cout << "Constructing C" << endl; }
|
||||
~C() { cout << "Destructing C" << endl; }
|
||||
B b;
|
||||
A a;
|
||||
};
|
||||
|
||||
int main() {
|
||||
C c;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
执行结果:
|
||||
Constructing B
|
||||
Constructing A
|
||||
Constructing C
|
||||
Destructing C
|
||||
Destructing A
|
||||
Destructing B
|
||||
*/
|
29
practical_exercises/10_day_practice/day5/ctor_dtor/ctor.cpp
Normal file
29
practical_exercises/10_day_practice/day5/ctor_dtor/ctor.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Base {
|
||||
private:
|
||||
int x;
|
||||
|
||||
public:
|
||||
Base(int a) {
|
||||
x = a;
|
||||
cout << "Base constructor x=" << x << endl;
|
||||
}
|
||||
~Base() { cout << "Base destructor..." << endl; }
|
||||
};
|
||||
class Derived : public Base {
|
||||
private:
|
||||
int y;
|
||||
|
||||
public:
|
||||
Derived(int a, int b) : Base(a) { //派生类构造函数的初始化列表
|
||||
y = b;
|
||||
cout << "Derived constructor y=" << y << endl;
|
||||
}
|
||||
~Derived() { cout << "Derived destructor..." << endl; }
|
||||
};
|
||||
int main() {
|
||||
Derived d(1, 2);
|
||||
|
||||
return 0;
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class A {
|
||||
public:
|
||||
A() { cout << "Constructing A" << endl; }
|
||||
~A() { cout << "Destructing A" << endl; }
|
||||
};
|
||||
class B {
|
||||
public:
|
||||
B() { cout << "Constructing B" << endl; }
|
||||
~B() { cout << "Destructing B" << endl; }
|
||||
};
|
||||
class C {
|
||||
public:
|
||||
C() { cout << "Constructing C" << endl; }
|
||||
~C() { cout << "Destructing C" << endl; }
|
||||
};
|
||||
class D : public C {
|
||||
public:
|
||||
D() { cout << "Constructing D" << endl; }
|
||||
~D() { cout << "Destructing D" << endl; }
|
||||
B b;
|
||||
A a;
|
||||
C c;
|
||||
};
|
||||
|
||||
int main() {
|
||||
D d;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
执行结果:
|
||||
Constructing C
|
||||
Constructing B
|
||||
Constructing A
|
||||
Constructing C
|
||||
Constructing D
|
||||
Destructing D
|
||||
Destructing C
|
||||
Destructing A
|
||||
Destructing B
|
||||
Destructing C
|
||||
*/
|
@@ -0,0 +1,15 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class A {
|
||||
public:
|
||||
A() { cout << "Constructing A" << endl; }
|
||||
~A() { cout << "Destructing A" << endl; }
|
||||
};
|
||||
class B : public A {
|
||||
public:
|
||||
~B() { cout << "Destructing B" << endl; }
|
||||
};
|
||||
int main() {
|
||||
B b;
|
||||
|
||||
}
|
27
practical_exercises/10_day_practice/day5/ctor_dtor/param.cpp
Normal file
27
practical_exercises/10_day_practice/day5/ctor_dtor/param.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Point {
|
||||
protected:
|
||||
int x, y;
|
||||
|
||||
public:
|
||||
Point(int a, int b = 0) {
|
||||
x = a;
|
||||
y = b;
|
||||
cout << "constructing point(" << x << "," << y << ")" << endl;
|
||||
}
|
||||
};
|
||||
class Line : public Point {
|
||||
protected:
|
||||
int len;
|
||||
|
||||
public:
|
||||
Line(int a, int b, int l) : Point(a, b) { //构造函数初始化列表
|
||||
len = l;
|
||||
cout << "Constructing Line,len ..." << len << endl;
|
||||
}
|
||||
};
|
||||
int main() {
|
||||
Line L1(1, 2, 3);
|
||||
|
||||
}
|
41
practical_exercises/10_day_practice/day5/ctor_dtor/seq.cpp
Normal file
41
practical_exercises/10_day_practice/day5/ctor_dtor/seq.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
// Eg6-12.cpp
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class A {
|
||||
int x;
|
||||
|
||||
public:
|
||||
A(int i = 0) {
|
||||
x = i;
|
||||
cout << "A-----" << x << endl;
|
||||
}
|
||||
};
|
||||
class B {
|
||||
int y;
|
||||
|
||||
public:
|
||||
B(int i) {
|
||||
y = i;
|
||||
cout << "B-----" << y << endl;
|
||||
}
|
||||
};
|
||||
class C {
|
||||
int z;
|
||||
|
||||
public:
|
||||
C(int i) {
|
||||
z = i;
|
||||
cout << "C-----" << z << endl;
|
||||
}
|
||||
};
|
||||
class D : public B {
|
||||
public:
|
||||
C c1, c2;
|
||||
A *a1 = new A(10);
|
||||
A a0, a4;
|
||||
D() : a4(4), c2(2), c1(1), B(1) { cout << "D-----5" << endl; }
|
||||
};
|
||||
int main() {
|
||||
D d;
|
||||
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day5/inherit_access:public`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day5/inherit_access:protected_inherit`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day5/inherit_access:protected`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day5/inherit_access:private`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "public",
|
||||
srcs = ["public.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "protected_inherit",
|
||||
srcs = ["protected_inherit.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "protected",
|
||||
srcs = ["protected.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "private",
|
||||
srcs = ["private.cpp"],
|
||||
)
|
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class base {
|
||||
int x;
|
||||
|
||||
public:
|
||||
void setx(int n) { x = n; }
|
||||
int getx() { return x; }
|
||||
void showx() { cout << x << endl; }
|
||||
};
|
||||
//派生类
|
||||
class derived : public base {
|
||||
int y;
|
||||
|
||||
public:
|
||||
void sety(int n) { y = n; }
|
||||
void sety() { y = getx(); }
|
||||
void showy() { cout << y << endl; }
|
||||
};
|
||||
//派生类不可直接访问基类的private成员,可通过基类的共有成员函数访问
|
||||
int main() {
|
||||
derived obj;
|
||||
obj.setx(10);
|
||||
obj.sety(20);
|
||||
obj.showx();
|
||||
obj.showy();
|
||||
obj.sety();
|
||||
obj.showx();
|
||||
obj.showy();
|
||||
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
基类中protected的成员
|
||||
类内部:可以访问
|
||||
类的使用者:不能访问
|
||||
类的派生类成员:可以访问
|
||||
*/
|
||||
#include <iostream>
|
||||
class B {
|
||||
private:
|
||||
int i;
|
||||
|
||||
protected:
|
||||
int j;
|
||||
|
||||
public:
|
||||
int k;
|
||||
};
|
||||
class D : public B {
|
||||
public:
|
||||
void f() {
|
||||
i = 1; // cannot access 派生类不可访问基类私有成员
|
||||
j = 2; //派生类可以访问基类保护成员
|
||||
k = 3;
|
||||
}
|
||||
};
|
||||
int main() {
|
||||
B b;
|
||||
b.i = 1; // cannot access 私有成员,类的使用者不能访问
|
||||
b.j = 2; // cannot access 保护成员,类的使用者不能访问
|
||||
b.k = 3;
|
||||
|
||||
return 0;
|
||||
}
|
@@ -5,28 +5,31 @@
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Base{
|
||||
int x;
|
||||
class Base {
|
||||
int x;
|
||||
|
||||
protected:
|
||||
int getx(){ return x; }
|
||||
int getx() { return x; }
|
||||
|
||||
public:
|
||||
void setx(int n){ x=n; }
|
||||
void showx(){ cout<<x<<endl; }
|
||||
void setx(int n) { x = n; }
|
||||
void showx() { cout << x << endl; }
|
||||
};
|
||||
class Derived:protected Base{
|
||||
int y;
|
||||
class Derived : protected Base {
|
||||
int y;
|
||||
|
||||
public:
|
||||
void sety(int n){ y=n; }
|
||||
void sety(){ y=getx();} //访问基类的保护成员
|
||||
void showy(){ cout<<y<<endl; }
|
||||
void sety(int n) { y = n; }
|
||||
void sety() { y = getx(); } //访问基类的保护成员
|
||||
void showy() { cout << y << endl; }
|
||||
};
|
||||
int main(){
|
||||
Derived obj;
|
||||
obj.setx(10); //错误
|
||||
obj.sety(20);
|
||||
obj.showx(); //错误,
|
||||
obj.showy();
|
||||
system("pause");
|
||||
int main() {
|
||||
Derived obj;
|
||||
obj.setx(10); //错误
|
||||
obj.sety(20);
|
||||
obj.showx(); //错误,
|
||||
obj.showy();
|
||||
|
||||
}
|
||||
|
||||
/*
|
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Base {
|
||||
int x;
|
||||
|
||||
public:
|
||||
void setx(int n) { x = n; }
|
||||
int getx() { return x; }
|
||||
void showx() { cout << x << endl; }
|
||||
};
|
||||
//私有继承
|
||||
//基类的中的public成员在派生类中是private, private成员在派生类中不可访问。
|
||||
class derived : private Base {
|
||||
int y;
|
||||
|
||||
public:
|
||||
void sety(int n) { y = n; }
|
||||
void sety() { y = getx(); }
|
||||
void showy() { cout << y << endl; }
|
||||
};
|
||||
int main() {
|
||||
derived obj;
|
||||
obj.setx(10); // cannot access
|
||||
obj.sety(20);
|
||||
obj.showx(); // cannot access
|
||||
obj.showy();
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user