Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Help] C++ Output

butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
I just copied the UnitTest from the TUT.
Thanks for explaining =]
--------
Btw, it seems these parts return a false
PHP:
               if ((one == two) != one || (one == two) != two){
                 return false;
               
               } else if ((one == -two) != 0.0 || (-one == two) != 0.0)  {
                 return false;
               
               } else if (((0.0, two) || (one, 0.0)) != (0.0 * two) / 2) {
                 return false;

is != one the correct way to tell that the return should be equal to one?
Could you help me on these please?
 
Last edited:
Joined
Mar 11, 2007
Messages
904
Reaction score
1,260
I just copied the UnitTest from the TUT.
Thanks for explaining =]
--------
Btw, it seems these parts return a false
PHP:
               if ((one == two) != one || (one == two) != two){
                 return false;
               
               } else if ((one == -two) != 0.0 || (-one == two) != 0.0)  {
                 return false;
               
               } else if (((0.0, two) || (one, 0.0)) != (0.0 * two) / 2) {
                 return false;

is != one the correct way to tell that the return should be equal to one?
Could you help me on these please?

the != logical operator pretty much means "does not equal"

ex.

int a = 2, b = 4;
if(a != b)
cout << "a doesn't equal b";
else
cout << "a does equal b";

if you want to check if two objects (in C++ that is what variables, classes, structs, etc. are called. Objects.) are equal then you need to use the equal logical operator or ==

So...

if ((one == two) != one || (one == two) != two)

is pretty much like saying... if one is equal to two and that is not equal to one OR if one is equal to two and that is not equal to two then we want to return false.

IMO that statement can be pretty confusing, i wonder where you found it from
 
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
(one == two) != one will evaluate like this:
First the (one == two) part becomes false (0) or true (1), depending on whether one equals two or not. You can think of this as a direct replacement. Then the rest is evaluated (assume one equals two): true != one. Even though it can be computed due to boolean values having also the integer representation 0/1, it doesn't make sense to you and me, and should be avoided.
 
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
Since I couldn't figure it out I downloaded the answers and it turns out i was complicating it too much lol. I should have just put numbers in stead of 'one' and 'two'. Thanks for your help anyways =].

-----------------------

I know taking a look at the answers is cheating, but I only took a look at one of the lines that didn't match as stated above.

This is what I finally made (working 100% =D):

PHP:
#include <iostream>

using namespace std;
// Avarage
int Ava(int nr1, int nr2) {
      // get's the avarage of 2 floating numbers
      return (nr1 + nr2) / 2;
}

bool AvaTest() {
     
                   // Test with 2 equal numbers
               if (Ava(5, 5) != 5) {
                 return false;
               }
               
               // Test with 2 opposite numbers
               if (Ava(5, -5) != 0)  {
                 return false;
               }
               
               // Test with one value == 0
               if (Ava(0, 6) != 3) {
                 return false;
               }
               
               // Test with special case
               if (Ava(3, 9) != 6) {
                  return false;
               }
               
     //if test is succesfull
     return true;
}
//UnitTest
void UnitTest() {
     // variable 
	bool mySuccesful = true;

	// executing test 
	mySuccesful &= AvaTest();

	// Tests successfully executed? 
	if (mySuccesful == true) {
		// yes, result to screen 
		cout << "Unit tests succesvol afgerond.";
	} else {
		// no, result to screen 
		cout << "Unit tests niet geslaagd!";
	}
}
//Program starts here
int main() {
     UnitTest();
     cout << endl; //Wait for enter
     system("Pause");
}
 
Last edited:
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
Lol, I'm finally done this is what I've made:
It took me 1.5 days of after-school work and tiny bit of cheating, but i finally managed to get it to work =D. The big text between the /**/ was the exercise in Dutch.

* I'm going to re-write this one, since I want to make sure I fully get this(without looking at this one or something else). Even though I already did this one already, I still believe repeating it wouldn't do any bad =]

