Skip to content

Instantly share code, notes, and snippets.

@JeGa
Created April 11, 2011 08:02
Show Gist options
  • Save JeGa/913215 to your computer and use it in GitHub Desktop.
Save JeGa/913215 to your computer and use it in GitHub Desktop.
//---------------------------------------------------------------------------
// Prüfungsaufgabe Sommer 2010
//---------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
class ArbeitsplatzPC {
private:
string macAdresse;
char netz;
bool checknetz(char);
bool checkMac(string);
bool checkHex(char);
public:
ArbeitsplatzPC(string, char);
bool setnetz(char);
char getnetz();
bool setMAC(string);
string getMAC();
};
ArbeitsplatzPC::ArbeitsplatzPC(string macAdresse, char netz) {
cout << "Check Mac " << macAdresse;
if(checkMac(macAdresse)) {
this->macAdresse = macAdresse;
this->netz = netz;
cout << " success" << endl;
} else {
cout << " error checking mac" << endl;
}
}
bool ArbeitsplatzPC::setnetz(char netz) {
this->netz = netz;
return true;
}
char ArbeitsplatzPC::getnetz() {
return netz;
}
bool ArbeitsplatzPC::setMAC(string macAdresse) {
if(checkMac(macAdresse)) {
this->macAdresse = macAdresse;
return true;
} else {
return false;
}
}
string ArbeitsplatzPC::getMAC() {
return macAdresse;
}
bool ArbeitsplatzPC::checknetz(char netz) {
return true;
}
bool ArbeitsplatzPC::checkMac(string macAdresse) {
int length = macAdresse.length();
if(length == 17) {
for(int i=1; i<=length; i++) {
if((i % 3) == 0) {
if(!(macAdresse[i-1] == '-'))
return false;
} else {
if(!(checkHex(macAdresse[i-1])))
return false;
}
}
} else {
return false;
}
return true;
}
bool ArbeitsplatzPC::checkHex(char z) {
switch(z) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
case 'd':
case 'D':
case 'e':
case 'E':
case 'f':
case 'F':
return true;
default:
return false;
}
}
void main() {
// test values ...
string mac1("12-A5-3F-B2-3F-23");
string mac2("12-A5-3F-B2-3F-XX");
string mac3("12A5-3F-B2-3F-XXX");
ArbeitsplatzPC pc1(mac1, 'A');
ArbeitsplatzPC pc2(mac2, 'A');
ArbeitsplatzPC pc3(mac3, 'A');
// Test with one object
cout << "\nObject 1: Netz: " << pc1.getnetz() << " Mac: " << pc1.getMAC() << endl;
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment