Skip to content

Commit

Permalink
improving format by clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jun 26, 2015
1 parent fb80272 commit f266ca1
Show file tree
Hide file tree
Showing 84 changed files with 1,077 additions and 952 deletions.
65 changes: 65 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: 0
AlignAfterOpenBracket: true
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AlignConsecutiveAssignments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AllowShortFunctionsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakTemplateDeclarations: false
AlwaysBreakBeforeMultilineStrings: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BinPackParameters: true
BinPackArguments: true
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
DerivePointerAlignment: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: false
IndentWrappedFunctionNames: false
IndentFunctionDeclarationAfterType: false
MaxEmptyLinesToKeep: 1
KeepEmptyLinesAtTheStartOfBlocks: true
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
SpacesBeforeTrailingComments: 1
Cpp11BracedListStyle: true
Standard: Cpp11
IndentWidth: 4
TabWidth: 4
UseTab: Never
BreakBeforeBraces: Stroustrup
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpaceAfterCStyleCast: false
SpacesInContainerLiterals: true
SpaceBeforeAssignmentOperators: true
ContinuationIndentWidth: 4
CommentPragmas: '^ IWYU pragma:'
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
SpaceBeforeParens: ControlStatements
DisableFormat: false
...
5 changes: 4 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ int main()
}
```

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

#### Tips for good commits and issues.

Expand Down
3 changes: 1 addition & 2 deletions ch01/ex1_20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
int main()
{
Sales_item item;
while (std::cin >> item)
std::cout << item << std::endl;
while (std::cin >> item) std::cout << item << std::endl;

return 0;
}
3 changes: 2 additions & 1 deletion ch01/ex1_23.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ int main()
if (valItem.isbn() == currItem.isbn())
++cnt;
else {
std::cout << currItem << " occurs " << cnt << " times " << std::endl;
std::cout << currItem << " occurs " << cnt << " times "
<< std::endl;
currItem = valItem;
cnt = 1;
}
Expand Down
20 changes: 10 additions & 10 deletions ch02/ex2_04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

int main()
{
unsigned u = 10, u2 = 42;
std::cout << u2 - u << std::endl; // 32
std::cout << u - u2 << std::endl; // 4294967264
int i = 10, i2 = 42;
std::cout << i2 - i << std::endl; // 32
std::cout << i - i2 << std::endl; // -32
std::cout << i - u << std::endl; // 0
std::cout << u - i << std::endl; // 0
return 0;
unsigned u = 10, u2 = 42;
std::cout << u2 - u << std::endl; // 32
std::cout << u - u2 << std::endl; // 4294967264
int i = 10, i2 = 42;
std::cout << i2 - i << std::endl; // 32
std::cout << i - i2 << std::endl; // -32
std::cout << i - u << std::endl; // 0
std::cout << u - i << std::endl; // 0

return 0;
}
26 changes: 15 additions & 11 deletions ch02/ex2_34.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

int main()
{
int i = 0, &r = i;
auto a = r; // a is an int (r is an alias for i, which has type int)
int i = 0, &r = i;
auto a = r; // a is an int (r is an alias for i, which has type int)

const int ci = i, &cr = ci;
auto b = ci; // b is an int (top-level const in ci is dropped)
auto c = cr; // c is an int (cr is an alias for ci whose const is top-level)
auto d = &i; // d is an int* (& ofan int objectis int*)
auto e = &ci; // e is const int*(& of a const object is low-level const)
const int ci = i, &cr = ci;
auto b = ci; // b is an int (top-level const in ci is dropped)
auto c = cr; // c is an int (cr is an alias for ci whose const is top-level)
auto d = &i; // d is an int* (& ofan int objectis int*)
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
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

a=42; b=42; c=42; *d=42; e=&c;
a = 42;
b = 42;
c = 42;
*d = 42;
e = &c;

return 0;
return 0;
}
24 changes: 11 additions & 13 deletions ch02/ex2_35.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@

int main()
{
const int i = 42;
auto j = i;
const auto &k = i;
auto *p = &i;
const auto j2 = i, &k2 = i;
const int i = 42;
auto j = 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()
<< "\np is " << typeid(p).name()
<< "\nj2 is " << typeid(j2).name()
<< "\nk2 is " << typeid(k2).name()
<< std::endl;
// print i means int, and PKi means pointer to const int.
std::cout << "j is " << typeid(j).name() << "\nk is " << typeid(k).name()
<< "\np is " << typeid(p).name() << "\nj2 is "
<< typeid(j2).name() << "\nk2 is " << typeid(k2).name()
<< std::endl;

return 0;
return 0;
}
57 changes: 27 additions & 30 deletions ch02/ex2_42.h
Original file line number Diff line number Diff line change
@@ -1,54 +1,51 @@
#include <string>
#include <iostream>

struct Sales_data
{
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;

void CalcRevenue(double price);
double CalcAveragePrice();
void SetData(Sales_data data);
void AddData(Sales_data data);
void Print();
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;

void CalcRevenue(double price);
double CalcAveragePrice();
void SetData(Sales_data data);
void AddData(Sales_data data);
void Print();
};

void Sales_data::CalcRevenue(double price)
{
revenue = units_sold * price;
revenue = units_sold * price;
}

void Sales_data::SetData(Sales_data data)
{
bookNo = data.bookNo;
units_sold = data.units_sold;
revenue = data.revenue;
bookNo = data.bookNo;
units_sold = data.units_sold;
revenue = data.revenue;
}

void Sales_data::AddData(Sales_data data)
{
if (bookNo != data.bookNo) return;
units_sold += data.units_sold;
revenue += data.revenue;
if (bookNo != data.bookNo) return;
units_sold += data.units_sold;
revenue += data.revenue;
}

double Sales_data::CalcAveragePrice()
{
if (units_sold != 0)
return revenue/units_sold;
else
return 0.0;
if (units_sold != 0)
return revenue / units_sold;
else
return 0.0;
}

void Sales_data::Print()
{
std::cout << bookNo << " " << units_sold << " " << revenue << " ";
double averagePrice = CalcAveragePrice();
if (averagePrice != 0.0)
std::cout << averagePrice << std::endl;
else
std::cout << "(no sales)" << std::endl;
std::cout << bookNo << " " << units_sold << " " << revenue << " ";
double averagePrice = CalcAveragePrice();
if (averagePrice != 0.0)
std::cout << averagePrice << std::endl;
else
std::cout << "(no sales)" << std::endl;
}


12 changes: 6 additions & 6 deletions ch02/ex2_42_1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

int main()
{
Sales_data book;
double price;
std::cin >> book.bookNo >> book.units_sold >> price;
book.CalcRevenue(price);
book.Print();
Sales_data book;
double price;
std::cin >> book.bookNo >> book.units_sold >> price;
book.CalcRevenue(price);
book.Print();

return 0;
return 0;
}
32 changes: 15 additions & 17 deletions ch02/ex2_42_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@

int main()
{
Sales_data book1, book2;
double price1, price2;
std::cin >> book1.bookNo >> book1.units_sold >> price1;
std::cin >> book2.bookNo >> book2.units_sold >> price2;
book1.CalcRevenue(price1);
book2.CalcRevenue(price2);
Sales_data book1, book2;
double price1, price2;
std::cin >> book1.bookNo >> book1.units_sold >> price1;
std::cin >> book2.bookNo >> book2.units_sold >> price2;
book1.CalcRevenue(price1);
book2.CalcRevenue(price2);

if (book1.bookNo == book2.bookNo)
{
book1.AddData(book2);
book1.Print();
if (book1.bookNo == book2.bookNo) {
book1.AddData(book2);
book1.Print();

return 0;
}
else
{
std::cerr << "Data must refer to same ISBN" << std::endl;
return -1; // indicate failure
}
return 0;
}
else {
std::cerr << "Data must refer to same ISBN" << std::endl;
return -1; // indicate failure
}
}
51 changes: 23 additions & 28 deletions ch02/ex2_42_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,31 @@

int main()
{
Sales_data total;
double totalPrice;
if (std::cin >> total.bookNo >> total.units_sold >> totalPrice)
{
total.CalcRevenue(totalPrice);
Sales_data total;
double totalPrice;
if (std::cin >> total.bookNo >> total.units_sold >> totalPrice) {
total.CalcRevenue(totalPrice);

Sales_data trans;
double transPrice;
while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice)
{
trans.CalcRevenue(transPrice);
Sales_data trans;
double transPrice;
while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice) {
trans.CalcRevenue(transPrice);

if (total.bookNo == trans.bookNo)
{
total.AddData(trans);
}
else
{
total.Print();
total.SetData(trans);
}
}
if (total.bookNo == trans.bookNo) {
total.AddData(trans);
}
else {
total.Print();
total.SetData(trans);
}
}

total.Print();
total.Print();

return 0;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
return 0;
}
else {
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
}
Loading

0 comments on commit f266ca1

Please sign in to comment.