ReLast - Klassik


C / C++


Hallo Welt

C

#include <stdio.h>
int main(void)
{
    puts("Hallo Welt! \n");
}

C++

#include <iostream>
int main()
{
    std::cout << "Hallo Welt! \n";
}

Zeiger

	int i = 13;
	int werte[] = {1,2,3};
	int *ptr1 = &i;
	int *ptr2 = ptr1;
	int *ptr3 = werte;
	ptr3 = NULL;

Klassen

Person.h

#pragma once
class Person
{
	public:
		Person(void);
		Person(string name, int alter);
		~Person(void);
		string getName();
		void setName(string name);
		int getAlter();
		void setAlter(int alter);
	private:
		string _name;
		int _alter;

};

Person.cpp

Person::Person(void)
{
}
Person::Person(string name, int alter)
{
	_name = name;
	_alter = alter;
}
string Person::getName()
{
	return _name;
}
void Person::setName(string name)
{
	_name = name;
}
int Person::getAlter()
{
	return _alter;
}
void Person::setAlter(int alter)
{
	_alter = alter;
}

Mitarbeiter.h

class Mitarbeiter : public Person
{
	public:
		Mitarbeiter(void);
		~Mitarbeiter(void);
		int getID();
		void setID(int id);
	private:
		int _id;
};

Mitarbeiter.cpp

int Mitarbeiter::getID()
{
	return _id;
}
void Mitarbeiter::setID(int id)
{
	_id = id;
}

Example

#include <iostream>
#include <string>
#include <limits.h>
#include <time.h>
#include <cmath>
#include <Windows.h>

using namespace std;

enum fehlercodes
{
	ERROR_LESEN = 101,
	ERROR_SCHREIBEN = 102
};

struct datum
{
	int tag;
	int monat;
	int jahr;
};

struct person
{
	string name;
	string vorname;
	datum geburtsdatum;
	string beruf;
};

struct vector
{
	double x;
	double y;
};

class Person
{
	public:
		Person(void);
		Person(string name, int alter);
		~Person(void);
		string getName();
		void setName(string name);
		int getAlter();
		void setAlter(int alter);
	private:
		string _name;
		int _alter;
};
Person::Person(void)
{
}
Person::Person(string name, int alter)
{
	_name = name;
	_alter = alter;
}
string Person::getName()
{
	return _name;
}
void Person::setName(string name)
{
	_name = name;
}
int Person::getAlter()
{
	return _alter;
}
void Person::setAlter(int alter)
{
	_alter = alter;
}

class Mitarbeiter : public Person
{
	public:
		Mitarbeiter(void);
		~Mitarbeiter(void);
		int getID();
		void setID(int id);
	private:
		int _id;
};
Mitarbeiter::Mitarbeiter(void)
{
}
int Mitarbeiter::getID()
{
	return _id;
}
void Mitarbeiter::setID(int id)
{
	_id = id;
}



void methodCounter()
{
	static int counter = 0;
	counter++;
	cout << counter << endl;
}

void fehlermeldung(fehlercodes number)
{
	switch(number)
	{
	case ERROR_LESEN: cout << endl << "Lesefehler!" << endl; break;
	case ERROR_SCHREIBEN: cout << endl << "Schreibfehler!" << endl; break;
	}
}

void sleep(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}

void zins()
{
	double betrag = 0;
	double betrag_neu = 0;
	double zins = 0;
	double ertrag = 0;
	int jahre = 0;
	cin >> betrag;
	cin >> zins;
	cin >> jahre;
	cout << "Jahr \t Zins \t\t Betr Alt \t Betr Neu \t Ertrag" << endl;
	for (int i = 1; i<=jahre; i++)
	{
		betrag_neu = betrag * zins;
		ertrag = betrag_neu - betrag;
		printf("%d \t %7.2f \t %7.2f \t %7.2f \t %7.2f \n", i, zins, betrag, betrag_neu, ertrag);
		betrag = betrag_neu;
	}
}

double fakul(int number)
{
	double result = 1;
	for (int i = 1; i<=number; i++)
	{
		result *= i;
	}
	return result;
}

