summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-08-20 13:51:53 +0200
committerOskar <[email protected]>2024-08-20 13:51:53 +0200
commit12dda8d5244e86096a5ea56014073dd1892cc409 (patch)
treeffaf835edd33da23a58405db5e733ebfc71e5a5e
parentd0c6ac2b68380020d76d7f938307e056a25eb5f5 (diff)
more
-rw-r--r--4p20.cpp35
-rw-r--r--4p21.cpp29
-rw-r--r--4p22.cpp73
-rw-r--r--4p23.cpp26
-rw-r--r--4p24.cpp13
-rw-r--r--Makefile4
-rw-r--r--conditional-operator-tests.cpp22
-rw-r--r--member-access-test.cpp22
8 files changed, 222 insertions, 2 deletions
diff --git a/4p20.cpp b/4p20.cpp
new file mode 100644
index 0000000..edc38ce
--- /dev/null
+++ b/4p20.cpp
@@ -0,0 +1,35 @@
+#include <iostream>
+#include <iterator>
+#include <vector>
+
+/*
+ *
+ * 4.20
+ *
+ *
+ */
+
+int main () {
+
+ std::string s1 = "Wow!";
+ std::string s2 = "C++ is very cool, i like it!";
+ std::string s3 = "I also really like C!";
+ std::vector<std::string> vs;
+ vs.push_back(s1);
+ vs.push_back(s2);
+ vs.push_back(s3);
+ std::vector<std::string>::iterator iter = vs.begin();
+ std::cout << *iter++ << std::endl;
+ std::cout << iter->empty() << std::endl;
+ std::cout << iter++->empty() << std::endl;
+ /*
+ (a) *iter++; // valid
+ (b) (*iter)++; // not valid? we dereference and try to increment a string which does not work
+ (c) *iter.empty() // not valid, we try to call .empty() member function on an iterator, which does not exist
+ (d) iter->empty(); // valid
+ (e) ++*iter; // not valid, we are dereferencing and then trying to increment a string. I am a bit confused because i thought precedence of the ++ operator would make it increment first and then dereference it but it does not seem so...
+ (f) iter++->empty(); // valid
+ */
+
+ return 0;
+}
diff --git a/4p21.cpp b/4p21.cpp
new file mode 100644
index 0000000..0be1faa
--- /dev/null
+++ b/4p21.cpp
@@ -0,0 +1,29 @@
+#include <iostream>
+#include <vector>
+
+/*
+ *
+ * 4.21
+ *
+ *
+ */
+
+int main () {
+
+ std::vector<int> odddouble = {1,34,53,11,2,35,442,90,100,23,20,33,10,40,20,50};
+ for(auto a : odddouble) {
+ std::cout << a << " ";
+ }
+
+ std::cout << std::endl;
+ for(auto &a : odddouble) {
+ a = a % 2 ? a * 2 : a ;
+ }
+
+ for(auto a : odddouble) {
+ std::cout << a << " ";
+ }
+
+ std::cout << std::endl;
+ return 0;
+}
diff --git a/4p22.cpp b/4p22.cpp
new file mode 100644
index 0000000..29475da
--- /dev/null
+++ b/4p22.cpp
@@ -0,0 +1,73 @@
+#include <iostream>
+
+/*
+ *
+ * 4.22
+ *
+ *
+ */
+
+void cond_op_grade() {
+
+ std::string finalgrade;
+ unsigned int grade = 0;
+ if(std::cin >> grade) {
+ if(grade > 100) { return; }
+
+ } else {
+ return;
+ }
+
+ finalgrade = (grade >= 90) ? "high pass" : (grade >= 75) ? "pass" : (grade >= 60) ? "low pass" : "fail";
+ std::cout << finalgrade << std::endl;
+}
+
+void if_grade() {
+
+ std::string finalgrade;
+ unsigned int grade = 0;
+ if(std::cin >> grade) {
+ if(grade > 100) { return; }
+
+ } else {
+ return;
+ }
+
+ if(grade >= 90) {
+ finalgrade = "high pass";
+ } else if(grade >= 75) {
+ finalgrade = "pass";
+ } else if(grade >= 60) {
+ finalgrade = "low pass";
+ } else {
+ finalgrade = "fail";
+ }
+
+ std::cout << finalgrade << std::endl;
+}
+
+int main () {
+
+ /*
+ Not really sure which of these is the 'better' alternative
+ i do like how conditional operator is very terse and can
+ do the same thing in a very small line. But it could also
+ be a bit more difficult to read and understand. It could
+ simply be a skill issue whether it's hard to read or not
+ but i am not really sure.
+
+ I do think the if statement is a bit easier to read because
+ it is simply just so common and widely used that you really
+ just default to it. I know that all operators and if
+ statements have their best moments to be used but i can't
+ really judge which one would be best for this case.
+
+ I guess the best use for the the conditional operator
+ is best when we want to do a simple if statement where there
+ are just two simple outcomes
+ */
+
+ cond_op_grade();
+ if_grade();
+ return 0;
+}
diff --git a/4p23.cpp b/4p23.cpp
new file mode 100644
index 0000000..b53cc84
--- /dev/null
+++ b/4p23.cpp
@@ -0,0 +1,26 @@
+#include <iostream>
+
+/*
+ *
+ * 4.23
+ *
+ *
+ */
+
+int main () {
+
+ /*
+
+ Took me a while to figure out
+ was not so sure what the author
+ wanted to achieve. I mean there
+ are several ways we can paranthesize
+ this so it was a little hard to do...
+
+ */
+
+ std::string s = "word";
+ std::string pl = s + (s[s.size() - 1] == 's' ? "" : "s") ;
+ std::cout << pl << std::endl;
+ return 0;
+}
diff --git a/4p24.cpp b/4p24.cpp
new file mode 100644
index 0000000..d26973d
--- /dev/null
+++ b/4p24.cpp
@@ -0,0 +1,13 @@
+
+/*
+ *
+ * 4.24
+ *
+ *
+ */
+
+int main () {
+
+ // I got not idea
+ return 0;
+}
diff --git a/Makefile b/Makefile
index 66bc0b7..4648bd7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
CC1=clang++
CC2=g++
-CFLAGS_CLANG=-O0 -Wfatal-errors -Wall -Werror -Wextra -g3 -fsanitize=address,leak,undefined,float-divide-by-zero -Wpedantic -Wformat=2 -Wshadow -fno-common -std=c++2b
-CFLAGS_GCC=-O0 -Wfatal-errors -Wall -Werror -Wextra -g3 -fsanitize=address,leak,undefined,float-divide-by-zero -Wpedantic -Wformat=2 -Wshadow -fno-common -std=c++2b -Wformat-truncation=2 -Wformat-overflow
+CFLAGS_CLANG=-O0 -Wfatal-errors -Wall -Werror -Wextra -g3 -fsanitize=address,leak,undefined,float-divide-by-zero -Wpedantic -Wformat=2 -Wshadow -fno-common -std=c++20
+CFLAGS_GCC=-O0 -Wfatal-errors -Wall -Werror -Wextra -g3 -fsanitize=address,leak,undefined,float-divide-by-zero -Wpedantic -Wformat=2 -Wshadow -fno-common -std=c++20 -Wformat-truncation=2 -Wformat-overflow
BINDIR=bin
SRCS=$(wildcard *.cpp)
OBJS_CLANG=$(patsubst %.cpp, $(BINDIR)/clang/%, $(SRCS))
diff --git a/conditional-operator-tests.cpp b/conditional-operator-tests.cpp
new file mode 100644
index 0000000..570b4ec
--- /dev/null
+++ b/conditional-operator-tests.cpp
@@ -0,0 +1,22 @@
+#include <iostream>
+
+/*
+ *
+ *
+ *
+ *
+ */
+
+int main () {
+
+ unsigned int grade = 0;
+ if(std::cin >> grade) {
+ if(grade > 100) { return -1; }
+
+ } else {
+ return -1;
+ }
+
+ std::cout << (grade < 60 ? "fail" : "pass") << std::endl;
+ return 0;
+}
diff --git a/member-access-test.cpp b/member-access-test.cpp
new file mode 100644
index 0000000..aeede89
--- /dev/null
+++ b/member-access-test.cpp
@@ -0,0 +1,22 @@
+#include <iostream>
+
+/*
+ *
+ *
+ *
+ *
+ */
+
+int main () {
+
+ std::string s1 = "a string";
+ std::string *p = &s1;
+ auto n1 = s1.size(); // Call size member function (method) on s1
+ auto n2 = p->size(); // same thing but p is dereferenced and then call size member function (method) on s1
+ auto n3 = (*p).size(); // same thing as above but without using '->' operator
+
+ std::cout << n1 << " "
+ << n2 << " "
+ << n3 << " " << std::endl;
+ return 0;
+}