PHP:
/*2. Schrijf een functie die als parameter meekrijgt hoeveel dagen er verstreken zijn sinds maandag 1 januari 1900 
en als return value de naam geeft van de opgegeven dag (maandag, dinsdag, woendag enz.). 
Schrijf unit tests voor de volgende situaties:

    * als er 0 dagen verstreken zijn, is het resultaat "maandag";
    * als het aantal verstreken dagen een veelvoud van zeven is (0, 7, 14, 21 enz.), is het resultaat "maandag".

Test tenslotte een specifiek geval; bijvoorbeeld, na 999 dagen is het een zaterdag.*/

#include <iostream>
#include <string>

using namespace std;
/**
 * Bepaalt de naam van de weekdag voor de opgegeven dag.
 *
 * @param	dag	het aantal dagen dat verstreken is sinds 1 januari 1900.
 *
 * @return	de naam van de weekdag
 */

string WeekDay(int day) {

    // input modulo 7.
   day %= 7;

  if (day == 0) {
     return "Monday";
     }
     
     // Day modulo 7 results 1
  if (day == 1) {
     return "Tuesday";
     }
     
     // Day modulo 7 results 2
  if (day == 2) {
     return "Wednesday";
     }
     
     // Day modulo 7 results 3
  if (day == 3) {
     return"Thursday";
     }
     
     // Day modulo 7 results 4
  if (day == 4) {
     return"Friday";
     }
     
     // Day modulo 7 results 5
  if (day == 5) {
     return"Saturday";
     }
     
     // Day modulo 7 results 6
  if (day == 6) {
     return"Sunday";
     }
     
  return "";
}
/*
3. Schrijf een functie die als parameters de dag, de maand en het jaar meekrijgt 
en als return value het aantal dagen geeft dat is verstreken sinds 1 januari 1900. 
Je hoeft geen rekening te houden met data vòòr 1 januari 1900 of met niet bestaande data. 
Voorbeeld: als de invoer 30, 1, 1900 is, dan is het antwoord 29.

Houd rekening met schrikkeljaren. 
Voor elk schrikkeljaar moet je een extra dag optellen bij het aantal verstreken dagen. 
Een schrikkeljaar doet zich iedere vier jaar voor, behalve als het jaartal deelbaar is 
door honderd. Echter, als het jaartal deelbaar is door vierhonderd, 
dan is het weer wel een schrikkeljaar. 
Dus, 1900 is geen schrikkeljaar, maar 1904 en 2000 wel.*/


int Past(int day, int month, int year) {
    // er zijn heel veel manieren om deze functie te schrijven. Onderstaande
	// oplossing is vrij lang, maar pakt het probleem wel stap-voor-stap aan.

	// bereken aantal verstreken jaren
	int myYear = year - 1900;
	// bereken aantal dagen in verstreken jaren
	int myDaysInYears = 365 * myYear;
	// bereken aantal schrikkeljaren
	int mySchrikkeljaar = (year - 1900) / 4;
	    mySchrikkeljaar -= (year - 1900) / 100;
	    mySchrikkeljaar += (year - 1900 + 300) / 400;
	// is dit jaar een schrikkeljaar?
	if (((year % 4) == 0)&& ((year % 100) != 100) && ((year % 400) == 0)) {       
       // ja, zijn we februari al voorbij?
	   if (month < 3) {
	   // nee, dan moeten we dit schrikkeljaar niet meetellen
	      mySchrikkeljaar--;
       }
    }
       
    // tel voor elk verstreken schrikkeljaar een dag op bij het aantal
    // dagen in de verstreken jaren
    myDaysInYears += mySchrikkeljaar;
	
    // bereken aantal verstreken maanden
    int myMonths = month - 1;
		
	// bereken aantal dagen in verstreken maanden
	int myDaysInMonths = myMonths * 31;
		
    // is februari verstreken?
    if (month > 2) {
	   // ja, trek dagen af (februari heeft geen 31 dagen)
	   myDaysInMonths -= 3;			
    }			
         
	// is april verstreken?
	if (month > 4) {
	   // ja, trek dagen af (april heeft geen 31 dagen)
	   myDaysInMonths -= 1;
    }
		
    // is juni verstreken?
    if (month > 6) {						
       // ja, trek dagen af (juni heeft geen 31 dagen)
	   myDaysInMonths -= 1;
    }

    // is september verstreken?
    if (month > 9) {						
       // ja, trek dagen af (september heeft geen 31 dagen)
       myDaysInMonths -= 1;
    }					
		
    // is november verstreken?
    if (month > 11) {							
       // ja, trek dagen af (november heeft geen 31 dagen)
       myDaysInMonths -= 1;
    }
        
    // bereken aantal verstreken dagen
    int myTotalDays = day - 1;
									
    // geef aantal verstreken dagen terug
    return myTotalDays + myDaysInMonths + myDaysInYears;
}
    