void zsp()
{
	int input; 
	int max = 0;
	int min = INT_MAX;
	double ds = 0;
	int summe = 0;
	cout << "Zahl eingeben:"; cin >> input;
	summe = summe + input;
	if (input > max) max = input;
	if (input < min) min = input;
	cout << "Zahl eingeben:"; cin >> input;
	summe = summe + input;
	if (input > max) max = input;
	if (input < min) min = input;
	cout << "Zahl eingeben:"; cin >> input;
	summe = summe + input;
	if (input > max) max = input;
	if (input < min) min = input;
	cout << "Zahl eingeben:"; cin >> input;
	summe = summe + input;
	if (input > max) max = input;
	if (input < min) min = input;
	cout << "Zahl eingeben:"; cin >> input;
	summe = summe + input;
	if (input > max) max = input;
	if (input < min) min = input;
	ds = (double)summe/5;
	cout << endl;
	cout << "Max: " << max << endl; 
	cout << "Min: " << min << endl; 
	cout << "Summe: " << summe << endl; 
	cout << "Durchschnitt: " << ds << endl; 
}

void help()
{
	cout << "Datentypen:" << endl;
	bool b = true;
	cout << "bool: " << sizeof(b) << endl;
	short s = SHRT_MAX;
	cout << "short(" << sizeof(s) << "): " << s << endl;
	s++;
	cout << "short(" << sizeof(s) << "): " << s << endl;
	int i = INT_MAX;
	cout << "int(" << sizeof(i) << "): " << i << endl;
	i++;
	cout << "int(" << sizeof(i) << "): " << i << endl;
	unsigned int ui = UINT_MAX;
	cout << "int unsigned(" << sizeof(ui) << "): " << ui << endl;
	ui++;
	cout << "int unsigned(" << sizeof(ui) << "): " << ui << endl;
	long l = LONG_MAX;
	cout << "long(" << sizeof(l) << "): " << l << endl;
	l++;
	cout << "long(" << sizeof(l) << "): " << l << endl;
	float f = FLT_MAX;
	cout << "float(" << sizeof(f) << "): " << f << endl;
	f++;
	cout << "float(" << sizeof(f) << "): " << f << endl;
	double d = DBL_MAX;
	cout << "double(" << sizeof(d) << "): " << d << endl;
	d++;
	cout << "double(" << sizeof(d) << "): " << d << endl;
	char c = 'c';
	cout << "char(" << sizeof(c) << "): " << c << endl;
}

unsigned int* arrayExpand(unsigned int oldarray[], unsigned int oldarrayc, unsigned int newsize)
{
	unsigned int *newarray = new unsigned int[newsize];
	for(unsigned int i = 0; i<=oldarrayc; i++)
	{
		newarray[i] = oldarray[i];
	}
	delete oldarray;
	return newarray;
}

void dateilisting(int ebene, HANDLE fHandle, WIN32_FIND_DATA wfd)
{
	do
	{
		// Eintrag nur behandeln, wenn es nicht . oder .. ist (werden nur bei Unterverzeichnissen mit zurückgeliefert)
		// hier könnte man z.B. auch mit lstrcmp auf . und .. vergleichen, was allerdings nicht ganz so effizient ist
		if (!( (wfd.cFileName[0]=='.') && ( (wfd.cFileName[1]=='.' && wfd.cFileName[2]==0) || wfd.cFileName[1]==0 ) ))
		{
			if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				dateilisting(1, fHandle, wfd);
				// Datei ist keine, sondern ein Verzeichnis...
				// Hier könnte man dasselbe nochmal machen, um auch die
				// Unterverzeichnisse zu scannen ;-)
			}
			else
			{
				for (int i=0; i<ebene; i++) cout << " ";
				for (int i=0; i<100; i++) cout << wfd.cFileName[i];
//				cout << wfd.cFileName << endl;
			}
		}
	}
	while (FindNextFile(fHandle,&wfd));
	FindClose(fHandle);
}

