
#include <iostream>

using namespace std;

typedef double Matrix33[3][3];
typedef int Sudoku[9][9];

Sudoku s ={
		{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
		{ 4, 5, 6, 7, 8, 9, 1, 2, 3 },
		{ 7, 8, 9, 1, 2, 3, 4, 5, 6 },
		{ 2, 3, 4, 5, 6, 7, 8, 9, 1 },
		{ 5, 6, 7, 8, 9, 1, 2, 3, 4 },
		{ 3, 4, 5, 6, 7, 8, 9, 1, 2 },
		{ 8, 9, 1, 2, 3, 4, 5, 6, 7 },
		{ 6, 7, 8, 9, 1, 2, 3, 4, 5 },
		{ 9, 1, 2, 3, 4, 5, 6, 7, 8 }
		};

void MatAdd(Matrix33 matOut, Matrix33 A, Matrix33 B) {
	for (int i = 0; i < 3; i++) {						//Läuft über die Spalten der Matrix
		for (int j = 0; j < 3; j++) {					//Läuft über die Zeilen der Matrix
			matOut[i][j] = A[i][j] + B[i][j];			//Addiert den Inhalt der Matrizen A und B in die neue Matrix C
		}
	}
}

void MatPrint(Matrix33 in) {
	cout << "Die Matrix C sieht nun wie folgt aus: " << endl;
	for (int i = 0; i < 3; i++) {						//Läuft über die Spalten der Matrix
		cout << "|\t";									//Sorgt in der Ausgabe für dezente Ordnung
		for (int j = 0; j < 3; j++) {					//Läuft über die Zeilen der Matrix
			cout << in[i][j] << "\t|\t";				//Gibt den Inhalt der Matrix aus
		}
		cout << endl;
	}
}

void MatMul(Matrix33 matOut, Matrix33 A, Matrix33 B) {
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			for (int k = 0; k < 3; k++) {
				matOut[i][k] = matOut[i][k] + A[i][j] * B[j][k];
			}
		}
	}
} //Gemäß http://de.wikipedia.org/wiki/Matrizenmultiplikation#Standardalgorithmus

bool SudokuRowIsValid(Sudoku s, int rowIndex) {
	int eins = 0;										//Vielleicht nicht die eleganteste Lösung,
	int zwei = 0;										//aber es funktioniert.
	int drei = 0;										//Die Idee ist es, zu zählen, wie oft eine
	int vier = 0;										//bestimmte Zahl bereits innerhalb der Spalte
	int fuenf = 0;										//vorkam. Am ende wird geguckt, ob jede
	int sechs = 0;										//Zahl exakt ein mal auftrat.
	int sieben = 0;										//Dann wird true zurück gegeben.
	int acht = 0;
	int neun = 0;
	for (int i = 0; i < 9; i++) {						//Hier wird das Auftreten der einzelnen Zahlen gezählt
		if (s[rowIndex][i] == 1)
			eins++;
		if (s[rowIndex][i] == 2)
			zwei++;
		if (s[rowIndex][i] == 3)
			drei++;
		if (s[rowIndex][i] == 4)
			vier++;
		if (s[rowIndex][i] == 5)
			fuenf++;
		if (s[rowIndex][i] == 6)
			sechs++;
		if (s[rowIndex][i] == 7)
			sieben++;
		if (s[rowIndex][i] == 8)
			acht++;
		if (s[rowIndex][i] == 9)
			neun++;
	}
	if (eins == 1 && zwei == 1 && drei == 1 && vier == 1 && fuenf == 1	//Kam eine Zahl doppelt vor
			&& sechs == 1 && sieben == 1 && acht == 1 && neun == 1)		//oder fehlt eine? -> return 0
		return 1;
	else {
		return 0;
	}
}

bool SudokuColumnIsValid(Sudoku s, int colIndex) {
	int eins = 0;										//Analog zur Methode SudokuRowIsValid
	int zwei = 0;
	int drei = 0;
	int vier = 0;
	int fuenf = 0;
	int sechs = 0;
	int sieben = 0;
	int acht = 0;
	int neun = 0;
	for (int i = 0; i < 9; i++) {
		if (s[i][colIndex] == 1)
			eins++;
		if (s[i][colIndex] == 2)
			zwei++;
		if (s[i][colIndex] == 3)
			drei++;
		if (s[i][colIndex] == 4)
			vier++;
		if (s[i][colIndex] == 5)
			fuenf++;
		if (s[i][colIndex] == 6)
			sechs++;
		if (s[i][colIndex] == 7)
			sieben++;
		if (s[i][colIndex] == 8)
			acht++;
		if (s[i][colIndex] == 9)
			neun++;
	}
	if ((eins == 1 && zwei == 1 && drei == 1 && vier == 1 && fuenf == 1
			&& sechs == 1 && sieben == 1 && acht == 1 && neun == 1))
		return 1;
	else {
		return 0;
	}
}

