summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-08-30 18:32:44 +0200
committerOskar <[email protected]>2024-08-30 18:32:44 +0200
commit8f54ef4d7e1c112e6dde2947a6a9a27c8393f899 (patch)
treeb03a37618329a3a00976028e702290596e2171d7
parent2952047fe741ca5b3717e066e0a6aeac699ee4c1 (diff)
more
-rw-r--r--5p15.cpp26
-rw-r--r--5p16.cpp57
-rw-r--r--5p17.cpp44
3 files changed, 127 insertions, 0 deletions
diff --git a/5p15.cpp b/5p15.cpp
new file mode 100644
index 0000000..4b9abd4
--- /dev/null
+++ b/5p15.cpp
@@ -0,0 +1,26 @@
+
+/*
+ *
+ * 5.15
+ *
+ *
+ */
+
+int main () {
+
+ /* Corrected versions
+
+ (a)
+ int ix = 0;
+ for ( ; ix != sz ; ++ix) {}
+ if (ix != sz)
+
+ (b)
+ int ix = 0;
+ for ( ; ix != sz ; ++ix) {}
+
+ (c)
+ for (int ix = 0 ; ix != sz ; ++ix) {}
+ */
+ return 0;
+}
diff --git a/5p16.cpp b/5p16.cpp
new file mode 100644
index 0000000..27c6bcb
--- /dev/null
+++ b/5p16.cpp
@@ -0,0 +1,57 @@
+#include <iostream>
+#include <vector>
+
+/*
+ *
+ * 5.16
+ *
+ *
+ */
+
+void idiomatic_while(std::vector<int> &idw) {
+
+ int i = 0;
+ while(std::cin >> i) {
+ idw.push_back(i);
+ }
+}
+
+void idiomatic_for(std::vector<int> &idf) {
+
+ for(auto a : idf) {
+ std::cout << a << std::endl;
+ }
+}
+
+void nonidiomatic_while(std::vector<int> &nidw) {
+
+ auto iter = nidw.cbegin();
+ while(iter != nidw.cend()) {
+ std::cout << *iter++ << std::endl;
+ }
+}
+
+void nonidiomatic_for(std::vector<int> &nidf) {
+
+ int i = 0;
+ for( ; std::cin >> i ; ) {
+ nidf.push_back(i);
+ }
+}
+
+int main (int argc, char *argv[]) {
+
+ if(argc > 2 || argc < 2) { return -1; }
+ std::string argv_s = argv[1];
+ std::vector<int> iv1;
+ std::vector<int> iv2;
+ if(argv_s == "1") {
+ idiomatic_while(iv1);
+ idiomatic_for(iv1);
+ } else if (argv_s == "2") {
+ nonidiomatic_for(iv2);
+ nonidiomatic_while(iv2);
+ }
+
+ return 0;
+}
diff --git a/5p17.cpp b/5p17.cpp
new file mode 100644
index 0000000..154996f
--- /dev/null
+++ b/5p17.cpp
@@ -0,0 +1,44 @@
+#include <iostream>
+#include <vector>
+
+/*
+ *
+ * 5.17
+ *
+ *
+ */
+
+int main () {
+
+ std::vector<int> v1 = {1,1,1,1,2};
+ std::vector <int> v2 = {1,1,1,1,2};
+ auto v1l = v1.size();
+ auto v2l = v2.size();
+ decltype(v1.cend()) smallestend;
+ decltype(v1.cbegin()) smallestbeg;
+ decltype(v1.cbegin()) biggestbeg;
+ if(v1l > v2l) {
+ smallestend = v2.cend();
+ smallestbeg = v2.cbegin();
+ biggestbeg = v1.cbegin();
+ } else if (v2l > v1l) {
+ smallestend = v1.cend();
+ smallestbeg = v1.cbegin();
+ biggestbeg = v2.cbegin();
+ } else if (v1l == v2l) {
+ smallestend = v1.cend();
+ smallestbeg = v1.cbegin();
+ biggestbeg = v2.cbegin();
+ }
+
+ bool determine_prefix = true;
+ for( ; smallestbeg != smallestend ; ++smallestbeg, ++biggestbeg) {
+ if(*smallestbeg != *biggestbeg) {
+ determine_prefix = false;
+ break;
+ }
+ }
+
+ std::cout << (determine_prefix ? "true" : "false") << std::endl;
+ return 0;
+}