int main() 
{
	bool running = true;
	bool promptTime = true;
	string command = "";
	static int commandCounter = 0;
	do
	{
		if (promptTime)
		{
			time_t ts;
			tm *now;
			ts = time(0);
			now = localtime(&ts);
			cout << now->tm_year+1900 << '.' << now->tm_mon+1 << '.'
				<< now->tm_mday << "-" << now->tm_hour
				<< ':' << now->tm_min << ":" << now->tm_sec << " " << clock() << " " ;
		}
		commandCounter++;
		cout << "input " << commandCounter << " =>";
		cin >> command;
		
		if (command == "klassen")
		{
			Person* p = new Person();
			p->setName("Rene");
			p->setAlter(30);
			cout << "Person " << p->getName() << ", " << p->getAlter() << endl;
			Mitarbeiter* m = new Mitarbeiter();
			m->setID(1);
			m->setName("Rene");
			m->setAlter(30);
			cout << "Mitarbeiter " << m->getID() << ", " << m->getName() << ", " << m->getAlter() << endl;
		}
		if (command == "files")
		{
			cout << "Geht leider nicht. Lieber nach einer POSIX-Lösung suchen!" << endl;
			//HANDLE fHandle;
			//WIN32_FIND_DATA wfd;
			//fHandle=FindFirstFile((LPCWSTR)"C:\\*",&wfd);
			//dateilisting(0, fHandle, wfd);
		}
		if (command == "zeiger")
		{
			int i = 13;
			int werte[] = {1,2,3};
			int *ptr1 = &i;
			int *ptr2 = ptr1;
			int *ptr3 = werte;
			cout << *ptr1 << " " << *ptr2 << " " << *ptr3 << " " << *(ptr3+1) << " " << *(ptr3+2) << endl;
			ptr1 = NULL;
			cout << " " << *ptr2 << " " << *ptr3 << " " << *(ptr3+1) << " " << *(ptr3+2) << endl;
			*ptr2 = 7;
			cout << i << " " << *ptr2 << " " << *ptr3 << " " << *(ptr3+1) << " " << *(ptr3+2) << endl;
		}
		if (command == "vector")
		{
			vector v[2];
			for (int i = 0; i<2; i++)
			{
				cout << endl;
				cout << i+1 << ".Vektor" << endl;
				cout << " x: "; cin >> v[i].x;
				cout << " y: "; cin >> v[i].y;
				cout << endl;
			}
			double l1, l2;
			l1 = sqrt(pow(v[0].x,2) + pow(v[0].y,2));
			l2 = sqrt(pow(v[1].x,2) + pow(v[1].y,2));
			if (l1 == l2) 
			{
				cout << "L1 = L2" << endl;
			}
			else 
			if (l1 > l2) 
			{
				cout << "L1 > L2" << endl;
			}
			else 
			if (l1 < l2) 
			{
				cout << "L1 < L2" << endl;
			}

		}
		if (command == "struct")
		{
			datum birthday;
			person rene;
			cin >> birthday.tag;
			cin >> birthday.monat;
			cin >> birthday.jahr;
			cin >> rene.vorname;
			cin >> rene.name;
			cin >> rene.beruf;
			rene.geburtsdatum = birthday;
			cout << endl;
			cout << rene.vorname << " " << rene.name << " " << rene.geburtsdatum.tag << "." << rene.geburtsdatum.monat << "." << rene.geburtsdatum.jahr << " " << rene.beruf << endl;
			cout << endl;
		}
		if (command == "tabelle")
		{
			int tab[10][10];
			for (int i=0; i<10; i++)
				for (int j=0; j<10; j++)
					tab[i][j]=(i+1)*(j+1);
			for (int i=0; i<10; i++)
				for (int j=0; j<10; j++)
					printf("%d\t",tab[i][j]);

		}
		if (command == "kerker")
		{
			bool doors[100];
			memset(doors, false, sizeof(doors));
			for(int x = 0; x<100; x++) cout << doors[x] << " ";
			for(int i = 0; i<100; i++)
			{
				for(int j = i; j<100; j+=(i+1))
				{
					doors[j] = !doors[j];
				}
				for(int x = 0; x<100; x++) cout << doors[x] << " ";
				cout << endl << endl;
			}
			for(int x = 0; x<100; x++) if(doors[x]) cout << x+1 << " ";
			cout << endl;
		}
		if (command == "prim")
		{
			//time_t ts;
			//tm *now;
			//ts = time(0);
			//now = localtime(&ts);
			unsigned int primesCount = 0;
			unsigned int primesMax = 100;
			unsigned int *primes = new unsigned int[100];
			unsigned int end = 0;
			cin >> end;
			//int end_hour = 0;
			//int end_min = 0;
			//unsigned int i = 1;
			//cin >> end_hour;
			//cin >> end_min;
			int time_start = clock();
			//while ( now->tm_hour <= end_hour && now->tm_min < end_min && i < INT_MAX )
			for (unsigned int i = 1; i<=end; i+=2)
			{
				//cout << (now->tm_hour <= end_hour) << " " << (now->tm_min < end_min) << " " << i << endl;
				bool prim = true;
				//int time_before = clock();
				for (unsigned int j=1; j<primesCount; j++)
				{
					if( (i%primes[j])==0 ) prim = false;
				}
				if (prim) 
				{
					//cout << "Zeit: " << (clock()-time_before) << ", Zahl: " << i << endl;
					if(primesCount>=primesMax) primes = arrayExpand(primes, primesCount, primesCount+100);
					primes[primesCount++] = i;
				}
				//i+=2;
			}
			cout << endl;
			cout << "Zeit: " << clock()-time_start << endl;
			cout << "Anzahl gefundener Primzahlen: " << primesCount << endl;
			cout << "Größte gefundene Primzahl: " << primes[primesCount-1] << endl;
		}
		if (command == "test")
		{
			const int array_length = 10;
			int zahlen[array_length];
			for (int i = 0; i<array_length; i++)
			{
//				zahlen[i] = i;
				for(int j=63;j>=0;--j) printf("%d",((zahlen[i]>>j)&1));
				cout << endl;
				int fubar = 12;
				for(int j=63;j>=0;--j) printf("%d",((fubar>>j)&1));
				cout << endl;
			}
		}
		if (command == "count")
		{
			methodCounter();
		}
		if (command == "zins")
		{
			zins();
		}
		if (command == "fibo")
		{
			int end = 0;
			int result = 0;
			int last1 = 1;
			int last2 = 0;
			cin >> end;
			while(result < end)
			{
				result = last1 + last2;
				last2 = last1;
				last1 = result;
				cout << result << endl;
			}
		}
		if (command == "fak") 
		{
			int number = 0;
			cin >> number;
			cout << number << "!=" << fakul(number) << endl;
		}
		if (command == "sort")
		{
			int a = 0;
			int b = 0;
			int c = 0;
			int tmp = 0;
			cin >> a;
			cin >> b;
			cin >> c;
			if (a>b) {tmp=b;b=a;a=tmp;}
			if (a>c) {tmp=c;c=a;a=tmp;}
			if (b>c) {tmp=c;c=b;b=tmp;}
			cout << a << ", " << b << ", " << c << endl;
		}
		if (command == "zsp")
		{
			zsp();
		}
		if (command == "kill")
		{
			string name = "";
			cin >> name;
			sleep(1000);
			cout << "PENG!" << endl;
			sleep(1000);
			cout << "PENG!" << endl;
			sleep(1000);
			cout << "argh! (and argv and argc too ...)" << endl;
			sleep(1000);
			cout << name << " ist tot ..." << endl;
		}

		if (command == "setTimePrompt")
		{
			string in = "";
			cin >> in;
			if (in == "true") promptTime = true;
			if (in == "false") promptTime = false;
		}
		if (command == "help") help();
		if (command == "exit")
		{
			running = false;
			cout << "exiting with " << commandCounter << " commands executed." << endl;
		}
		if (command == "fun")
		{
			cout << "format c: ";
			for (int i = 0; i<20; i++)
			{
				cout << "=";
				sleep(1000);
			}
			fehlermeldung(ERROR_SCHREIBEN);
			cout << "> finished." << endl;
		}
	} while(running);

}

Copyright © 2024

Datenschutz | Impressum