Skip to content

Commit

Permalink
提交第一章
Browse files Browse the repository at this point in the history
代码 + txt笔记
  • Loading branch information
AndyHsu-cn committed Mar 23, 2022
1 parent c47b6f1 commit 2a7048e
Show file tree
Hide file tree
Showing 15 changed files with 574 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added chapt1/.DS_Store
Binary file not shown.
14 changes: 14 additions & 0 deletions chapt1/1_01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
#include <string>
using namespace std;

int main()
{
string user_name;

cout << "Please enter your first name: ";
cin >> user_name;
cout << "\nHello, " << "Hello, " << user_name << " ... and goodbye!\n";

return 0;
}
97 changes: 97 additions & 0 deletions chapt1/1_02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
string user_name;
int user_guess;
int num_tries; // 总的猜测数
int num_right; // 总的正确数
double user_score = 0.0;
char user_rsp;
char try_again;
bool next_seq = true; // 显示下一组数列
bool go_for_it = true; // 用户想继续猜一次
bool got_it = false; // 用户是否猜对
const int max_tries = 3;

const int seq_size = 18;
const int max_seq = 6;
int elem_seq[seq_size] = {
1, 2, 3, // Fibonacci
3, 4, 7, // Lucas
2, 5, 12, // Pell
3, 6, 10, // Triangular
4, 9, 16, // Square
5, 12, 22 // Pentagonal
};
string seq_names[max_seq] = {
"Fibonacci",
"Lucas",
"Pell",
"Triangular",
"Square",
"Pentagonal"
};
int cur_index = 0;


// 用户想要继续猜某个数列
while (next_seq == true && cur_index < seq_size)
{
// 显示数列的前两个数字
std::cout << "The first 2 elements of the sequence are: "
<< elem_seq[cur_index] << ", " << elem_seq[cur_index+1] << endl;
std::cout << "What is the next element?";
int tries_cnt = 0;
next_seq = true;
go_for_it = true;
got_it = false;
// 用户所猜不正确 && 用户想要再猜一次
while(!got_it && go_for_it && (++tries_cnt <= max_tries))
{
std::cin >> user_guess;
++num_tries;
// 如果答案正确
if(user_guess == elem_seq[cur_index+2])
{
++num_right;
std::cout << "Very good, yes, " << elem_seq[cur_index+2]
<< " is the next element in the "
<< seq_names[cur_index/3] << " sequence." << endl;
got_it = true;
}else{
// 如果答案错误
switch (tries_cnt)
{
case 1:
std::cout << "Oops, Nice guess but not quite it." << endl;
break;
case 2:
std::cout << "Sorry, Wrong a second time!" << endl;
break;
case 3:
std::cout << "Ah, this is harder than it looks, isn't it?" << endl;
break;
default:
std::cout << "It must be getting pretty frustrating by now!!!" << endl;
break;
}

std::cout << "do you want to continue?(y/n):" << endl;
std::cin >> user_rsp;
if(user_rsp == 'N' || user_rsp == 'n')
go_for_it = false;
}
} // 内层while结束
std::cout << "Want to try another sequence? (y/n)?" << endl;
std::cin >> try_again;
if(try_again == 'N' || try_again == 'n')
next_seq = false;
else
cur_index += 3;
} // 外层while结束

return 0;
}
20 changes: 20 additions & 0 deletions chapt1/1_03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <vector>
using namespace std;

