From 12dda8d5244e86096a5ea56014073dd1892cc409 Mon Sep 17 00:00:00 2001 From: Oskar Date: Tue, 20 Aug 2024 13:51:53 +0200 Subject: more --- 4p20.cpp | 35 ++++++++++++++++++++ 4p21.cpp | 29 +++++++++++++++++ 4p22.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++ 4p23.cpp | 26 +++++++++++++++ 4p24.cpp | 13 ++++++++ Makefile | 4 +-- conditional-operator-tests.cpp | 22 +++++++++++++ member-access-test.cpp | 22 +++++++++++++ 8 files changed, 222 insertions(+), 2 deletions(-) create mode 100644 4p20.cpp create mode 100644 4p21.cpp create mode 100644 4p22.cpp create mode 100644 4p23.cpp create mode 100644 4p24.cpp create mode 100644 conditional-operator-tests.cpp create mode 100644 member-access-test.cpp 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 +#include +#include + +/* + * + * 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 vs; + vs.push_back(s1); + vs.push_back(s2); + vs.push_back(s3); + std::vector::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 +#include + +/* + * + * 4.21 + * + * + */ + +int main () { + + std::vector 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 + +/* + * + * 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 + +/* + * + * 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 + +/* + * + * + * + * + */ + +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 + +/* + * + * + * + * + */ + +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; +} -- cgit v1.2.3