support bazel complie this project and format code.

This commit is contained in:
zhangxing
2023-03-30 00:15:11 +08:00
committed by light-city
parent 1f86192576
commit 7529ae3a55
636 changed files with 10025 additions and 9387 deletions

View 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"],
)

View 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;
}

View 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;
}

View 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);
}
}

View 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;
}

View 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);
}
}

View 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);
}
}

View 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;
}

View 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;
}

View 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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}