int main()
{
const int seq_size = 18;
vector<int> pell_seq(seq_size);
pell_seq[0] = 1;
pell_seq[1] = 2;
for (int index = 2; index < seq_size; index++)
pell_seq[index] = 2 * pell_seq[index-1] + pell_seq[index-2];

cout << "The first " << pell_seq.size() << " elements of the Pell series:" << endl;
for (int index = 0; index < seq_size; index++)
cout << pell_seq[index] << " ";
cout << endl;

return 0;
}
112 changes: 112 additions & 0 deletions chapt1/1_04.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(void)
{
string user_name;
int user_guess;
int num_tries; // 总的猜测数
int num_right; // 总的正确数
double user_score = 0.0;
char user_rsp;
char try_again;
bool next_seq = true; // 显示下一组数列
bool go_for_it = true; // 用户想继续猜一次
bool got_it = false; // 用户是否猜对
const int max_tries = 3;

const int seq_size = 18;
int elem_seq[seq_size] = {
1, 2, 3, // Fibonacci
3, 4, 7, // Lucas
2, 5, 12, // Pell
3, 6, 10, // Triangular
4, 9, 16, // Square
5, 12, 22 // Pentagonal
};
vector<int> fibonacci(elem_seq, elem_seq+3);
vector<int> Lucas(elem_seq+3, elem_seq+6);
vector<int> Pell(elem_seq+6, elem_seq+9);
vector<int> Triangular(elem_seq+9, elem_seq+12);
vector<int> Square(elem_seq+12, elem_seq+15);
vector<int> Pentagonal(elem_seq+15, elem_seq+18);

const int max_seq = 6;
string seq_names[max_seq] = {
"Fibonacci",
"Lucas",
"Pell",
"Triangular",
"Square",
"Pentagonal"
};
vector<int> * seq_addrs[max_seq] = {
&fibonacci, &Lucas, &Pell,
&Triangular, &Square, &Pentagonal
};

vector<int> *current_vec = 0;
int seq_index;
srand(time(NULL));

// 用户想要继续猜某个数列
while (next_seq == true)
{
seq_index = rand() % max_seq;
current_vec = seq_addrs[seq_index];
std::cout << "The first 2 elements of the sequence are: "
<< (*current_vec)[0] << ", " << (*current_vec)[1] << endl;
std::cout << "What is the next element?";
int tries_cnt = 0;
next_seq = true;
go_for_it = true;
got_it = false;
// 用户所猜不正确 && 用户想要再猜一次
while(!got_it && go_for_it && (++tries_cnt <= max_tries))
{
std::cin >> user_guess;
++num_tries;
// 如果答案正确
if(user_guess == (*current_vec)[2])
{
++num_right;
std::cout << "Very good, yes, " << (*current_vec)[2]
<< " is the next element in the "
<< seq_names[seq_index] << " sequence." << endl;
got_it = true;
}else{
// 如果答案错误
switch (tries_cnt)
{
case 1:
std::cout << "Oops, Nice guess but not quite it." << endl;
break;
case 2:
std::cout << "Sorry, Wrong a second time!" << endl;
break;
case 3:
std::cout << "Ah, this is harder than it looks, isn't it?" << endl;
break;
default:
std::cout << "It must be getting pretty frustrating by now!!!" << endl;
break;
}

std::cout << "do you want to continue?(y/n):" << endl;
std::cin >> user_rsp;
if(user_rsp == 'N' || user_rsp == 'n')
go_for_it = false;
}
} // 内层while结束
std::cout << "Want to try another sequence? (y/n)?" << endl;
std::cin >> try_again;
if(try_again == 'N' || try_again == 'n')
next_seq = false;
} // 外层while结束

return 0;
}
28 changes: 28 additions & 0 deletions chapt1/1_05.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
string name;
int num_tries;
int num_rights;
ofstream outfile("1_05.txt", ios_base::app);

cout << "Enter your name: ";
cin >> name;
cout << "Enter your num_tries, num_rights: ";
cin >> num_tries >>num_rights;
if (!outfile)
{
cerr << "Oops, unable to save session data!" << endl;
}else
{
outfile << name << ' '
<< num_tries << ' '
<< num_rights << ' ' << endl;
}


return 0;
}
25 changes: 25 additions & 0 deletions chapt1/1_06.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
string name;
int num_tries;
int num_rights;
ifstream infile("1_05.txt");

if (!infile)
{
cerr << "Oops, unable to open file!" << endl;
}else
{
infile >> name >> num_tries >> num_rights;
cout << "name: " << name << endl;
cout << "tot: " << num_tries << endl;
cout << "right: " << num_rights << endl;
}


return 0;
}
34 changes: 34 additions & 0 deletions chapt1/1_07.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
string name;
int num_tries;
int num_rights;

fstream iofile("data.txt", ios_base::in | ios_base::app);
if(!iofile)
{
cerr << "Oops, unable to open file!" << endl;

}else
{
iofile << "andy " << 10 << ' ' << 7 << endl;
cout << "______________________" << endl;
string usr_name;
int num_tries = 0;
int num_rights = 0;

// 由于ios_base::app的原因,开始读取之前,请将文件重新定位到起始处
iofile.seekg(0);
iofile >> usr_name >> num_tries >> num_rights;
cout << "name: " << usr_name << endl;
cout << "tot: " << num_tries << endl;
cout << "right: " << num_rights << endl;
}


return 0;
}
Loading

0 comments on commit 2a7048e

Please sign in to comment.