diff options
author | Oskar <[email protected]> | 2024-08-05 20:57:13 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-08-05 20:57:13 +0200 |
commit | 4483699cc8cc937cb27040cfbd809b8a90029391 (patch) | |
tree | 6edbe3e65d3c922c535078161b35d0892e6d34ec | |
parent | 1d105b91b9678ffaa8eddea2218ed1789f5be27c (diff) |
more exercises
-rw-r--r-- | 3p5.cpp | 5 | ||||
-rw-r--r-- | 3p6-v2.cpp | 33 | ||||
-rw-r--r-- | 3p6.cpp | 28 | ||||
-rw-r--r-- | 3p7.cpp | 30 | ||||
-rw-r--r-- | 3p8-v1.cpp | 32 | ||||
-rw-r--r-- | 3p8-v2.cpp | 33 | ||||
-rw-r--r-- | 3p9.cpp | 25 |
7 files changed, 183 insertions, 3 deletions
@@ -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 <iostream> +#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; +} @@ -0,0 +1,28 @@ +#include <iostream> +#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; +} @@ -0,0 +1,30 @@ +#include <iostream> +#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 <iostream> +#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 <iostream> +#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; +} @@ -0,0 +1,25 @@ +#include <iostream> +#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; +} |