summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-08-01 12:11:23 +0200
committerOskar <[email protected]>2024-08-01 12:11:23 +0200
commit0de72ab100011ab54f99f298e936327011ebc706 (patch)
treeb0658b0e5d9b67b214e03177f7d08704038eed14
parenta450487a18ec72ea3a3815bfaf17b2f701796b6f (diff)
more exercises
-rw-r--r--2p39.cpp17
-rw-r--r--2p40.cpp28
-rw-r--r--2p41-1p20.cpp25
-rw-r--r--2p41-1p21-v1.cpp32
-rw-r--r--2p41-1p21-v2.cpp36
-rw-r--r--2p41-1p22-v1.cpp36
-rw-r--r--2p41-1p22-v2.cpp46
7 files changed, 220 insertions, 0 deletions
diff --git a/2p39.cpp b/2p39.cpp
new file mode 100644
index 0000000..c608394
--- /dev/null
+++ b/2p39.cpp
@@ -0,0 +1,17 @@
+#include <iostream>
+
+
+/*
+ *
+ * 2.39
+ *
+ *
+ */
+
+struct fff {/*...*/};
+
+int main () {
+
+ std::cout << "Remove semicolon after struct in file '2p39.cpp' and try compiling to see what the compiler says" << std::endl;
+ return 0;
+}
diff --git a/2p40.cpp b/2p40.cpp
new file mode 100644
index 0000000..2247529
--- /dev/null
+++ b/2p40.cpp
@@ -0,0 +1,28 @@
+#include <iostream>
+
+
+/*
+ *
+ * 2.40
+ *
+ *
+ */
+
+struct SalesData {
+ std::string BookNo;
+ unsigned int UnitsSold = 0;
+ double Revenue = 0.0;
+};
+
+int main () {
+
+ struct SalesData Book;
+ Book.BookNo = "this is the BookNo";
+ Book.UnitsSold = 243;
+ Book.Revenue = 3294;
+ std::cout << Book.BookNo << "\n"
+ << Book.UnitsSold << "\n"
+ << Book.Revenue
+ << std::endl;
+ return 0;
+}
diff --git a/2p41-1p20.cpp b/2p41-1p20.cpp
new file mode 100644
index 0000000..8cbd0b1
--- /dev/null
+++ b/2p41-1p20.cpp
@@ -0,0 +1,25 @@
+#include <iostream>
+#include "sales_item.hpp"
+
+/*
+ *
+ * 2.41 - 1.20
+ *
+ *
+ */
+
+struct SalesData {
+ std::string BookNo;
+ unsigned int UnitsSold = 0;
+ double Revenue = 0.0;
+};
+
+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/2p41-1p21-v1.cpp b/2p41-1p21-v1.cpp
new file mode 100644
index 0000000..be6e972
--- /dev/null
+++ b/2p41-1p21-v1.cpp
@@ -0,0 +1,32 @@
+#include <iostream>
+#include "sales_item.hpp"
+
+/*
+ *
+ * 2.41 - 1.21
+ *
+ *
+ */
+
+struct SalesData {
+ std::string BookNo;
+ unsigned int UnitsSold = 0;
+ double Revenue = 0.0;
+};
+
+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/2p41-1p21-v2.cpp b/2p41-1p21-v2.cpp
new file mode 100644
index 0000000..248a059
--- /dev/null
+++ b/2p41-1p21-v2.cpp
@@ -0,0 +1,36 @@
+#include <iostream>
+#include "sales_item.hpp"
+
+/*
+ *
+ * 2.41 - 1.21
+ *
+ *
+ */
+
+struct SalesData {
+ std::string BookNo;
+ unsigned int UnitsSold = 0;
+ double Revenue = 0.0;
+};
+
+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/2p41-1p22-v1.cpp b/2p41-1p22-v1.cpp
new file mode 100644
index 0000000..95af902
--- /dev/null
+++ b/2p41-1p22-v1.cpp
@@ -0,0 +1,36 @@
+#include <iostream>
+#include "sales_item.hpp"
+
+/*
+ *
+ * 2.41 - 1.22
+ *
+ *
+ */
+
+struct SalesData {
+ std::string BookNo;
+ unsigned int UnitsSold = 0;
+ double Revenue = 0.0;
+};
+
+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/2p41-1p22-v2.cpp b/2p41-1p22-v2.cpp
new file mode 100644
index 0000000..25a49bd
--- /dev/null
+++ b/2p41-1p22-v2.cpp
@@ -0,0 +1,46 @@
+#include <iostream>
+#include "sales_item.hpp"
+
+/*
+ *
+ * 2.41 - 1.22
+ * version 2, a bit more interactive but might not be desirable if using file redirections
+ *
+ */
+
+struct SalesData {
+ std::string BookNo;
+ unsigned int UnitsSold = 0;
+ double Revenue = 0.0;
+};
+
+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;
+}