Skip to content

Commit

Permalink
added ex3.1~3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Aug 24, 2014
1 parent da73ff1 commit cf44ffb
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ch03/ex3_1a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// use `using` for 1.4.1

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
int sum = 0, val = 1;
// keep executing the while as long as val is less than or equal to 10
while (val <= 10)
{
sum += val;
++val;
}
cout << "Sum of 1 to 10 inclusive is " << sum << endl;
return 0;
}
48 changes: 48 additions & 0 deletions ch03/ex3_1b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <string>
#include "../ch02/Sales_data.h"

using std::cin;
using std::cout;
using std::endl;
using std::cerr;

int main()
{
Sales_data data1, data2;

// code to read into data1 and data2
double price = 0; // price per book, used to calculate total revenue

// read the first transactions: ISBN, number of books sold, price per book
cin >> data1.bookNo >> data1.units_sold >> price;
// calculate total revenue from price and units_sold
data1.revenue = data1.units_sold * price;

// read the second transaction
cin >> data2.bookNo >> data2.units_sold >> price;
data2.revenue = data2.units_sold * price;

// code to check whether data1 and data2 have the same ISBN
// and if so print the sum of data1 and data2
if (data1.bookNo == data2.bookNo)
{
unsigned totalCnt = data1.units_sold + data2.units_sold;
double totalRevenue = data1.revenue + data2.revenue;

// print: ISBN, total sold, total revenue, average price per book
cout << data1.bookNo << " " << totalCnt
<< " " << totalRevenue << " ";
if (totalCnt != 0)
cout << totalRevenue/totalCnt << endl;
else
cout << "(no sales)" << endl;

return 0; // indicate success
}
else
{ // transactions weren't for the same ISBN
cerr << "Data must refer to the same ISBN" << endl;
return -1; // indicate failure
}
}
18 changes: 18 additions & 0 deletions ch03/ex3_2a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// read the standard input a line at a time.
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::getline;

int main()
{
string input;
while (getline(cin, input))
cout << input << endl;

return 0;
}
17 changes: 17 additions & 0 deletions ch03/ex3_2b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// read the standard input a word at a time
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
string word;
while (cin >> word)
cout << word << endl;

return 0;
}
23 changes: 23 additions & 0 deletions ch03/ex3_4a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// read two strings and report whether the strings are equal
// If not, report which of the two is larger.
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
string str1, str2;
while (cin >> str1 >> str2)
{
if (str1 == str2)
cout << "The two strings are equal." << endl;
else
cout << "The larger string is " << ((str1 > str2) ? str1 : str2);
}

return 0;
}
23 changes: 23 additions & 0 deletions ch03/ex3_4b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// read two strings and report whether the strings have the same length
// If not, report which is longer
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
string str1, str2;
while (cin >> str1 >> str2)
{
if (str1.size() == str2.size())
cout << "The two strings have the same length." << endl;
else
cout << "The longer string is " << ((str1.size() > str2.size()) ? str1 : str2);
}

return 0;
}
23 changes: 23 additions & 0 deletions ch03/ex3_5a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//read strings from the standard input, concatenating what is read into one large string.
//Print the concatenated string.
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
string largeStr;
string str;
while (cin >> str)
{
largeStr += str;
}

cout << "The concatenated string is " << largeStr << endl;

return 0;
}
25 changes: 25 additions & 0 deletions ch03/ex3_5b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//separate adjacent input strings by a space.
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
string largeStr;
string str;
while (cin >> str)
{
if (largeStr.empty())
largeStr += str;
else
largeStr += " " + str;
}

cout << "The concatenated string is " << largeStr << endl;

return 0;
}
1 change: 1 addition & 0 deletions docs/ch03/ex3.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
##Exercise 3.3>Explain how whitespace characters are handled in the stringinput operator and in the getline function.The `getline` function takes an input stream and a string.This function reads the given stream up to and including the first newline and stores what it read—not includingthe newline—in its string argument.After getline sees a newline, even if it is the first character in the input,it stops reading and returns.If the first character in the input is a newline,then the resulting string is the empty string.`getline` function whitespace handling,do not ignore the beginning of the line blank characters read charactersuntil it encounters a line break,read to termination and discard newline(line breaks removed from the input stream but is not stored in the string object).[Read more](http://www.cplusplus.com/reference/string/string/getline/)
Expand Down
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ C++ Primer 5th Answer Note
- [Exercise 2.36 ~ 2.38](/Cpp-Primer/ch02/ex2.36_2.38)
- [Exercise 2.39 ~ 2.42](/Cpp-Primer/ch02/ex2.39_2.42)
- Chapter 3. Strings, Vectors, and Arrays
- Exercise [3.1a](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_1a.cpp)&[3.1b](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_1b.cpp)
- Exercise [3.2a](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_2a.cpp)&[3.2b](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_2b.cpp) |
[3.3](/Cpp-Primer/ch03/ex3.3) |
[3.4a](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_4a.cpp)&[3.4b](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_4b.cpp) |
[3.5a](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_5a.cpp)&[3.5b](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_5b.cpp)
- Chapter 4. Expressions
- Chapter 5. Statements
- Chapter 6. Functions
Expand Down

0 comments on commit cf44ffb

Please sign in to comment.