From 23df6a3e18d4e12b2974659dc5c3353ac3baee2a Mon Sep 17 00:00:00 2001 From: orazaro Date: Thu, 16 Feb 2012 13:51:35 +0400 Subject: [PATCH] tab => 4 spaces --- 00-00.cpp | 4 +- 00-02.cpp | 4 +- 00-03.cpp | 4 +- 00-04.cpp | 8 +- 01.00.cpp | 10 +- 01.01.cpp | 32 ++--- 02-00.cpp | 58 ++++----- 02-01.cpp | 70 +++++------ 02-05.cpp | 52 ++++---- 02-07.cpp | 6 +- 02-08.cpp | 10 +- 02-09.cpp | 20 ++-- 03-00.cpp | 70 +++++------ 03-01.cpp | 82 ++++++------- 03-02-quartiles.cpp | 82 ++++++------- 03-03-wordscount.cpp | 48 ++++---- 03-04-longest.cpp | 40 +++---- 03-05-several.cpp | 74 ++++++------ 04-00-CppUnit.cpp | 172 +++++++++++++-------------- 04-00-functions.cpp | 94 +++++++-------- 04-02-squares.cpp | 6 +- 04-03-setw.cpp | 16 +-- 04-04-double.cpp | 16 +-- 04-05-wordcount.cpp | 52 ++++---- 04-06-average-test.cpp | 34 +++--- 04-06-average.cpp | 28 ++--- 04-08-lvalue.cpp | 14 +-- 05-00-extract.cpp | 46 +++---- 05-00-pics.cpp | 98 +++++++-------- 05-00-split-test.cpp | 124 +++++++++---------- 05-00-split.cpp | 48 ++++---- 05-01-permuted-index.cpp | 250 +++++++++++++++++++-------------------- 05-05-center-pic.cpp | 46 +++---- 05-09-uppercase.cpp | 16 +-- 05-10-palindromes.cpp | 40 +++---- 05-11-ascenders.cpp | 48 ++++---- 06-00-copy.cpp | 60 +++++----- 06-00-palindromes.cpp | 28 ++--- 06-00-rbackup.cpp | 16 +-- 06-00-split.cpp | 90 +++++++------- 06-00-urls.cpp | 110 ++++++++--------- Makefile | 28 ++--- cppunit_example.cpp | 34 +++--- permuted-index.cpp | 248 +++++++++++++++++++------------------- 44 files changed, 1218 insertions(+), 1218 deletions(-) diff --git a/00-00.cpp b/00-00.cpp index ece8bac..7070a59 100644 --- a/00-00.cpp +++ b/00-00.cpp @@ -3,6 +3,6 @@ int main() { - std::cout << "Hello, world!" << std::endl; - return 0; + std::cout << "Hello, world!" << std::endl; + return 0; } diff --git a/00-02.cpp b/00-02.cpp index 748267b..b51426f 100644 --- a/00-02.cpp +++ b/00-02.cpp @@ -2,6 +2,6 @@ int main() { - std::cout << "This (\") is a quote, and this (\\) is a backslash" << std::endl; - return 0; + std::cout << "This (\") is a quote, and this (\\) is a backslash" << std::endl; + return 0; } diff --git a/00-03.cpp b/00-03.cpp index 0f439d2..b9603fa 100644 --- a/00-03.cpp +++ b/00-03.cpp @@ -2,6 +2,6 @@ int main() { - std::cout << "a\tb\t\t\t\tc" << std::endl; - return 0; + std::cout << "a\tb\t\t\t\tc" << std::endl; + return 0; } diff --git a/00-04.cpp b/00-04.cpp index e4fce58..41ba74a 100644 --- a/00-04.cpp +++ b/00-04.cpp @@ -2,14 +2,14 @@ int main() { - std::cout <<"\ + std::cout <<"\ // a small C++ program\n\ #include \n\ \n\ int main()\n\ {\n\ - std::cout << \"Hello, world!\" << std::endl;\n\ - return 0;\n\ + std::cout << \"Hello, world!\" << std::endl;\n\ + return 0;\n\ }" << std::endl; - return 0; + return 0; } diff --git a/01.00.cpp b/01.00.cpp index d4fe63b..276e787 100644 --- a/01.00.cpp +++ b/01.00.cpp @@ -3,11 +3,11 @@ int main() { - std::cout << "Please enter your first name: "; + std::cout << "Please enter your first name: "; - std::string name; - std::cin >> name; + std::string name; + std::cin >> name; - std::cout << "Hello, " << name << "!" << std::endl; - return 0; + std::cout << "Hello, " << name << "!" << std::endl; + return 0; } diff --git a/01.01.cpp b/01.01.cpp index e7e9fd1..bf1ad1a 100644 --- a/01.01.cpp +++ b/01.01.cpp @@ -3,22 +3,22 @@ int main() { - std::cout << "Please enter your first name: "; + std::cout << "Please enter your first name: "; - std::string name; - std::cin >> name; - - const std::string greeting = "Hello, " + name + "!"; - const std::string spaces(greeting.size(),' '); - const std::string second = "* " + spaces + " *"; - const std::string first(second.size(),'*'); - std::cout - << first << std::endl - << second << std::endl - << "* " << greeting << " *" << std::endl - << second << std::endl - << first << std::endl - ; + std::string name; + std::cin >> name; + + const std::string greeting = "Hello, " + name + "!"; + const std::string spaces(greeting.size(),' '); + const std::string second = "* " + spaces + " *"; + const std::string first(second.size(),'*'); + std::cout + << first << std::endl + << second << std::endl + << "* " << greeting << " *" << std::endl + << second << std::endl + << first << std::endl + ; - return 0; + return 0; } diff --git a/02-00.cpp b/02-00.cpp index 85dcf43..1fc9695 100644 --- a/02-00.cpp +++ b/02-00.cpp @@ -1,40 +1,40 @@ #include #include -using std::cout; using std::endl; +using std::cout; using std::endl; using std::cin; int main() { - cout << "Please enter your first name: "; - std::string name; - cin >> name; - const std::string greeting = "Hello, " + name + "!"; + cout << "Please enter your first name: "; + std::string name; + cin >> name; + const std::string greeting = "Hello, " + name + "!"; - const int pad = 1; - const int rows = pad * 2 + 3; - const std::string::size_type cols = greeting.size() + pad * 2 + 2; + const int pad = 1; + const int rows = pad * 2 + 3; + const std::string::size_type cols = greeting.size() + pad * 2 + 2; - cout << endl; - - // invariant: r rows written so far - for(int r = 0; r != rows; ++r) { - std::string::size_type c = 0; - // invariant: c columns written so far - while( c != cols) { - if(r == pad+1 && (int)c == pad+1) { - cout << greeting; - c += greeting.size(); - } else { - if( r == 0 || r == rows-1 || c == 0 || c == cols-1 ) - cout << '*'; - else - cout << ' '; - ++c; - } - } - cout << endl; - } + cout << endl; + + // invariant: r rows written so far + for(int r = 0; r != rows; ++r) { + std::string::size_type c = 0; + // invariant: c columns written so far + while( c != cols) { + if(r == pad+1 && (int)c == pad+1) { + cout << greeting; + c += greeting.size(); + } else { + if( r == 0 || r == rows-1 || c == 0 || c == cols-1 ) + cout << '*'; + else + cout << ' '; + ++c; + } + } + cout << endl; + } - return 0; + return 0; } diff --git a/02-01.cpp b/02-01.cpp index 0e03411..483d645 100644 --- a/02-01.cpp +++ b/02-01.cpp @@ -1,48 +1,48 @@ #include #include -using std::cout; using std::endl; +using std::cout; using std::endl; using std::cin; int main() { - cout << "Please enter your first name: "; - std::string name; - cin >> name; - const std::string greeting = "Hello, " + name + "!"; + cout << "Please enter your first name: "; + std::string name; + cin >> name; + const std::string greeting = "Hello, " + name + "!"; - int pad = 0; - cout << "Pad: "; - cin >> pad; + int pad = 0; + cout << "Pad: "; + cin >> pad; - cout << endl; + cout << endl; - const int rows = pad * 2 + 3; - const std::string::size_type cols = greeting.size() + pad * 2 + 2; + const int rows = pad * 2 + 3; + const std::string::size_type cols = greeting.size() + pad * 2 + 2; - std::string spaces(greeting.size() + pad * 2,' '); - - // invariant: r rows written so far - for(int r = 0; r != rows; ++r) { - std::string::size_type c = 0; - // invariant: c columns written so far - while( c != cols) { - if(r == pad+1 && (int)c == pad+1) { - cout << greeting; - c += greeting.size(); - } else if(r != 0 && r != rows-1 && r != pad+1 && c == 1) { - cout << spaces; - c += spaces.size(); - } else { - if( r == 0 || r == rows-1 || c == 0 || c == cols-1 ) - cout << '*'; - else - cout << ' '; - ++c; - } - } - cout << endl; - } + std::string spaces(greeting.size() + pad * 2,' '); + + // invariant: r rows written so far + for(int r = 0; r != rows; ++r) { + std::string::size_type c = 0; + // invariant: c columns written so far + while( c != cols) { + if(r == pad+1 && (int)c == pad+1) { + cout << greeting; + c += greeting.size(); + } else if(r != 0 && r != rows-1 && r != pad+1 && c == 1) { + cout << spaces; + c += spaces.size(); + } else { + if( r == 0 || r == rows-1 || c == 0 || c == cols-1 ) + cout << '*'; + else + cout << ' '; + ++c; + } + } + cout << endl; + } - return 0; + return 0; } diff --git a/02-05.cpp b/02-05.cpp index ec676fc..0ab2cb6 100644 --- a/02-05.cpp +++ b/02-05.cpp @@ -7,33 +7,33 @@ using std::cin; int main() { - const int width = 10; - const int height = 4; + const int width = 10; + const int height = 4; - cout << "square" << endl; - for(int i = 0; i < height; ++i) { - for(int j = 0; j < height; ++j) - cout << '*'; - cout << endl; - } - cout << "rectangle" << endl; - for(int i = 0; i < height; ++i) { - for(int j = 0; j < width; ++j) - cout << '*'; - cout << endl; - } - cout << "triangle" << endl; - for(int i = 0; i < height; ++i) { - for(int j = 0; j <= i; ++j) - cout << '*'; - cout << endl; - } + cout << "square" << endl; + for(int i = 0; i < height; ++i) { + for(int j = 0; j < height; ++j) + cout << '*'; + cout << endl; + } + cout << "rectangle" << endl; + for(int i = 0; i < height; ++i) { + for(int j = 0; j < width; ++j) + cout << '*'; + cout << endl; + } + cout << "triangle" << endl; + for(int i = 0; i < height; ++i) { + for(int j = 0; j <= i; ++j) + cout << '*'; + cout << endl; + } - int i = 0; - while (i < 10) { - i += 1; - std::cout << i << std::endl; - } + int i = 0; + while (i < 10) { + i += 1; + std::cout << i << std::endl; + } - return 0; + return 0; } diff --git a/02-07.cpp b/02-07.cpp index dbea1d4..c6e3f10 100644 --- a/02-07.cpp +++ b/02-07.cpp @@ -2,7 +2,7 @@ int main() { - for(int i = 10; i >= -5; --i) - std::cout << i << std::endl; - return 0; + for(int i = 10; i >= -5; --i) + std::cout << i << std::endl; + return 0; } diff --git a/02-08.cpp b/02-08.cpp index 39b23e9..631c987 100644 --- a/02-08.cpp +++ b/02-08.cpp @@ -2,9 +2,9 @@ int main() { - int product = 1; - for(int i = 1; i < 10; ++i) - product *= i; - std::cout << product << std::endl; - return 0; + int product = 1; + for(int i = 1; i < 10; ++i) + product *= i; + std::cout << product << std::endl; + return 0; } diff --git a/02-09.cpp b/02-09.cpp index be125ac..a39e562 100644 --- a/02-09.cpp +++ b/02-09.cpp @@ -2,17 +2,17 @@ int main() { - std::cout << "Please, enter two numbers: "; - int i1, i2; - std::cin >> i1 >> i2; + std::cout << "Please, enter two numbers: "; + int i1, i2; + std::cin >> i1 >> i2; - std::string op = "="; - if(i1 > i2) - op = ">"; - else if(i2 > i1) - op = "<"; + std::string op = "="; + if(i1 > i2) + op = ">"; + else if(i2 > i1) + op = "<"; - std::cout << i1 << " " << op << " " << i2 << std::endl; + std::cout << i1 << " " << op << " " << i2 << std::endl; - return 0; + return 0; } diff --git a/03-00.cpp b/03-00.cpp index c05f08a..cba0f55 100644 --- a/03-00.cpp +++ b/03-00.cpp @@ -7,39 +7,39 @@ using namespace std; int main() { - cout << "Please, enter your name: "; - string name; - cin >> name; - cout << "Hello, " << name << "!" << endl; - - cout << "Please enter your midterm and final exam grades: "; - double midterm, final; - cin >> midterm >> final; - - cout << "Enter all your homework grades, " - "followed by end-of-file: "; - - int count = 0; - double sum = 0; - double x; - while( cin >> x ) { - ++count; - sum += x; - } - - double final_grade = 0.2 * midterm + 0.4 * final; - if(count > 0) { - final_grade += 0.4 * sum / count; - } else { - cerr << "Bad data in input" << endl; - return 1; - } - - streamsize prec = cout.precision(); - cout << "Your final grade is " << setprecision(3) - << final_grade - << setprecision(prec) << endl; - cout << "Precision was " << prec << endl; - - return 0; + cout << "Please, enter your name: "; + string name; + cin >> name; + cout << "Hello, " << name << "!" << endl; + + cout << "Please enter your midterm and final exam grades: "; + double midterm, final; + cin >> midterm >> final; + + cout << "Enter all your homework grades, " + "followed by end-of-file: "; + + int count = 0; + double sum = 0; + double x; + while( cin >> x ) { + ++count; + sum += x; + } + + double final_grade = 0.2 * midterm + 0.4 * final; + if(count > 0) { + final_grade += 0.4 * sum / count; + } else { + cerr << "Bad data in input" << endl; + return 1; + } + + streamsize prec = cout.precision(); + cout << "Your final grade is " << setprecision(3) + << final_grade + << setprecision(prec) << endl; + cout << "Precision was " << prec << endl; + + return 0; } diff --git a/03-01.cpp b/03-01.cpp index beeb6b7..3c94bca 100644 --- a/03-01.cpp +++ b/03-01.cpp @@ -9,45 +9,45 @@ using namespace std; int main() { - cout << "Please, enter your name: "; - string name; - cin >> name; - cout << "Hello, " << name << "!" << endl; - - cout << "Please enter your midterm and final exam grades: "; - double midterm, final; - cin >> midterm >> final; - - cout << "Enter all your homework grades, " - "followed by end-of-file: "; - - double x; - vector homework; - - // invariant: homework contains all the homework grades read so far - while( cin >> x ) { - homework.push_back(x); - } - - typedef vector::size_type vec_sz; - vec_sz size = homework.size(); - if(size == 0) { - cout << endl << "You must enter your grades. " - "Please try again." << endl; - return 1; - } - - sort(homework.begin(), homework.end()); - vec_sz mid = size / 2; - double median; - median = size % 2 == 0 ? - (homework[mid-1] + homework[mid]) / 2 : - homework[mid]; - - streamsize prec = cout.precision(); - cout << "Your final grade is " << setprecision(3) - << 0.2 * midterm + 0.4 * final + 0.4 * median - << setprecision(prec) << endl; - - return 0; + cout << "Please, enter your name: "; + string name; + cin >> name; + cout << "Hello, " << name << "!" << endl; + + cout << "Please enter your midterm and final exam grades: "; + double midterm, final; + cin >> midterm >> final; + + cout << "Enter all your homework grades, " + "followed by end-of-file: "; + + double x; + vector homework; + + // invariant: homework contains all the homework grades read so far + while( cin >> x ) { + homework.push_back(x); + } + + typedef vector::size_type vec_sz; + vec_sz size = homework.size(); + if(size == 0) { + cout << endl << "You must enter your grades. " + "Please try again." << endl; + return 1; + } + + sort(homework.begin(), homework.end()); + vec_sz mid = size / 2; + double median; + median = size % 2 == 0 ? + (homework[mid-1] + homework[mid]) / 2 : + homework[mid]; + + streamsize prec = cout.precision(); + cout << "Your final grade is " << setprecision(3) + << 0.2 * midterm + 0.4 * final + 0.4 * median + << setprecision(prec) << endl; + + return 0; } diff --git a/03-02-quartiles.cpp b/03-02-quartiles.cpp index 3e64db9..04e5e0f 100644 --- a/03-02-quartiles.cpp +++ b/03-02-quartiles.cpp @@ -9,45 +9,45 @@ using namespace std; int main() { - cout << "Enter all your homework grades, \n" - "followed by end-of-file: "; - - double x; - vector homework; - - // invariant: homework contains all the homework grades read so far - while( cin >> x ) { - homework.push_back(x); - } - - typedef vector::size_type vec_sz; - vec_sz size = homework.size(); - if(size == 0) { - cout << endl << "You must enter your grades. " - "Please try again." << endl; - return 1; - } - - sort(homework.begin(), homework.end()); - - int i2 = (int) ( size / 2 ); - int i1 = (int) ( (0 + i2) / 2 ); - int i3 = (int) ( (i2 + size) / 2); - - cout << "Quartiles:" << endl; - for(int i = 0; i < i1; i++) - cout << homework[i] << ' '; - cout << endl; - for(int i = i1; i < i2; i++) - cout << homework[i] << ' '; - cout << endl; - for(int i = i2; i < i3; i++) - cout << homework[i] << ' '; - cout << endl; - for(int i = i3; i < (int)size; i++) - cout << homework[i] << ' '; - cout << endl; - - - return 0; + cout << "Enter all your homework grades, \n" + "followed by end-of-file: "; + + double x; + vector homework; + + // invariant: homework contains all the homework grades read so far + while( cin >> x ) { + homework.push_back(x); + } + + typedef vector::size_type vec_sz; + vec_sz size = homework.size(); + if(size == 0) { + cout << endl << "You must enter your grades. " + "Please try again." << endl; + return 1; + } + + sort(homework.begin(), homework.end()); + + int i2 = (int) ( size / 2 ); + int i1 = (int) ( (0 + i2) / 2 ); + int i3 = (int) ( (i2 + size) / 2); + + cout << "Quartiles:" << endl; + for(int i = 0; i < i1; i++) + cout << homework[i] << ' '; + cout << endl; + for(int i = i1; i < i2; i++) + cout << homework[i] << ' '; + cout << endl; + for(int i = i2; i < i3; i++) + cout << homework[i] << ' '; + cout << endl; + for(int i = i3; i < (int)size; i++) + cout << homework[i] << ' '; + cout << endl; + + + return 0; } diff --git a/03-03-wordscount.cpp b/03-03-wordscount.cpp index 171f7d6..c8704d7 100644 --- a/03-03-wordscount.cpp +++ b/03-03-wordscount.cpp @@ -6,30 +6,30 @@ using namespace std; int main() { - vector words; - vector counts; - - for(string word; cin >> word; ) - { - bool found = false; - int size = (int) words.size(); - for(int i = 0; i < size; i++) - { - if(words[i] == word) { - counts[i]++; - found = true; - break; - } - } - if(!found) { - words.push_back(word); - counts.push_back(1); - } - } + vector words; + vector counts; + + for(string word; cin >> word; ) + { + bool found = false; + int size = (int) words.size(); + for(int i = 0; i < size; i++) + { + if(words[i] == word) { + counts[i]++; + found = true; + break; + } + } + if(!found) { + words.push_back(word); + counts.push_back(1); + } + } - int size = (int) words.size(); - for(int i = 0; i < size; i++) - cout << words[i] << " " << counts[i] << endl; + int size = (int) words.size(); + for(int i = 0; i < size; i++) + cout << words[i] << " " << counts[i] << endl; - return 0; + return 0; } diff --git a/03-04-longest.cpp b/03-04-longest.cpp index 1f88d20..6b679fe 100644 --- a/03-04-longest.cpp +++ b/03-04-longest.cpp @@ -5,28 +5,28 @@ using namespace std; int main() { - string strmin, strmax, word; - string::size_type lenmin,lenmax; + string strmin, strmax, word; + string::size_type lenmin,lenmax; - cin >> word; - strmin = strmax = word; - lenmin = lenmax = word.size(); + cin >> word; + strmin = strmax = word; + lenmin = lenmax = word.size(); - while(cin >> word) - { - string::size_type len = word.size(); - if(len > lenmax) { - lenmax = len; - strmax = word; - } - if(len < lenmin) { - lenmin = len; - strmin = word; - } - } + while(cin >> word) + { + string::size_type len = word.size(); + if(len > lenmax) { + lenmax = len; + strmax = word; + } + if(len < lenmin) { + lenmin = len; + strmin = word; + } + } - cout << "longest: " << strmax << " [" << lenmax << "]" << endl; - cout << "shortest: " << strmin << " [" << lenmin << "]" << endl; + cout << "longest: " << strmax << " [" << lenmax << "]" << endl; + cout << "shortest: " << strmin << " [" << lenmin << "]" << endl; - return 0; + return 0; } diff --git a/03-05-several.cpp b/03-05-several.cpp index ebf0264..7464e69 100644 --- a/03-05-several.cpp +++ b/03-05-several.cpp @@ -6,41 +6,41 @@ using namespace std; int main() { - vector students; - vector grades; - - while(1) { - cout << "Please, enter your name or end: "; - string name; - cin >> name; - if( name == "end" ) break; - cout << "Hello, " << name << "!" << endl; - - cout << "Please enter your midterm and final exam grades: "; - double midterm, final; - cin >> midterm >> final; - - int ngrades = 3; - cout << "Enter "<< ngrades << " your homework grades: "; - - int count = 0; - double sum = 0; - double x; - for(int i = 0; i < ngrades && cin >> x; i++) { - ++count; - sum += x; - } - - double grade = 0.2 * midterm + 0.4 * final + 0.4 * sum / count; - students.push_back(name); - grades.push_back(grade); - } - - cout << endl << "Students:" << endl; - for(int i = 0; i < (int)students.size(); i++) - { - cout << students[i] << " " << grades[i] << endl; - } - - return 0; + vector students; + vector grades; + + while(1) { + cout << "Please, enter your name or end: "; + string name; + cin >> name; + if( name == "end" ) break; + cout << "Hello, " << name << "!" << endl; + + cout << "Please enter your midterm and final exam grades: "; + double midterm, final; + cin >> midterm >> final; + + int ngrades = 3; + cout << "Enter "<< ngrades << " your homework grades: "; + + int count = 0; + double sum = 0; + double x; + for(int i = 0; i < ngrades && cin >> x; i++) { + ++count; + sum += x; + } + + double grade = 0.2 * midterm + 0.4 * final + 0.4 * sum / count; + students.push_back(name); + grades.push_back(grade); + } + + cout << endl << "Students:" << endl; + for(int i = 0; i < (int)students.size(); i++) + { + cout << students[i] << " " << grades[i] << endl; + } + + return 0; } diff --git a/04-00-CppUnit.cpp b/04-00-CppUnit.cpp index 93dc626..490fa46 100644 --- a/04-00-CppUnit.cpp +++ b/04-00-CppUnit.cpp @@ -10,45 +10,45 @@ using namespace std; double grade(double midterm, double final, double homework) { - return 0.2 * midterm + 0.4 * final + 0.4 * homework; + return 0.2 * midterm + 0.4 * final + 0.4 * homework; } double median(vector vec) { - typedef vector::size_type vec_sz; - - 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-1] + vec[mid]) / 2 : - vec[mid]; + typedef vector::size_type vec_sz; + + 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-1] + vec[mid]) / 2 : + vec[mid]; } 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)); + if( hw.size() == 0 ) + throw domain_error("student has done no homework"); + return grade(midterm, final, median(hw)); } istream& read_hw(istream& in, vector& hw) { - if(in) { - // reset vector - hw.clear(); - double x; - while( in >> x ) - hw.push_back(x); - // clear failure flag for istream - in.clear(); - } - return in; + if(in) { + // reset vector + hw.clear(); + double x; + while( in >> x ) + hw.push_back(x); + // clear failure flag for istream + in.clear(); + } + return in; } /* CppUnit tests START */ @@ -56,34 +56,34 @@ istream& read_hw(istream& in, vector& hw) class myTest : public CppUnit::TestFixture { public: - void setUp() {} - void tearDown() {} - - void checkGrade() { - CPPUNIT_ASSERT_MESSAGE("grade_0_0_0", 0 == grade(0,0,0)); - double grade_1_1_1 = 1; - CPPUNIT_ASSERT_EQUAL(grade_1_1_1, grade(1,1,1)); - double grade_1_2_3 = 2.2; - CPPUNIT_ASSERT_EQUAL(grade_1_2_3, grade(1,2,3)); - } - void checkMedian() { - vector vec(5,0); - double med = 0; - CPPUNIT_ASSERT_EQUAL_MESSAGE("bad median", med, median(vec)); - vec[0] = 1; med = 0; - CPPUNIT_ASSERT_EQUAL_MESSAGE("bad median", med, median(vec)); - vec[0] = 1; vec[1] = 2; med = 0; - CPPUNIT_ASSERT_EQUAL_MESSAGE("bad median", med, median(vec)); - vec[0] = 1; vec[1] = 2; vec[2] = 3; med = 1; - CPPUNIT_ASSERT_EQUAL_MESSAGE("bad median", med, median(vec)); - double array[] = {2,5,4,3,1}; med = 3; - vec = vector(array, array+sizeof(array)/sizeof(double)); - CPPUNIT_ASSERT_EQUAL_MESSAGE("bad median", med, median(vec)); - } - CPPUNIT_TEST_SUITE( myTest ); - CPPUNIT_TEST( checkGrade ); - CPPUNIT_TEST( checkMedian ); - CPPUNIT_TEST_SUITE_END(); + void setUp() {} + void tearDown() {} + + void checkGrade() { + CPPUNIT_ASSERT_MESSAGE("grade_0_0_0", 0 == grade(0,0,0)); + double grade_1_1_1 = 1; + CPPUNIT_ASSERT_EQUAL(grade_1_1_1, grade(1,1,1)); + double grade_1_2_3 = 2.2; + CPPUNIT_ASSERT_EQUAL(grade_1_2_3, grade(1,2,3)); + } + void checkMedian() { + vector vec(5,0); + double med = 0; + CPPUNIT_ASSERT_EQUAL_MESSAGE("bad median", med, median(vec)); + vec[0] = 1; med = 0; + CPPUNIT_ASSERT_EQUAL_MESSAGE("bad median", med, median(vec)); + vec[0] = 1; vec[1] = 2; med = 0; + CPPUNIT_ASSERT_EQUAL_MESSAGE("bad median", med, median(vec)); + vec[0] = 1; vec[1] = 2; vec[2] = 3; med = 1; + CPPUNIT_ASSERT_EQUAL_MESSAGE("bad median", med, median(vec)); + double array[] = {2,5,4,3,1}; med = 3; + vec = vector(array, array+sizeof(array)/sizeof(double)); + CPPUNIT_ASSERT_EQUAL_MESSAGE("bad median", med, median(vec)); + } + CPPUNIT_TEST_SUITE( myTest ); + CPPUNIT_TEST( checkGrade ); + CPPUNIT_TEST( checkMedian ); + CPPUNIT_TEST_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION ( myTest ); #include @@ -101,36 +101,36 @@ bool main_tests () int main() { - if(!main_tests()) { - std::cerr << "bad test result: exit" << endl; - return 1; - } - cout << "Please, enter your name: "; - string name; - cin >> name; - cout << "Hello, " << name << "!" << endl; - - cout << "Please enter your midterm and final exam grades: "; - double midterm, final; - cin >> midterm >> final; - - cout << "Enter all your homework grades, " - "followed by end-of-file: "; - - vector homework; - - read_hw(cin, homework); - - try { - double final_grade = grade(midterm, final, homework); - streamsize prec = cout.precision(); - cout << "Your final grade is " << setprecision(3) - << final_grade << setprecision(prec) << endl; - } catch (domain_error) { - cout << endl << "You must enter your grades. " - "Please try again" << endl; - return 1; - } - return 0; + if(!main_tests()) { + std::cerr << "bad test result: exit" << endl; + return 1; + } + cout << "Please, enter your name: "; + string name; + cin >> name; + cout << "Hello, " << name << "!" << endl; + + cout << "Please enter your midterm and final exam grades: "; + double midterm, final; + cin >> midterm >> final; + + cout << "Enter all your homework grades, " + "followed by end-of-file: "; + + vector homework; + + read_hw(cin, homework); + + try { + double final_grade = grade(midterm, final, homework); + streamsize prec = cout.precision(); + cout << "Your final grade is " << setprecision(3) + << final_grade << setprecision(prec) << endl; + } catch (domain_error) { + cout << endl << "You must enter your grades. " + "Please try again" << endl; + return 1; + } + return 0; } diff --git a/04-00-functions.cpp b/04-00-functions.cpp index 4fabf67..f0e0fc6 100644 --- a/04-00-functions.cpp +++ b/04-00-functions.cpp @@ -10,74 +10,74 @@ using namespace std; double grade(double midterm, double final, double homework) { - return 0.2 * midterm + 0.4 * final + 0.4 * homework; + return 0.2 * midterm + 0.4 * final + 0.4 * homework; } double median(vector vec) { - typedef vector::size_type vec_sz; + typedef vector::size_type vec_sz; - 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; + 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-1] + vec[mid]) / 2 : - vec[mid]; + return + size % 2 == 0 ? + (vec[mid-1] + vec[mid]) / 2 : + vec[mid]; } 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)); + if( hw.size() == 0 ) + throw domain_error("student has done no homework"); + return grade(midterm, final, median(hw)); } istream& read_hw(istream& in, vector& hw) { - if(in) { - // reset vector - hw.clear(); - double x; - while( in >> x ) - hw.push_back(x); - // clear failure flag for istream - in.clear(); - } - return in; + if(in) { + // reset vector + hw.clear(); + double x; + while( in >> x ) + hw.push_back(x); + // clear failure flag for istream + in.clear(); + } + return in; } int main() { - cout << "Please, enter your name: "; - string name; - cin >> name; - cout << "Hello, " << name << "!" << endl; + cout << "Please, enter your name: "; + string name; + cin >> name; + cout << "Hello, " << name << "!" << endl; - cout << "Please enter your midterm and final exam grades: "; - double midterm, final; - cin >> midterm >> final; + cout << "Please enter your midterm and final exam grades: "; + double midterm, final; + cin >> midterm >> final; - cout << "Enter all your homework grades, " - "followed by end-of-file: "; + cout << "Enter all your homework grades, " + "followed by end-of-file: "; - vector homework; + vector homework; - read_hw(cin, homework); + read_hw(cin, homework); - try { - double final_grade = grade(midterm, final, homework); - streamsize prec = cout.precision(); - cout << "Your final grade is " << setprecision(3) - << final_grade << setprecision(prec) << endl; - } catch (domain_error) { - cout << endl << "You must enter your grades. " - "Please try again" << endl; - return 1; - } - return 0; + try { + double final_grade = grade(midterm, final, homework); + streamsize prec = cout.precision(); + cout << "Your final grade is " << setprecision(3) + << final_grade << setprecision(prec) << endl; + } catch (domain_error) { + cout << endl << "You must enter your grades. " + "Please try again" << endl; + return 1; + } + return 0; } diff --git a/04-02-squares.cpp b/04-02-squares.cpp index aa94d7d..e809adb 100644 --- a/04-02-squares.cpp +++ b/04-02-squares.cpp @@ -5,7 +5,7 @@ using std::setw; int main() { - for(int i = 0; i <= 100; ++i) { - std::cout << setw(3) << i << setw(6) << i*i << std::endl; - } + for(int i = 0; i <= 100; ++i) { + std::cout << setw(3) << i << setw(6) << i*i << std::endl; + } } diff --git a/04-03-setw.cpp b/04-03-setw.cpp index 59503f3..d4285e6 100644 --- a/04-03-setw.cpp +++ b/04-03-setw.cpp @@ -5,15 +5,15 @@ using std::setw; int main() { - int n = 99; - int w1 = 0; + int n = 99; + int w1 = 0; - for(int m = n+1; m > 0; m = m / 10) - w1++; + for(int m = n+1; m > 0; m = m / 10) + w1++; - int w2 = w1 << 1; + int w2 = w1 << 1; - for(int i = 0; i < n; ++i) { - std::cout << setw(w1) << i << setw(w2) << i*i << std::endl; - } + for(int i = 0; i < n; ++i) { + std::cout << setw(w1) << i << setw(w2) << i*i << std::endl; + } } diff --git a/04-04-double.cpp b/04-04-double.cpp index 784b4e9..8f3d0bb 100644 --- a/04-04-double.cpp +++ b/04-04-double.cpp @@ -7,14 +7,14 @@ using std::fixed; int main() { - double xmin = 0; - double xmax = 9.9; - double dx = 0.1; + double xmin = 0; + double xmax = 9.9; + double dx = 0.1; - int w1 = 4, w2 = w1 * 2; + int w1 = 4, w2 = w1 * 2; - for(double x = xmin; x < xmax; x+=dx) { - std::cout << setprecision(3) << fixed - << setw(w1) << x << setw(w2) << x*x << std::endl; - } + for(double x = xmin; x < xmax; x+=dx) { + std::cout << setprecision(3) << fixed + << setw(w1) << x << setw(w2) << x*x << std::endl; + } } diff --git a/04-05-wordcount.cpp b/04-05-wordcount.cpp index f8dfd92..4a1fcf9 100644 --- a/04-05-wordcount.cpp +++ b/04-05-wordcount.cpp @@ -3,42 +3,42 @@ #include struct WordCount { - std::string word; - int count; + std::string word; + int count; }; typedef std::vector::size_type vec_sz; int read(std::istream& in, std::vector& vec) { - std::string word; - int words = 0; - while(in >> word) { - words++; - bool found = false; - for(vec_sz i = 0; i != vec.size(); ++i) - if(vec[i].word == word) { - vec[i].count ++; - found = true; - break; - } - if(!found) { - WordCount wc = {word,1}; - vec.push_back(wc); - } - } - return words; + std::string word; + int words = 0; + while(in >> word) { + words++; + bool found = false; + for(vec_sz i = 0; i != vec.size(); ++i) + if(vec[i].word == word) { + vec[i].count ++; + found = true; + break; + } + if(!found) { + WordCount wc = {word,1}; + vec.push_back(wc); + } + } + return words; } int main() { - std::vector vec; - int words = read(std::cin, vec); - std::cout << "Result:" << std::endl; - for(vec_sz i = 0; i != vec.size(); ++i) - std::cout << vec[i].word << " " << vec[i].count << std::endl; - std::cout << "sum=" << words << std::endl; + std::vector vec; + int words = read(std::cin, vec); + std::cout << "Result:" << std::endl; + for(vec_sz i = 0; i != vec.size(); ++i) + std::cout << vec[i].word << " " << vec[i].count << std::endl; + std::cout << "sum=" << words << std::endl; - return 0; + return 0; } diff --git a/04-06-average-test.cpp b/04-06-average-test.cpp index 21959aa..7650cd1 100644 --- a/04-06-average-test.cpp +++ b/04-06-average-test.cpp @@ -3,12 +3,12 @@ #include double CalcAverage(const std::vector& vec) -{ - typedef std::vector::size_type vec_sz; - double sum = 0; - for(vec_sz i = 0; i < vec.size(); ++i) - sum += vec[i]; - return sum / vec.size(); +{ + typedef std::vector::size_type vec_sz; + double sum = 0; + for(vec_sz i = 0; i < vec.size(); ++i) + sum += vec[i]; + return sum / vec.size(); } /* SETUP TESTS */ @@ -16,22 +16,22 @@ double CalcAverage(const std::vector& vec) #include class myTest : public CppUnit::TestFixture { public: - void setUp() {} - void tearDown() {} + void setUp() {} + void tearDown() {} - void checkResult(); - CPPUNIT_TEST_SUITE( myTest ); - CPPUNIT_TEST( checkResult ); - CPPUNIT_TEST_SUITE_END(); + void checkResult(); + CPPUNIT_TEST_SUITE( myTest ); + CPPUNIT_TEST( checkResult ); + CPPUNIT_TEST_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION ( myTest ); - + void myTest::checkResult() { - double g = 4.16666666666667; - double array[] = {1,2,3,4,5,10}; - std::vector vec = std::vector(array, array+sizeof(array)/sizeof(double)); - CPPUNIT_ASSERT_DOUBLES_EQUAL(g, CalcAverage(vec), 0.001); + double g = 4.16666666666667; + double array[] = {1,2,3,4,5,10}; + std::vector vec = std::vector(array, array+sizeof(array)/sizeof(double)); + CPPUNIT_ASSERT_DOUBLES_EQUAL(g, CalcAverage(vec), 0.001); } #include "cppunit_run.inc" diff --git a/04-06-average.cpp b/04-06-average.cpp index cc291c4..9c1538a 100644 --- a/04-06-average.cpp +++ b/04-06-average.cpp @@ -3,19 +3,19 @@ int main() -{ - typedef std::vector::size_type vec_sz; - std::vector vec; - for(double x; std::cin >> x;) - vec.push_back(x); - double sum = 0; - for(vec_sz i = 0; i < vec.size(); ++i) - sum += vec[i]; +{ + typedef std::vector::size_type vec_sz; + std::vector vec; + for(double x; std::cin >> x;) + vec.push_back(x); + double sum = 0; + for(vec_sz i = 0; i < vec.size(); ++i) + sum += vec[i]; - std::cout - << "average=" - << sum / vec.size() - << std::endl; - - return 0; + std::cout + << "average=" + << sum / vec.size() + << std::endl; + + return 0; } diff --git a/04-08-lvalue.cpp b/04-08-lvalue.cpp index 11c0471..600dd6f 100644 --- a/04-08-lvalue.cpp +++ b/04-08-lvalue.cpp @@ -3,15 +3,15 @@ std::vector& f() { - std::vector *v = new std::vector; - v->push_back(1); - v->push_back(2); - return *v; + std::vector *v = new std::vector; + v->push_back(1); + v->push_back(2); + return *v; } int main() { - int n = 1; - double d = f()[n]; - std::cout << d << std::endl; + int n = 1; + double d = f()[n]; + std::cout << d << std::endl; } diff --git a/05-00-extract.cpp b/05-00-extract.cpp index e2e881a..8ee7484 100644 --- a/05-00-extract.cpp +++ b/05-00-extract.cpp @@ -4,36 +4,36 @@ std::vector extract_odd(std::vector& vec) { - std::vector odd, even; - for(std::vector::size_type i = 0; - i != vec.size(); ++i) - if( vec[i] % 2 == 0 ) - even.push_back(vec[i]); - else - odd.push_back(vec[i]); - - vec = even; - return odd; + std::vector odd, even; + for(std::vector::size_type i = 0; + i != vec.size(); ++i) + if( vec[i] % 2 == 0 ) + even.push_back(vec[i]); + else + odd.push_back(vec[i]); + + vec = even; + return odd; } void out_vec(const std::vector& vec, const std::string title) { - std::cout << title; - for(std::vector::size_type i = 0; - i != vec.size(); ++i) - std::cout << " " << vec[i]; + std::cout << title; + for(std::vector::size_type i = 0; + i != vec.size(); ++i) + std::cout << " " << vec[i]; } int main() { - std::vector vec; - - for(int x; std::cin >> x;) - vec.push_back(x); - - std::vector odd = extract_odd(vec); - out_vec(odd,"odd"); std::cout << std::endl; - out_vec(vec,"even"); std::cout << std::endl; + std::vector vec; + + for(int x; std::cin >> x;) + vec.push_back(x); + + std::vector odd = extract_odd(vec); + out_vec(odd,"odd"); std::cout << std::endl; + out_vec(vec,"even"); std::cout << std::endl; - return 0; + return 0; } diff --git a/05-00-pics.cpp b/05-00-pics.cpp index 6351f55..3951030 100644 --- a/05-00-pics.cpp +++ b/05-00-pics.cpp @@ -6,78 +6,78 @@ using namespace std; void out(const std::vector& v) { - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) - cout << *iter << endl; + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) + cout << *iter << endl; } std::string::size_type width(const std::vector& v) { - string::size_type maxlen = 0; - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) - maxlen = max(maxlen, iter->size()); - return maxlen; + string::size_type maxlen = 0; + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) + maxlen = max(maxlen, iter->size()); + return maxlen; } std::vector frame(const std::vector& v) { - vector ret; - string::size_type maxlen = width(v); - string border(maxlen + 4, '*'); - ret.push_back(border); - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) { - ret.push_back("* " + (*iter) + string(maxlen - iter->size(),' ') + " *"); - } - ret.push_back(border); - return ret; + vector ret; + string::size_type maxlen = width(v); + string border(maxlen + 4, '*'); + ret.push_back(border); + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) { + ret.push_back("* " + (*iter) + string(maxlen - iter->size(),' ') + " *"); + } + ret.push_back(border); + return ret; } std::vector vcat(const std::vector& top, - const std::vector& bottom) + const std::vector& bottom) { - vector ret = top; + vector ret = top; #if 0 - for(std::vector::const_iterator it = bottom.begin(); - it != bottom.end(); ++it) { - ret.push_back(*it); - } + for(std::vector::const_iterator it = bottom.begin(); + it != bottom.end(); ++it) { + ret.push_back(*it); + } #else - ret.insert(ret.end(),bottom.begin(),bottom.end()); + ret.insert(ret.end(),bottom.begin(),bottom.end()); #endif - return ret; + return ret; } std::vector hcat(const std::vector& left, - const std::vector& right) + const std::vector& right) { - vector ret; - string::size_type width1 = width(left) + 1; - vector::size_type i = 0, j = 0; + vector ret; + string::size_type width1 = width(left) + 1; + vector::size_type i = 0, j = 0; - while(i != left.size() || j != right.size()) - { - string s; - if(i != left.size()) - s = left[i++]; - s += string(width1 - s.size(),' '); - if(j != right.size()) - s += right[j++]; - ret.push_back(s); - } + while(i != left.size() || j != right.size()) + { + string s; + if(i != left.size()) + s = left[i++]; + s += string(width1 - s.size(),' '); + if(j != right.size()) + s += right[j++]; + ret.push_back(s); + } - return ret; + return ret; } int main() { - // read data - vector v,f; - string line; - while(getline(cin, line)) - v.push_back(line); - f = frame(v); - out(vcat(v,f)); - out(hcat(v,f)); + // read data + vector v,f; + string line; + while(getline(cin, line)) + v.push_back(line); + f = frame(v); + out(vcat(v,f)); + out(hcat(v,f)); } diff --git a/05-00-split-test.cpp b/05-00-split-test.cpp index e0d73f9..288806d 100644 --- a/05-00-split-test.cpp +++ b/05-00-split-test.cpp @@ -5,46 +5,46 @@ #if 1 std::vector split(const std::string& s) -{ - std::vector vec; - std::string::const_iterator i = s.begin(); - while(i != s.end()) - { - while(i != s.end() && isspace(*i)) - ++i; - std::string::const_iterator j = i; - while(j != s.end() && !isspace(*j)) - ++j; - if(j != i) { - std::string token = std::string(i, j); - vec.push_back(token); - i = j; - } - } +{ + std::vector vec; + std::string::const_iterator i = s.begin(); + while(i != s.end()) + { + while(i != s.end() && isspace(*i)) + ++i; + std::string::const_iterator j = i; + while(j != s.end() && !isspace(*j)) + ++j; + if(j != i) { + std::string token = std::string(i, j); + vec.push_back(token); + i = j; + } + } - return vec; + return vec; } #else std::vector split(const std::string& s) -{ - std::vector vec; - typedef std::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(j != i) { - std::string token = s.substr(i, j-i); - vec.push_back(token); - i = j; - } - } +{ + std::vector vec; + typedef std::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(j != i) { + std::string token = s.substr(i, j-i); + vec.push_back(token); + i = j; + } + } - return vec; + return vec; } #endif @@ -53,41 +53,41 @@ std::vector split(const std::string& s) #include class myTest : public CppUnit::TestFixture { public: - void setUp() {} - void tearDown() {} + void setUp() {} + void tearDown() {} - void checkResult(); - CPPUNIT_TEST_SUITE( myTest ); - CPPUNIT_TEST( checkResult ); - CPPUNIT_TEST_SUITE_END(); + void checkResult(); + CPPUNIT_TEST_SUITE( myTest ); + CPPUNIT_TEST( checkResult ); + CPPUNIT_TEST_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION ( myTest ); - + void myTest::checkResult() { - typedef std::vector vec_str; - - vec_str vec = split(std::string("")); - CPPUNIT_ASSERT_EQUAL((vec_str::size_type)0, vec.size()); - vec = split(std::string("test")); - CPPUNIT_ASSERT_EQUAL((vec_str::size_type)1, vec.size()); + typedef std::vector vec_str; + + vec_str vec = split(std::string("")); + CPPUNIT_ASSERT_EQUAL((vec_str::size_type)0, vec.size()); + vec = split(std::string("test")); + CPPUNIT_ASSERT_EQUAL((vec_str::size_type)1, vec.size()); - const std::string input(" \tone two\n tree\tfour five\t\t"); - vec_str etalon; - etalon.push_back("one"); - etalon.push_back("two"); - etalon.push_back("tree"); - etalon.push_back("four"); - etalon.push_back("five"); + const std::string input(" \tone two\n tree\tfour five\t\t"); + vec_str etalon; + etalon.push_back("one"); + etalon.push_back("two"); + etalon.push_back("tree"); + etalon.push_back("four"); + etalon.push_back("five"); - vec = split(input); + vec = split(input); - CPPUNIT_ASSERT_EQUAL((vec_str::size_type)5, vec.size()); - std::cout << std::endl << input << std::endl; - for(vec_str::size_type i = 0; i != vec.size(); ++i) { - std::cout << i << " " << vec[i] << std::endl; - CPPUNIT_ASSERT_EQUAL(etalon[i], vec[i]); - } + CPPUNIT_ASSERT_EQUAL((vec_str::size_type)5, vec.size()); + std::cout << std::endl << input << std::endl; + for(vec_str::size_type i = 0; i != vec.size(); ++i) { + std::cout << i << " " << vec[i] << std::endl; + CPPUNIT_ASSERT_EQUAL(etalon[i], vec[i]); + } } #include "cppunit_run.inc" diff --git a/05-00-split.cpp b/05-00-split.cpp index 5e1bcbf..8fbb73c 100644 --- a/05-00-split.cpp +++ b/05-00-split.cpp @@ -4,32 +4,32 @@ #include std::vector split(const std::string& s) -{ - std::vector vec; - std::string::const_iterator i = s.begin(); - while(i != s.end()) - { - while(i != s.end() && isspace(*i)) - ++i; - std::string::const_iterator j = i; - while(j != s.end() && !isspace(*j)) - ++j; - if(j != i) { - std::string token = std::string(i, j); - vec.push_back(token); - i = j; - } - } +{ + std::vector vec; + std::string::const_iterator i = s.begin(); + while(i != s.end()) + { + while(i != s.end() && isspace(*i)) + ++i; + std::string::const_iterator j = i; + while(j != s.end() && !isspace(*j)) + ++j; + if(j != i) { + std::string token = std::string(i, j); + vec.push_back(token); + i = j; + } + } - return vec; + return vec; } int main() { - std::string s; - while(std::getline(std::cin, s)) { - std::vector v = split(s); - for(std::vector::size_type i = 0; i != v.size(); ++i) - std::cout << "[" << v[i] << "]" << std::endl; - } - return 0; + std::string s; + while(std::getline(std::cin, s)) { + std::vector v = split(s); + for(std::vector::size_type i = 0; i != v.size(); ++i) + std::cout << "[" << v[i] << "]" << std::endl; + } + return 0; } diff --git a/05-01-permuted-index.cpp b/05-01-permuted-index.cpp index 0c897bb..2f62eb4 100644 --- a/05-01-permuted-index.cpp +++ b/05-01-permuted-index.cpp @@ -5,76 +5,76 @@ using namespace std; std::vector split(const std::string& s) -{ - std::vector vec; - std::string::const_iterator i = s.begin(); - while(i != s.end()) - { - while(i != s.end() && isspace(*i)) - ++i; - std::string::const_iterator j = i; - while(j != s.end() && !isspace(*j)) - ++j; - if(j != i) { - std::string token = std::string(i, j); - vec.push_back(token); - i = j; - } - } - - return vec; +{ + std::vector vec; + std::string::const_iterator i = s.begin(); + while(i != s.end()) + { + while(i != s.end() && isspace(*i)) + ++i; + std::string::const_iterator j = i; + while(j != s.end() && !isspace(*j)) + ++j; + if(j != i) { + std::string token = std::string(i, j); + vec.push_back(token); + i = j; + } + } + + return vec; } std::string::size_type width(const std::vector& v) { - string::size_type maxlen = 0; - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) - maxlen = max(maxlen, iter->size()); - return maxlen; + string::size_type maxlen = 0; + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) + maxlen = max(maxlen, iter->size()); + return maxlen; } std::vector hcat(const std::vector& left, - const std::vector& right) + const std::vector& right) { - vector ret; - string::size_type w1 = 36, w2 = 38, dw = 3; - vector::size_type i = 0, j = 0; - - while(i != left.size() || j != right.size()) - { - string s; - if(i != left.size()) { - string s2 = left[i++]; - if(s2.size() >= w1) { - s = s2.substr(s2.size() - w1, w1); - } else { - s = string(w1 - s2.size(),' ') + s2; - } - s += string(dw, ' '); - } else { - s = string(w1 + dw, ' '); - } - if(j != right.size()) { - string s2 = right[j++]; - if(s2.size() >= w2) { - s += s2.substr(0, w2); - } else { - s += s2; - s += string(w2 - s2.size(),' '); - } - } - ret.push_back(s); - } - - return ret; + vector ret; + string::size_type w1 = 36, w2 = 38, dw = 3; + vector::size_type i = 0, j = 0; + + while(i != left.size() || j != right.size()) + { + string s; + if(i != left.size()) { + string s2 = left[i++]; + if(s2.size() >= w1) { + s = s2.substr(s2.size() - w1, w1); + } else { + s = string(w1 - s2.size(),' ') + s2; + } + s += string(dw, ' '); + } else { + s = string(w1 + dw, ' '); + } + if(j != right.size()) { + string s2 = right[j++]; + if(s2.size() >= w2) { + s += s2.substr(0, w2); + } else { + s += s2; + s += string(w2 - s2.size(),' '); + } + } + ret.push_back(s); + } + + return ret; } void out(const std::vector& v) { - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) - cout << *iter << endl; + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) + cout << *iter << endl; } const string endmark(""); @@ -82,88 +82,88 @@ const string sepmark(""); void rotate(vector& rot) { - string beg = rot[0]; - rot.erase(rot.begin()); - rot.push_back(beg); + string beg = rot[0]; + rot.erase(rot.begin()); + rot.push_back(beg); } void rotateback(vector& rot) { - string end = *(rot.rbegin()); - rot.pop_back(); - rot.insert(rot.begin(), end); + string end = *(rot.rbegin()); + rot.pop_back(); + rot.insert(rot.begin(), end); } string merge(const vector& vec) { - if(!vec.size()) return string(""); - vector::const_iterator it = vec.begin(); - string m = *it; - for(++it; it != vec.end(); ++it) { - m += " " + *it; - } - return m; + if(!vec.size()) return string(""); + vector::const_iterator it = vec.begin(); + string m = *it; + for(++it; it != vec.end(); ++it) { + m += " " + *it; + } + return m; } void read(vector& rot, const string& str) { - /*convert*/ - string s = str; - for(string::size_type i = 0; i != str.size(); ++i) { - if(isalnum(str[i])) - s[i] = tolower(str[i]); - else - s[i] = ' '; - } - - vector vec = split(s); - int n = (int)vec.size(); - vec.push_back(endmark); - for(int i = 0; i < n; ++i) { - rot.push_back(merge(vec)); - rotate(vec); - } + /*convert*/ + string s = str; + for(string::size_type i = 0; i != str.size(); ++i) { + if(isalnum(str[i])) + s[i] = tolower(str[i]); + else + s[i] = ' '; + } + + vector vec = split(s); + int n = (int)vec.size(); + vec.push_back(endmark); + for(int i = 0; i < n; ++i) { + rot.push_back(merge(vec)); + rotate(vec); + } } int main(int argc, char* argv[]) { - vector rot; - - if(argc > 1 && string(argv[1]) == string("test")) { - read(rot, "The quick brown fox"); - read(rot, "jumped over the fence ogogo"); - } - else { - string s; - while(getline(cin, s)) - read(rot, s); - } - - sort(rot.begin(), rot.end()); - - vector left, right, ret; - for(vector::iterator it = rot.begin(); - it != rot.end(); ++it) - { - vector vec = split(*it); - vec.insert(vec.begin(), sepmark); - while(*(vec.rbegin()) != endmark) - rotateback(vec); - vector l, r; - vector::const_iterator i = vec.begin(),j; - j = i; - while(*j != sepmark) ++j; - l = vector(i,j); - ++j; i = j; - while(*j != endmark) ++j; - r = vector(i,j); - left.push_back(merge(l)); - right.push_back(merge(r)); - } - - ret = hcat(left, right); - out(ret); - - return 0; + vector rot; + + if(argc > 1 && string(argv[1]) == string("test")) { + read(rot, "The quick brown fox"); + read(rot, "jumped over the fence ogogo"); + } + else { + string s; + while(getline(cin, s)) + read(rot, s); + } + + sort(rot.begin(), rot.end()); + + vector left, right, ret; + for(vector::iterator it = rot.begin(); + it != rot.end(); ++it) + { + vector vec = split(*it); + vec.insert(vec.begin(), sepmark); + while(*(vec.rbegin()) != endmark) + rotateback(vec); + vector l, r; + vector::const_iterator i = vec.begin(),j; + j = i; + while(*j != sepmark) ++j; + l = vector(i,j); + ++j; i = j; + while(*j != endmark) ++j; + r = vector(i,j); + left.push_back(merge(l)); + right.push_back(merge(r)); + } + + ret = hcat(left, right); + out(ret); + + return 0; } diff --git a/05-05-center-pic.cpp b/05-05-center-pic.cpp index 1639e64..260c2f4 100644 --- a/05-05-center-pic.cpp +++ b/05-05-center-pic.cpp @@ -6,39 +6,39 @@ using namespace std; void out(const std::vector& v) { - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) - cout << *iter << endl; + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) + cout << *iter << endl; } std::string::size_type width(const std::vector& v) { - string::size_type maxlen = 0; - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) - maxlen = max(maxlen, iter->size()); - return maxlen; + string::size_type maxlen = 0; + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) + maxlen = max(maxlen, iter->size()); + return maxlen; } std::vector center(const std::vector& v) { - vector ret; - string::size_type maxlen = 80; - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) { - string::size_type len = (maxlen - iter->size()) / 2; - ret.push_back(string(len,' ') + (*iter) + string(len,' ')); - } - return ret; + vector ret; + string::size_type maxlen = 80; + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) { + string::size_type len = (maxlen - iter->size()) / 2; + ret.push_back(string(len,' ') + (*iter) + string(len,' ')); + } + return ret; } int main() { - // read data - vector v,f; - string line; - while(getline(cin, line)) - v.push_back(line); - f = center(v); - out(f); + // read data + vector v,f; + string line; + while(getline(cin, line)) + v.push_back(line); + f = center(v); + out(f); } diff --git a/05-09-uppercase.cpp b/05-09-uppercase.cpp index e19d844..d025ad6 100644 --- a/05-09-uppercase.cpp +++ b/05-09-uppercase.cpp @@ -3,12 +3,12 @@ int main() { - std::string s; - while(getline(std::cin, s)) { - for(std::string::iterator it = s.begin(); - it != s.end(); ++it) - *it = toupper(*it); - std::cout << s << std::endl; - } - return 0; + std::string s; + while(getline(std::cin, s)) { + for(std::string::iterator it = s.begin(); + it != s.end(); ++it) + *it = toupper(*it); + std::cout << s << std::endl; + } + return 0; } diff --git a/05-10-palindromes.cpp b/05-10-palindromes.cpp index 71c86df..4822068 100644 --- a/05-10-palindromes.cpp +++ b/05-10-palindromes.cpp @@ -4,24 +4,24 @@ int main() { - typedef std::string::const_iterator iter; - std::string word; - int maxlen = 0; - std::string poly; - while(std::cin >> word) { - int s = (int)word.size(); - if(s < 2) continue; - int l = 0, r = s - 1; - while( l < r && tolower(word[l]) == tolower(word[r]) ) { - ++l; --r; - } - if(l >= r){ - if(s > maxlen) { - maxlen = s; - poly = word; - } - } - } - std::cout << "palindrom: " << poly << std::endl; - return 0; + typedef std::string::const_iterator iter; + std::string word; + int maxlen = 0; + std::string poly; + while(std::cin >> word) { + int s = (int)word.size(); + if(s < 2) continue; + int l = 0, r = s - 1; + while( l < r && tolower(word[l]) == tolower(word[r]) ) { + ++l; --r; + } + if(l >= r){ + if(s > maxlen) { + maxlen = s; + poly = word; + } + } + } + std::cout << "palindrom: " << poly << std::endl; + return 0; } diff --git a/05-11-ascenders.cpp b/05-11-ascenders.cpp index 4e5acf3..960dc1f 100644 --- a/05-11-ascenders.cpp +++ b/05-11-ascenders.cpp @@ -6,29 +6,29 @@ using namespace std; int main() { - typedef std::string::const_iterator citer; - typedef std::string::iterator iter; - string::size_type maxlen = 0; - std::string best, ascenders("bdfhklt"), descenders("gjpqy"); + typedef std::string::const_iterator citer; + typedef std::string::iterator iter; + string::size_type maxlen = 0; + std::string best, ascenders("bdfhklt"), descenders("gjpqy"); - std::string word; - while(std::cin >> word) { - string t; - for(iter i = word.begin(); i != word.end(); ++i) { - char c = *i = tolower(*i); - if(string::npos != ascenders.find_first_of(c)) - t += "a"; - if(string::npos != descenders.find_first_of(c)) - t += "d"; - } - if(!t.size()){ - if(word.size() > maxlen) { - maxlen = word.size(); - best = word; - } - } - //std::cout << word << "\t" << t << endl; - } - std::cout << "the best: " << best << std::endl; - return 0; + std::string word; + while(std::cin >> word) { + string t; + for(iter i = word.begin(); i != word.end(); ++i) { + char c = *i = tolower(*i); + if(string::npos != ascenders.find_first_of(c)) + t += "a"; + if(string::npos != descenders.find_first_of(c)) + t += "d"; + } + if(!t.size()){ + if(word.size() > maxlen) { + maxlen = word.size(); + best = word; + } + } + //std::cout << word << "\t" << t << endl; + } + std::cout << "the best: " << best << std::endl; + return 0; } diff --git a/06-00-copy.cpp b/06-00-copy.cpp index 087ebdc..12b25fe 100644 --- a/06-00-copy.cpp +++ b/06-00-copy.cpp @@ -6,53 +6,53 @@ using namespace std; void out(const std::vector& v) { - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) - cout << *iter << endl; + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) + cout << *iter << endl; } std::string::size_type width(const std::vector& v) { - string::size_type maxlen = 0; - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) - maxlen = max(maxlen, iter->size()); - return maxlen; + string::size_type maxlen = 0; + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) + maxlen = max(maxlen, iter->size()); + return maxlen; } std::vector frame(const std::vector& v) { - vector ret; - string::size_type maxlen = width(v); - string border(maxlen + 4, '*'); - ret.push_back(border); - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) { - ret.push_back("* " + (*iter) + string(maxlen - iter->size(),' ') + " *"); - } - ret.push_back(border); - return ret; + vector ret; + string::size_type maxlen = width(v); + string border(maxlen + 4, '*'); + ret.push_back(border); + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) { + ret.push_back("* " + (*iter) + string(maxlen - iter->size(),' ') + " *"); + } + ret.push_back(border); + return ret; } std::vector vcat(const std::vector& top, - const std::vector& bottom) + const std::vector& bottom) { - vector ret = top; + vector ret = top; #if 0 - copy(bottom.begin(), bottom.end(), back_inserter(ret)); + copy(bottom.begin(), bottom.end(), back_inserter(ret)); #else - ret.resize(top.size()+bottom.size()); - copy(bottom.begin(), bottom.end(), ret.begin()+top.size()); + ret.resize(top.size()+bottom.size()); + copy(bottom.begin(), bottom.end(), ret.begin()+top.size()); #endif - return ret; + return ret; } int main() { - vector v,f; - string line; - while(getline(cin, line)) - v.push_back(line); - f = frame(v); - out(vcat(v,f)); + vector v,f; + string line; + while(getline(cin, line)) + v.push_back(line); + f = frame(v); + out(vcat(v,f)); } diff --git a/06-00-palindromes.cpp b/06-00-palindromes.cpp index 8e69c5b..3c43947 100644 --- a/06-00-palindromes.cpp +++ b/06-00-palindromes.cpp @@ -4,18 +4,18 @@ int main() { - typedef std::string::const_iterator iter; - std::string word; - std::string::size_type maxlen = 0; - std::string poly; - while(std::cin >> word) { - if( equal(word.begin(), word.end(), word.rbegin()) ) { - if(word.size() > maxlen) { - maxlen = word.size(); - poly = word; - } - } - } - std::cout << "palindrom: " << poly << std::endl; - return 0; + typedef std::string::const_iterator iter; + std::string word; + std::string::size_type maxlen = 0; + std::string poly; + while(std::cin >> word) { + if( equal(word.begin(), word.end(), word.rbegin()) ) { + if(word.size() > maxlen) { + maxlen = word.size(); + poly = word; + } + } + } + std::cout << "palindrom: " << poly << std::endl; + return 0; } diff --git a/06-00-rbackup.cpp b/06-00-rbackup.cpp index 515251e..aaffad1 100644 --- a/06-00-rbackup.cpp +++ b/06-00-rbackup.cpp @@ -4,12 +4,12 @@ int main() { - typedef std::string::const_reverse_iterator iter; - std::string word; - while(std::cin >> word) { - for(iter r = word.rbegin(); r != word.rend(); r++) - std::cout << *r; - std::cout << std::endl; - } - return 0; + typedef std::string::const_reverse_iterator iter; + std::string word; + while(std::cin >> word) { + for(iter r = word.rbegin(); r != word.rend(); r++) + std::cout << *r; + std::cout << std::endl; + } + return 0; } diff --git a/06-00-split.cpp b/06-00-split.cpp index 13db2e0..97159bd 100644 --- a/06-00-split.cpp +++ b/06-00-split.cpp @@ -6,31 +6,31 @@ // isspace is overloaded so we use wrapper function as argument for find_if bool space(char c) { - return isspace(c); + return isspace(c); } bool not_space(char c) { - return !isspace(c); + return !isspace(c); } std::vector split(const std::string& str) -{ - using namespace std; - typedef string::const_iterator iter; - vector ret; +{ + using namespace std; + typedef string::const_iterator iter; + vector ret; - iter i = str.begin(); - while(i != str.end()) - { - // ignore leading spaces - i = find_if(i, str.end(), not_space); - // find end of next word - iter j = find_if(i, str.end(), space); - if(i != str.end()) - ret.push_back(string(i, j)); - i = j; - } - return ret; + iter i = str.begin(); + while(i != str.end()) + { + // ignore leading spaces + i = find_if(i, str.end(), not_space); + // find end of next word + iter j = find_if(i, str.end(), space); + if(i != str.end()) + ret.push_back(string(i, j)); + i = j; + } + return ret; } /* SETUP TESTS */ @@ -38,41 +38,41 @@ std::vector split(const std::string& str) #include class myTest : public CppUnit::TestFixture { public: - void setUp() {} - void tearDown() {} + void setUp() {} + void tearDown() {} - void checkResult(); - CPPUNIT_TEST_SUITE( myTest ); - CPPUNIT_TEST( checkResult ); - CPPUNIT_TEST_SUITE_END(); + void checkResult(); + CPPUNIT_TEST_SUITE( myTest ); + CPPUNIT_TEST( checkResult ); + CPPUNIT_TEST_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION ( myTest ); - + void myTest::checkResult() { - typedef std::vector vec_str; - - vec_str vec = split(std::string("")); - CPPUNIT_ASSERT_EQUAL((vec_str::size_type)0, vec.size()); - vec = split(std::string("test")); - CPPUNIT_ASSERT_EQUAL((vec_str::size_type)1, vec.size()); + typedef std::vector vec_str; + + vec_str vec = split(std::string("")); + CPPUNIT_ASSERT_EQUAL((vec_str::size_type)0, vec.size()); + vec = split(std::string("test")); + CPPUNIT_ASSERT_EQUAL((vec_str::size_type)1, vec.size()); - const std::string input(" \tone two\n tree\tfour five\t\t"); - vec_str etalon; - etalon.push_back("one"); - etalon.push_back("two"); - etalon.push_back("tree"); - etalon.push_back("four"); - etalon.push_back("five"); + const std::string input(" \tone two\n tree\tfour five\t\t"); + vec_str etalon; + etalon.push_back("one"); + etalon.push_back("two"); + etalon.push_back("tree"); + etalon.push_back("four"); + etalon.push_back("five"); - vec = split(input); + vec = split(input); - CPPUNIT_ASSERT_EQUAL((vec_str::size_type)5, vec.size()); - std::cout << std::endl << input << std::endl; - for(vec_str::size_type i = 0; i != vec.size(); ++i) { - std::cout << i << " " << vec[i] << std::endl; - CPPUNIT_ASSERT_EQUAL(etalon[i], vec[i]); - } + CPPUNIT_ASSERT_EQUAL((vec_str::size_type)5, vec.size()); + std::cout << std::endl << input << std::endl; + for(vec_str::size_type i = 0; i != vec.size(); ++i) { + std::cout << i << " " << vec[i] << std::endl; + CPPUNIT_ASSERT_EQUAL(etalon[i], vec[i]); + } } #include "cppunit_run.inc" diff --git a/06-00-urls.cpp b/06-00-urls.cpp index 31d9f74..74cdfd8 100644 --- a/06-00-urls.cpp +++ b/06-00-urls.cpp @@ -13,41 +13,41 @@ url_beg(std::string::const_iterator b, std::string::const_iterator e) bool not_url_char(char c) { - static const std::string url_ch = "~;/?:@=&$-_.+!*'(),"; - return !( - isalnum(c) || - find(url_ch.begin(), url_ch.end(), c) != url_ch.end() - ); + static const std::string url_ch = "~;/?:@=&$-_.+!*'(),"; + return !( + isalnum(c) || + find(url_ch.begin(), url_ch.end(), c) != url_ch.end() + ); } std::string::const_iterator url_end(std::string::const_iterator b, std::string::const_iterator e) { - return find_if(b, e, not_url_char); + return find_if(b, e, not_url_char); } std::vector find_urls(const std::string& s) -{ - using namespace std; - 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 URL - iter after = url_end(b, e); +{ + using namespace std; + 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 URL + iter after = url_end(b, e); - ret.push_back(string(b, after)); - b = after; - } - } + ret.push_back(string(b, after)); + b = after; + } + } - return ret; + return ret; } /* SETUP TESTS */ @@ -55,44 +55,44 @@ std::vector find_urls(const std::string& s) #include class myTest : public CppUnit::TestFixture { public: - void setUp() {} - void tearDown() {} + void setUp() {} + void tearDown() {} - void checkResult(); - CPPUNIT_TEST_SUITE( myTest ); - CPPUNIT_TEST( checkResult ); - CPPUNIT_TEST_SUITE_END(); + void checkResult(); + CPPUNIT_TEST_SUITE( myTest ); + CPPUNIT_TEST( checkResult ); + CPPUNIT_TEST_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION ( myTest ); - + void myTest::checkResult() { - using namespace std; - typedef vector vec_str; - const string dummy1("http://www.com/b.b?p1=1&p2=2"); - vec_str vec = find_urls(dummy1); - CPPUNIT_ASSERT_EQUAL((vec_str::size_type)1, vec.size()); - const string dummy0("www.com/b.b?p1=1&p2=2"); - vec = find_urls(dummy0); - CPPUNIT_ASSERT_EQUAL((vec_str::size_type)0, vec.size()); + using namespace std; + typedef vector vec_str; + const string dummy1("http://www.com/b.b?p1=1&p2=2"); + vec_str vec = find_urls(dummy1); + CPPUNIT_ASSERT_EQUAL((vec_str::size_type)1, vec.size()); + const string dummy0("www.com/b.b?p1=1&p2=2"); + vec = find_urls(dummy0); + CPPUNIT_ASSERT_EQUAL((vec_str::size_type)0, vec.size()); - const string dummy2("http://a.com/b/c.d?p1=e&p2=f"); - string input(" \tone two\n"); - input += dummy1 + "tree\tfour five\tlink"; - - vec_str etalon; - etalon.push_back(dummy1); - etalon.push_back(dummy2); + const string dummy2("http://a.com/b/c.d?p1=e&p2=f"); + string input(" \tone two\n"); + input += dummy1 + "tree\tfour five\tlink"; + + vec_str etalon; + etalon.push_back(dummy1); + etalon.push_back(dummy2); - vec = find_urls(input); + vec = find_urls(input); - CPPUNIT_ASSERT_EQUAL((vec_str::size_type)2, vec.size()); - std::cout << std::endl << input << std::endl; - for(vec_str::size_type i = 0; i != vec.size(); ++i) { - std::cout << i << " " << vec[i] << std::endl; - CPPUNIT_ASSERT_EQUAL(etalon[i], vec[i]); - } + CPPUNIT_ASSERT_EQUAL((vec_str::size_type)2, vec.size()); + std::cout << std::endl << input << std::endl; + for(vec_str::size_type i = 0; i != vec.size(); ++i) { + std::cout << i << " " << vec[i] << std::endl; + CPPUNIT_ASSERT_EQUAL(etalon[i], vec[i]); + } } #include "cppunit_run.inc" diff --git a/Makefile b/Makefile index f1bf498..2c01367 100644 --- a/Makefile +++ b/Makefile @@ -1,27 +1,27 @@ #$Id: Makefile,v 1.2 2012/02/08 16:56:33 oraz Exp $ -CPP = $(wildcard *.cpp) -PROGRAMS = $(CPP:%.cpp=%) -CFILES = $(wildcard *.c) -PROGRAMC = $(CFILES:%.c=%) -LIBRARIES = cppunit - -CC = gcc -CXX = g++ -CFLAGS += -Wall -O3 -CPPFLAGS += -Wall -O2 -LDFLAGS = $(LIBRARIES:%=-l%) +CPP = $(wildcard *.cpp) +PROGRAMS = $(CPP:%.cpp=%) +CFILES = $(wildcard *.c) +PROGRAMC = $(CFILES:%.c=%) +LIBRARIES = cppunit + +CC = gcc +CXX = g++ +CFLAGS += -Wall -O3 +CPPFLAGS += -Wall -O2 +LDFLAGS = $(LIBRARIES:%=-l%) .PHONY: all clean all: $(PROGRAMS) $(PROGRAMC) %: %.cpp - $(CXX) $(CPPFLAGS) $(LDFLAGS) $^ -o $@ + $(CXX) $(CPPFLAGS) $(LDFLAGS) $^ -o $@ %: %.c - $(CC) $(CFLAGS) $^ -o $@ + $(CC) $(CFLAGS) $^ -o $@ clean: - rm -f *.o *.a $(PROGRAMS) $(PROGRAMC) + rm -f *.o *.a $(PROGRAMS) $(PROGRAMC) diff --git a/cppunit_example.cpp b/cppunit_example.cpp index 21959aa..7650cd1 100644 --- a/cppunit_example.cpp +++ b/cppunit_example.cpp @@ -3,12 +3,12 @@ #include double CalcAverage(const std::vector& vec) -{ - typedef std::vector::size_type vec_sz; - double sum = 0; - for(vec_sz i = 0; i < vec.size(); ++i) - sum += vec[i]; - return sum / vec.size(); +{ + typedef std::vector::size_type vec_sz; + double sum = 0; + for(vec_sz i = 0; i < vec.size(); ++i) + sum += vec[i]; + return sum / vec.size(); } /* SETUP TESTS */ @@ -16,22 +16,22 @@ double CalcAverage(const std::vector& vec) #include class myTest : public CppUnit::TestFixture { public: - void setUp() {} - void tearDown() {} + void setUp() {} + void tearDown() {} - void checkResult(); - CPPUNIT_TEST_SUITE( myTest ); - CPPUNIT_TEST( checkResult ); - CPPUNIT_TEST_SUITE_END(); + void checkResult(); + CPPUNIT_TEST_SUITE( myTest ); + CPPUNIT_TEST( checkResult ); + CPPUNIT_TEST_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION ( myTest ); - + void myTest::checkResult() { - double g = 4.16666666666667; - double array[] = {1,2,3,4,5,10}; - std::vector vec = std::vector(array, array+sizeof(array)/sizeof(double)); - CPPUNIT_ASSERT_DOUBLES_EQUAL(g, CalcAverage(vec), 0.001); + double g = 4.16666666666667; + double array[] = {1,2,3,4,5,10}; + std::vector vec = std::vector(array, array+sizeof(array)/sizeof(double)); + CPPUNIT_ASSERT_DOUBLES_EQUAL(g, CalcAverage(vec), 0.001); } #include "cppunit_run.inc" diff --git a/permuted-index.cpp b/permuted-index.cpp index b29cd4f..2cd088e 100644 --- a/permuted-index.cpp +++ b/permuted-index.cpp @@ -9,75 +9,75 @@ using namespace std; typedef std::list wordlist; wordlist split(const std::string& s) -{ - wordlist words; - std::string::const_iterator i = s.begin(); - while(i != s.end()) - { - while(i != s.end() && isspace(*i)) - ++i; - std::string::const_iterator j = i; - while(j != s.end() && !isspace(*j)) - ++j; - if(j != i) { - words.push_back(std::string(i, j)); - i = j; - } - } - - return words; +{ + wordlist words; + std::string::const_iterator i = s.begin(); + while(i != s.end()) + { + while(i != s.end() && isspace(*i)) + ++i; + std::string::const_iterator j = i; + while(j != s.end() && !isspace(*j)) + ++j; + if(j != i) { + words.push_back(std::string(i, j)); + i = j; + } + } + + return words; } std::string::size_type width(const wordlist& v) { - string::size_type maxlen = 0; - for(wordlist::const_iterator iter = v.begin(); - iter != v.end(); ++iter) - maxlen = max(maxlen, iter->size()); - return maxlen; + string::size_type maxlen = 0; + for(wordlist::const_iterator iter = v.begin(); + iter != v.end(); ++iter) + maxlen = max(maxlen, iter->size()); + return maxlen; } std::vector hcat(const std::vector& left, - const std::vector& right) + const std::vector& right) { - vector ret; - string::size_type w1 = 36, w2 = 38, dw = 3; - vector::size_type i = 0, j = 0; - - while(i != left.size() || j != right.size()) - { - string s; - if(i != left.size()) { - string s2 = left[i++]; - if(s2.size() >= w1) { - s = s2.substr(s2.size() - w1, w1); - } else { - s = string(w1 - s2.size(),' ') + s2; - } - s += string(dw, ' '); - } else { - s = string(w1 + dw, ' '); - } - if(j != right.size()) { - string s2 = right[j++]; - if(s2.size() >= w2) { - s += s2.substr(0, w2); - } else { - s += s2; - s += string(w2 - s2.size(),' '); - } - } - ret.push_back(s); - } - - return ret; + vector ret; + string::size_type w1 = 36, w2 = 38, dw = 3; + vector::size_type i = 0, j = 0; + + while(i != left.size() || j != right.size()) + { + string s; + if(i != left.size()) { + string s2 = left[i++]; + if(s2.size() >= w1) { + s = s2.substr(s2.size() - w1, w1); + } else { + s = string(w1 - s2.size(),' ') + s2; + } + s += string(dw, ' '); + } else { + s = string(w1 + dw, ' '); + } + if(j != right.size()) { + string s2 = right[j++]; + if(s2.size() >= w2) { + s += s2.substr(0, w2); + } else { + s += s2; + s += string(w2 - s2.size(),' '); + } + } + ret.push_back(s); + } + + return ret; } void out(const std::vector& v) { - for(std::vector::const_iterator iter = v.begin(); - iter != v.end(); ++iter) - cout << *iter << endl; + for(std::vector::const_iterator iter = v.begin(); + iter != v.end(); ++iter) + cout << *iter << endl; } const string endmark(""); @@ -85,84 +85,84 @@ const string sepmark(""); void rotate(wordlist& rot, bool left = true) { - if(left) { - rot.push_back(*rot.begin()); - rot.erase(rot.begin()); - } else { - rot.insert(rot.begin(), *rot.rbegin()); - rot.pop_back(); - } + if(left) { + rot.push_back(*rot.begin()); + rot.erase(rot.begin()); + } else { + rot.insert(rot.begin(), *rot.rbegin()); + rot.pop_back(); + } } string merge(const wordlist& vec) { - if(!vec.size()) return string(""); - wordlist::const_iterator it = vec.begin(); - string m = *it; - for(++it; it != vec.end(); ++it) { - m += " " + *it; - } - return m; + if(!vec.size()) return string(""); + wordlist::const_iterator it = vec.begin(); + string m = *it; + for(++it; it != vec.end(); ++it) { + m += " " + *it; + } + return m; } void read(vector& rot, const string& str) { - /*convert*/ - string s = str; - for(string::size_type i = 0; i != str.size(); ++i) { - if(isalnum(str[i])) - s[i] = tolower(str[i]); - else - s[i] = ' '; - } - - wordlist vec = split(s); - int n = (int)vec.size(); - vec.push_back(endmark); - for(int i = 0; i < n; ++i) { - rot.push_back(merge(vec)); - rotate(vec); - } + /*convert*/ + string s = str; + for(string::size_type i = 0; i != str.size(); ++i) { + if(isalnum(str[i])) + s[i] = tolower(str[i]); + else + s[i] = ' '; + } + + wordlist vec = split(s); + int n = (int)vec.size(); + vec.push_back(endmark); + for(int i = 0; i < n; ++i) { + rot.push_back(merge(vec)); + rotate(vec); + } } int main(int argc, char* argv[]) { - vector rot; - - if(argc > 1 && string(argv[1]) == string("test")) { - read(rot, "The quick brown fox"); - read(rot, "jumped over the fence ogogo"); - } - else { - string s; - while(getline(cin, s)) - read(rot, s); - } - - sort(rot.begin(), rot.end()); - - vector left, right, ret; - for(vector::iterator it = rot.begin(); - it != rot.end(); ++it) - { - wordlist vec = split(*it); - vec.insert(vec.begin(), sepmark); - while(*(vec.rbegin()) != endmark) - rotate(vec, false); - wordlist l, r; - wordlist::const_iterator i = vec.begin(), j = i; - while(*j != sepmark) ++j; - l = wordlist(i,j); - ++j; i = j; - while(*j != endmark) ++j; - r = wordlist(i,j); - left.push_back(merge(l)); - right.push_back(merge(r)); - } - - ret = hcat(left, right); - out(ret); - - return 0; + vector rot; + + if(argc > 1 && string(argv[1]) == string("test")) { + read(rot, "The quick brown fox"); + read(rot, "jumped over the fence ogogo"); + } + else { + string s; + while(getline(cin, s)) + read(rot, s); + } + + sort(rot.begin(), rot.end()); + + vector left, right, ret; + for(vector::iterator it = rot.begin(); + it != rot.end(); ++it) + { + wordlist vec = split(*it); + vec.insert(vec.begin(), sepmark); + while(*(vec.rbegin()) != endmark) + rotate(vec, false); + wordlist l, r; + wordlist::const_iterator i = vec.begin(), j = i; + while(*j != sepmark) ++j; + l = wordlist(i,j); + ++j; i = j; + while(*j != endmark) ++j; + r = wordlist(i,j); + left.push_back(merge(l)); + right.push_back(merge(r)); + } + + ret = hcat(left, right); + out(ret); + + return 0; }