diff options
author | Oskar <[email protected]> | 2024-08-03 22:51:15 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-08-03 22:51:15 +0200 |
commit | 1d105b91b9678ffaa8eddea2218ed1789f5be27c (patch) | |
tree | 89563fd6bcbe5411b244ac361098fbdddc321f5f | |
parent | 0de72ab100011ab54f99f298e936327011ebc706 (diff) |
more assignments
-rw-r--r-- | 1p25.cpp | 26 | ||||
-rw-r--r-- | 2.42-v1.cpp | 19 | ||||
-rw-r--r-- | 2.42-v2-2.cpp | 30 | ||||
-rw-r--r-- | 2.42-v2.cpp | 26 | ||||
-rw-r--r-- | 2.42-v3-2.cpp | 40 | ||||
-rw-r--r-- | 2.42-v3.cpp | 30 | ||||
-rw-r--r-- | 2.42-v4.cpp | 30 | ||||
-rw-r--r-- | 2.42-v5.cpp | 41 | ||||
-rw-r--r-- | 2p41-1p23-1p24.cpp | 36 | ||||
-rw-r--r-- | 2p41-1p25.cpp | 47 | ||||
-rw-r--r-- | 3.1-v1.cpp | 21 | ||||
-rw-r--r-- | 3.1-v2-2.cpp | 32 | ||||
-rw-r--r-- | 3.1-v2.cpp | 28 | ||||
-rw-r--r-- | 3.1-v3-2.cpp | 42 | ||||
-rw-r--r-- | 3.1-v3.cpp | 32 | ||||
-rw-r--r-- | 3.1-v4.cpp | 32 | ||||
-rw-r--r-- | 3.1-v5.cpp | 44 | ||||
-rw-r--r-- | 3p1-1p10.cpp | 23 | ||||
-rw-r--r-- | 3p1-1p11.cpp | 27 | ||||
-rw-r--r-- | 3p1-1p9.cpp | 25 | ||||
-rw-r--r-- | 3p2-v1.cpp | 28 | ||||
-rw-r--r-- | 3p2-v2.cpp | 26 | ||||
-rw-r--r-- | 3p3.cpp | 24 | ||||
-rw-r--r-- | 3p4.cpp | 42 | ||||
-rw-r--r-- | 3p5.cpp | 32 | ||||
-rw-r--r-- | decltype-tests.cpp | 6 | ||||
-rw-r--r-- | exercise-template.cpp | 9 | ||||
-rw-r--r-- | sales_data.hpp | 10 | ||||
-rw-r--r-- | strings-tests.cpp | 56 |
29 files changed, 862 insertions, 2 deletions
diff --git a/1p25.cpp b/1p25.cpp new file mode 100644 index 0000000..880a94b --- /dev/null +++ b/1p25.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include "sales_item.hpp" +int main() { + Sales_item total; // variable to hold data for the next transaction + // read the first transaction and ensure that there are data to process + if (std::cin >> total) { + Sales_item trans; // variable to hold the running sum + // read and process the remaining transactions + while (std::cin >> trans) { + // if we’re still processing the same book + if (total.isbn() == trans.isbn()) + total += trans; // update the running total + else { + // print results for the previous book + std::cout << total << std::endl; + total = trans; // total now refers to the next book + } + } + std::cout << total << std::endl; // print the last transaction + } else { + // no input! warn the user + std::cerr << "No data?!" << std::endl; + return -1; // indicate failure + } + return 0; +} diff --git a/2.42-v1.cpp b/2.42-v1.cpp new file mode 100644 index 0000000..eba28a3 --- /dev/null +++ b/2.42-v1.cpp @@ -0,0 +1,19 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.20 + * + * + */ + +int main () { + + SalesData book1; + while(std::cin >> book1.BookNo >> book1.UnitsSold >> book1.Revenue) { + std::cout << book1.BookNo << " " << book1.UnitsSold << " " << book1.Revenue << std::endl; + } + + return 0; +} diff --git a/2.42-v2-2.cpp b/2.42-v2-2.cpp new file mode 100644 index 0000000..f8f7872 --- /dev/null +++ b/2.42-v2-2.cpp @@ -0,0 +1,30 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.21 + * + * + */ + +int main () { + + SalesData book1; + SalesData book2; + if(std::cin >> book1.BookNo >> book1.UnitsSold >> book1.Revenue) { + while(std::cin >> book2.BookNo >> book2.UnitsSold >> book2.Revenue) { + if(book1.BookNo != book2.BookNo) { + std::cout << "These books do not have the same ISBN" << std::endl; + continue; + } + + std::cout << book1.BookNo << " " << book1.UnitsSold + book2.UnitsSold << " " << book1.Revenue + book2.Revenue << std::endl; + } + } else { + std::cout << "Error!" << std::endl; + return -1; + } + + return 0; +} diff --git a/2.42-v2.cpp b/2.42-v2.cpp new file mode 100644 index 0000000..f7d2261 --- /dev/null +++ b/2.42-v2.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.21 + * + * + */ + +int main () { + + SalesData book1; + SalesData book2; + while(std::cin >> book1.BookNo >> book1.UnitsSold >> book1.Revenue) { + std::cin >> book2.BookNo >> book2.UnitsSold >> book2.Revenue; + if(book1.BookNo != book2.BookNo) { + std::cout << "These books do not have the same ISBN" << std::endl; + continue; + } + + std::cout << book1.BookNo << " " << book1.UnitsSold + book2.UnitsSold << " " << book1.Revenue + book2.Revenue << std::endl; + } + + return 0; +} diff --git a/2.42-v3-2.cpp b/2.42-v3-2.cpp new file mode 100644 index 0000000..768983b --- /dev/null +++ b/2.42-v3-2.cpp @@ -0,0 +1,40 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.22 + * version 2, a bit more interactive but might not be desirable if using file redirections + * + */ + +int main () { + + SalesData book1; + SalesData Sum; + if(std::cin >> Sum.BookNo >> Sum.UnitsSold >> Sum.Revenue) { + while(std::cin >> book1.BookNo >> book1.UnitsSold >> book1.Revenue) { + if(Sum.BookNo == book1.BookNo) { + Sum.UnitsSold += book1.UnitsSold; + Sum.Revenue += book1.Revenue; + } else { + std::cout << "These books do not have the same ISBN" << std::endl; + while(1) { + std::string yesno; + std::cout << "Do you wish to continue? Yes/No " << std::endl; + std::cin >> yesno; + if(yesno == "Yes") { + break; + } else if(yesno == "No") { + return 0; + } + } + continue; + } + } + } + + std::cout << Sum.BookNo << " " << Sum.UnitsSold << " " << Sum.Revenue << std::endl; + + return 0; +} diff --git a/2.42-v3.cpp b/2.42-v3.cpp new file mode 100644 index 0000000..bc16fff --- /dev/null +++ b/2.42-v3.cpp @@ -0,0 +1,30 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.22 + * + * + */ + +int main () { + + SalesData book1; + SalesData Sum; + if(std::cin >> Sum.BookNo >> Sum.UnitsSold >> Sum.Revenue) { + while(std::cin >> book1.BookNo >> book1.UnitsSold >> book1.Revenue) { + if(Sum.BookNo == book1.BookNo) { + Sum.UnitsSold += book1.UnitsSold; + Sum.Revenue += book1.Revenue; + } else { + std::cout << "These books do not have the same ISBN" << std::endl; + return -1; + } + } + } + + std::cout << Sum.BookNo << " " << Sum.UnitsSold << " " << Sum.Revenue << std::endl; + + return 0; +} diff --git a/2.42-v4.cpp b/2.42-v4.cpp new file mode 100644 index 0000000..f0d5a7e --- /dev/null +++ b/2.42-v4.cpp @@ -0,0 +1,30 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.23 - 1.24 + * + * + */ + +int main () { + + SalesData Item; + SalesData CurItem; + if(std::cin >> CurItem.BookNo >> CurItem.UnitsSold >> CurItem.Revenue) { + unsigned int Count = CurItem.UnitsSold; + while(std::cin >> Item.BookNo >> Item.UnitsSold >> Item.Revenue) { + if(Item.BookNo == CurItem.BookNo) { + Count += Item.UnitsSold; + } else { + std::cout << "ISBN: " << CurItem.BookNo << " has " << Count << " transactions" << std::endl; + CurItem = Item; + Count = Item.UnitsSold; + } + } + std::cout << "ISBN: " << CurItem.BookNo << " has " << Count << " transactions" << std::endl; + } + + return 0; +} diff --git a/2.42-v5.cpp b/2.42-v5.cpp new file mode 100644 index 0000000..d28b6b8 --- /dev/null +++ b/2.42-v5.cpp @@ -0,0 +1,41 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.25 + * + * + */ + +int main() { + + SalesData total; // variable to hold data for the next transaction + double price = 0.0; + if (std::cin >> total.BookNo >> total.UnitsSold >> price) { + total.Revenue = price * total.UnitsSold; + SalesData trans; // variable to hold the running sum + while (std::cin >> trans.BookNo >> trans.UnitsSold >> price) { + trans.Revenue = price * trans.UnitsSold; // We can either keep this line, or comment this line out and uncomment line 34. Same behavior either way, just 2 different ways of doing the same thing. Though i do think that this is the more "correct" way of doing it + if (total.BookNo == trans.BookNo) { + total.UnitsSold += trans.UnitsSold; + total.Revenue += trans.Revenue; + } else { + double average = total.Revenue / total.UnitsSold; + std::cout << total.BookNo << " " << total.UnitsSold << " " + << total.Revenue << " avg: " << average << std::endl; + total = trans; + //total.Revenue = price * total.UnitsSold; + } + } + + double average = total.Revenue / total.UnitsSold; + std::cout << total.BookNo << " " << total.UnitsSold << " " + << total.Revenue << " avg: " << average << std::endl; + } else { + std::cerr << "No data?!" << std::endl; + return -1; + } + + return 0; +} diff --git a/2p41-1p23-1p24.cpp b/2p41-1p23-1p24.cpp new file mode 100644 index 0000000..0f55761 --- /dev/null +++ b/2p41-1p23-1p24.cpp @@ -0,0 +1,36 @@ +#include <iostream> +#include "sales_item.hpp" + +/* + * + * 2.41 - 1.23 - 1.24 + * + * + */ + +struct SalesData { + std::string BookNo; + unsigned int UnitsSold = 0; + double Revenue = 0.0; +}; + +int main () { + + SalesData Item; + SalesData CurItem; + if(std::cin >> CurItem.BookNo >> CurItem.UnitsSold >> CurItem.Revenue) { + unsigned int Count = CurItem.UnitsSold; + while(std::cin >> Item.BookNo >> Item.UnitsSold >> Item.Revenue) { + if(Item.BookNo == CurItem.BookNo) { + Count += Item.UnitsSold; + } else { + std::cout << "ISBN: " << CurItem.BookNo << " has " << Count << " transactions" << std::endl; + CurItem = Item; + Count = Item.UnitsSold; + } + } + std::cout << "ISBN: " << CurItem.BookNo << " has " << Count << " transactions" << std::endl; + } + + return 0; +} diff --git a/2p41-1p25.cpp b/2p41-1p25.cpp new file mode 100644 index 0000000..01fe709 --- /dev/null +++ b/2p41-1p25.cpp @@ -0,0 +1,47 @@ +#include <iostream> +#include "sales_item.hpp" + +/* + * + * 2.41 - 1.25 + * + * + */ + +struct SalesData { + std::string BookNo; + unsigned int UnitsSold = 0; + double Revenue = 0.0; +}; + +int main() { + + SalesData total; // variable to hold data for the next transaction + double price = 0.0; + if (std::cin >> total.BookNo >> total.UnitsSold >> price) { + total.Revenue = price * total.UnitsSold; + SalesData trans; // variable to hold the running sum + while (std::cin >> trans.BookNo >> trans.UnitsSold >> price) { + trans.Revenue = price * trans.UnitsSold; // We can either keep this line, or comment this line out and uncomment line 34. Same behavior either way, just 2 different ways of doing the same thing. Though i do think that this is the more "correct" way of doing it + if (total.BookNo == trans.BookNo) { + total.UnitsSold += trans.UnitsSold; + total.Revenue += trans.Revenue; + } else { + double average = total.Revenue / total.UnitsSold; + std::cout << total.BookNo << " " << total.UnitsSold << " " + << total.Revenue << " avg: " << average << std::endl; + total = trans; + //total.Revenue = price * total.UnitsSold; + } + } + + double average = total.Revenue / total.UnitsSold; + std::cout << total.BookNo << " " << total.UnitsSold << " " + << total.Revenue << " avg: " << average << std::endl; + } else { + std::cerr << "No data?!" << std::endl; + return -1; + } + + return 0; +} diff --git a/3.1-v1.cpp b/3.1-v1.cpp new file mode 100644 index 0000000..c8bbee9 --- /dev/null +++ b/3.1-v1.cpp @@ -0,0 +1,21 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.20 + * + * + */ +using std::cout; +using std::cin; +using std::endl; +int main () { + + SalesData book1; + while(cin >> book1.BookNo >> book1.UnitsSold >> book1.Revenue) { + cout << book1.BookNo << " " << book1.UnitsSold << " " << book1.Revenue << endl; + } + + return 0; +} diff --git a/3.1-v2-2.cpp b/3.1-v2-2.cpp new file mode 100644 index 0000000..0ac8ac3 --- /dev/null +++ b/3.1-v2-2.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.21 + * + * + */ +using std::cout; +using std::cin; +using std::endl; +int main () { + + SalesData book1; + SalesData book2; + if(cin >> book1.BookNo >> book1.UnitsSold >> book1.Revenue) { + while(cin >> book2.BookNo >> book2.UnitsSold >> book2.Revenue) { + if(book1.BookNo != book2.BookNo) { + cout << "These books do not have the same ISBN" << endl; + continue; + } + + cout << book1.BookNo << " " << book1.UnitsSold + book2.UnitsSold << " " << book1.Revenue + book2.Revenue << endl; + } + } else { + cout << "Error!" << endl; + return -1; + } + + return 0; +} diff --git a/3.1-v2.cpp b/3.1-v2.cpp new file mode 100644 index 0000000..91ecaf3 --- /dev/null +++ b/3.1-v2.cpp @@ -0,0 +1,28 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.21 + * + * + */ +using std::cout; +using std::cin; +using std::endl; +int main () { + + SalesData book1; + SalesData book2; + while(cin >> book1.BookNo >> book1.UnitsSold >> book1.Revenue) { + cin >> book2.BookNo >> book2.UnitsSold >> book2.Revenue; + if(book1.BookNo != book2.BookNo) { + cout << "These books do not have the same ISBN" << endl; + continue; + } + + cout << book1.BookNo << " " << book1.UnitsSold + book2.UnitsSold << " " << book1.Revenue + book2.Revenue << endl; + } + + return 0; +} diff --git a/3.1-v3-2.cpp b/3.1-v3-2.cpp new file mode 100644 index 0000000..9da81db --- /dev/null +++ b/3.1-v3-2.cpp @@ -0,0 +1,42 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.22 + * version 2, a bit more interactive but might not be desirable if using file redirections + * + */ +using std::cout; +using std::cin; +using std::endl; +int main () { + + SalesData book1; + SalesData Sum; + if(cin >> Sum.BookNo >> Sum.UnitsSold >> Sum.Revenue) { + while(cin >> book1.BookNo >> book1.UnitsSold >> book1.Revenue) { + if(Sum.BookNo == book1.BookNo) { + Sum.UnitsSold += book1.UnitsSold; + Sum.Revenue += book1.Revenue; + } else { + cout << "These books do not have the same ISBN" << endl; + while(1) { + std::string yesno; + cout << "Do you wish to continue? Yes/No " << endl; + cin >> yesno; + if(yesno == "Yes") { + break; + } else if(yesno == "No") { + return 0; + } + } + continue; + } + } + } + + cout << Sum.BookNo << " " << Sum.UnitsSold << " " << Sum.Revenue << endl; + + return 0; +} diff --git a/3.1-v3.cpp b/3.1-v3.cpp new file mode 100644 index 0000000..4457731 --- /dev/null +++ b/3.1-v3.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.22 + * + * + */ +using std::cout; +using std::cin; +using std::endl; +int main () { + + SalesData book1; + SalesData Sum; + if(cin >> Sum.BookNo >> Sum.UnitsSold >> Sum.Revenue) { + while(cin >> book1.BookNo >> book1.UnitsSold >> book1.Revenue) { + if(Sum.BookNo == book1.BookNo) { + Sum.UnitsSold += book1.UnitsSold; + Sum.Revenue += book1.Revenue; + } else { + cout << "These books do not have the same ISBN" << endl; + return -1; + } + } + } + + cout << Sum.BookNo << " " << Sum.UnitsSold << " " << Sum.Revenue << endl; + + return 0; +} diff --git a/3.1-v4.cpp b/3.1-v4.cpp new file mode 100644 index 0000000..df74ddb --- /dev/null +++ b/3.1-v4.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.23 - 1.24 + * + * + */ +using std::cout; +using std::cin; +using std::endl; +int main () { + + SalesData Item; + SalesData CurItem; + if(cin >> CurItem.BookNo >> CurItem.UnitsSold >> CurItem.Revenue) { + unsigned int Count = CurItem.UnitsSold; + while(cin >> Item.BookNo >> Item.UnitsSold >> Item.Revenue) { + if(Item.BookNo == CurItem.BookNo) { + Count += Item.UnitsSold; + } else { + cout << "ISBN: " << CurItem.BookNo << " has " << Count << " transactions" << endl; + CurItem = Item; + Count = Item.UnitsSold; + } + } + cout << "ISBN: " << CurItem.BookNo << " has " << Count << " transactions" << endl; + } + + return 0; +} diff --git a/3.1-v5.cpp b/3.1-v5.cpp new file mode 100644 index 0000000..95df66f --- /dev/null +++ b/3.1-v5.cpp @@ -0,0 +1,44 @@ +#include <iostream> +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.25 + * + * + */ +using std::cout; +using std::cin; +using std::endl; +using std::cerr; +int main() { + + SalesData total; // variable to hold data for the next transaction + double price = 0.0; + if (cin >> total.BookNo >> total.UnitsSold >> price) { + total.Revenue = price * total.UnitsSold; + SalesData trans; // variable to hold the running sum + while (cin >> trans.BookNo >> trans.UnitsSold >> price) { + trans.Revenue = price * trans.UnitsSold; // We can either keep this line, or comment this line out and uncomment line 34. Same behavior either way, just 2 different ways of doing the same thing. Though i do think that this is the more "correct" way of doing it + if (total.BookNo == trans.BookNo) { + total.UnitsSold += trans.UnitsSold; + total.Revenue += trans.Revenue; + } else { + double average = total.Revenue / total.UnitsSold; + cout << total.BookNo << " " << total.UnitsSold << " " + << total.Revenue << " avg: " << average << endl; + total = trans; + //total.Revenue = price * total.UnitsSold; + } + } + + double average = total.Revenue / total.UnitsSold; + cout << total.BookNo << " " << total.UnitsSold << " " + << total.Revenue << " avg: " << average << endl; + } else { + cerr << "No data?!" << endl; + return -1; + } + + return 0; +} diff --git a/3p1-1p10.cpp b/3p1-1p10.cpp new file mode 100644 index 0000000..79a1396 --- /dev/null +++ b/3p1-1p10.cpp @@ -0,0 +1,23 @@ +#include <iostream> +#include "sales_item.hpp" + +/* + * + * 3.1, 1.10 + * + * + */ + +using std::cout; +using std::cin; +using std::endl; +int main () { + + int i = 10; + while(i >= 0) { + cout << i << endl; + i--; + } + + return 0; +} diff --git a/3p1-1p11.cpp b/3p1-1p11.cpp new file mode 100644 index 0000000..8f8a647 --- /dev/null +++ b/3p1-1p11.cpp @@ -0,0 +1,27 @@ +#include <iostream> +#include "sales_item.hpp" + +/* + * + * 3.1, 1.11 + * + * + */ + +using std::cout; +using std::cin; +using std::endl; +int main () { + + int First = 0; + int Second = 0; + cin >> First >> Second; + cout << endl; + First++; + while(First < Second) { + cout << First << endl; + First++; + } + + return 0; +} diff --git a/3p1-1p9.cpp b/3p1-1p9.cpp new file mode 100644 index 0000000..ba717e3 --- /dev/null +++ b/3p1-1p9.cpp @@ -0,0 +1,25 @@ +#include <iostream> +#include "sales_item.hpp" + +/* + * + * 3.1, 1.9 + * + * + */ + +using std::cout; +using std::cin; +using std::endl; +int main () { + + int i = 50; + int sum = 0; + while(i <= 100) { + sum += i; + i++; + } + + cout << sum << endl; + return 0; +} diff --git a/3p2-v1.cpp b/3p2-v1.cpp new file mode 100644 index 0000000..0facda6 --- /dev/null +++ b/3p2-v1.cpp @@ -0,0 +1,28 @@ +#include <iostream> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.2 v1 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string line; + while(getline(cin, line)) { + if(!line.empty()) { + cout << line << endl; + } + } + + return 0; +} diff --git a/3p2-v2.cpp b/3p2-v2.cpp new file mode 100644 index 0000000..8b88120 --- /dev/null +++ b/3p2-v2.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.2 v2 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string word; + while(cin >> word) { + cout << word << endl; + } + + return 0; +} @@ -0,0 +1,24 @@ +#include <iostream> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.3 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string word; + getline(cin, word); // Getline does not care about anything except the newline character. If it sees a newline it stops reading. + cout << word << endl; + return 0; +} @@ -0,0 +1,42 @@ +#include <iostream> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.4 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string word1; + string word2; + cin >> word1 >> word2; + if(word1 == word2) { + cout << word1 << " and " << word2 << " are equal! " << endl; + } else if (word1 > word2) { + cout << word1 << " is larger than " << word2 << endl; + } else if (word2 > word1) { + cout << word2 << " is larger than " << word1 << endl; + } + + string::size_type w1 = word1.size(); + string::size_type w2 = word2.size(); + if(w1 == w2) { + cout << word1 << " and " << word2 << " have the same length" << endl; + } else if (w1 > w2) { + cout << word1 << " has a longer length than " << word2 << endl; + } else if (w2 > w1) { + cout << word2 << " has a longer length than " << word1 << endl; + } + + return 0; +} @@ -0,0 +1,32 @@ +#include <iostream> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * Description + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string full; + string full_sep; + //string whitespace(" "); + string DD; + while(cin >> DD) { + full += DD; + full_sep += DD + ' '; + } + cout << full << endl; + cout << full_sep << endl; + + return 0; +} diff --git a/decltype-tests.cpp b/decltype-tests.cpp index d0bf7e2..e18fd0a 100644 --- a/decltype-tests.cpp +++ b/decltype-tests.cpp @@ -19,10 +19,14 @@ int main () { decltype(ii + 0) dtii2 = 102; //int decltype(*iii) dtiii = i; //int& decltype(*iii + 0) dtiii2 = i; //int + decltype(iii) dtiii3 = &i; //int* std::cout << dti << " " // Will print 101 << dtii << " " // Will print 101 << dtii2 << " " // Will print 102 << dtiii << " " // Will print 100 - << dtiii2 << " " << std::endl; // Will print 100 + << dtiii2 << " " // Will print 100 + << dtiii3 << " " // Address if i + << &i << " " // address of i + << std::endl; return 0; } diff --git a/exercise-template.cpp b/exercise-template.cpp index c60907f..38099b7 100644 --- a/exercise-template.cpp +++ b/exercise-template.cpp @@ -1,4 +1,5 @@ #include <iostream> +#include "sales_data.hpp" #include "sales_item.hpp" /* @@ -8,8 +9,14 @@ * */ +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; int main () { - + return 0; } diff --git a/sales_data.hpp b/sales_data.hpp new file mode 100644 index 0000000..cce4469 --- /dev/null +++ b/sales_data.hpp @@ -0,0 +1,10 @@ +#ifndef SALES_DATA_H +#define SALES_DATA_H +#include <string> +struct SalesData { + std::string BookNo; + unsigned int UnitsSold = 0; + double Revenue = 0.0; +}; + +#endif diff --git a/strings-tests.cpp b/strings-tests.cpp new file mode 100644 index 0000000..7cce52e --- /dev/null +++ b/strings-tests.cpp @@ -0,0 +1,56 @@ +#include <iostream> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * Description + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string s1; // default initialization + string s2 = s1; // s2 copy from s1 + string s3 = "Hello, World!"; // initialize by copying string literal + string s4(10, 'a'); // initialize with n copies of a + string s5 = s3 + s4; // Initialize with s3+s4. Both are combined and copied to s5? + string s6("Hello, World! (s6)"); // Initialize by copying string literal + string s7(s3); // s7 is a copy of s3 + cout << s1 << endl; + cout << s2 << endl; + cout << s3 << endl; + cout << s4 << endl; + cout << s5 << endl; + cout << s6 << endl; + cout << s7 << endl; + + int choice = 0; + cin >> choice; + if(choice == 1) { + string input; + while(cin >> input) { cout << input << " "; } + + } else if (choice == 2) { + string word; + string::size_type total_len = 0; + while(getline(cin, word)) { + if(!word.empty()) { + total_len += word.size(); + cout << word << endl; + } + } + cout << "Total output amount of characters: " << total_len << endl; + } else { + return -1; + } + + return 0; +} |