From 4483699cc8cc937cb27040cfbd809b8a90029391 Mon Sep 17 00:00:00 2001 From: Oskar Date: Mon, 5 Aug 2024 20:57:13 +0200 Subject: more exercises --- 3p5.cpp | 5 ++--- 3p6-v2.cpp | 33 +++++++++++++++++++++++++++++++++ 3p6.cpp | 28 ++++++++++++++++++++++++++++ 3p7.cpp | 30 ++++++++++++++++++++++++++++++ 3p8-v1.cpp | 32 ++++++++++++++++++++++++++++++++ 3p8-v2.cpp | 33 +++++++++++++++++++++++++++++++++ 3p9.cpp | 25 +++++++++++++++++++++++++ 7 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 3p6-v2.cpp create mode 100644 3p6.cpp create mode 100644 3p7.cpp create mode 100644 3p8-v1.cpp create mode 100644 3p8-v2.cpp create mode 100644 3p9.cpp diff --git a/3p5.cpp b/3p5.cpp index 7f1be44..628557d 100644 --- a/3p5.cpp +++ b/3p5.cpp @@ -4,7 +4,7 @@ /* * - * Description + * 3.5 * * */ @@ -19,7 +19,6 @@ int main () { string full; string full_sep; - //string whitespace(" "); string DD; while(cin >> DD) { full += DD; @@ -27,6 +26,6 @@ int main () { } cout << full << endl; cout << full_sep << endl; - + return 0; } diff --git a/3p6-v2.cpp b/3p6-v2.cpp new file mode 100644 index 0000000..31a8cef --- /dev/null +++ b/3p6-v2.cpp @@ -0,0 +1,33 @@ +#include +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.6 v1 + * + * Stupid pointer version of this exercise because i wanted to. + * Though i will say, i definitely say we should be using the range for. So much simpler. + * This version is also way too hard to read especially compared to the other characters. + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string Xer; + cout << "Enter a string" << endl; + cin >> Xer; + decltype(&Xer[0]) Character; // make pointer + for(decltype(Xer.size()) Index = 0 ; Index != Xer.size() ; Index++ ) { // When index is same as Xer.size() we stop the loop + Character = &Xer[Index]; // Point towards address of character in index + *Character = 'X'; // replace it with X + } + + cout << Xer << endl; + return 0; +} diff --git a/3p6.cpp b/3p6.cpp new file mode 100644 index 0000000..d33055a --- /dev/null +++ b/3p6.cpp @@ -0,0 +1,28 @@ +#include +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.6 v1 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string Xer; + cout << "Enter a string" << endl; + cin >> Xer; + for(auto &c : Xer) { // Make reference for every character in Xer + c = 'X'; + } + cout << Xer << endl; + return 0; +} diff --git a/3p7.cpp b/3p7.cpp new file mode 100644 index 0000000..9acc8fb --- /dev/null +++ b/3p7.cpp @@ -0,0 +1,30 @@ +#include +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.7 + * + * Nothing different happens, pretty much same result. Though auto might just be better so we are sure what type it is. + * Unless the author meant that we were supposed to do char rather than (char&). If we were to just do 'char' type then + * we wouln't be able to modify the characters in the string, therefore the string remains unchanged. + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string Xer; + cout << "Enter a string" << endl; + cin >> Xer; + for(char &c : Xer) { // Make reference for every character in Xer + c = 'X'; + } + cout << Xer << endl; + return 0; +} diff --git a/3p8-v1.cpp b/3p8-v1.cpp new file mode 100644 index 0000000..a53a408 --- /dev/null +++ b/3p8-v1.cpp @@ -0,0 +1,32 @@ +#include +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.8 v1 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string Xer; + cout << "Enter a string" << endl; + cin >> Xer; + decltype(Xer.size()) Index = 0; + while(Index != Xer.size()) { + Xer[Index++] = 'X'; // We can use post increment operator + //Index++; // Or we can increment it here + //++Index; // We could also do this + } + + cout << Xer << endl; + return 0; +} diff --git a/3p8-v2.cpp b/3p8-v2.cpp new file mode 100644 index 0000000..158de77 --- /dev/null +++ b/3p8-v2.cpp @@ -0,0 +1,33 @@ +#include +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.8 v2 + * + * I think both while and for are viable but i will say the range for is very minimal and nice. + * My C programmer brain feels more at home using something like a regular for / while loop with subscripts + * But i do think the range for is nice. It might take a bit of time getting used to using ranged for loops + * and it might take some time being able to recognise which situations it's best used but i think its a + * great tool to have either way. + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string Xer; + cout << "Enter a string" << endl; + cin >> Xer; + for(decltype(Xer.size()) Index = 0 ; Index != Xer.size() ; ++Index /*or Index++*/) { // Make reference for every character in Xer + Xer[Index] = 'X'; + } + + cout << Xer << endl; + return 0; +} diff --git a/3p9.cpp b/3p9.cpp new file mode 100644 index 0000000..25077ce --- /dev/null +++ b/3p9.cpp @@ -0,0 +1,25 @@ +#include +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.9 + * + * Program does compile but not sure if valid. It has a size of 0 so its probably undefined behavior. + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +int main () { + + string s; + auto Size = s.size(); + cout << Size << endl; + cout << s[0] << endl; + return 0; +} -- cgit v1.2.3