Skip to content

Instantly share code, notes, and snippets.

@joennlae
Created November 5, 2016 13:25
Show Gist options
  • Save joennlae/aeb3c08b9dd6df93850b675c2313baeb to your computer and use it in GitHub Desktop.
Save joennlae/aeb3c08b9dd6df93850b675c2313baeb to your computer and use it in GitHub Desktop.
problem6
// UNCOMMENT THE NEXT LINE TO ENABLE THE TESTS AND BEFORE SUBMITTING
#include "tests.h"
#include <iostream>
// PRE: a year greater than 1900
// POST: returns whether that year was a leap year
bool is_leap_year(unsigned int year);
// PRE: a year greater than 1900
// POST: returns the number of days in that year
unsigned int count_days_in_year(unsigned int year);
// PRE: a month between 1 and 12 and a year greater than 1900
// POST: returns the number of days in the month of that year
unsigned int count_days_in_month(unsigned int month, unsigned int year);
// PRE: n/a
// POST: returns whether the given values represent a valid date
bool is_valid_date(unsigned int day, unsigned int month, unsigned int year);
// PRE: the given values represent a valid date
// POST: returns the number of days between January 1, 1900 and this date
unsigned int count_days(unsigned int day, unsigned int month, unsigned int year);
int main(){
unsigned int d,m,y,days;
std::cin >> d >> m >> y;
if(is_valid_date(d,m,y)){
days = count_days(d,m,y);
switch(days % 7){
case 0 :
std::cout << "sunday";
break;
case 1 :
std::cout << "monday";
break;
case 2 :
std::cout << "tuesday";
break;
case 3 :
std::cout << "wednesday";
break;
case 4 :
std::cout << "thursday";
break;
case 5 :
std::cout << "friday";
break;
case 6 :
std::cout << "saturday";
break;
}
//std::cout << "\n" << days;
}
else std::cout << "invalid date";
}
bool is_valid_date(unsigned int day, unsigned int month, unsigned int year){
if(day > 31 || month > 12 || year < 1900){ // forchecking
return false;
}
else if(month == 4 || month == 6 || month == 9 || month == 11){ // all with 30 days
if(day >30){
return false;
}
}
else if(month == 2){
if (is_leap_year(year)){
if(day > 29){
return false;
}
}
else{
if(day > 28){
return false;
}
}
}
else return true;
}
bool is_leap_year(unsigned int year){
if( year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
return true;
}
else return false;
}
unsigned int count_days_in_month(unsigned int month, unsigned int year){
//if would be faster but a switch for the excercise
switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
break;
case 4: case 6: case 9: case 11:
return 30;
break;
case 2:
if(is_leap_year(year)){
return 29;
}
else return 28;
break;
}
}
unsigned int count_days_in_year(unsigned int year){
unsigned int days = 0;
for(int i = 1; i < 13; i++){
days += count_days_in_month(i,year);
}
return days;
}
unsigned int count_days(unsigned int day, unsigned int month, unsigned int year){
unsigned int days = 0;
days += day; // days of the current input month
for(int i = 1; i < month; i++){ //days of the month in teh current input year
days += count_days_in_month(i, year);
}
for(int i = 1900; i < year; i++){
days += count_days_in_year(i);
}
return days;
}
// UNCOMMENT THE NEXT LINE TO ENABLE THE TESTS AND BEFORE SUBMITTING
#include "tests.h"
#include <iostream>
//PRE correct sequence starting with 0 ending with -1 and no value > 255
//POST encodes the sequence as described in the problem set
void encode();
//PRE n/a
//POST determines if the value is -1<x<256 i.e correct
bool is_in_correct(int input);
//PRE correct sequence starting with 1 ending with -1 and no value > 255
//POST decodes the sequence as described in the problem set
void decode();
int main(){
int first;
std::cin >> first;
if(first == 0){ // encode
encode();
}
else if (first == 1){//decode
decode();
}
else std::cout << "error";
}
void encode(){
int input = 0, last = -1, count = 0;
while(true){
std::cin >> input;
if(input == -1){
if(count > 0){
std::cout << count << "\n" << last << "\n";
}
return; //finish
}
else{
if(is_in_correct(input)){
if(last==0){ //first option
count++;
last = input;
}
else if(last == input){
count ++;
}
else{
std::cout << count << "\n" << last << "\n";
count = 1;
last = input;
}
if(count == 255){
std::cout << count << "\n" << last << "\n";
count = 0;
}
}
else{
std::cout << "error";
return;
}
}
}
}
bool is_in_correct(int input){
return input > -1 && input < 256;
}
void decode(){
int var = 0, count = 0, rep = 0, input = 0;
while(true){
count++;
std::cin >> input;
if(input == -1){
if(var == -1){
std::cout << "error" << "\n";
}
return; //finish
}
else{
if(is_in_correct(input)){
if(count % 2 == 1){ //ungerade
rep = input;
var = -1;
}
else{
var = input;
for(int i = 0; i < rep; i++){
std::cout << var << "\n";
}
}
}
else{
std::cout << "error";
return;
}
}
}
}
// UNCOMMENT THE NEXT LINE TO ENABLE THE TESTS AND BEFORE SUBMITTING
#include "tests.h"
#include <iostream>
//PRE correct sequence starting with 0 ending with -1 and no value > 255
//POST encodes the sequence as described in the problem set
void encode();
//PRE n/a
//POST determines if the value is -1<x<256 i.e correct
bool is_in_correct(int input);
//PRE correct sequence starting with 1 ending with -1 and no value > 255
//POST decodes the sequence as described in the problem set
void decode();
int main(){
int first;
std::cin >> first;
if(first == 0){ // encode
encode();
}
else if (first == 1){//decode
decode();
}
else std::cout << "error";
}
void encode(){
int input = 0, last = -1, count = 0;
while(true){
std::cin >> input;
if(input == -1){
if(count > 0){
if(last < 128 && count == 1){
std::cout << last << "\n";
}
else{
std::cout << count + 128 << "\n" << last << "\n";
}
}
break; //finish
}
else{
if(is_in_correct(input)){
if(last== -1){ //first option
count++;
last = input;
}
else if(last == input){
count ++;
}
else{
if(last < 128 && count == 1){
std::cout << last << "\n";
}
else{
std::cout << count + 128 << "\n" << last << "\n";
}
count = 1;
last = input;
}
if(count == 127){
std::cout << count + 128 << "\n" << last << "\n";
count = 0;
}
}
else{
std::cout << "error";
break;
}
}
}
}
bool is_in_correct(int input){
return input > -1 && input < 256;
}
void decode(){
int var = 0, count = 0, rep = 0, input = 0;
while(true){
count++;
std::cin >> input;
if(input == -1){
if(var == -1){
std::cout << "error" << "\n";
}
break; //finish
}
else{
if(is_in_correct(input)){
if(count % 2 == 1 && input < 128){
std::cout << input << "\n";
count--;
}
else if(count % 2 == 1){ //ungerade
rep = input - 128;
var = -1;
}
else{
var = input;
for(int i = 0; i < rep; i++){
std::cout << var << "\n";
}
}
}
else{
std::cout << "error";
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment