Advertisement
Shinmera

Stupid Fucking Exercises.

Oct 22nd, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. #include<iostream> // For outputting shit, obvsly.
  2. #include<stdlib.h> // So I can end with a fuck you.
  3. #include<stdio.h>  // Actually, screw cout.
  4. #include<math.h>   // For the better is_even function.
  5. #include<assert.h> // Asserting stuff.
  6. using namespace std; //Fuck typing std:: everywhere, seriously.
  7.  
  8. void ex41a(){
  9.   unsigned int n = 2; // I don't want to be prompted every time I run this shit.
  10.   //cin >> n;
  11.   unsigned int x = 1;
  12.   if (n > 0) {
  13.     unsigned int k = 0;
  14.     bool e = true;
  15.     do {
  16.       if (++k == n) { e = false; }
  17.       x *= 2;
  18.     }
  19.     while(e);
  20.   }
  21.   printf("[ex41a] f(%d): %d\n", n, x);
  22. }
  23.  
  24. void ex41d(){ //Time to make a non-fucked version, yay.
  25.   unsigned int n = 2;
  26.   unsigned int x = 1;
  27.   for(unsigned int i=0;i<n;++i){ x*=2;} //Single line because it's basically one statement. Fuck you.
  28.   printf("[ex41d] f(%d): %d\n", n, x);
  29. }
  30.  
  31. void ex44a(){
  32.   double n = 0; //Changed from float to double. DUH.
  33.   while (n < 100000000) {
  34.     ++n;
  35.   }
  36.   printf("[ex44a] f(): %d\n", n);
  37. }
  38.  
  39. void ex44b(){
  40.   float x = 5000;
  41.   float y = 4999;
  42.   float res = (double)(x) * x - (double)(y) * y; //Casting to doubles to momentarily have double precision calculations.
  43.  
  44.   printf("[ex44b] f(%f,%f): %f\n", x,y, res);
  45. }
  46.  
  47. bool is_even (const double i){ //Biggest possible data type is probably best here.
  48.   //if (i % 2 == 0) return true;
  49.   return (fmod(i,2) == 0); //Fuck you.
  50. }
  51.  
  52. void ex51a(){
  53.   int n = -21;
  54.   printf("[ex51a] f(%d): %d\n", n, is_even(n));
  55. }
  56.  
  57. double invert (double x){
  58.   return (x == 0)? 0 : 1/x;
  59. }
  60.  
  61. void ex51b(){
  62.   double n = 20;
  63.   printf("[ex51b] f(%f): %f\n", n, invert(n));
  64. }
  65.  
  66. int min2(int a, int b){
  67.   return (a<=b)? a : b; // This is fucking kindergarden shit.
  68. }
  69.  
  70. void ex52a(){
  71.   int n = 20, m = 0;
  72.   printf("[ex52a] f(%d,%d): %d\n", n,m, min2(n,m));
  73.   n = -10; m = 20;
  74.   printf("[ex52a] f(%d,%d): %d\n", n,m, min2(n,m));
  75. }
  76.  
  77. int min4(int a, int b, int c, int d){
  78.   return min2(min2(a,b),min2(c,d));
  79. }
  80.  
  81. void ex52b(){
  82.   int a=4, b=2, c=1, d=3;
  83.   printf("[ex52b] f(%d,%d,%d,%d): %d\n", a,b,c,d, min4(a,b,c,d));
  84. }
  85.  
  86. //Not entirely accurate, but Good Enough(TM)
  87. bool is_leap_year(int year){
  88.   return ((fmod(year,4) == 0 && fmod(year,100) != 0) || fmod(year,400) == 0);
  89. }
  90.  
  91. //Holy shiet, this really needed a function.
  92. bool is_valid_month(int month){
  93.   return (month<=12 && month>=1);
  94. }
  95.  
  96. //Used twice, so validation got split off.
  97. //Ugly switch, but whatever.
  98. int days_in_month(int year, int month){
  99.   switch(month){
  100.     case 1: // January
  101.     case 3:  // March
  102.     case 5:  // May
  103.     case 7: // July
  104.     case 8: // August
  105.     case 10:  // October
  106.     case 12:  // december
  107.       return 31;
  108.       break;
  109.     case 2: // February
  110.       return(is_leap_year(year))? 29 : 28;
  111.       break;
  112.     default: //Rest
  113.       return 30;
  114.       break;
  115.   }
  116. }
  117.  
  118. bool is_valid_day(int year, int month, int day){
  119.   return (day>=1 && day<=days_in_month(year, month));
  120. }
  121.  
  122. int day_of_week(int year, int month, int day){
  123.   assert (is_valid_month(month));
  124.   assert (is_valid_day(year, month, day));
  125.  
  126.   //We use here that January 1st 2006 was a Sunday. Hence the +1 at the end there, to wrap around to monday.
  127.   int refyear = 2006;
  128.  
  129.   //Calculate all the days in the months gone by.
  130.   int days_of_months = 0;
  131.   for(int i=1;i<month;i++){
  132.     days_of_months+=days_in_month(year, i);
  133.   }
  134.   //Normally I'd push this all into the return but fuck it.
  135.   int days_total = ((is_leap_year(year))? 366 : 366)*(year-refyear)+days_of_months+day;
  136.  
  137.   //Mod7 gives us the day of week. YEA.
  138.   return fmod(days_total+1, 7);
  139. }
  140.  
  141. int c_day_of_week(int y, int m, int d){
  142.   return (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7; // C fun times.
  143.   // Actually really nifty and still relatively easy to read.
  144.   // Taken from: http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
  145. }
  146.  
  147. void ex53(){
  148.   int year = 2013;
  149.   int month = 10;
  150.   int day = 22;
  151.  
  152.   printf("[ex53] f(%d,%d,%d): %d\n", year,month,day, day_of_week(year,month,day));
  153.   printf("[ex53] c_f(%d,%d,%d): %d\n", year,month,day, c_day_of_week(year,month,day));
  154. }
  155.  
  156. // Stupid fucking main function to call everything else.
  157. int main (){
  158.   ex41a();
  159.   ex41d();
  160.   ex44a();
  161.   ex44b();
  162.  
  163.   ex51a();
  164.   ex51b();
  165.   ex52a();
  166.   ex52b();
  167.   ex53();
  168.   return atoi("Fuck You.");
  169. }
  170. //I want my REPL back.
  171.  
  172. //WTFs encountered:
  173. //You can't have varags in any sane manner.
  174. //You can't concatenate things into strings in any easy manner.
  175. //There's to_string() in C++11, but it can't be passed to printf?
  176. //Everything
  177. //TRWTF is C++
  178. //No local functions. I think they added lambdas (fucked) in 11 though??
  179. //Silent failings in general.
  180. //I can't abstract away the printfs without an immense amount of wtf, no matter how I try
  181. //Teaching something like C++ as a first.
  182.  
  183.  
  184. //Good things:
  185. //I can return "Fuck you" as exit value.
  186. //I don't have to work with this for my own projects.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement