commit 4e2bf47db00f7382a7284048a5a55c58dfb2969c Author: Cx330 <1487537121@qq.com> Date: Sat Mar 29 23:02:48 2025 +0800 2024年12月完成(补充提交) diff --git a/Chapter 0/0-2.cpp b/Chapter 0/0-2.cpp new file mode 100644 index 0000000..e923750 --- /dev/null +++ b/Chapter 0/0-2.cpp @@ -0,0 +1,7 @@ +#include + +int main() +{ + std::cout<<"This (\") is a quote , and this (\\) is a backlash."< +#include + +int main() +{ + std::cout<<"What is your name? "; + std::string name; + std::cin>>name; + std::cout<<"Hello, "<>name; + std::cout<<"Hello, "< +int main() +{ + std::cout << "εı߳:"; + float a, b, c, d; + std::cin >> a; + std::cout << "볤εijͿ:"; + std::cin >> b >> c; + std::cout << "εĸ:"; + std::cin >> d; + + std::cout << ":" << std::endl; + for (int i = 0; i < a; i++) { + for (int j = 0; j < a; j++) { + if (i == a - 1 || i == 0 || j == a - 1 || j == 0) { + std::cout << "*"; + } + else { + std::cout << " "; + } + } + std::cout << std::endl; + } + + std::cout << ":" << std::endl; + for (int i = 0; i < c; i++) { + for (int j = 0; j < b; j++) { + if (i == c - 1 || i == 0 || j == b - 1 || j == 0) { + std::cout << "*"; + } + else { + std::cout << " "; + } + } + std::cout << std::endl; + } + + std::cout << ":" << std::endl; + for (int i = 0; i < d; i++) { + for (int j = 0; j <= i; j++) { + if (j == 0 || j == i || i == 0 || i == d - 1) { + std::cout << "*"; + } + else { + std::cout << " "; + } + } + std::cout << std::endl; + } +} \ No newline at end of file diff --git a/Chapter 2/2-8.cpp b/Chapter 2/2-8.cpp new file mode 100644 index 0000000..289b634 --- /dev/null +++ b/Chapter 2/2-8.cpp @@ -0,0 +1,13 @@ +#include + +int main() +{ + std::cout << "Ҫ䷶Χ:"; + float a, b, c; + c = 1; + std::cin >> a >> b; + for (float i = a; i < b; i++) { + c = i * c; + } + std::cout << c; +} \ No newline at end of file diff --git a/Chapter 3/3-3.cpp b/Chapter 3/3-3.cpp new file mode 100644 index 0000000..128f75f --- /dev/null +++ b/Chapter 3/3-3.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +int main() +{ + std::cout << "뵥:" << std::endl; + std::string word; + std::mapword_data; + while (std::cin >> word) + word_data[word] = word_data[word] + 1; + std::map::iterator mp_next = word_data.begin(); + while (mp_next != word_data.end()) + { + std::cout << mp_next->first << " ijִ:" << mp_next->second << std::endl; + mp_next++; + } + +} + + +#include // ׼ +#include // ַ +#include // map + +int main() +{ + // ʾϢ + std::cout << "뵥:" << std::endl; + + std::string word; // ڴ洢ûĵ + std::map word_data; // һmapkeyΪʣvalueΪʳִ + + // ѭȡûĵʣֱ簴 Ctrl+D + while (std::cin >> word) + word_data[word] = word_data[word] + 1; // µʳִ + + // һڱmapеԪ + std::map::iterator mp_next = word_data.begin(); + + // mapÿʼִ + while (mp_next != word_data.end()) + { + std::cout << mp_next->first << " ijִ:" << mp_next->second << std::endl; + mp_next++; // ƶһԪ + } + + return 0; // +} diff --git a/Chapter 4/4-5.cpp b/Chapter 4/4-5.cpp new file mode 100644 index 0000000..b930bf5 --- /dev/null +++ b/Chapter 4/4-5.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +int main() +{ + int a = 0; + std::cout << "뵥:" << std::endl; + std::cout << "˳ctrl+zس" << std::endl; + std::string word; + std::mapword_data; + while (std::cin >> word) { + word_data[word] = word_data[word] + 1; + a = a + 1; + } + std::map::iterator mp_next = word_data.begin(); + std::cout << "ĵʸ:" << a << std::endl; + while (mp_next != word_data.end()) + { + std::cout << mp_next->first << " ijִ:" << mp_next->second << std::endl; + mp_next++; + } +} + + +#include // ׼ +#include // ַ +#include // map + +int main() +{ + int a = 0; // ͳĵ + std::cout << "뵥:" << std::endl; // ʾû뵥 + std::cout << "˳ctrl+zس" << std::endl; // ʾûν + + std::string word; // һַڴ洢 + std::map word_data; // һmapkeyΪʣvalueΪʳֵĴ + + // ѭȡûĵʣֱûCtrl+Zس + while (std::cin >> word) { + word_data[word] = word_data[word] + 1; // µʳִʼΪ1 + a = a + 1; // ÿȡһʣ1 + } + + // һڱmap + std::map::iterator mp_next = word_data.begin(); + + // ܵ뵥 + std::cout << "ĵʸ:" << a << std::endl; + + // mapÿʼִ + while (mp_next != word_data.end()) + { + std::cout << mp_next->first << " ijִ:" << mp_next->second << std::endl; // ʺͶӦ + mp_next++; // ƶһԪ + } + + return 0; // +} diff --git a/Chapter 4/4-7.cpp b/Chapter 4/4-7.cpp new file mode 100644 index 0000000..c596dc9 --- /dev/null +++ b/Chapter 4/4-7.cpp @@ -0,0 +1,55 @@ +#include + +int main() +{ + double a, b = 0, c = 0; + std::cout << ":" << std::endl << "˳ctrl+zس" << std::endl; + while (std::cin >> a) { + b = b + a; + c++; + } + b = b / c; + std::cout << "ƽΪ:" << b << std::endl; +} + + +/* +CSDN + +#include +#include +#include + +using namespace std; + +int main() { + vector nums; + double num; + + while (cin >> num) + nums.push_back(num); + + cout << accumulate(nums.begin(), nums.end(), 0.0) / nums.size() << endl; +} +*/ + +#include // ׼ +#include // numeric⣬ʹaccumulate +#include // vector + +using namespace std; + +int main() { + vector nums; // һ̬飬ڴ洢ûֵ + double num; // ڽûĵֵ + + // ѭȡûֱֵCtrl+Z Ctrl+D س + while (cin >> num) + nums.push_back(num); // ֵӵvector + + // ʹaccumulatevectorֵֵܺͣԼƽֵ + // ʼֵΪ0.0Ƶͳʼֵ + cout << accumulate(nums.begin(), nums.end(), 0.0) / nums.size() << endl; + + return 0; // +} diff --git a/Chapter 5/5-8例题改/5-8pics_main.cpp b/Chapter 5/5-8例题改/5-8pics_main.cpp new file mode 100644 index 0000000..1dbf60b --- /dev/null +++ b/Chapter 5/5-8例题改/5-8pics_main.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include + +#include "pics.h" + +using std::cout; +using std::copy; +using std::endl; +using std::ostream_iterator; +using std::string; +using std::vector; + +int main() +{ + vector p; + p.push_back("this is an"); + p.push_back("example"); + p.push_back("to"); + p.push_back("illustrate"); + p.push_back("framing"); + + ostream_iteratorofile(cout, "\n"); + copy(p.begin(), p.end(), ofile); + cout << endl; + + vector f = frame(p); + copy(f.begin(), f.end(), ofile); cout << endl; + + vector h = hcat(p,frame(p)); + copy(h.begin(), h.end(), ofile); + cout << endl; + + return 0; +} + + +#include // ׼㷨⣨std::copy㷨 +#include // ׼ +#include // ⣨std::ostream_iterator +#include // ַ +#include // 붯̬飨vector + +#include "pics.h" // Զͷļṩframehcat + +using std::cout; // ׼ +using std::copy; // ׼㷨 +using std::endl; // ׼з +using std::ostream_iterator; // +using std::string; // ׼ַ +using std::vector; // ׼ + +int main() +{ + // һ洢ַvectorӶַ + vector p; + p.push_back("this is an"); // ַvector + p.push_back("example"); + p.push_back("to"); + p.push_back("illustrate"); + p.push_back("framing"); + + // ڽvector׼ + ostream_iterator ofile(cout, "\n"); + + // vector pݵն + copy(p.begin(), p.end(), ofile); + cout << endl; // һзָ + + // frameΪvector pӱ߿򣬲vector + vector f = frame(p); + + // ߿vector + copy(f.begin(), f.end(), ofile); + cout << endl; // һзָ + + // hcatԭʼvector p߿vector fˮƽ + vector h = hcat(p, frame(p)); + + // ˮƽӺvector + copy(h.begin(), h.end(), ofile); + cout << endl; // һзָ + + return 0; // +} + diff --git a/Chapter 5/5-8例题改/pics.cpp b/Chapter 5/5-8例题改/pics.cpp new file mode 100644 index 0000000..5eac46d --- /dev/null +++ b/Chapter 5/5-8例题改/pics.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + +#include "pics.h" + +using std::string; +using std::vector; +using std::max; + +#ifdef _MSC_VER +//#include "../minmax.h" +#else +//using std::max; +#endif + +string::size_type width(const vector& v) +{ + string::size_type maxlen = 0; +#ifdef _MSC_VER + for(std::vector::size_type i = 0; i != v.size(); ++i) +#else + for(vector::size_type i = 0; i != v.size(); ++i) +#endif + maxlen = max(maxlen, v[i].size()); + return maxlen; +} + +vector frame(const vector& v) +{ + vector ret; + string::size_type maxlen = width(v); + string border(maxlen + 4, '*'); + + // write the top border + ret.push_back(border); + + // write each interior row, bordered by an asterisk and a space +#ifdef _MSC_VER + for (std::vector::size_type i = 0; i != v.size(); ++i) { +#else + for (vector::size_type i = 0; i != v.size(); ++i) { +#endif + ret.push_back("* " + v[i] + + string(maxlen - v[i].size(), ' ') + " *"); + } + + // write the bottom border + ret.push_back(border); + return ret; +} + +vector vcat(const vector& top, + const vector& bottom) +{ + // copy the `top' picture + vector ret = top; + + // copy entire `bottom' picture +#ifdef _MSC_VER + for (std::vector::const_iterator it = bottom.begin(); +#else + for (vector::const_iterator it = bottom.begin(); +#endif + it != bottom.end(); ++it) + ret.push_back(*it); + + return ret; +} + +vector +hcat(const vector& left, const vector& right) +{ + vector ret; + + // add 1 to leave a space between pictures + string::size_type width1 = width(left) + 1; + + // indices to look at elements from `left' and `right' respectively +#ifdef _MSC_VER + std::vector::size_type i = 0, j = 0; +#else + vector::size_type i = 0, j = 0; +#endif + string s; + // continue until we've seen all rows from both pictures + while (i != left.size() || j != right.size()) { + // construct new `string' to hold characters from both pictures + //string s; + + // copy a row from the left-hand side, if there is one + if (i != left.size()) + s = left[i++]; + + // pad to full width + s += string(width1 - s.size(), ' '); + + // copy a row from the right-hand side, if there is one + if (j != right.size()) + s += right[j++]; + + // add `s' to the picture we're creating + ret.push_back(s); + } + + return ret; +} + diff --git a/Chapter 5/5-8例题改/pics.h b/Chapter 5/5-8例题改/pics.h new file mode 100644 index 0000000..ee976c2 --- /dev/null +++ b/Chapter 5/5-8例题改/pics.h @@ -0,0 +1,17 @@ +#ifndef GUARD_pics_h +#define GUARD_pics_h + +#include +#include + +std::string::size_type width(const std::vector& v); + +std::vector frame(const std::vector& v); + +std::vector vcat(const std::vector& top, + const std::vector& bottom); + +std::vector hcat(const std::vector& left, + const std::vector& right); + +#endif diff --git a/Chapter 6/6.1.cpp b/Chapter 6/6.1.cpp new file mode 100644 index 0000000..257a5f2 --- /dev/null +++ b/Chapter 6/6.1.cpp @@ -0,0 +1,238 @@ +#include +#include +#include //̬ +#include +#include +#include + +using std::cin; +using std::cout; +using std::endl; +using std::string; +using std::vector; +using std::max; + + +vector split(const string& s) { //ַָոָɵʣ洢vector + vector ret; + typedef string::size_type string_size; + string_size i = 0; + + while (i != s.size()) { + //ǰεĿհף[ǰi,i)ȫַǿո + while (i != s.size() && isspace(s[i])) { + i++; + } + //ҳһʵս + string_size j = i; + //[ǰj,j)еַǿո + while (j != s.size() && !isspace(s[j])) { + j++; + } + //ҵһЩǿհ׷ + if (i != j) { + ret.push_back(s.substr(i, j - i)); + i = j; + } + } + return ret; +} + + +//ҳַij +string::size_type width(const vector& v) { //ַַij + string::size_type maxlen = 0; + for (vector::size_type i = 0; i != v.size(); ++i) { + maxlen = max(maxlen, v[i].size()); + } + return maxlen; +} + +vector frame(const vector& v) { + vector ret; + string::size_type maxlen = width(v); //ÿַ + //߿ + string border(maxlen + 4, '*'); + + //ڲУÿжһǺźһո + ret.push_back(border); + //ʹõʵ + vector::const_iterator iter = v.begin(); + while (iter != v.end()) { //ÿмַոʹһ + ret.push_back("* " + (*iter) + string(maxlen - iter->size(), ' ') + " *"); + iter++; + } + ret.push_back(border); + return ret; +} + +// +vector vcat(const vector& top, const vector& bottom) { + vector ret = top; + ret.insert(ret.end(), bottom.begin(), bottom.end()); + return ret; +} + +// +vector hcat(const vector& left, const vector& right) { + vectorret; + //ͼ֮ո + string::size_type width1 = width(left) + 1; + //ڱleftright + //ʹõ + vector::const_iterator i, j; + i = left.begin(); + j = right.begin(); + while (i != left.end() || j != right.end()) { + string s; + if (i != left.end()) { + s = *i++; + } + + s += string(width1 - s.size(), ' '); + + if (j != right.end()) { + s += *j++; + } + ret.push_back(s); + } + + return ret; +} + +int main(int argc, const char* argv[]) { + string s; + while (getline(cin, s)) { + vector v = split(s); + vector fra = frame(v); //frameԷָĵӿ + vector col, row; + col = vcat(v, fra); + row = hcat(v, fra); //ͨhcatָıͿܻıʾĻ + for (vector::size_type j = 0; j != row.size(); ++j) { + cout << row[j] << endl; + } + } + return 0; +} + + +#include // ׼ +#include // ַ +#include // 붯̬飨vector +#include // ⣨δʹã +#include // 㷨⣨std::max +#include // ַ⣨std::isspace + +using std::cin; +using std::cout; +using std::endl; +using std::string; +using std::vector; +using std::max; + +// ոַָطָĵ +vector split(const string& s) { + vector ret; // 洢ָ + typedef string::size_type string_size; // ַȵ + string_size i = 0; + + while (i != s.size()) { + // ո + while (i != s.size() && isspace(s[i])) { + i++; + } + + // ҵһʵĽλ + string_size j = i; + while (j != s.size() && !isspace(s[j])) { + j++; + } + + // ҵʣӵ + if (i != j) { + ret.push_back(s.substr(i, j - i)); // ȡʲ + i = j; // λ + } + } + return ret; +} + +// ҳַij +string::size_type width(const vector& v) { + string::size_type maxlen = 0; // ʼ󳤶Ϊ0 + for (vector::size_type i = 0; i != v.size(); ++i) { + maxlen = max(maxlen, v[i].size()); // 󳤶 + } + return maxlen; +} + +// Ϊеַӿܣ߿ +vector frame(const vector& v) { + vector ret; // 洢ַܻ + string::size_type maxlen = width(v); // ַ + string border(maxlen + 4, '*'); // ߿ַ + + ret.push_back(border); // Ӷ߿ + vector::const_iterator iter = v.begin(); + + while (iter != v.end()) { + // ÿұ߿ʵ + ret.push_back("* " + (*iter) + string(maxlen - iter->size(), ' ') + " *"); + iter++; + } + ret.push_back(border); // ӵײ߿ + return ret; +} + +// ַ +vector vcat(const vector& top, const vector& bottom) { + vector ret = top; // ʼΪ + ret.insert(ret.end(), bottom.begin(), bottom.end()); // ӵײ + return ret; +} + +// ַ +vector hcat(const vector& left, const vector& right) { + vector ret; + string::size_type width1 = width(left) + 1; // ΪһոĿ + vector::const_iterator i = left.begin(), j = right.begin(); + + // ͬʱ + while (i != left.end() || j != right.end()) { + string s; + + if (i != left.end()) { + s = *i++; // ȡַ + } + + s += string(width1 - s.size(), ' '); // ոԶ + + if (j != right.end()) { + s += *j++; // Ҳַ + } + + ret.push_back(s); // ƴӺַӵ + } + + return ret; +} + +int main(int argc, const char* argv[]) { + string s; + while (getline(cin, s)) { + // ոַָ + vector v = split(s); + // Ϊַָӿ + vector fra = frame(v); + // ԭʼַͿַܻ + vector col = vcat(v, fra); + // ԭʼַͿַܻ + vector row = hcat(v, fra); + + // ӺĽ + for (vector::size_type j = 0; j != row.size(); ++j) { + cout << row[j] << endl; + } + } + return 0; +} diff --git a/Chapter 6/6.9.cpp b/Chapter 6/6.9.cpp new file mode 100644 index 0000000..a89fd4c --- /dev/null +++ b/Chapter 6/6.9.cpp @@ -0,0 +1,48 @@ +#include +#include //̬ +#include +#include + +using std::cin; +using std::endl; +using std::cout; +using std::vector; +using std::accumulate; //accumulate()Ԫصۻֵ +using std::string; + +int main(int argc, const char* argv[]) { + vector v = { "zhang", "meng", "nan" , "1220310013"}; + string str; //ַ + str = accumulate(v.begin(), v.end(), str); //vַstr + cout << str; + return 0; +} + + +#include // ׼ +#include // 붯̬飨vector +#include // numeric⣬ṩaccumulate +#include // ַ + +using std::cin; +using std::endl; +using std::cout; +using std::vector; +using std::accumulate; // accumulateۼڵԪ +using std::string; + +int main(int argc, const char* argv[]) { + // ʼһַ + vector v = { "zhang", "meng", "nan", "1220310013" }; + + string str; // һַڴ洢ۻĽ + + // ʹaccumulatevector vеַ + // accumulateĵۻijʼֵַstrڿʼۻ + str = accumulate(v.begin(), v.end(), str); + + // ۻַ + cout << str; // "zhangmengnan1220310013" + + return 0; // +} diff --git a/Chapter 7/7-3/7-3xref.cpp b/Chapter 7/7-3/7-3xref.cpp new file mode 100644 index 0000000..14f9d38 --- /dev/null +++ b/Chapter 7/7-3/7-3xref.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include + +#include "split.h" + +using std::cin; using std::cout; +using std::endl; using std::getline; +using std::istream; using std::string; +using std::vector; using std::map; +using std::set; + +// find all the lines that refer to each word in the input +map > + xref(istream& in, + vector find_words(const string&) = split) +{ + string line; + int line_number = 0; + map > ret; + + // read the next line + while (getline(in, line)) { + ++line_number; + + // break the input line into words + vector words = find_words(line); + + // remember that each word occurs on the current line +#ifdef _MSC_VER + for (std::vector::const_iterator it = words.begin(); +#else + for (vector::const_iterator it = words.begin(); +#endif + it != words.end(); ++it) + ret[*it].insert(line_number); + } + return ret; +} + +int main() +{ + // call `xref' using `split' by default + map > ret = xref(cin); + + // write the results +#ifdef _MSC_VER + for (std::map >::const_iterator it = ret.begin(); +#else + for (map >::const_iterator it = ret.begin(); +#endif + it != ret.end(); ++it) { + // write the word + cout << it->first << " occurs on line(s): "; + + // followed by one or more line numbers +#ifdef _MSC_VER + std::set::const_iterator line_it = it->second.begin(); +#else + set::const_iterator line_it = it->second.begin(); +#endif + cout << *line_it; // write the first line number + + ++line_it; + // write the rest of the line numbers, if any + while (line_it != it->second.end()) { + cout << ", " << *line_it; + ++line_it; + } + // write a new line to separate each word from the next + cout << endl; + } + + return 0; +} + + + +#include // ӳ⣨mapڴ洢ÿĶӦϵ +#include // ׼ +#include // ַ +#include // 붯̬飨vector +#include // 뼯⣨setڴ洢к + +#include "split.h" // Զͷļsplit.hsplit + +using std::cin; // ׼ +using std::cout; // ׼ +using std::endl; // 뻻з +using std::getline; // getlineжȡ +using std::istream; // +using std::string; // ַ +using std::vector; // 붯̬ +using std::map; // ӳ +using std::set; // 뼯 + +// xref ÿгֵк +map > +xref(istream& in, // ĬΪcin + vector find_words(const string&) = split) // ʹsplitָʣĬϣ +{ + string line; // ڴ洢ÿеı + int line_number = 0; // ǰк + map> ret; // 洢ʼֵкţʹmap<, set<к>>ṹ + + // жȡ + while (getline(in, line)) { + ++line_number; // к + + // ǰвΪʣʹfind_wordsĬsplit + vector words = find_words(line); + + // ǰеÿʣӦкŲ뵽map + for (vector::const_iterator it = words.begin(); it != words.end(); ++it) + ret[*it].insert(line_number); // ʼкŴmap + } + return ret; // mapÿʼֵк +} + +int main() +{ + // xrefĬʹsplitеʲ֣cin + map> ret = xref(cin); + + // ÿʼֵк + for (map>::const_iterator it = ret.begin(); it != ret.end(); ++it) { + // + cout << it->first << " occurs on line(s): "; + + // õʳֵк + set::const_iterator line_it = it->second.begin(); + cout << *line_it; // һк + + ++line_it; + // к + while (line_it != it->second.end()) { + cout << ", " << *line_it; + ++line_it; + } + + // зԷָÿʵ + cout << endl; + } + + return 0; // +} diff --git a/Chapter 7/7-3/split.cpp b/Chapter 7/7-3/split.cpp new file mode 100644 index 0000000..2d7e05f --- /dev/null +++ b/Chapter 7/7-3/split.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +#include "split.h" + +using std::vector; +using std::string; + +#ifndef _MSC_VER +using std::isspace; +#endif + +vector split(const string& s) +{ + vector ret; + typedef string::size_type string_size; + string_size i = 0; + + // invariant: we have processed characters `['original value of `i', `i)' + while (i != s.size()) { + // ignore leading blanks + // invariant: characters in range `['original `i', current `i)' are all spaces + while (i != s.size() && isspace(s[i])) + ++i; + + // find end of next word + string_size j = i; + // invariant: none of the characters in range `['original `j', current `j)' is a space + while (j != s.size() && !isspace(s[j])) + ++j; + + // if we found some nonwhitespace characters + if (i != j) { + // copy from `s' starting at `i' and taking `j' `\-' `i' chars + ret.push_back(s.substr(i, j - i)); + i = j; + } + + } + return ret; +} + diff --git a/Chapter 7/7-3/split.h b/Chapter 7/7-3/split.h new file mode 100644 index 0000000..7d49a45 --- /dev/null +++ b/Chapter 7/7-3/split.h @@ -0,0 +1,8 @@ +#ifndef GUARD_split_h +#define GUARD_split_h + +#include +#include +std::vector split(const std::string&); + +#endif diff --git a/Chapter 8/8-5gen_sentence/8-5grammar.cpp b/Chapter 8/8-5gen_sentence/8-5grammar.cpp new file mode 100644 index 0000000..5348fe6 --- /dev/null +++ b/Chapter 8/8-5gen_sentence/8-5grammar.cpp @@ -0,0 +1,251 @@ +#ifdef _MSC_VER +#pragma warning(disable:4503) // `silence compiler complaints about generated names being too long' +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include "split.h" +#include + +using std::istream; using std::cin; +using std::copy; using std::cout; +using std::endl; using std::find; +using std::getline; using std::logic_error; +using std::map; using std::string; +using std::vector; using std::domain_error; +using std::rand; + +typedef vector Rule; +typedef vector Rule_collection; +typedef map Grammar; + +// read a grammar from a given input stream +Grammar read_grammar(istream& in) +{ + Grammar ret; + string line; + + // read the input + while (getline(in, line)) { + + // `split' the input into words + vector entry = split(line); + + if (!entry.empty()) + // use the category to store the associated rule + ret[entry[0]].push_back( + Rule(entry.begin() + 1, entry.end())); + } + return ret; +} + +void gen_aux(const Grammar&, const string&, vector&); + +int nrand(int); + +vector gen_sentence(const Grammar& g) +{ + vector ret; + gen_aux(g, "", ret); + return ret; +} + +bool bracketed(const string& s) +{ + return s.size() > 1 && s[0] == '<' && s[s.size() - 1] == '>'; +} + +void +gen_aux(const Grammar& g, const string& word, vector& ret) +{ + + if (!bracketed(word)) { + ret.push_back(word); + } else { + // locate the rule that corresponds to `word' + Grammar::const_iterator it = g.find(word); + if (it == g.end()) + throw logic_error("empty rule"); + + // fetch the set of possible rules + const Rule_collection& c = it->second; + + // from which we select one at random + const Rule& r = c[nrand(c.size())]; + + // recursively expand the selected rule + for (Rule::const_iterator i = r.begin(); i != r.end(); ++i) + gen_aux(g, *i, ret); + } +} + +int main() +{ + // generate the sentence + vector sentence = gen_sentence(read_grammar(cin)); + + // write the first word, if any +#ifdef _MSC_VER + std::vector::const_iterator it = sentence.begin(); +#else + vector::const_iterator it = sentence.begin(); +#endif + if (!sentence.empty()) { + cout << *it; + ++it; + } + + // write the rest of the words, each preceded by a space + while (it != sentence.end()) { + cout << " " << *it; + ++it; + } + + cout << endl; + return 0; +} + +// return a random integer in the range `[0,' `n)' +int nrand(int n) +{ + if (n <= 0 || n > RAND_MAX) + throw domain_error("Argument to nrand is out of range"); + + const int bucket_size = RAND_MAX / n; + int r; + + do r = rand() / bucket_size; + while (r >= n); + + return r; +} + + + +#ifdef _MSC_VER +#pragma warning(disable:4503) // رձ棬ɵƹıԹ +#endif + +#include // 㷨⣬ṩõ㷨 +#include // C׼⣬rand() +#include // ׼ +#include // ӳmap +#include // ׼쳣⣬ṩ߼쳣 +#include // ַ +#include // 붯̬飨vector +#include "split.h" // Զsplit.hͷļٶṩַֹܣ +#include // ʱ⣬֧ + +using std::istream; using std::cin; +using std::copy; using std::cout; +using std::endl; using std::find; +using std::getline; using std::logic_error; +using std::map; using std::string; +using std::vector; using std::domain_error; +using std::rand; // rand + +// ַϣ﷨򼯺ϣ +typedef vector Rule; // һַļ +typedef vector Rule_collection; // 򼯺ǹļ +typedef map Grammar; // ﷨һӳ䣺 -> 򼯺 + +// жȡ﷨ +Grammar read_grammar(istream& in) { + Grammar ret; // ڴ洢﷨ + string line; + + // ȡÿһ + while (getline(in, line)) { + // ǰвֳɵ + vector entry = split(line); + + // ֳĵʲΪգ洢 + if (!entry.empty()) + ret[entry[0]].push_back( + Rule(entry.begin() + 1, entry.end())); // ʹΪkeyΪvalue + } + return ret; // ﷨ +} + +// ݹɾӵĸ +void gen_aux(const Grammar&, const string&, vector&); + +// һΧ[0, n)ڵ +int nrand(int); + +// һӣgen_auxеݹ +vector gen_sentence(const Grammar& g) { + vector ret; + gen_aux(g, "", ret); // ʼɾ + return ret; +} + +// жһַǷDZŰΧ +bool bracketed(const string& s) { + return s.size() > 1 && s[0] == '<' && s[s.size() - 1] == '>'; +} + +// ݹɾеһ +void gen_aux(const Grammar& g, const string& word, vector& ret) { + if (!bracketed(word)) { + ret.push_back(word); // DZŰΧĵʣֱӼ + } + else { + // ҶӦĹ + Grammar::const_iterator it = g.find(word); + if (it == g.end()) // ûҵ׳쳣 + throw logic_error("empty rule"); + + // ȡõʵĹ򼯺 + const Rule_collection& c = it->second; + + // ӹ򼯺ѡһ + const Rule& r = c[nrand(c.size())]; + + // ѡеĹеÿʽеݹ鴦 + for (Rule::const_iterator i = r.begin(); i != r.end(); ++i) + gen_aux(g, *i, ret); + } +} + +int main() { + // ɾ + vector sentence = gen_sentence(read_grammar(cin)); + + // һʣУ + vector::const_iterator it = sentence.begin(); + if (!sentence.empty()) { + cout << *it; + ++it; + } + + // ʣĵʣÿǰһո + while (it != sentence.end()) { + cout << " " << *it; + ++it; + } + + cout << endl; // + return 0; +} + +// һΧ[0, n) +int nrand(int n) { + if (n <= 0 || n > RAND_MAX) + throw domain_error("Argument to nrand is out of range"); + + const int bucket_size = RAND_MAX / n; // ȷֲ + int r; + + // ȷЧΧ + do r = rand() / bucket_size; + while (r >= n); + + return r; +} diff --git a/Chapter 8/8-5gen_sentence/split.cpp b/Chapter 8/8-5gen_sentence/split.cpp new file mode 100644 index 0000000..2d7e05f --- /dev/null +++ b/Chapter 8/8-5gen_sentence/split.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +#include "split.h" + +using std::vector; +using std::string; + +#ifndef _MSC_VER +using std::isspace; +#endif + +vector split(const string& s) +{ + vector ret; + typedef string::size_type string_size; + string_size i = 0; + + // invariant: we have processed characters `['original value of `i', `i)' + while (i != s.size()) { + // ignore leading blanks + // invariant: characters in range `['original `i', current `i)' are all spaces + while (i != s.size() && isspace(s[i])) + ++i; + + // find end of next word + string_size j = i; + // invariant: none of the characters in range `['original `j', current `j)' is a space + while (j != s.size() && !isspace(s[j])) + ++j; + + // if we found some nonwhitespace characters + if (i != j) { + // copy from `s' starting at `i' and taking `j' `\-' `i' chars + ret.push_back(s.substr(i, j - i)); + i = j; + } + + } + return ret; +} + diff --git a/Chapter 8/8-5gen_sentence/split.h b/Chapter 8/8-5gen_sentence/split.h new file mode 100644 index 0000000..7d49a45 --- /dev/null +++ b/Chapter 8/8-5gen_sentence/split.h @@ -0,0 +1,8 @@ +#ifndef GUARD_split_h +#define GUARD_split_h + +#include +#include +std::vector split(const std::string&); + +#endif diff --git a/Chapter 8/8-5gen_sentence/例题改/pics.cpp b/Chapter 8/8-5gen_sentence/例题改/pics.cpp new file mode 100644 index 0000000..5eac46d --- /dev/null +++ b/Chapter 8/8-5gen_sentence/例题改/pics.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + +#include "pics.h" + +using std::string; +using std::vector; +using std::max; + +#ifdef _MSC_VER +//#include "../minmax.h" +#else +//using std::max; +#endif + +string::size_type width(const vector& v) +{ + string::size_type maxlen = 0; +#ifdef _MSC_VER + for(std::vector::size_type i = 0; i != v.size(); ++i) +#else + for(vector::size_type i = 0; i != v.size(); ++i) +#endif + maxlen = max(maxlen, v[i].size()); + return maxlen; +} + +vector frame(const vector& v) +{ + vector ret; + string::size_type maxlen = width(v); + string border(maxlen + 4, '*'); + + // write the top border + ret.push_back(border); + + // write each interior row, bordered by an asterisk and a space +#ifdef _MSC_VER + for (std::vector::size_type i = 0; i != v.size(); ++i) { +#else + for (vector::size_type i = 0; i != v.size(); ++i) { +#endif + ret.push_back("* " + v[i] + + string(maxlen - v[i].size(), ' ') + " *"); + } + + // write the bottom border + ret.push_back(border); + return ret; +} + +vector vcat(const vector& top, + const vector& bottom) +{ + // copy the `top' picture + vector ret = top; + + // copy entire `bottom' picture +#ifdef _MSC_VER + for (std::vector::const_iterator it = bottom.begin(); +#else + for (vector::const_iterator it = bottom.begin(); +#endif + it != bottom.end(); ++it) + ret.push_back(*it); + + return ret; +} + +vector +hcat(const vector& left, const vector& right) +{ + vector ret; + + // add 1 to leave a space between pictures + string::size_type width1 = width(left) + 1; + + // indices to look at elements from `left' and `right' respectively +#ifdef _MSC_VER + std::vector::size_type i = 0, j = 0; +#else + vector::size_type i = 0, j = 0; +#endif + string s; + // continue until we've seen all rows from both pictures + while (i != left.size() || j != right.size()) { + // construct new `string' to hold characters from both pictures + //string s; + + // copy a row from the left-hand side, if there is one + if (i != left.size()) + s = left[i++]; + + // pad to full width + s += string(width1 - s.size(), ' '); + + // copy a row from the right-hand side, if there is one + if (j != right.size()) + s += right[j++]; + + // add `s' to the picture we're creating + ret.push_back(s); + } + + return ret; +} + diff --git a/Chapter 8/8-5gen_sentence/例题改/pics.h b/Chapter 8/8-5gen_sentence/例题改/pics.h new file mode 100644 index 0000000..ee976c2 --- /dev/null +++ b/Chapter 8/8-5gen_sentence/例题改/pics.h @@ -0,0 +1,17 @@ +#ifndef GUARD_pics_h +#define GUARD_pics_h + +#include +#include + +std::string::size_type width(const std::vector& v); + +std::vector frame(const std::vector& v); + +std::vector vcat(const std::vector& top, + const std::vector& bottom); + +std::vector hcat(const std::vector& left, + const std::vector& right); + +#endif diff --git a/Chapter 8/8-5gen_sentence/例题改/pics_main.cpp b/Chapter 8/8-5gen_sentence/例题改/pics_main.cpp new file mode 100644 index 0000000..b994600 --- /dev/null +++ b/Chapter 8/8-5gen_sentence/例题改/pics_main.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include + +#include "pics.h" + +using std::cout; +using std::copy; +using std::endl; +using std::ostream_iterator; +using std::string; +using std::vector; + +int main() +{ + vector p; + p.push_back("this is an"); + p.push_back("example"); + p.push_back("to"); + p.push_back("illustrate"); + p.push_back("framing"); + + ostream_iteratorofile(cout, "\n"); + copy(p.begin(), p.end(), ofile); + cout << endl; + + vector f = frame(p); + copy(f.begin(), f.end(), ofile); cout << endl; + + vector h = hcat(p,frame(p)); + copy(h.begin(), h.end(), ofile); + cout << endl; + + return 0; +} + + + +#include // 㷨⣬ṩõ㷨 +#include // ׼ +#include // +#include // ַ +#include // 붯̬飨vector + +#include "pics.h" // Զ pics.h ⣨˿ɺƴصĹܣ + +using std::cout; // ʹñ׼ +using std::copy; // copy㷨 +using std::endl; // 뻻з +using std::ostream_iterator; // +using std::string; // ʹַ +using std::vector; // ʹvector + +int main() { + // һvector͵ p洢ַ + vector p; + p.push_back("this is an"); // һַ + p.push_back("example"); // һַ + p.push_back("to"); // һַ + p.push_back("illustrate"); // һַ + p.push_back("framing"); // һַ + + // ָ coutÿԪ + ostream_iterator ofile(cout, "\n"); + + // ʹcopy㷨vector pе + copy(p.begin(), p.end(), ofile); + cout << endl; // з + + // vector pӦÿܲһµvector f + vector f = frame(p); + // ʹcopy㷨vector fе + copy(f.begin(), f.end(), ofile); + cout << endl; // з + + // vector pvector fкƴӣһµvector h + vector h = hcat(p, frame(p)); + // ʹcopy㷨vector hе + copy(h.begin(), h.end(), ofile); + cout << endl; // з + + return 0; // +} diff --git a/Chapter 8/8-5xref/8-5xref.cpp b/Chapter 8/8-5xref/8-5xref.cpp new file mode 100644 index 0000000..97cd07a --- /dev/null +++ b/Chapter 8/8-5xref/8-5xref.cpp @@ -0,0 +1,145 @@ +#include +#include +#include +#include + +#include "split.h" + +using std::cin; using std::cout; +using std::endl; using std::getline; +using std::istream; using std::string; +using std::vector; using std::map; + +// find all the lines that refer to each word in the input +map > + xref(istream& in, + vector find_words(const string&) = split) +{ + string line; + int line_number = 0; + map > ret; + + // read the next line + while (getline(in, line)) { + ++line_number; + + // break the input line into words + vector words = find_words(line); + + // remember that each word occurs on the current line +#ifdef _MSC_VER + for (std::vector::const_iterator it = words.begin(); +#else + for (vector::const_iterator it = words.begin(); +#endif + it != words.end(); ++it) + ret[*it].push_back(line_number); + } + return ret; +} + +int main() +{ + // call `xref' using `split' by default + map > ret = xref(cin); + + // write the results +#ifdef _MSC_VER + for (std::map >::const_iterator it = ret.begin(); +#else + for (map >::const_iterator it = ret.begin(); +#endif + it != ret.end(); ++it) { + // write the word + cout << it->first << " occurs on line(s): "; + + // followed by one or more line numbers +#ifdef _MSC_VER + std::vector::const_iterator line_it = it->second.begin(); +#else + vector::const_iterator line_it = it->second.begin(); +#endif + cout << *line_it; // write the first line number + + ++line_it; + // write the rest of the line numbers, if any + while (line_it != it->second.end()) { + cout << ", " << *line_it; + ++line_it; + } + // write a new line to separate each word from the next + cout << endl; + } + + return 0; +} + + + +#include // map ڴ洢ÿʼֵк +#include // ׼ +#include // ַ +#include // 붯̬⣬ڴ洢ÿһеĵ + +#include "split.h" // Զ split.h ⣬ڷַָĺ + +using std::cin; // ʹñ׼ +using std::cout; // ʹñ׼ +using std::endl; // ʹûз +using std::getline; // ʹgetlineȡ +using std::istream; // ʹ +using std::string; // ʹַ +using std::vector; // ʹvector +using std::map; // ʹmap + +// xrefÿıгֵк +// in һָ find_wordsڷָÿеĵʣĬΪ split +map > +xref(istream& in, + vector find_words(const string&) = split) +{ + string line; // 洢ÿһеı + int line_number = 0; // к + map > ret; // map洢ÿʼӦк + + // жȡ + while (getline(in, line)) { + ++line_number; // к + + // find_wordsһıָɵ + vector words = find_words(line); + + // ǰкӵÿʵĶӦλ + for (vector::const_iterator it = words.begin(); it != words.end(); ++it) + ret[*it].push_back(line_number); // ʺкŶӦ + } + return ret; // ذÿʼкŵmap +} + +int main() +{ + // xref Ĭʹ split ָ + map > ret = xref(cin); + + // + for (map >::const_iterator it = ret.begin(); it != ret.end(); ++it) { + // + cout << it->first << " occurs on line(s): "; + + // ȡʳֵк + vector::const_iterator line_it = it->second.begin(); + cout << *line_it; // һк + + ++line_it; + // к + while (line_it != it->second.end()) { + cout << ", " << *line_it; + ++line_it; + } + // + cout << endl; + } + + return 0; // +} + diff --git a/Chapter 8/8-5xref/split.cpp b/Chapter 8/8-5xref/split.cpp new file mode 100644 index 0000000..2d7e05f --- /dev/null +++ b/Chapter 8/8-5xref/split.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +#include "split.h" + +using std::vector; +using std::string; + +#ifndef _MSC_VER +using std::isspace; +#endif + +vector split(const string& s) +{ + vector ret; + typedef string::size_type string_size; + string_size i = 0; + + // invariant: we have processed characters `['original value of `i', `i)' + while (i != s.size()) { + // ignore leading blanks + // invariant: characters in range `['original `i', current `i)' are all spaces + while (i != s.size() && isspace(s[i])) + ++i; + + // find end of next word + string_size j = i; + // invariant: none of the characters in range `['original `j', current `j)' is a space + while (j != s.size() && !isspace(s[j])) + ++j; + + // if we found some nonwhitespace characters + if (i != j) { + // copy from `s' starting at `i' and taking `j' `\-' `i' chars + ret.push_back(s.substr(i, j - i)); + i = j; + } + + } + return ret; +} + diff --git a/Chapter 8/8-5xref/split.h b/Chapter 8/8-5xref/split.h new file mode 100644 index 0000000..7d49a45 --- /dev/null +++ b/Chapter 8/8-5xref/split.h @@ -0,0 +1,8 @@ +#ifndef GUARD_split_h +#define GUARD_split_h + +#include +#include +std::vector split(const std::string&); + +#endif diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 1/main.cpp b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 1/main.cpp new file mode 100644 index 0000000..2bed64a --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 1/main.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include "split.h" + +using std::cin; +using std::cout; +using std::endl; +using std::string; +using std::vector; + +// תַ +string reverseString(const string& s) { + return string(s.rbegin(), s.rend()); +} + +int main() { + // ȡı + cout << "һı: "; + string input; + std::getline(cin, input); + + // ıΪ + vector words = split(input); + + // ÿʽзת + for (auto& word : words) { + word = reverseString(word); + } + + // շתĵ + std::sort(words.begin(), words.end()); + + // ĵʣԭʽһ + for (size_t i = 0; i < words.size(); ++i) { + if (i > 0) { + cout << " "; // ʼӿո + } + cout << words[i]; + } + cout << endl; + + return 0; +} diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 1/split.cpp b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 1/split.cpp new file mode 100644 index 0000000..2d7e05f --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 1/split.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +#include "split.h" + +using std::vector; +using std::string; + +#ifndef _MSC_VER +using std::isspace; +#endif + +vector split(const string& s) +{ + vector ret; + typedef string::size_type string_size; + string_size i = 0; + + // invariant: we have processed characters `['original value of `i', `i)' + while (i != s.size()) { + // ignore leading blanks + // invariant: characters in range `['original `i', current `i)' are all spaces + while (i != s.size() && isspace(s[i])) + ++i; + + // find end of next word + string_size j = i; + // invariant: none of the characters in range `['original `j', current `j)' is a space + while (j != s.size() && !isspace(s[j])) + ++j; + + // if we found some nonwhitespace characters + if (i != j) { + // copy from `s' starting at `i' and taking `j' `\-' `i' chars + ret.push_back(s.substr(i, j - i)); + i = j; + } + + } + return ret; +} + diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 1/split.h b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 1/split.h new file mode 100644 index 0000000..7d49a45 --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 1/split.h @@ -0,0 +1,8 @@ +#ifndef GUARD_split_h +#define GUARD_split_h + +#include +#include +std::vector split(const std::string&); + +#endif diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 2/main.cpp b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 2/main.cpp new file mode 100644 index 0000000..90d86a2 --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 2/main.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include "urls.h" + +using std::cin; +using std::cout; +using std::endl; +using std::sort; +using std::string; +using std::vector; + +int main() { + // ı + cout << "һı: "; + string input; + std::getline(cin, input); + + // ȡ URL + vector urls = find_urls(input); + + // ɸѡ http:// https:// ͷ URL + vector http_urls; + for (const string& url : urls) { + if (url.find("http://") == 0 || url.find("https://") == 0) { + http_urls.push_back(url); + } + } + + // ĸ˳ + sort(http_urls.begin(), http_urls.end()); + + // + cout << " HTTP URLs:" << endl; + for (const string& url : http_urls) { + cout << url << endl; + } + + return 0; +} diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 2/urls.cpp b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 2/urls.cpp new file mode 100644 index 0000000..5585b77 --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 2/urls.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#include + +#include "urls.h" + +using std::find; +using std::find_if; + +#ifndef _MSC_VER +using std::isalnum; +using std::isalpha; +using std::isdigit; +#endif + +using std::search; +using std::string; +using std::vector; + +bool not_url_char(char); + +string::const_iterator +url_end(string::const_iterator, string::const_iterator); + +string::const_iterator +url_beg(string::const_iterator, string::const_iterator); +vector find_urls(const string& s) +{ + vector ret; + typedef string::const_iterator iter; + iter b = s.begin(), e = s.end(); + + // look through the entire input + while (b != e) { + + // look for one or more letters followed by `://' + b = url_beg(b, e); + + // if we found it + if (b != e) { + // get the rest of the \s-1URL\s0 + iter after = url_end(b, e); + + // remember the \s-1URL\s0 + ret.push_back(string(b, after)); + + // advance `b' and check for more \s-1URL\s0s on this line + b = after; + } + } + return ret; +} + +string::const_iterator +url_end(string::const_iterator b, string::const_iterator e) +{ + return find_if(b, e, not_url_char); +} + +bool not_url_char(char c) +{ + // characters, in addition to alphanumerics, that can appear in a \s-1URL\s0 + static const string url_ch = "~;/?:@=&$-_.+!*'(),"; + + // see whether `c' can appear in a \s-1URL\s0 and return the negative + return !(isalnum(c) || + find(url_ch.begin(), url_ch.end(), c) != url_ch.end()); +} + +string::const_iterator +url_beg(string::const_iterator b, string::const_iterator e) +{ + static const string sep = "://"; + + typedef string::const_iterator iter; + + // `i' marks where the separator was found + iter i = b; + + while ((i = search(i, e, sep.begin(), sep.end())) != e) { + + // make sure the separator isn't at the beginning or end of the line + if (i != b && i + sep.size() != e) { + + // `beg' marks the beginning of the protocol-name + iter beg = i; + while (beg != b && isalpha(beg[-1])) + --beg; + + // is there at least one appropriate character before and after the separator? + if (beg != i && !not_url_char(i[sep.size()])) + return beg; + } + + // the separator we found wasn't part of a \s-1URL\s0; advance `i' past this separator + i += sep.size(); + } + return e; +} + diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 2/urls.h b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 2/urls.h new file mode 100644 index 0000000..3b881c5 --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 2/urls.h @@ -0,0 +1,9 @@ +#ifndef GUARD_urls_h +#define GUARD_urls_h + +#include +#include + +std::vector find_urls(const std::string& s); + +#endif diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/Student_info.cpp b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/Student_info.cpp new file mode 100644 index 0000000..2da2db9 --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/Student_info.cpp @@ -0,0 +1,37 @@ +// source file for `Student_info'-related functions +#include "Student_info.h" + +using std::istream; using std::vector; + +bool compare(const Student_info& x, const Student_info& y) +{ + return x.name < y.name; +} + +istream& read(istream& is, Student_info& s) +{ + // read and store the student's name and midterm and final exam grades + is >> s.name >> s.midterm >> s.final; + + read_hw(is, s.homework); // read and store all the student's homework grades + return is; +} + +// read homework grades from an input stream into a `vector' +istream& read_hw(istream& in, vector& hw) +{ + if (in) { + // get rid of previous contents + hw.clear(); + + // read homework grades + double x; + while (in >> x) + hw.push_back(x); + + // clear the stream so that input will work for the next student + in.clear(); + } + return in; +} + diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/Student_info.h b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/Student_info.h new file mode 100644 index 0000000..65c7257 --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/Student_info.h @@ -0,0 +1,19 @@ +#ifndef GUARD_Student_info +#define GUARD_Student_info + +// `Student_info.h' header file +#include +#include +#include + +struct Student_info { + std::string name; + double midterm, final; + std::vector homework; +}; + +bool compare(const Student_info&, const Student_info&); +std::istream& read(std::istream&, Student_info&); +std::istream& read_hw(std::istream&, std::vector&); +#endif + diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/grade.cpp b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/grade.cpp new file mode 100644 index 0000000..43d757f --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/grade.cpp @@ -0,0 +1,41 @@ +#include +#include +#include "grade.h" +#include "median.h" +#include "Student_info.h" + +using std::domain_error; using std::vector; + + +// compute a student's overall grade from midterm and final exam grades and homework grade +double grade(double midterm, double final, double homework) +{ + return 0.2 * midterm + 0.4 * final + 0.4 * homework; +} + +// compute a student's overall grade from midterm and final exam grades +// and vector of homework grades. +// this function does not copy its argument, because `median' does so for us. +double grade(double midterm, double final, const vector& hw) +{ + if (hw.size() == 0) + throw domain_error("student has done no homework"); + return grade(midterm, final, median(hw)); +} + +double grade(const Student_info& s) +{ + return grade(s.midterm, s.final, s.homework); +} + +// predicate to determine whether a student failed +bool fgrade(const Student_info& s) +{ + return grade(s) < 60; +} + +bool pgrade(const Student_info& s) +{ + return !fgrade(s); +} + diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/grade.h b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/grade.h new file mode 100644 index 0000000..df1e0f5 --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/grade.h @@ -0,0 +1,14 @@ +#ifndef GUARD_grade_h +#define GUARD_grade_h + +#include +#include "Student_info.h" + +double grade(double, double, double); +double grade(double, double, const std::vector&); +double grade(const Student_info&); + +bool pgrade(const Student_info&); +bool fgrade(const Student_info&); + +#endif diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/main.cpp b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/main.cpp new file mode 100644 index 0000000..727e5da --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/main.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include "Student_info.h" +#include "grade.h" + +using std::cin; +using std::cout; +using std::endl; +using std::string; +using std::vector; + +// 峣 +const double PASS_GRADE = 60.0; + +int main() { + vector students; + Student_info record; + + // ȡѧϢ + cout << "гɼռ20%ĩɼռ40%ƽʱɼռ40%:" << endl; + cout << "ѧϢгɼĩɼƽʱɼ:" << endl; + cout << "Ctrl+zس˳" << endl; + + while (read(cin, record)) { + students.push_back(record); + } + + // ѧΪͲ + vector pass, fail; + for (const auto& student : students) { + if (grade(student) >= PASS_GRADE) { + pass.push_back(student); + } + else { + fail.push_back(student); + } + } + + // + std::sort(pass.begin(), pass.end(), compare); + std::sort(fail.begin(), fail.end(), compare); + + // ѧϢ + cout << "ϸѧ:" << endl; + for (const auto& student : pass) { + cout << student.name << ": ܳɼ = " << grade(student) << endl; + } + + // ѧϢ + cout << "ϸѧ:" << endl; + for (const auto& student : fail) { + cout << student.name << ": ܳɼ = " << grade(student) << endl; + } + + return 0; +} diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/median.cpp b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/median.cpp new file mode 100644 index 0000000..04515a1 --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/median.cpp @@ -0,0 +1,30 @@ +// source file for the `median' function +#include // to get the declaration of `sort' +#include // to get the declaration of `domain_error' +#include // to get the declaration of `vector' + +using std::domain_error; using std::sort; using std::vector; + +#include "median.h" + +// compute the median of a `vector' +// note that calling this function copies the entire argument `vector' +double median(vector vec) +{ +#ifdef _MSC_VER + typedef std::vector::size_type vec_sz; +#else + typedef vector::size_type vec_sz; +#endif + + vec_sz size = vec.size(); + if (size == 0) + throw domain_error("median of an empty vector"); + + sort(vec.begin(), vec.end()); + + vec_sz mid = size/2; + + return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid]; +} + diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/median.h b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/median.h new file mode 100644 index 0000000..10d2c4f --- /dev/null +++ b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/Curriculum design 3/median.h @@ -0,0 +1,9 @@ +#ifndef GUARD_median_h +#define GUARD_median_h + +// `median.h'--final version +#include +double median(std::vector); + +#endif + diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/张梦南 1220310013 高级语言程序课程设计.docx b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/张梦南 1220310013 高级语言程序课程设计.docx new file mode 100644 index 0000000..b174c2a Binary files /dev/null and b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/张梦南 1220310013 高级语言程序课程设计.docx differ diff --git a/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/张梦南 1220310013 高级语言程序课程设计.pdf b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/张梦南 1220310013 高级语言程序课程设计.pdf new file mode 100644 index 0000000..9719fcf Binary files /dev/null and b/Curriculum Design and Report/张梦南 1220310013 高级语言程序课程设计/张梦南 1220310013 高级语言程序课程设计.pdf differ