bool WeekDayTest() {
     
     if (WeekDay(0) != "Monday") {
		return false;
	    }
     
     if (WeekDay(21) != "Monday") {
         return false;
         }    
         
     if (WeekDay(121) != "Wednesday") {
         return false;
         }

         return true;
}

bool PastTest() {
     
     // op 1 januari 1900 zijn er 0 dagen verstreken;
     if (Past(1, 1, 1900) != 0) {
        return false;
     }
     
     // op 2 januari 1900 is er 1 dag verstreken;
     if (Past(2, 1, 1900) != 1) {
        return false;
     }
     
     // op 1 februari 1900 zijn er 31 dagen verstreken;
     if (Past(1, 2, 1900) != 31) {
        return false;
     }
     
     // op 1 januari 1901 zijn er 365 dagen verstreken;
     if (Past(1, 1, 1901) != 365) {
        return false;
     }   
     
     // op 1 januari 1904 zijn er 1460 dagen verstreken;
     if (Past(1, 1, 1904) != 1460) {
        return false;
     }
     
     // op 28 februari 1904 zijn er 1518 dagen verstreken;
     if (Past(28, 2, 1904) != 1518) {
        return false;
     }
     
     // op 1 maart 1904 zijn er 1520 dagen verstreken;
     if (Past(1, 3, 1904) != 1520) {
        return false;
     }
     
    // op 1 januari 1905 zijn er 1826 dagen verstreken;
    if (Past(1, 1, 1905) != 1826) {
       return false;
    }
    
    // op 1 januari 2000 zijn er 36524 dagen verstreken;
    if (Past(1, 1, 2000) != 36524) {
       return false;
    } 
    
    // op 1 maart 2000 zijn er 36584 dagen verstreken;
    if (Past(1, 3, 2000) != 36584) {
       return false;
    }
    
    // op 1 maart 2200 zijn er 109632 dagen verstreken;
    if (Past(1, 3, 2200) != 109632) {
       return false;
    }
    
    // op 17 oktober 2002 zijn er 37544 dagen verstreken.
    if (Past(17, 10, 2002) != 37544) {
       return false;
    }
    
    return true;
}

void UnitTest() {
     // variable 
	bool mySuccesful = true;

	// executing test 
	mySuccesful &= WeekDayTest(), PastTest();
	// Tests successfully executed? 
	if (mySuccesful == true) {
		// yes, result to screen 
		cout << "Unit tests Succesful.";
	} else {
		// no, result to screen 
		cout << "Unit tests failed!";
	}
}
     
int main() {
    // UnitTest();
    
    int myDay, myMonth, myYear;
    
    cout << "Hello, type in a random date. \n";
    cout << "Day: ";
    cin >> myDay;
    cout << "Month: ";
    cin >> myMonth;
    cout << "Year: ";
    cin >> myYear;
    
    cout << "That day is a: " << WeekDay(Past(myDay, myMonth, myYear)) << " .";
    cout << endl;
    system("pause");
    return 0;
}
 
Last edited:
Back
Top