Skip to main content
Loading...

More C++ Posts

#include "goat.h" //include goat.h

void Goat::setBreed(string breed) {
   this->breed = breed;
}
void Goat::setWeight(float weight) {
   this->weight = weight;
}
void Goat::setName(string name) {
   this->name = name;
}
void Goat::setGender(char gender) {
   this->gender = gender;
}
void Goat::setSpayed(bool goatIsSpayed) {
   this->goatIsSpayed = goatIsSpayed;
}
void Goat::setRegistrationID(string registrationID) {
   this->registrationID = registrationID;
}
void Goat::setColor(string color) {
   this->color = color;
}
void Goat::setOtherComments(string otherComments) {
   this->otherComments = otherComments;
}
string Goat::getBreed() {
   return breed;
}
float Goat::getWeight() {
   return weight;
}
string Goat::getName() {
   return name;
}
char Goat::getGender() {
   return gender;
}
bool Goat::getSpayed() {
   return goatIsSpayed;
}
string Goat::getRegistrationID() {
   return registrationID;
}
string Goat::getColor() {
   return color;
}
string Goat::getOtherComments() {
   return otherComments;
}

Goat::Goat() {
   breed = "";
   weight = 0.0;
   name = "";
   gender = '\0';
   goatIsSpayed = false;
   registrationID = "";
   color = "";
   otherComments = "";
}

Goat::Goat(string goatBreed, float goatWeight, string goatName, char goatGender, bool goatSpayedStatus, string goatRegistrationID, string goatColor, string goatOtherComments) {
  
   breed = goatBreed;
   weight = goatWeight;
   name = goatName;
   gender = goatGender;
   goatIsSpayed = goatSpayedStatus;
   registrationID = goatRegistrationID;
   color = goatColor;
   otherComments = goatOtherComments;
}

Goat::~Goat() {
   cout << "goat destroyed" << endl;
}

void Goat::printinfo() {  
   cout << "Breed: " << breed << endl << "weight: " << weight << endl << "Name: " << name << endl << "Gender: " << gender << endl << "is Spayed: ";
   if(goatIsSpayed) {  //here I do a logical test on boolean goatIsSpayed. if true cout << true else cout << false
      cout << "True";
   } else {
      cout << "False";
   }
   cout << endl << "Registration ID: " << registrationID << endl << "Color Description: " << color << endl << "Other Comments: " << otherComments << endl << endl;
}
#include <iostream>
int main(){
    const char* const hello = "Hello, world!";
    
    const char* bruh = hello;
    
    char* const yeet = hello;
    
    std::cout << bruh << std::endl;
    
    std::cout << yeet << std::endl;
    
    return 0;
}

/*
    Place your bets!
    
    Will the program:
    a.) Print "Hello, world!" twice?
    b.) Compile error on line 5 (bruh initialize line) because the pointer gets implicit cast to non-const?
    c.) Compile error on line 7 (yeet initialize line) because the char gets implicit cast to non-const?
    d.) Both b and c?
    e.) Compile error line 11 (print yeet) because the pointer is constant and can't be incremented
    f.) Print "Hello, world!" then print the pointer address in hexadecimal
    g.) Both b and e?
    h.) Both c and e?
    i.) B, c, and e?
    
*/

// The answer is in this base 64 string:
// T25seSBjLikKVGhlIGNvbXBpbGVyIGRvZXNuJ3QgYXBwcmVjaWF0ZSB5b3UgbWFraW5nIHRoZSBjaGFyYWN0ZXJzIHRoZSBwb2ludGVyIHJlZmVycyB0byBub24tY29uc3QsIGJ1dCBpdCdzIGZpbmUgd2l0aCB5b3UgY29weWluZyBhIGNvbnN0YW50IHZhbHVlLCBpLmUuIHRoZSBwb2ludGVyLCB0byBhIG5vbi1jb25zdGFudCB2YXJpYWJsZS4KSWYgeW91IHJlcGxhY2UgdGhhdCBsaW5lIHdpdGggY2hhciogY29uc3QgeWVldCA9IGNvbnN0X2Nhc3Q8Y2hhciogY29uc3Q+KGhlbGxvKTsgSXQnbGwgcHJpbnQgIkhlbGxvLCB3b3JsZCEiIHR3aWNlLCB3aGljaCBpcyB2ZXJ5IHN0cmFuZ2UgY29uc2lkZXJpbmcgdGhhdCB5ZWV0IGlzIGEgY29uc3QgcG9pbnRlciwgc28geW91J2QgdGhpbmsgaXQgd291bGQgcHJpbnQgYXMgYSBoZXhhZGVjaW1hbCBiZWNhdXNlIGlmIHlvdSB0cnkgdG8gKCsreWVldCkgd2hpbGUgbG9vcGluZyB0aHJvdWdoIHRoZSBzdHJpbmcsIHlvdSdkIGdldCBhbiBlcnJvciwgYmVjYXVzZSBpdCdzIGNvbnN0IGFuZCBjYW4ndCBiZSBjaGFuZ2VkLgpJbnN0ZWFkIG9mIHVzaW5nIGEgdGVtcGxhdGUgZnVuY3Rpb24gZm9yIG9zdHJlYW06Om9wZXJhdG9yPDwsIHRoZXkgbWFrZSBpdCBhIGZ1bmN0aW9uIHRoYXQgdGFrZXMgdHlwZSBjb25zdCBjaGFyKiwgYW5kIEMrKyBoYXMgbm8gcHJvYmxlbXMgcHJvbW90aW5nIGEgdmFyaWFibGUgdG8gY29uc3RhbnQgd2hlbiBpbXBsaWNpdCBjYXN0aW5nLCBhbmQgaXQgaGFzIG5vIHByb2JsZW1zIGltcGxpY2l0IGNhc3RpbmcgdGhlIGNvbnN0IHBvaW50ZXIgdG8gYSBub3JtYWwgcG9pbnRlciBiZWNhdXNlIGl0J3MgbWFraW5nIGEgY29weS4gVGhlIHBvaW50ZXIgZ2V0cyBjb3BpZWQgYmVjYXVzZSB0aGUgcG9pbnRlciBpcyBwYXNzZWQgYnkgdmFsdWUsIG5vdCByZWZlcmVuY2Uu
//Leif Messinger
//Finds all sets of 5 5 letter words that don't have duplicate letters in either themselves or each other.
//First it reads the words in and puts them in groups of their bitmasks
//After that, we recurse on each group. Before doing that, we remove the group from the set of other groups to check it against.
#include <cstdio> //getchar, printf
#include <cassert> //assert
#include <vector>
#include <set>
#include <algorithm> //std::copy_if
#include <iterator> //std::back_inserter

