Skip to content

Commit f266ca1

Browse files
committed
improving format by clang-format
1 parent fb80272 commit f266ca1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1077
-952
lines changed

Diff for: .clang-format

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: LLVM
4+
AccessModifierOffset: 0
5+
AlignAfterOpenBracket: true
6+
AlignEscapedNewlinesLeft: false
7+
AlignOperands: true
8+
AlignTrailingComments: true
9+
AlignConsecutiveAssignments: false
10+
AllowAllParametersOfDeclarationOnNextLine: false
11+
AllowShortBlocksOnASingleLine: false
12+
AllowShortCaseLabelsOnASingleLine: false
13+
AllowShortIfStatementsOnASingleLine: true
14+
AllowShortLoopsOnASingleLine: true
15+
AllowShortFunctionsOnASingleLine: false
16+
AlwaysBreakAfterDefinitionReturnType: false
17+
AlwaysBreakTemplateDeclarations: false
18+
AlwaysBreakBeforeMultilineStrings: false
19+
BreakBeforeBinaryOperators: None
20+
BreakBeforeTernaryOperators: true
21+
BreakConstructorInitializersBeforeComma: false
22+
BinPackParameters: true
23+
BinPackArguments: true
24+
ColumnLimit: 80
25+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
26+
ConstructorInitializerIndentWidth: 4
27+
DerivePointerAlignment: false
28+
ExperimentalAutoDetectBinPacking: false
29+
IndentCaseLabels: false
30+
IndentWrappedFunctionNames: false
31+
IndentFunctionDeclarationAfterType: false
32+
MaxEmptyLinesToKeep: 1
33+
KeepEmptyLinesAtTheStartOfBlocks: true
34+
NamespaceIndentation: None
35+
ObjCBlockIndentWidth: 2
36+
ObjCSpaceAfterProperty: false
37+
ObjCSpaceBeforeProtocolList: true
38+
PenaltyBreakBeforeFirstCallParameter: 19
39+
PenaltyBreakComment: 300
40+
PenaltyBreakString: 1000
41+
PenaltyBreakFirstLessLess: 120
42+
PenaltyExcessCharacter: 1000000
43+
PenaltyReturnTypeOnItsOwnLine: 60
44+
PointerAlignment: Right
45+
SpacesBeforeTrailingComments: 1
46+
Cpp11BracedListStyle: true
47+
Standard: Cpp11
48+
IndentWidth: 4
49+
TabWidth: 4
50+
UseTab: Never
51+
BreakBeforeBraces: Stroustrup
52+
SpacesInParentheses: false
53+
SpacesInSquareBrackets: false
54+
SpacesInAngles: false
55+
SpaceInEmptyParentheses: false
56+
SpacesInCStyleCastParentheses: false
57+
SpaceAfterCStyleCast: false
58+
SpacesInContainerLiterals: true
59+
SpaceBeforeAssignmentOperators: true
60+
ContinuationIndentWidth: 4
61+
CommentPragmas: '^ IWYU pragma:'
62+
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
63+
SpaceBeforeParens: ControlStatements
64+
DisableFormat: false
65+
...

