forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters