diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..8f48b1d Binary files /dev/null and b/.DS_Store differ diff --git a/chapt1/.DS_Store b/chapt1/.DS_Store new file mode 100644 index 0000000..3d22b93 Binary files /dev/null and b/chapt1/.DS_Store differ diff --git a/chapt1/1_01.cpp b/chapt1/1_01.cpp new file mode 100644 index 0000000..5ba2d0b --- /dev/null +++ b/chapt1/1_01.cpp @@ -0,0 +1,14 @@ +#include +#include +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; +} \ No newline at end of file diff --git a/chapt1/1_02.cpp b/chapt1/1_02.cpp new file mode 100644 index 0000000..4f9bc7e --- /dev/null +++ b/chapt1/1_02.cpp @@ -0,0 +1,97 @@ +#include +#include +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; +} \ No newline at end of file diff --git a/chapt1/1_03.cpp b/chapt1/1_03.cpp new file mode 100644 index 0000000..94cd206 --- /dev/null +++ b/chapt1/1_03.cpp @@ -0,0 +1,20 @@ +#include +#include +using namespace std; + +int main() +{ + const int seq_size = 18; + vector 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; +} \ No newline at end of file diff --git a/chapt1/1_04.cpp b/chapt1/1_04.cpp new file mode 100644 index 0000000..2082eda --- /dev/null +++ b/chapt1/1_04.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +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 fibonacci(elem_seq, elem_seq+3); + vector Lucas(elem_seq+3, elem_seq+6); + vector Pell(elem_seq+6, elem_seq+9); + vector Triangular(elem_seq+9, elem_seq+12); + vector Square(elem_seq+12, elem_seq+15); + vector Pentagonal(elem_seq+15, elem_seq+18); + + const int max_seq = 6; + string seq_names[max_seq] = { + "Fibonacci", + "Lucas", + "Pell", + "Triangular", + "Square", + "Pentagonal" + }; + vector * seq_addrs[max_seq] = { + &fibonacci, &Lucas, &Pell, + &Triangular, &Square, &Pentagonal + }; + + vector *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; +} \ No newline at end of file diff --git a/chapt1/1_05.cpp b/chapt1/1_05.cpp new file mode 100644 index 0000000..7a28e95 --- /dev/null +++ b/chapt1/1_05.cpp @@ -0,0 +1,28 @@ +#include +#include +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; +} \ No newline at end of file diff --git a/chapt1/1_06.cpp b/chapt1/1_06.cpp new file mode 100644 index 0000000..89e7173 --- /dev/null +++ b/chapt1/1_06.cpp @@ -0,0 +1,25 @@ +#include +#include +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; +} \ No newline at end of file diff --git a/chapt1/1_07.cpp b/chapt1/1_07.cpp new file mode 100644 index 0000000..672bab6 --- /dev/null +++ b/chapt1/1_07.cpp @@ -0,0 +1,34 @@ +#include +#include +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; +} \ No newline at end of file diff --git a/chapt1/1_08.cpp b/chapt1/1_08.cpp new file mode 100644 index 0000000..c7a6f4f --- /dev/null +++ b/chapt1/1_08.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +#include +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 fibonacci(elem_seq, elem_seq+3); + vector Lucas(elem_seq+3, elem_seq+6); + vector Pell(elem_seq+6, elem_seq+9); + vector Triangular(elem_seq+9, elem_seq+12); + vector Square(elem_seq+12, elem_seq+15); + vector Pentagonal(elem_seq+15, elem_seq+18); + + const int max_seq = 6; + string seq_names[max_seq] = { + "Fibonacci", + "Lucas", + "Pell", + "Triangular", + "Square", + "Pentagonal" + }; + vector * seq_addrs[max_seq] = { + &fibonacci, &Lucas, &Pell, + &Triangular, &Square, &Pentagonal + }; + + vector *current_vec = 0; + int seq_index; + + srand(time(NULL)); + cout << "Enter your name: "; + cin >> user_name; + ifstream infile("seq_data.txt"); + if(!infile) + cerr << "Oops, unable to open file!" << endl; + else + { + string name; + int nt; // 猜的总次数 + int nc; // 猜对的次数 + int find = 0; + while (infile >> name) + { + infile >> nt >> nc; + if(name == user_name) + { + find = 1; + num_tries = nt; + num_right = nc; + } + } + if(find == 1){ + cout << "Welcome back, " << user_name + << "TOT[" << nt << "]" + << "RIGHT[" << nc << "]" + << "\nGood, Luck!" << endl; + }else + cout << "Welcome, freshman " << user_name << endl; + } + + // 用户想要继续猜某个数列 + 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结束 + + ofstream outfile("seq_data.txt", ios_base::app); + if(!outfile) + cerr << "Oops, unable to open file!" << endl; + else + { + outfile << user_name << ' ' + << num_tries << ' ' + << num_right << endl; + } + + + return 0; +} \ No newline at end of file diff --git "a/chapt1/C++\347\274\226\347\250\213\345\237\272\347\241\200.txt" "b/chapt1/C++\347\274\226\347\250\213\345\237\272\347\241\200.txt" new file mode 100644 index 0000000..61ccbce --- /dev/null +++ "b/chapt1/C++\347\274\226\347\250\213\345\237\272\347\241\200.txt" @@ -0,0 +1,28 @@ +p205 附录A +p255 附录B + +01 什么是class + 用户自定义的数据类型(user-defined data type),增强类型抽象化的层次 + class定义应该分为两部分 + (1)头文件(header file)——用来声明该class的各种操作行为 + (2)代码文件(program text)——包含了操作行为的具体实现 + +02 对象初始化方式 + (1)使用=运算符,如int num_tries = 0 + 源自C语言 + (2)构造函数语法,如int num_right(0) + 解决“多值初始化”问题 + 使内置类型与class类型的初始化方式得到统一 + +03 vector的定义 + (1)包含vector头文件 + (2)类名后尖括号内指定元素类型,vector的大小写在变量名后的小括号内, + 如vector vec_name(vec_size); + +04 文件读写 + (0)包含头文件fstream + (1)若写文件,则创建ofstream对象 + 如ofstream outfile("filename");则以覆盖的方式写文件 + 若ofstream outfile("filename", ios_base::app);则以追加的方式写文件 + (2)若读文件,则创建ifstream对象 + 如ifstream infile("filename"); \ No newline at end of file diff --git a/chapt1/exe1.6.cpp b/chapt1/exe1.6.cpp new file mode 100644 index 0000000..65b14a9 --- /dev/null +++ b/chapt1/exe1.6.cpp @@ -0,0 +1,19 @@ +#include +#include +using namespace std; + +int main(void) +{ + vector ivec; + int ival; + int sum = 0; + + while(cin >> ival) + ivec.push_back(ival); + + for (int index = 0; index < ivec.size(); index++) + sum += ivec[index]; + cout << "sum = " << sum << endl; + + return 0; +} \ No newline at end of file diff --git a/chapt1/exe1.7.cpp b/chapt1/exe1.7.cpp new file mode 100644 index 0000000..a217b2f --- /dev/null +++ b/chapt1/exe1.7.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +using namespace std; + +int main(void) +{ + ifstream infile("string.txt"); + ofstream outfile("string_sort.txt"); + if(!infile || !outfile) + cerr << "Unable to open file" << endl; + else + { + string words; + vector text_vec; + while(infile >> words) + text_vec.push_back(words); + int index = 0; + cout << "Unsorted text: " << endl; + for(index = 0; index < text_vec.size(); index++) + cout <