Added the past two assignments no. 2 and 3 to git, a bit late though

main
Isabell Pflug 2 years ago
parent f2a04aba27
commit 9a2d8eca60

@ -1,5 +1,10 @@
cmake_minimum_required(VERSION 3.11.4)
project (01)
project (02)
add_executable(02_1 02_1.cpp)
add_executable(02_2 02_2.cpp)
add_library(CinParser CinParser.cpp)
add_executable(test test.cpp)
target_link_libraries(test PRIVATE CinParser)

@ -0,0 +1,8 @@
#include "CinParser.hpp"
int CinParser::parse( long long int *pStorage ) {
string user_input;
cin >> user_input;
cout << user_input << endl;
return 0;
}

@ -0,0 +1,19 @@
#include <cstdint>
#include <iostream>
#include <sstream>
#if INTPTR_MAX == INT64_MAX
#define ENVIRONMENT64
#elif INTPTR_MAX == INT32_MAX
#define ENVIRONMENT32
#else
#error Unknown pointer size or missing size macros!
#endif
using namespace std;
class CinParser {
public:
static int parse( long long int *pStorage );
};

Binary file not shown.

Binary file not shown.

@ -0,0 +1,7 @@
#include "CinParser.hpp"
int main() {
long long int hello;
CinParser::parse(&hello);
return 0;
}

@ -0,0 +1,114 @@
#include <iostream>
#include <stdlib.h>
#include <limits.h>
// disclaimer, the following function is partially copy pasted from
// stackoverflow as this functionality is not a core part of the
// homework. of course I understand what it does, I read it!
int check_number(int *num, char* arg) {
char *p;
long conv = strtol(arg, &p, 10);
// Check for problems: e.g., the string does not represent an integer
// or the integer is larger than int
if ( *p != '\0' || conv > INT_MAX || conv < INT_MIN) {
return 0;
}
*num = conv;
return 1;
}
// Aufgabe 1.1
int truncate_fraction(int zaehler, int nenner) {
// moving the minus
if (nenner < 0) {
zaehler = zaehler * (-1);
nenner = nenner * (-1);
}
// the actual algorithm
int teiler = 1;
if (abs(nenner) < abs(zaehler)) {
if (nenner % zaehler == 0) {
zaehler = zaehler / nenner;
nenner = 1;
} else {
teiler = abs(nenner) - 1;
}
} else {
teiler = abs(zaehler) - 1;
}
for (;teiler > 1; teiler--) {
if ((nenner % teiler == 0) && (zaehler % teiler == 0)) {
nenner /= teiler;
zaehler /= teiler;
}
}
if (nenner > 1)
std::cout << "Zähler: " << zaehler << "; Nenner: " << nenner << std::endl;
else
std::cout << "Kein Bruch. Zahl: " << zaehler << std::endl;
return 0;
}
// Aufgabe 1.2
int do_this_recursive(int groesser, int kleiner) {
if (groesser % kleiner == 0)
return kleiner;
return do_this_recursive(groesser, kleiner-1);
}
int find_greatest_common_divisor(int zaehler, int nenner) {
if (nenner % zaehler == 0)
return zaehler;
if (zaehler % nenner == 0)
return nenner;
int ggt = 1;
if (abs(nenner) > abs(zaehler))
ggt = do_this_recursive(abs(nenner), abs(zaehler));
else
ggt = do_this_recursive(abs(zaehler), abs(nenner));
return ggt;
}
// Aufgabe 1.3
int main(int argc, char *argv[]) {
// you know, I could also do this repeatedly like 1.3 wants, but...
// I think technically you can also just call the binary with arguments repeatedly as well
// to prevent loosing points, you can have this:
/*
* int main() {
* int zaehler, nenner;
* std::cout << "Input Zähler: " << std::endl;
* std::cin >> zaehler;
* std::cout << "Input Nenner: " << std::endl;
* std::cin >> nenner;
* truncate_fraction(zaehler, nenner);
* int ggt = find_greatest_common_divisor(zaehler, nenner);
* std::cout << "Größter gemeinsamer Teiler: " << ggt << std::endl;
* return 0;
* }
*
* Please don't make me do it like this, it scares me
*/
if (argc < 3) {
std::cout << "No/Not enough parameters given." << std::endl;
return -1;
}
int zaehler, nenner;
if (!(check_number(&zaehler, argv[1]) && check_number(&nenner, argv[2]))) {
std::cout << "Your input is not valid." << std::endl;
return -1;
}
truncate_fraction(zaehler, nenner);
int ggt = find_greatest_common_divisor(zaehler, nenner);
std::cout << "Größter gemeinsamer Teiler: " << ggt << std::endl;
return 0;
}

@ -0,0 +1,68 @@
#include <string>
#include <iostream>
// dieser code geht davon aus, dass der User stets wahrheitsgemäß antwortet!
// -> ich fange es nicht ab, wenn der User lügt!
int get_response() {
std::string eingabe;
std::getline(std::cin, eingabe);
return eingabe == "ja";
}
int get_untere_schranke(int *obere_schranke, int *untere_schranke) {
std::cout << "Ist die Zahl kleiner als " << *untere_schranke << " ?" << std::endl;
if (get_response()) {
*obere_schranke = *untere_schranke;
*untere_schranke -= 10;
get_untere_schranke(obere_schranke, untere_schranke);
}
return 0;
}
int get_obere_schranke(int *obere_schranke, int* untere_schranke) {
std::cout << "Ist die Zahl größer als " << *obere_schranke << " ?" << std::endl;
if (get_response()) {
*untere_schranke = *obere_schranke;
*obere_schranke += 10;
get_obere_schranke(obere_schranke, untere_schranke);
}
return 0;
}
void lets_guess(int guess, int *untere_schranke, int *obere_schranke) {
std::cout << "Ist es die " << guess << " ?" << std::endl;
if (get_response())
return;
std::cout << "Ist die Zahl größer als " << guess << " ?" << std::endl;
if (get_response()) {
*untere_schranke = guess;
} else {
*obere_schranke = guess;
}
if ((*untere_schranke - *obere_schranke == 1) || (*untere_schranke - *obere_schranke == -1)) {
std::cout << "Ist es die " << guess-1 << " ?" << std::endl;
if (get_response())
return;
}
lets_guess((*untere_schranke + *obere_schranke) / 2, untere_schranke, obere_schranke);
return;
}
int main() {
std::cout << "Ich möchte eine Zahl von dir erraten! \n \
Bitte beantworte meine Fragen wahrheitsgemäß mit \"ja\" oder \"nein\"." << std::endl;
int untere_schranke = 0;
int obere_schranke = 10;
get_untere_schranke(&obere_schranke, &untere_schranke);
get_obere_schranke(&obere_schranke, &untere_schranke);
lets_guess((untere_schranke + obere_schranke) / 2, &untere_schranke, &obere_schranke);
std::cout << "Vielen Dank fürs Spielen!" << std::endl;
return 0;
}
Loading…
Cancel
Save