Exercise 5 draft

Added Exercise 5 but incomplete
This commit is contained in:
2025-03-21 23:35:38 +01:00
parent d1c3fcd614
commit 4655cd3477
4 changed files with 226 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
#include "calendar.h"
#include <string>
// PRE: a year greater or equal than 1900
// POST: returns whether that year was a leap year
bool is_leap_year(int year) {
if (year >= 1900) {
if (year % 4 == 0 && year % 100 != 0)
return true;
else if (year % 4 == 0 && year % 400 == 0)
return true;
}
return false;
}
// PRE: a year greater or equal than 1900
// POST: returns the number of days in that year
int count_days_in_year(int year) {
if (year >= 1900) {
if (is_leap_year(year) == true)
return 366;
return 365;
}
return 0;
}
// PRE: a month between 1 and 12 and a year greater or equal than 1900
// POST: returns the number of days in the month of that year
int count_days_in_month(int month, int year) {
if (year >= 1900 && month > 0 && month <= 12) {
if (is_leap_year(year) == false) {
if (month % 2 != 0 && month != 2 && month <= 7)
return 31;
else if (month % 2 == 0 && month != 2 && month > 7)
return 31;
else if (month == 2)
return 28;
else
return 30;
} else {
if (month % 2 == 0 && month != 2 && month <= 7)
return 31;
else if (month % 2 == 0 && month != 2 && month > 7)
return 31;
else if (month == 2)
return 29;
else
return 30;
}
}
return 0;
}
// PRE: n/a
// POST: returns whether the given values represent a valid date
bool is_valid_date(int day, int month, int year) {
if (day > 0 && day <= count_days_in_month(month, year) && month > 0 &&
month <= 12 && year >= 1900)
return true;
return false;
}
// PRE: the given values represent a valid date
// POST: returns the number of days between January 1, 1900 and this date
// (excluding this date)
int count_days(int day, int month, int year) {
int res_day = 0;
if (is_valid_date(day, month, year) == true) {
for (int j = 1901; j <= year; ++j) {
res_day += count_days_in_year(j);
}
for (int j = 1; j < month; ++j) {
res_day += count_days_in_month(j, year);
}
res_day += (day - 1);
}
return res_day;
}
// PRE: the given values represent a (potentially invalid) date
// POST: prints the weekday if the date is valid or "invalid date" otherwise.
// Everything must be printed in lowercase.
void print_weekday(int day, int month, int year) {
if (is_valid_date(day, month, year) == true) {
std::string days[7] = {"sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday"};
std::cout << days[((count_days(day, month, year) + 1) % 7)];
} else
std::cout << "invalid date";
}

View File

@@ -0,0 +1,38 @@
// PRE: x > 0
// POST: returns n^x
int f1(int n, int x) {
int res = 1;
for (; x > 0; x--) {
res *= n;
}
return res;
}
// PRE: n > 0
// POST: returns number of devisions by 10 with n
int f2(int n) {
int i = 0;
while (n > 0) {
n = n / 10;
++i;
}
return i;
}
// PRE: n > 1
// POST: returns a bool to determine if n is prime
bool f3(int n) {
int i = 2;
for (; n % i != 0; ++i);
return n == i;
}
// PRE: n is square number
// POST: returns root of n
int f4(int n) {
int i = 0;
while (i * i != n) {
++i;
}
return i;
}

View File

@@ -0,0 +1,35 @@
Please write your solution here.
For your convenience we have added a template for your answers below.
Note: Remember to also describe the problem of the function.
# Function `c++|is_even`:
The problem with this function is that it does not return a bool if the number `i` is uneven.
```c++
// PRE: n/a
// POST: returns true if i is even. If not it returns false.
bool is_even(int i) {
if (i % 2 == 0) return true;
else return false;
}
```
# Function `c++|invert`:
The problem with this function is
```c++
// PRE: x is positive or negative but not zero.
// POST: returns inverse with respect to multiplication of x.
double invert(int x) {
// TODO: Fix code below.
if (x != 0) {
return 1.0 / x;
}
else
std::cout << "0 has no inverse!";
}
```

View File

@@ -0,0 +1,63 @@
#include "run_length.h"
void encode() {
int i;
int c;
int count;
std::cin >> i;
std::cout << 1 << " ";
while (i != -1) {
count = 0;
c = i;
while (i == c && i >= 0 && i <= 255) {
++count;
std::cin >> i;
if (count == 255) {
break;
}
}
if (i < -1 || i > 255) {
if (count == 0) {
std::cout << "error";
return;
} else {
std::cout << count << " ";
std::cout << c << " ";
std::cout << "error";
return;
}
}
std::cout << count << " ";
std::cout << c << " ";
}
std::cout << -1;
}
void decode() {
int i;
int count;
std::cin >> count;
std::cin >> i;
std::cout << 0 << " ";
while (count != -1) {
if (i < -1 || i > 255) {
std::cout << "error";
return;
}
if (count < -1 || count > 255) {
std::cout << "error";
return;
}
for (int j = 0; j < count; ++j) {
std::cout << i << " ";
}
std::cin >> count;
if (count != -1)
std::cin >> i;
if (i == -1) {
std::cout << "error";
return;
}
}
std::cout << -1;
}