Diff for: CONTRIBUTING.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ int main()
2525
}
2626
```
2727

28-
Thank you for your suggestions!
28+
If you intall **[ClangFormat](http://clang.llvm.org/docs/ClangFormat.html)**, You wouldn't care about the format, just run the command:
29+
```sh
30+
clang-format -i your-code.cpp
31+
```
2932

3033
#### Tips for good commits and issues.
3134

Diff for: ch01/ex1_20.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
int main()
55
{
66
Sales_item item;
7-
while (std::cin >> item)
8-
std::cout << item << std::endl;
7+
while (std::cin >> item) std::cout << item << std::endl;
98

109
return 0;
1110
}

Diff for: ch01/ex1_23.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ int main()
1010
if (valItem.isbn() == currItem.isbn())
1111
++cnt;
1212
else {
13-
std::cout << currItem << " occurs " << cnt << " times " << std::endl;
13+
std::cout << currItem << " occurs " << cnt << " times "
14+
<< std::endl;
1415
currItem = valItem;
1516
cnt = 1;
1617
}

Diff for: ch02/ex2_04.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
int main()
44
{
5-
unsigned u = 10, u2 = 42;
6-
std::cout << u2 - u << std::endl; // 32
7-
std::cout << u - u2 << std::endl; // 4294967264
8-
int i = 10, i2 = 42;
9-
std::cout << i2 - i << std::endl; // 32
10-
std::cout << i - i2 << std::endl; // -32
11-
std::cout << i - u << std::endl; // 0
12-
std::cout << u - i << std::endl; // 0
13-
14-
return 0;
5+
unsigned u = 10, u2 = 42;
6+
std::cout << u2 - u << std::endl; // 32
7+
std::cout << u - u2 << std::endl; // 4294967264
8+
int i = 10, i2 = 42;
9+
std::cout << i2 - i << std::endl; // 32
10+
std::cout << i - i2 << std::endl; // -32
11+
std::cout << i - u << std::endl; // 0
12+
std::cout << u - i << std::endl; // 0
13+
14+
return 0;
1515
}

Diff for: ch02/ex2_34.cpp

+15-11
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22

33
int main()
44
{
5-
int i = 0, &r = i;
6-
auto a = r; // a is an int (r is an alias for i, which has type int)
5+
int i = 0, &r = i;
6+
auto a = r; // a is an int (r is an alias for i, which has type int)
77

8-
const int ci = i, &cr = ci;
9-
auto b = ci; // b is an int (top-level const in ci is dropped)
10-
auto c = cr; // c is an int (cr is an alias for ci whose const is top-level)
11-
auto d = &i; // d is an int* (& ofan int objectis int*)
12-
auto e = &ci; // e is const int*(& of a const object is low-level const)
8+
const int ci = i, &cr = ci;
9+
auto b = ci; // b is an int (top-level const in ci is dropped)
10+
auto c = cr; // c is an int (cr is an alias for ci whose const is top-level)
11+
auto d = &i; // d is an int* (& ofan int objectis int*)
12+
auto e = &ci; // e is const int*(& of a const object is low-level const)
1313

14-
const auto f = ci; // deduced type of ci is int; f has type const int
15-
auto &g = ci; // g is a const int& that is bound to ci
14+
const auto f = ci; // deduced type of ci is int; f has type const int
15+
auto &g = ci; // g is a const int& that is bound to ci
1616

17-
a=42; b=42; c=42; *d=42; e=&c;
17+
a = 42;
18+
b = 42;
19+
c = 42;
20+
*d = 42;
21+
e = &c;
1822

19-
return 0;
23+
return 0;
2024
}

Diff for: ch02/ex2_35.cpp

+11-13
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33

44
int main()
55
{
6-
const int i = 42;
7-
auto j = i;
8-
const auto &k = i;
9-
auto *p = &i;
10-
const auto j2 = i, &k2 = i;
6+
const int i = 42;
7+
auto j = i;
8+
const auto &k = i;
9+
auto *p = &i;
10+
const auto j2 = i, &k2 = i;
1111

12-
// print i means int, and PKi means pointer to const int.
13-
std::cout << "j is " << typeid(j).name()
14-
<< "\nk is " << typeid(k).name()
15-
<< "\np is " << typeid(p).name()
16-
<< "\nj2 is " << typeid(j2).name()
17-
<< "\nk2 is " << typeid(k2).name()
18-
<< std::endl;
12+
// print i means int, and PKi means pointer to const int.
13+
std::cout << "j is " << typeid(j).name() << "\nk is " << typeid(k).name()
14+
<< "\np is " << typeid(p).name() << "\nj2 is "
15+
<< typeid(j2).name() << "\nk2 is " << typeid(k2).name()
16+
<< std::endl;
1917

20-
return 0;
18+
return 0;
2119
}

Diff for: ch02/ex2_42.h

+27-30
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,51 @@
11
#include <string>
22
#include <iostream>
33

4-
struct Sales_data
5-
{
6-
std::string bookNo;
7-
unsigned units_sold = 0;
8-
double revenue = 0.0;
9-
10-
void CalcRevenue(double price);
11-
double CalcAveragePrice();
12-
void SetData(Sales_data data);
13-
void AddData(Sales_data data);
14-
void Print();
4+
struct Sales_data {
5+
std::string bookNo;
6+
unsigned units_sold = 0;
7+
double revenue = 0.0;
8+
9+
void CalcRevenue(double price);
10+
double CalcAveragePrice();
11+
void SetData(Sales_data data);
12+
void AddData(Sales_data data);
13+
void Print();
1514
};
1615

1716
void Sales_data::CalcRevenue(double price)
1817
{
19-
revenue = units_sold * price;
18+
revenue = units_sold * price;
2019
}
2120

2221
void Sales_data::SetData(Sales_data data)
2322
{
24-
bookNo = data.bookNo;
25-
units_sold = data.units_sold;
26-
revenue = data.revenue;
23+
bookNo = data.bookNo;
24+
units_sold = data.units_sold;
25+
revenue = data.revenue;
2726
}
2827

2928
void Sales_data::AddData(Sales_data data)
3029
{
31-
if (bookNo != data.bookNo) return;
32-
units_sold += data.units_sold;
33-
revenue += data.revenue;
30+
if (bookNo != data.bookNo) return;
31+
units_sold += data.units_sold;
32+
revenue += data.revenue;
3433
}
3534

3635
double Sales_data::CalcAveragePrice()
3736
{
38-
if (units_sold != 0)
39-
return revenue/units_sold;
40-
else
41-
return 0.0;
37+
if (units_sold != 0)
38+
return revenue / units_sold;
39+
else
40+
return 0.0;
4241
}
4342

4443
void Sales_data::Print()
4544
{
46-
std::cout << bookNo << " " << units_sold << " " << revenue << " ";
47-
double averagePrice = CalcAveragePrice();
48-
if (averagePrice != 0.0)
49-
std::cout << averagePrice << std::endl;
50-
else
51-
std::cout << "(no sales)" << std::endl;
45+
std::cout << bookNo << " " << units_sold << " " << revenue << " ";
46+
double averagePrice = CalcAveragePrice();
47+
if (averagePrice != 0.0)
48+
std::cout << averagePrice << std::endl;
49+
else
50+
std::cout << "(no sales)" << std::endl;
5251
}
53-
54-

Diff for: ch02/ex2_42_1.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
int main()
55
{
6-
Sales_data book;
7-
double price;
8-
std::cin >> book.bookNo >> book.units_sold >> price;
9-
book.CalcRevenue(price);
10-
book.Print();
6+
Sales_data book;
7+
double price;
8+
std::cin >> book.bookNo >> book.units_sold >> price;
9+
book.CalcRevenue(price);
10+
book.Print();
1111

12-
return 0;
12+
return 0;
1313
}

Diff for: ch02/ex2_42_2.cpp

+15-17
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,21 @@
33

44
int main()
55
{
6-
Sales_data book1, book2;
7-
double price1, price2;
8-
std::cin >> book1.bookNo >> book1.units_sold >> price1;
9-
std::cin >> book2.bookNo >> book2.units_sold >> price2;
10-
book1.CalcRevenue(price1);
11-
book2.CalcRevenue(price2);
6+
Sales_data book1, book2;
7+
double price1, price2;
8+
std::cin >> book1.bookNo >> book1.units_sold >> price1;
9+
std::cin >> book2.bookNo >> book2.units_sold >> price2;
10+
book1.CalcRevenue(price1);
11+
book2.CalcRevenue(price2);
1212

13-
if (book1.bookNo == book2.bookNo)
14-
{
15-
book1.AddData(book2);
16-
book1.Print();
13+
if (book1.bookNo == book2.bookNo) {
14+
book1.AddData(book2);
15+
book1.Print();
1716

18-
return 0;
19-
}
20-
else
21-
{
22-
std::cerr << "Data must refer to same ISBN" << std::endl;
23-
return -1; // indicate failure
24-
}
17+
return 0;
18+
}
19+
else {
20+
std::cerr << "Data must refer to same ISBN" << std::endl;
21+
return -1; // indicate failure
22+
}
2523
}

Diff for: ch02/ex2_42_3.cpp

+23-28
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,31 @@
33

44
int main()
55
{
6-
Sales_data total;
7-
double totalPrice;
8-
if (std::cin >> total.bookNo >> total.units_sold >> totalPrice)
9-
{
10-
total.CalcRevenue(totalPrice);
6+
Sales_data total;
7+
double totalPrice;
8+
if (std::cin >> total.bookNo >> total.units_sold >> totalPrice) {
9+
total.CalcRevenue(totalPrice);
1110

12-
Sales_data trans;
13-
double transPrice;
14-
while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice)
15-
{
16-
trans.CalcRevenue(transPrice);
11+
Sales_data trans;
12+
double transPrice;
13+
while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice) {
14+
trans.CalcRevenue(transPrice);
1715

18-
if (total.bookNo == trans.bookNo)
19-
{
20-
total.AddData(trans);
21-
}
22-
else
23-
{
24-
total.Print();
25-
total.SetData(trans);
26-
}
27-
}
16+
if (total.bookNo == trans.bookNo) {
17+
total.AddData(trans);
18+
}
19+
else {
20+
total.Print();
21+
total.SetData(trans);
22+
}
23+
}
2824

29-
total.Print();
25+
total.Print();
3026

31-
return 0;
32-
}
33-
else
34-
{
35-
std::cerr << "No data?!" << std::endl;
36-
return -1; // indicate failure
37-
}
27+
return 0;
28+
}
29+
else {
30+
std::cerr << "No data?!" << std::endl;
31+
return -1; // indicate failure
32+
}
3833
}

0 commit comments

Comments
 (0)