Skip to content

Commit

Permalink
improved ch01 code formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jun 22, 2015
1 parent 3d5a7a1 commit 20b2d61
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 132 deletions.
94 changes: 47 additions & 47 deletions ch01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ int main()

int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The product of " << v1 << " and " << v2
<< " is " << v1 * v2 << std::endl;
return 0;
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The product of " << v1 << " and " << v2
<< " is " << v1 * v2 << std::endl;
return 0;
}
```

Expand All @@ -63,17 +63,17 @@ int main()

int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The product of ";
std::cout << v1;
std::cout << " and ";
std::cout << v2;
std::cout << " is ";
std::cout << v1 * v2;
std::cout << std::endl;
return 0;
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The product of ";
std::cout << v1;
std::cout << " and ";
std::cout << v2;
std::cout << " is ";
std::cout << v1 * v2;
std::cout << std::endl;
return 0;
}
```

Expand Down Expand Up @@ -138,8 +138,9 @@ std::cout << /* "*/" /* "/*" */;
```

Output:

```sh
/**/ */ /*
```

##[Exercise 1.9](ex1_9.cpp)
##[Exercise 1.10](ex1_10.cpp)
Expand All @@ -151,7 +152,7 @@ of sum?
```cpp
int sum = 0;
for (int i = -100; i <= 100; ++i)
sum += i;
sum += i;
```

the loop sums the numbers from -100 to 100. the final value of sum is zero.
Expand All @@ -165,13 +166,13 @@ Ex1.9:

int main()
{
int sum = 0;
for (int i=50; i<=100; ++i)
sum += i;
int sum = 0;
for (int i=50; i<=100; ++i)
sum += i;

std::cout << "the sum is: " << sum << std::endl;
std::cout << "the sum is: " << sum << std::endl;

return 0;
return 0;
}
```

Expand All @@ -181,10 +182,10 @@ Ex1.10:

int main()
{
for (int i=10; i>=0; --i)
std::cout << i << std::endl;
for (int i=10; i>=0; --i)
std::cout << i << std::endl;

return 0;
return 0;
}
```

Expand All @@ -194,21 +195,20 @@ Ex1.11:

int main()
{
int val_small = 0, val_big = 0;
std::cout << "please input two integers:";
std::cin >> val_small >> val_big;
int val_small = 0, val_big = 0;
std::cout << "please input two integers:";
std::cin >> val_small >> val_big;

if (val_small > val_big)
{
int tmp = val_small;
val_small = val_big;
val_big = tmp;
}
if (val_small > val_big) {
int tmp = val_small;
val_small = val_big;
val_big = tmp;
}

for (int i=val_small; i<=val_big; ++i)
std::cout << i << std::endl;
for (int i = val_small; i <= val_big; ++i)
std::cout << i << std::endl;

return 0;
return 0;
}
```

Expand Down Expand Up @@ -245,17 +245,17 @@ The following are my own version:

int main()
{
int limit = 0, sum = 0, value = 0;
std::cout << "How many integers would you like to enter?";
std::cin >> limit;
int limit = 0, sum = 0, value = 0;
std::cout << "How many integers would you like to enter?";
std::cin >> limit;

// assume we don't know what is EOF(End-Of-File).
while (std::cin >> value && (--limit != 0))
sum += value;
// assume we don't know what is EOF(End-Of-File).
while (std::cin >> value && (--limit != 0))
sum += value;

std::cout << sum + value << std::endl;
std::cout << sum + value << std::endl;

return 0;
return 0;
}
```

Expand Down
2 changes: 1 addition & 1 deletion ch01/ex1_1.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
int main()
{
return 0;
return 0;
}
15 changes: 7 additions & 8 deletions ch01/ex1_10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

int main()
{
int val = 10;
while (val >= 0)
{
std::cout << val << std::endl;
--val;
}

return 0;
int val = 10;
while (val >= 0) {
std::cout << val << std::endl;
--val;
}

return 0;
}
34 changes: 16 additions & 18 deletions ch01/ex1_11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

int main()
{
int val_small = 0, val_big = 0;
std::cout << "please input two integers:";
std::cin >> val_small >> val_big;

if (val_small > val_big)
{
int tmp = val_small;
val_small = val_big;
val_big = tmp;
}

while (val_small <= val_big)
{
std::cout << val_small << std::endl;
++val_small;
}

return 0;
int val_small = 0, val_big = 0;
std::cout << "please input two integers:";
std::cin >> val_small >> val_big;

if (val_small > val_big) {
int tmp = val_small;
val_small = val_big;
val_big = tmp;
}

while (val_small <= val_big) {
std::cout << val_small << std::endl;
++val_small;
}

return 0;
}
16 changes: 8 additions & 8 deletions ch01/ex1_16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

int main()
{
int limit = 0, sum = 0, value = 0;
std::cout << "How many integers would you like to enter?";
std::cin >> limit;
int limit = 0, sum = 0, value = 0;
std::cout << "How many integers would you like to enter?";
std::cin >> limit;

// assume we don't know what is EOF(End-Of-File).
while (std::cin >> value && (--limit != 0))
sum += value;
// assume we don't know what is EOF(End-Of-File).
while (std::cin >> value && (--limit != 0))
sum += value;

std::cout << sum + value << std::endl;
std::cout << sum + value << std::endl;

return 0;
return 0;
}
10 changes: 5 additions & 5 deletions ch01/ex1_20.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <iostream>
#include "include/Sales_item.h"
#include "../include/Sales_item.h"

int main()
{
Sales_item item;
while (std::cin >> item)
std::cout << item << std::endl;
Sales_item item;
while (std::cin >> item)
std::cout << item << std::endl;

return 0;
return 0;
}
24 changes: 11 additions & 13 deletions ch01/ex1_21.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#include <iostream>
#include "include/Sales_item.h"
#include "../include/Sales_item.h"

int main()
{
Sales_item item1, item2;
std::cin >> item1 >> item2;
if (item1.isbn() == item2.isbn())
{
std::cout << item1 + item2 << std::endl;
return 0;
}
else
{
std::cerr << "Data must refer to same ISBN." << std::endl;
return -1;
}
Sales_item item1, item2;
std::cin >> item1 >> item2;
if (item1.isbn() == item2.isbn()) {
std::cout << item1 + item2 << std::endl;
return 0;
}
else {
std::cerr << "Data must refer to same ISBN." << std::endl;
return -1;
}
}
2 changes: 1 addition & 1 deletion ch01/ex1_22.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>
#include "include/Sales_item.h"
#include "../include/Sales_item.h"

int main()
{
Expand Down
38 changes: 17 additions & 21 deletions ch01/ex1_23.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
#include <iostream>
#include "include/Sales_item.h"
#include "../include/Sales_item.h"

int main()
{
Sales_item currItem, valItem;
if (std::cin >> currItem)
{
int cnt = 1;
while (std::cin >> valItem)
{
if (valItem.isbn() == currItem.isbn())
++cnt;
else
{
Sales_item currItem, valItem;
if (std::cin >> currItem) {
int cnt = 1;
while (std::cin >> valItem) {
if (valItem.isbn() == currItem.isbn())
++cnt;
else {
std::cout << currItem << " occurs "
<< cnt << " times " << std::endl;
currItem = valItem;
cnt = 1;
}
}

std::cout << currItem << " occurs "
<< cnt << " times " << std::endl;
currItem = valItem;
cnt = 1;
}
<< cnt << " times " << std::endl;
}

std::cout << currItem << " occurs "
<< cnt << " times " << std::endl;
}
return 0;
return 0;
}

19 changes: 9 additions & 10 deletions ch01/ex1_9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

int main()
{
int sum = 0, val = 50;
while (val <= 100)
{
sum += val;
++val;
}

std::cout << "the sum is: " << sum << std::endl;

return 0;
int sum = 0, val = 50;
while (val <= 100) {
sum += val;
++val;
}

std::cout << "the sum is: " << sum << std::endl;

return 0;
}

// output: the sum is: 3825

0 comments on commit 20b2d61

Please sign in to comment.