bool SudokuBlockIsValid(Sudoku s, int blockIndX, int blockIndY) {
	if (blockIndX > 2 || blockIndY > 2) {				//Ohne dieser Abfrage könnten bittere Errors entstehen x_X
		cout << "Dieser Block existiert nicht im Sudoku." << endl;
		return 0;
	}
	int eins = 0;										//Hier wird wieder mit der gleichen Idee
	int zwei = 0;										//gearbeitet, das Vorkommen von bestimmten
	int drei = 0;										//zahlen zu zählen.
	int vier = 0;
	int fuenf = 0;
	int sechs = 0;
	int sieben = 0;
	int acht = 0;
	int neun = 0;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			if (s[i + (3 * blockIndX)][j + (3 * blockIndY)] == 1)	//s[i + (3 * blockIndX/Y)] sorgt
				eins++;												//dafür, dass der korrekte Block
			if (s[i + (3 * blockIndX)][j + (3 * blockIndY)] == 2)	//auf korrektheit untersucht wird.
				zwei++;
			if (s[i + (3 * blockIndX)][j + (3 * blockIndY)] == 3)
				drei++;
			if (s[i + (3 * blockIndX)][j + (3 * blockIndY)] == 4)
				vier++;
			if (s[i + (3 * blockIndX)][j + (3 * blockIndY)] == 5)
				fuenf++;
			if (s[i + (3 * blockIndX)][j + (3 * blockIndY)] == 6)
				sechs++;
			if (s[i + (3 * blockIndX)][j + (3 * blockIndY)] == 7)
				sieben++;
			if (s[i + (3 * blockIndX)][j + (3 * blockIndY)] == 8)
				acht++;
			if (s[i + (3 * blockIndX)][j + (3 * blockIndY)] == 9)
				neun++;
		}
		if ((eins == 1 && zwei == 1 && drei == 1 && vier == 1 && fuenf == 1
				&& sechs == 1 && sieben == 1 && acht == 1 && neun == 1))
			return 1;
	}
	return 0;
}

bool SudokuIsValid(Sudoku s) {
	for (int i = 0; i < 9; i++) {						//Überprüft alle Zeilen und Spalten auf Richtigkeit
		if (!SudokuRowIsValid(s, i) || !SudokuColumnIsValid(s, i)) {
			cout << "Zeile oder Spalte " << i << " ist fehlerhaft." << endl;
			return 0;									//Sollte es nur einen Fehler geben, wird 0 zurück gegeben.
		}
	}
	for (int i = 0; i < 3; i++) {						//Auch die Blöcke werden untersucht.
		for (int j = 0; j < 3; j++) {
			if (!SudokuBlockIsValid(s, i, j)) {
				cout << "Der 3x3 Block " << j << "-" << i << " ist fehlerhaft." //Die Ausgabe hilft einem beim Fehler finden.
						<< endl;
				return 0;								//Hier wird sofort abgebroche, sollte nur ein Block fehlerhaft sein.
			}
		}
	}
	return 1;											//Die Methode läuft bis zu diesem Punkt durch?
}														//Dann ist das Sudoku wohl richtig!

bool StringBeginsWithSubstring(const char* str, const char* subStr) {
	int i = 0;
	while (subStr[i] == str[i]) {						//Solange str mit dem subStr übereinstimmt
		i++;											//wird das nächste Zeichen überprüft.
		if (subStr[i] == 0) {							//subStr ist zu Ende, und str stimmt bis dahin komplett überein?
			cout << "'" << str << "' beginnt mit den Zeichen: '" << subStr
					<< "'" << endl;
			return 1;									//Dann wird 1 zurück gegeben.
		}
	}
	cout << "'" << str << "' beginnt nicht mit den Zeichen: '" << subStr << "'"
			<< endl;
	return 0;											//Oder 0, falls str sich von subStr unterscheidet, bevor subStr endet.
}

bool Sbws(const char* str, const char* subStr) {		//Eine Kopie der Methode "StringBeginsWithSubstring"
	int i = 0;											//nur ohne Konsole-Ausgaben, damit später
	while (subStr[i] == str[i]) {						//bei der Rekursion nicht etliche ungewollte
		i++;											//Ausgaben in der Konsole erscheinen.
		if (subStr[i] == 0) {
			return 1;
		}
	}
	return 0;
}

const char* FindSubstringInString(const char* str, const char* subStr) {
	if (Sbws(str, subStr))								//Nutzt die bereits geschriebene Methode
		return str;										//Ist subStr in str gefunden, wird str zurück gegeben.
	if(str == 0) return 0;								//Ist str zu Ende, wird 0 zurück gegeben (Abbruchkriterium der Rekursion)
	FindSubstringInString(++str, subStr);				//Ansonsten wird FindSubstringInString rekursiv aufgerufen,
}														//allerdings rutscht der Pointer von str ein Zeichen weiter nach rechts.

int main() {											//Main-Methode gemäß der Aufgabenstellung
	Matrix33 A = { { 2.1, 8.3, -1.3 }, { -1.1, 2.3, 4 }, { 1, -7.3, 8 } };
	Matrix33 B = { { 0.9, -3, 4 }, { -1.1, -1.3, 19 }, { 6, -3.3, 4 } };
	Matrix33 C;
	MatAdd(C, A, B);
	cout << "A + B = C" << endl;
	MatPrint(C);
	MatMul(C, A, B);
	cout << endl << "A * B = C" << endl;
	MatPrint(C);
	cout << endl << endl;
	//cout << SudokuRowIsValid(s, 0);
	//cout << SudokuColumnIsValid(s, 0);
	//cout << SudokuBlockIsValid(s, 1, 0);
	cout << SudokuIsValid(s) << endl << endl;
	const char* text = "Dieser Text besteht aus mehreren Worten";
	const char* word = "meh";
	cout << StringBeginsWithSubstring(text, word) << endl;
	const char* subText = FindSubstringInString(text, word);
	if (subText != 0) {
		cout << "Der Teil des Textes ’" << text << "’, der mit ’" << word
				<< "’ beginnt, lautet: ’" << subText << "’ " << endl;
	} else {
		cout << "Das Wort ’" << word << "’ ist nicht in dem Text ’" << text
				<< "’ enthalten." << endl;
	}
	return 0;
	return 0;
}
