Browse Source

Ex1: Copied Code from Intro CPP repo:

git@git.deboone.nl:ericteunis/documenten/uni/m.cpp.git
master
Eric Teunis de Boone 2 years ago
parent
commit
4c31b5a287
  1. 3
      .gitignore
  2. 7
      ex1/ex1.1/hello_world.cpp
  3. 49
      ex1/ex1.2/string_analyzer.cpp
  4. 53
      ex1/ex1.3/join_strings.cpp
  5. 26
      ex1/ex1.4/leak_memory.cpp
  6. 109
      ex1/ex1.5/base32.cpp

3
.gitignore

@ -0,0 +1,3 @@
*.out
*.o
*.a

7
ex1/ex1.1/hello_world.cpp

@ -0,0 +1,7 @@
#include <iostream>
using namespace std ;
int main() {
cout << "Hello World" << endl ;
return 0 ;
}

49
ex1/ex1.2/string_analyzer.cpp

@ -0,0 +1,49 @@
#include <iostream>
using namespace std ;
#define MAX_STR_LENGTH 50
int main() {
char str[MAX_STR_LENGTH];
// set pointer to beginning of str
char* ptr = &str[0];
// get string from user
std::cout << "Enter string (N < " << MAX_STR_LENGTH << ") :";
std::cin.getline (str, MAX_STR_LENGTH);
std::cout << std::endl;
// loop while the pointer does not point to a null byte
// move the pointer one position at the end of the loop
unsigned int chars_capital = 0, chars_lower = 0, chars_digits = 0, chars_other = 0;
while ( *ptr != 0 )
{
if ( *ptr >= int('A') && *ptr <= int('Z') )
{
++chars_capital;
}
else if ( *ptr >= int('a') && *ptr <= int('z') )
{
++chars_lower;
}
else if ( *ptr >= int('0') && *ptr <= int('9') )
{
++chars_digits;
}
else
{
++chars_other;
}
// move pointer one char further
++ptr;
}
std::cout << chars_capital << " Uppercase" << std::endl;
std::cout << chars_lower << " Lowercase" << std::endl;
std::cout << chars_digits << " Digits" << std::endl;
std::cout << chars_other << " Other" << std::endl;
return 0 ;
}

53
ex1/ex1.3/join_strings.cpp

@ -0,0 +1,53 @@
#include <iostream>
#include <string.h>
// join will return a pointer of type 'char'
char* join(const char*, const char*) ;
char* joinb(const char*, const char*) ;
int main()
{
std::cout << join("alpha","bet") << std::endl ;
std::cout << joinb("duck","soup") << std::endl ;
return 0 ;
}
/* this function is owner of new_str,
* since it doesn't delete it, there is leakage
*/
char* join(const char* str1 , const char* str2)
{
/* use 'new' because we don't know how big the strings are going to be
* the length should be 'strlen(str1) + strlen(str2) + 1'
* since strlen does not count the null bytes we need to add one
*/
int size = strlen(str1) + strlen(str2) + 1;
char* new_str = new char[size];
*new_str = 0; // Null String
strcat(new_str, str1); // new_str = 0 + str1
strcat(new_str, str2); // new_str = new_str + str2
return new_str;
}
char* joinb(const char* str1 , const char* str2)
{
int size = strlen(str1) + strlen(str2) + 1;
char* new_str = new char[size];
*new_str = 0;
strcat(new_str, str1);
strcat(new_str, " "); // insert space, note it is important to have it as a string => \"
strcat(new_str, str2);
return new_str;
}

26
ex1/ex1.4/leak_memory.cpp

@ -0,0 +1,26 @@
/*
* Deliberately leak memory
*
* Free memory decreases for a second.
* However, because the program quits, the kernel frees the used memory
*
* If run for some time, my swap space would fill, afterwards my computer is brought to a halt.
*
*/
#include <iostream>
#define K 1000
#define M K*K
#define G K*M
#define MEMORY_SIZE_IN_CHARS G
int main() {
char* throwaway_ptr = new char[MEMORY_SIZE_IN_CHARS];
std::cout << "We leaked " << MEMORY_SIZE_IN_CHARS * sizeof " " << " bits" << std::endl;
return 0;
}

109
ex1/ex1.5/base32.cpp

@ -0,0 +1,109 @@
/*
* Print a base10 number in base32
*
* Using a multiple of bits, arithmetic is done very fast
* since cpus talk bits, we can use bit operations which are faster
*+ than regular (base10) operations
*
* also, in base 2^k you can see the splitting on bits
* with k the amount of bits used to represent the character
* =>
* b2: "1100001010100101"
* b10: "49829"
* ==
* b2 per 4 bits: "1100 0010 1010 0101"
* b10: "12 2 10 5"
* b16: "C 2 A 5"
* ==
* b2 per 5 bits: "(0000)1 10000 10101 00101"
* b10: "1 16 21 5"
* b32: "1 G L 5"
*/
#include <iostream>
#include <cmath>
#define VERBOSE false
char b32_lookup_table[] = "ABCDEFGHIJKLMNOPQRSTUV";
int base = 8;
int main() {
//a
unsigned int number = 16384;
// get the input unsigned integer
std::cout << "Enter a Positive Integer: ";
std::cin >> number;
if ( VERBOSE ) {
std::cout << "Number " << number << std::endl;
std::cout << "Base " << base << std::endl;
}
//b
int bits_in_char = log(base)/log(2);// 5 = log2(32)
int bits_in_int = 8 * sizeof(int);// 32 bits
int chars_per_int = std::ceil(bits_in_int / bits_in_char);// 7 chars needed
if ( VERBOSE ) {
std::cout << "Chars per int = " << chars_per_int << " ( " << bits_in_int << " / " << bits_in_char << " ) " << std::endl;
}
//c
unsigned short digits[chars_per_int];
//d & e
short digit_num = 0;
short lsb_bitmask = ( 1 << bits_in_char ) - 1;// 00011111 = 0x1F
if ( VERBOSE ) {
std::cout << "Bitmask = 0x" << std::uppercase << std::hex << lsb_bitmask << std::dec << std::endl;
std::cout << std::endl;
}
// Loop until the remainder is 0
// I'm truncating all 0s in front of MSB
while ( number > 0 )
{
if ( VERBOSE ) {
std::cout << "Entering loop with number = " << number << std::endl;
std::cout << " - b16: 0x" << std::hex << number << std::dec << std::endl;
}
// Mask with the last 5 bits
digits[digit_num] = number & lsb_bitmask;
// Shift off the bits we have put in to the array
number = ( number >> bits_in_char );
++digit_num;
}
--digit_num;
if ( VERBOSE ) {
std::cout << "We have " << digit_num + 1 << " digits" << std::endl;
std::cout << std::endl;
}
//f
// Loop the other way so the first digit we get out is MSB
while ( digit_num >= 0 )
{
if ( digits[digit_num] < 10 )
{
std::cout << digits[digit_num];
}
else
{
std::cout << b32_lookup_table[digits[digit_num] - 10];
}
digit_num--;
}
std::cout << std::endl;
return 0;
}