Skip to content

Commit

Permalink
added format generator and format ch01-14
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jun 26, 2015
1 parent f266ca1 commit aae117b
Show file tree
Hide file tree
Showing 262 changed files with 4,763 additions and 4,481 deletions.
6 changes: 3 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: 0
AccessModifierOffset: -4
AlignAfterOpenBracket: true
AlignEscapedNewlinesLeft: false
AlignOperands: true
Expand All @@ -12,7 +12,7 @@ AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AllowShortFunctionsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakTemplateDeclarations: false
AlwaysBreakBeforeMultilineStrings: false
Expand Down Expand Up @@ -41,7 +41,7 @@ PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
PointerAlignment: Left
SpacesBeforeTrailingComments: 1
Cpp11BracedListStyle: true
Standard: Cpp11
Expand Down
2 changes: 1 addition & 1 deletion ch02/ex2_34.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main()
auto e = &ci; // e is const int*(& of a const object is low-level const)

const auto f = ci; // deduced type of ci is int; f has type const int
auto &g = ci; // g is a const int& that is bound to ci
auto& g = ci; // g is a const int& that is bound to ci

a = 42;
b = 42;
Expand Down
6 changes: 3 additions & 3 deletions ch02/ex2_35.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ int main()
{
const int i = 42;
auto j = i;
const auto &k = i;
auto *p = &i;
const auto j2 = i, &k2 = i;
const auto& k = i;
auto* p = &i;
const auto j2 = i, & k2 = i;

// print i means int, and PKi means pointer to const int.
std::cout << "j is " << typeid(j).name() << "\nk is " << typeid(k).name()
Expand Down
2 changes: 1 addition & 1 deletion ch03/ex3_06.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using std::endl;
int main()
{
string str("a simple string");
for (auto &c : str) c = 'X';
for (auto& c : str) c = 'X';
cout << str << endl;

return 0;
Expand Down
4 changes: 2 additions & 2 deletions ch03/ex3_17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ int main()
string word;
while (cin >> word) vec.push_back(word);

for (auto &str : vec)
for (auto &c : str) c = toupper(c);
for (auto& str : vec)
for (auto& c : str) c = toupper(c);

for (decltype(vec.size()) i = 0; i != vec.size(); ++i) {
if (i != 0 && i % 8 == 0) cout << endl;
Expand Down
4 changes: 2 additions & 2 deletions ch03/ex3_21.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using std::string;
using std::cout;
using std::endl;

void check(const vector<int> &vec)
void check(const vector<int>& vec)
{
if (vec.empty())
cout << "size: 0; no values." << endl;
Expand All @@ -23,7 +23,7 @@ void check(const vector<int> &vec)
}
}

void check(const vector<string> &vec)
void check(const vector<string>& vec)
{
if (vec.empty())
cout << "size: 0; no values." << endl;
Expand Down
2 changes: 1 addition & 1 deletion ch03/ex3_22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main()
for (string line; getline(cin, line);) text.push_back(line);

for (auto it = text.begin(); it != text.end() && !it->empty(); ++it) {
for (auto &c : *it) c = toupper(c);
for (auto& c : *it) c = toupper(c);
cout << *it << endl;
}

Expand Down
6 changes: 3 additions & 3 deletions ch03/ex3_35.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ using std::endl;
int main()
{
int arr[10];
int *b = begin(arr);
int *e = end(arr);
int* b = begin(arr);
int* e = end(arr);

for (int *i = b; i != e; ++i) *i = 0;
for (int* i = b; i != e; ++i) *i = 0;

for (auto i : arr) cout << i << " ";
cout << endl;
Expand Down
2 changes: 1 addition & 1 deletion ch03/ex3_36.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using std::endl;
using std::vector;

// pb point to begin of the array, pe point to end of the array.
bool compare(int *const pb1, int *const pe1, int *const pb2, int *const pe2)
bool compare(int* const pb1, int* const pe1, int* const pb2, int* const pe2)
{
if ((pe1 - pb1) != (pe2 - pb2)) // have different size.
return false;
Expand Down
4 changes: 2 additions & 2 deletions ch03/ex3_39.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ int main()

cout << "=========" << endl;
// use C-Style character strings.
const char *cs1 = "Wangyue";
const char *cs2 = "Pezy";
const char* cs1 = "Wangyue";
const char* cs2 = "Pezy";

auto result = strcmp(cs1, cs2);
if (result == 0)
Expand Down
2 changes: 1 addition & 1 deletion ch03/ex3_42.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main()
vector<int> ivec{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int int_arr[10];

for (int *i = begin(int_arr); i != end(int_arr); ++i)
for (int* i = begin(int_arr); i != end(int_arr); ++i)
*i = ivec[i - begin(int_arr)];

for (auto i : int_arr) cout << i << " ";
Expand Down
2 changes: 1 addition & 1 deletion ch03/ex3_43.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main()

// using pointers.
for (int(*p)[4] = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q) cout << *q << " ";
for (int* q = *p; q != *p + 4; ++q) cout << *q << " ";
cout << endl;

return 0;
Expand Down
6 changes: 3 additions & 3 deletions ch03/ex3_44.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main()
// a range for to manage the iteration
// use type alias
using int_array = int[4];
for (int_array &p : ia)
for (int_array& p : ia)
for (int q : p) cout << q << " ";
cout << endl;

Expand All @@ -21,8 +21,8 @@ int main()

// using pointers.
// use type alias
for (int_array *p = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q) cout << *q << " ";
for (int_array* p = ia; p != ia + 3; ++p)
for (int* q = *p; q != *p + 4; ++q) cout << *q << " ";
cout << endl;

return 0;
Expand Down
4 changes: 2 additions & 2 deletions ch03/ex3_45.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int main()
int ia[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

// a range for to manage the iteration
for (auto &p : ia)
for (auto& p : ia)
for (int q : p) cout << q << " ";
cout << endl;

Expand All @@ -19,7 +19,7 @@ int main()

// using pointers.
for (auto p = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q) cout << *q << " ";
for (int* q = *p; q != *p + 4; ++q) cout << *q << " ";
cout << endl;

return 0;
Expand Down
2 changes: 1 addition & 1 deletion ch04/ex4_21.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
int main()
{
std::vector<int> ivec{1, 2, 3, 4, 5, 6, 7, 8, 9};
for (auto &i : ivec) i = (i % 2) ? (i * 2) : i;
for (auto& i : ivec) i = (i % 2) ? (i * 2) : i;

// Check
for (auto i : ivec) std::cout << i << " ";
Expand Down
2 changes: 1 addition & 1 deletion ch06/ex6_10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <string>
#include <stdexcept>

void swap(int *lhs, int *rhs)
void swap(int* lhs, int* rhs)
{
int tmp;
tmp = *lhs;
Expand Down
2 changes: 1 addition & 1 deletion ch06/ex6_11.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <iostream>

void reset(int &i)
void reset(int& i)
{
i = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion ch06/ex6_12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <iostream>
#include <string>

void swap(int &lhs, int &rhs)
void swap(int& lhs, int& rhs)
{
int temp = lhs;
lhs = rhs;
Expand Down
6 changes: 3 additions & 3 deletions ch06/ex6_17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ using std::cout;
using std::endl;
using std::string;

bool hasUppercase(const string &str)
bool hasUppercase(const string& str)
{
for (auto c : str)
if (isupper(c)) return true;
return false;
}

void makeLowercase(string &str)
void makeLowercase(string& str)
{
for (auto &c : str)
for (auto& c : str)
if (isupper(c)) c = tolower(c);
}

Expand Down
2 changes: 1 addition & 1 deletion ch06/ex6_21.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <iostream>
using std::cout;

int LargerOne(const int i, const int *ip)
int LargerOne(const int i, const int* ip)
{
return (i > *ip) ? i : *ip;
}
Expand Down
2 changes: 1 addition & 1 deletion ch06/ex6_22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//!
#include <iostream>

void swap(int *&lft, int *&rht)
void swap(int*& lft, int*& rht)
{
auto tmp = lft;
lft = rht;
Expand Down
6 changes: 3 additions & 3 deletions ch06/ex6_23.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ using std::endl;
using std::begin;
using std::end;

void print(const int *pi)
void print(const int* pi)
{
if (pi) cout << *pi << endl;
}

void print(const char *p)
void print(const char* p)
{
if (p)
while (*p) cout << *p++;
cout << endl;
}

void print(const int *beg, const int *end)
void print(const int* beg, const int* end)
{
while (beg != end) cout << *beg++ << endl;
}
Expand Down
2 changes: 1 addition & 1 deletion ch06/ex6_25_26.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <iostream>
#include <string>

int main(int argc, char **argv)
int main(int argc, char** argv)
{
std::string str;
for (int i = 1; i != argc; ++i) str += argv[i] + " ";
Expand Down
2 changes: 1 addition & 1 deletion ch06/ex6_42.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using std::string;
using std::cout;
using std::endl;

string make_plural(size_t ctr, const string &word, const string &ending = "s")
string make_plural(size_t ctr, const string& word, const string& ending = "s")
{
return (ctr > 1) ? word + ending : word;
}
Expand Down
2 changes: 1 addition & 1 deletion ch06/ex6_44.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

using std::string;

inline bool isShorter(const string &s1, const string &s2)
inline bool isShorter(const string& s1, const string& s2)
{
return s1.size() < s2.size();
}
Expand Down
2 changes: 1 addition & 1 deletion ch06/ex6_47.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using std::endl;

#define NDEBUG

void printVec(vector<int> &vec)
void printVec(vector<int>& vec)
{
#ifdef NDEBUG
cout << "vector size: " << vec.size() << endl;
Expand Down
2 changes: 1 addition & 1 deletion ch06/ex6_54_55_56.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ inline int NumDiv(const int n1, const int n2)
return n1 / n2;
}

vector<fp *> v{NumAdd, NumSub, NumMul, NumDiv};
vector<fp*> v{NumAdd, NumSub, NumMul, NumDiv};

int main()
{
Expand Down
7 changes: 5 additions & 2 deletions ch07/ex7_05.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class Person {
std::string name;
std::string address;

public:
const std::string& getName() const { return name; }
const std::string& getAddress() const { return address; }
Expand All @@ -23,5 +24,7 @@ class Person {

// Should these functions be const?

// Yes, A const following the parameter list indicates that this is a pointer to const.
// These functions my read but not write to the data members of the objects on which it is called.
// Yes, A const following the parameter list indicates that this is a pointer to
// const.
// These functions my read but not write to the data members of the objects on
// which it is called.
6 changes: 3 additions & 3 deletions ch07/ex7_06.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ Sales_data& Sales_data::combine(const Sales_data& rhs)
}

// nonmember functions
std::istream &read(std::istream &is, Sales_data &item)
std::istream& read(std::istream& is, Sales_data& item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
}

std::ostream &print(std::ostream &os, const Sales_data &item)
std::ostream& print(std::ostream& os, const Sales_data& item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue;
return os;
}

Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
Sales_data add(const Sales_data& lhs, const Sales_data& rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
Expand Down
8 changes: 3 additions & 5 deletions ch07/ex7_07.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
int main()
{
Sales_data total;
if (read(std::cin, total))
{
if (read(std::cin, total)) {
Sales_data trans;
while (read(std::cin, trans)) {
if (total.isbn() == trans.isbn())
Expand All @@ -24,11 +23,10 @@ int main()
}
print(std::cout, total) << std::endl;
}
else
{
else {
std::cerr << "No data?!" << std::endl;
return -1;
}

return 0;
}
Loading

0 comments on commit aae117b

Please sign in to comment.