#define CHECK_FOR_CRLF true
#define MIN_WORDS 5
#define MAX_WORDS 5
#define WORD_TOO_LONG(len) (len != 5)

const unsigned int charToBitmask(const char bruh){
	assert(bruh >= 'a' && bruh <= 'z');
	return (1 << (bruh - 'a'));
}

void printBitmask(unsigned int bitmask){
	char start = 'a';
	while(bitmask != 0){
		if(bitmask & 1){
			putchar(start);
		}

		bitmask >>= 1;
		++start;
	}
}

//Pointer needs to be deleted
const std::set<unsigned int>* getBitmasks(){
	std::set<unsigned int>* bitmasksPointer = new std::set<unsigned int>;
	std::set<unsigned int>& bitmasks = (*bitmasksPointer);

	unsigned int bitmask = 0;
	unsigned int wordLength = 0;
	bool duplicateLetters = false;
	for(char c = getchar(); c >= 0; c = getchar()){
		if(CHECK_FOR_CRLF && c == '\r'){
			continue;
		}
		if(c == '\n'){
			if(!(WORD_TOO_LONG(wordLength) || duplicateLetters)) bitmasks.insert(bitmask);
			bitmask = 0;
			wordLength = 0;
			duplicateLetters = false;
			continue;
		}
		if((bitmask & charToBitmask(c)) != 0) duplicateLetters = true;
		bitmask |= charToBitmask(c);
		++wordLength;
	}

	return bitmasksPointer;
}

void printBitmasks(const std::vector<unsigned int>& bitmasks){
	for(unsigned int bruh : bitmasks){
		printBitmask(bruh);
		putchar(','); putchar(' ');
	}
	puts("");
}

//Just to be clear, when I mean "word", I mean a group of words with the same letters.
void recurse(std::vector<unsigned int>& oldBitmasks, std::vector<unsigned int> history, const unsigned int currentBitmask){
	//If there's not enough words left
	if(oldBitmasks.size() + (-(history.size())) + (-MIN_WORDS) <= 0){
		//If there's enough words
		if(history.size() >= MIN_WORDS){
			//Print the list
			printBitmasks(history);
		}
		return;
	//To make it faster, we can stop it after 5 words too
	}else if(history.size() >= MAX_WORDS){
		//Print the list
		printBitmasks(history);
		return;
	}

	//Thin out the array with only stuff that matches the currentBitmask.
	std::vector<unsigned int> newBitmasks;
	std::copy_if(oldBitmasks.begin(), oldBitmasks.end(), std::back_inserter(newBitmasks), [&currentBitmask](unsigned int bruh){
		return (bruh & currentBitmask) == 0;
	});

	while(newBitmasks.size() > 0){
		//I know this modifies 'oldBitmasks' too. It's intentional.
		//This makes it so that the word is never involved in any of the child serches or any of the later searches in this while loop.
		const unsigned int word = newBitmasks.back(); newBitmasks.pop_back();

		std::vector<unsigned int> newHistory = history;
		newHistory.push_back(word);

		recurse(newBitmasks, newHistory, currentBitmask | word);
	}
}

int main(){
	const std::set<unsigned int>* bitmasksSet = getBitmasks();
	std::vector<unsigned int> bitmasks(bitmasksSet->begin(), bitmasksSet->end());
	delete bitmasksSet;

	recurse(bitmasks, std::vector<unsigned int>(), 0);

	return 0;
}