Created
June 26, 2015 10:43
-
-
Save indywidualny/8fa41ae42127387aa149 to your computer and use it in GitHub Desktop.
Programowanie 2 - Egzamin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> //( 1) | |
#include <list> //( 2) | |
using namespace std; //( 3) | |
// ############################################## //( 4) | |
class Zadanie; //( 5) | |
typedef list<Zadanie*> Lista; //( 6) | |
class Egzekutor{ //( 7) | |
public: //( 8) | |
virtual void nastepne() = 0; //( 9) | |
virtual void operator+=(Zadanie& z) = 0; //(10) | |
virtual ~Egzekutor() {} //(11) | |
}; //(12) | |
// ############################################## //(13) | |
class Zadanie{ //(14) | |
protected: //(15) | |
static int nr, czeka; //(16) | |
bool wykonane; //(17) | |
Egzekutor* e; //(18) | |
public: //(19) | |
const int id, priorytet; //(20) | |
Zadanie(int p=0) //(21) | |
: wykonane(false),e(0),id(++nr),priorytet(p) //(22) | |
{ //(23) | |
++czeka; //(24) | |
} //(25) | |
virtual ~Zadanie() { if(!wykonane) --czeka; } //(26) | |
virtual void wykonaj() { //(27) | |
if(wykonane) throw *this; //(28) | |
cout << "\nZadanie " << this->id << ": "; //(29) | |
--czeka; wykonane=true; //(30) | |
} //(31) | |
void ustawEgzekutor(Egzekutor* _e){ //(32) | |
e = _e; //(33) | |
} //(34) | |
}; //(35) | |
int Zadanie::nr=0; //(36) | |
int Zadanie::czeka=0; //(37) | |
// ############################################## //(38) | |
template<class Z> //(39) | |
class Tekst : public Z{ //(40) | |
string s; //(41) | |
public: //(42) | |
Tekst(int p=0) : Z(p) {} //(43) | |
void wykonaj() { //(44) | |
Z::wykonaj(); //(45) | |
cout << s; //(46) | |
} //(47) | |
void ustawTekst(string _s) { s=_s; } //(48) | |
}; //(49) | |
// ############################################## //(50) | |
template<class Z> //(51) | |
class Log : public Z{ //(52) | |
public: //(53) | |
Log(int p=0) : Z(p) {} //(54) | |
void wykonaj() { //(55) | |
Z::wykonaj(); //(56) | |
cout << "pozostalo " << Z::czeka; //(57) | |
} //(58) | |
};// ############################################ //(59) | |
template<class Z> //(60) | |
class OkresoweZadanie : public Z{ //(61) | |
public: //(62) | |
OkresoweZadanie(int p=0) : Z(p) {} //(63) | |
void wykonaj() { //(64) | |
Z::wykonaj(); //(65) | |
if(this->e!=0 and this->wykonane){ //(66) | |
this->wykonane=false; //(67) | |
Z::czeka++; //(68) | |
(*this->e) += *this; //(69) | |
} //(70) | |
} //(71) | |
}; //(72) | |
// ############################################## //(73) | |
class FIFOEgzekutor : public Egzekutor, //(74) | |
protected Lista //(75) | |
{ //(76) | |
public: //(77) | |
void nastepne() { //(78) | |
if(size()==0) throw *this; //(79) | |
front()->wykonaj(); //(80) | |
pop_front(); //(81) | |
} //(82) | |
void operator+=(Zadanie& z) { //(83) | |
push_back(&z); //(84) | |
z.ustawEgzekutor(this); //(85) | |
} //(86) | |
}; //(87) | |
// ############################################## //(88) | |
class PriorytetowyEgzekutor : public FIFOEgzekutor //(89) | |
{ //(90) | |
public: //(91) | |
void operator+=(Zadanie& z) { //(92) | |
iterator i=begin(); //(93) | |
while(i!=end() and //(94) | |
(*i)->priorytet>=z.priorytet) ++i; //(95) | |
insert(i,&z); //(96) | |
z.ustawEgzekutor(this); //(97) | |
} //(98) | |
}; //(99) | |
// ############################################## //(100) | |
void test(Egzekutor& e){ //(101) | |
OkresoweZadanie<Log<Zadanie> > log(1); //(102) | |
Tekst<Zadanie> tekst(2); //(103) | |
Zadanie z(3); //(104) | |
tekst.ustawTekst("wazna wiadomosc"); //(105) | |
e += tekst; //(106) | |
e += log; //(107) | |
e += z; //(108) | |
for(int i=0;i<5;++i) e.nastepne(); //(109) | |
} //(110) | |
// ############################################## //(111) | |
int main(){ //(112) | |
try{ //(113) | |
FIFOEgzekutor f; //(114) | |
PriorytetowyEgzekutor p; //(115) | |
test(f); //(116) | |
Tekst<Tekst<Zadanie> > t(0); //(117) | |
t.ustawTekst("Koniec programu"); //(118) | |
t.Tekst<Zadanie>::wykonaj(); //(119) | |
}catch(Zadanie& z){ //(120) | |
cout << "Blad obslugi zadania " << z.id; //(121) | |
} //(122) | |
catch(Egzekutor& z) { //(123) | |
cout << "Blad obslugi egzekutora"; //(124) | |
} //(125) | |
return 0; //(126) | |
} //(127) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define merge ; ###### merge ################### ;( 1) | |
(lambda (p q) ;( 2) | |
(if (pair? p) ;( 3) | |
(merge (cdr p) (cons (car p) q)) ;( 4) | |
q ;( 5) | |
) ;( 6) | |
) ;( 7) | |
) ;( 8) | |
(define join-loop ; ###### loop ############### ;( 9) | |
(lambda (p r) ;(10) | |
(if (pair? p) ;(11) | |
(join-loop (cdr p) (merge (car p) r) ) ;(12) | |
r ;(13) | |
) ;(14) | |
) ;(15) | |
) ;(16) | |
(define join ; ######## join ################### ;(17) | |
(lambda (p) (join-loop p (list)) ) ;(18) | |
) ;(19) | |
(define insert ; #### insert ############## ;(20) | |
(lambda (e p q r) ;(21) | |
(if (not (pair? q)) ;(22) | |
(cons (cons e p) (reverse r)) ;(23) | |
(insert e ;(24) | |
(cons (car q) p) ;(25) | |
(cdr q) ;(26) | |
(cons ;(27) | |
(merge ;(28) | |
(cons e (cons (car q) p)) ;(29) | |
(cdr q) ;(30) | |
) ;(31) | |
r ;(32) | |
) ;(33) | |
) ;(34) | |
) ;(35) | |
) ;(36) | |
) ;(37) | |
(define insert-elem ; ###### insert-elem ####### ;(38) | |
(lambda (e) ;(39) | |
(lambda (q) (insert e (list) q (list)) ) ;(40) | |
) ;(41) | |
) ;(42) | |
(define permutations ; ##### permutations ###### ;(43) | |
(lambda (p) ;(44) | |
(if (pair? p) ;(45) | |
(join ;(46) | |
(map ;(47) | |
(insert-elem (car p)) ;(48) | |
(permutations (cdr p)) ;(49) | |
) ;(50) | |
) ;(51) | |
(list p) ;(52) | |
) ;(53) | |
) ;(54) | |
) ;(55) | |
; ############## main ########################## ;(56) | |
(define p (list 1 2 3)) ;(57) | |
(define q (list 4 5)) ;(58) | |
(define r (list (list p p) (list q q))) ;(59) | |
(display "r: ")(display r)(newline) ;(60) | |
(display "join(r): ")(display (join r))(newline) ;(61) | |
(display "insert(9,q): ") ;(62) | |
(display ((insert-elem 9) q))(newline) ;(63) | |
(display "permutations: ") ;(64) | |
(display (permutations (list "a" "b" "c"))) ;(65) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Email{ //( 1) | |
public String getFrom(); //( 2) | |
public String getMessage(); //( 3) | |
} //( 4) | |
// ############################################## //( 5) | |
class TextEmail implements Email{ //( 6) | |
private String from, message; //( 7) | |
public TextEmail(String from, String message){ //( 8) | |
this.from = from; //( 9) | |
this.message = message; //(10) | |
} //(11) | |
public String getFrom() { return from; } //(12) | |
public String getMessage(){ return message; } //(13) | |
} //(14) | |
// ############################################## //(15) | |
class Filter{ //(16) | |
public boolean check(Email r) {return true;} //(17) | |
public void process(Email e){} //(18) | |
} //(19) | |
// ############################################## //(20) | |
class PatternFilter extends Filter{ //(21) | |
private String pattern; //(22) | |
public PatternFilter(String pattern){ //(23) | |
this.pattern = pattern; //(24) | |
} //(25) | |
public boolean check(Email e){ //(26) | |
return (e.getFrom() + e.getMessage()). //(27) | |
matches(pattern); //(28) | |
} //(29) | |
} //(30) | |
// ############################################## //(31) | |
class FolderFilter extends PatternFilter{ //(32) | |
private String folder; //(33) | |
public FolderFilter(String f,String p){ //(34) | |
super(p); //(35) | |
this.folder = f; //(36) | |
} //(37) | |
public void process(Email e){ //(38) | |
System.out.println("Moved to "+folder); //(39) | |
} //(40) | |
} //(41) | |
// ############################################## //(42) | |
class PriorityFilter extends PatternFilter{ //(43) | |
public PriorityFilter(String p){ //(44) | |
super(p); //(45) | |
} //(46) | |
public void process(Email e){ //(47) | |
System.out.println("Marked as important"); //(48) | |
} //(49) | |
} //(50) | |
// ############################################## //(51) | |
class GroupFilter extends Filter{ //(52) | |
class Node{ //(53) | |
Filter filter; //(54) | |
Node next; //(55) | |
Node(Filter filter, Node next){ //(56) | |
this.filter = filter; //(57) | |
this.next = next; //(58) | |
} //(59) | |
} //(60) | |
Node node; //(61) | |
public boolean check(Email e){ //(62) | |
Node n = this.node; //(63) | |
while(n!=null){ //(64) | |
if(n.filter.check(e)) return true; //(65) | |
n = n.next; //(66) | |
} //(67) | |
return false; //(68) | |
} //(69) | |
public void process(Email e){ //(70) | |
Node n = this.node; //(71) | |
boolean r = false; //(72) | |
while(n!=null){ //(73) | |
if(n.filter.check(e)){ //(74) | |
n.filter.process(e); //(75) | |
r = true; //(76) | |
} //(77) | |
n = n.next; //(78) | |
} //(79) | |
if(!r) throw new RuntimeException( //(80) | |
"Cannot process message"); //(81) | |
} //(82) | |
public void setNext(Filter filter){ //(83) | |
this.node = new Node(filter,this.node); //(84) | |
} //(85) | |
} //(86) | |
// ############################################## //(87) | |
class EmailProcessor{ //(88) | |
GroupFilter filter = new GroupFilter(); //(89) | |
public void addFilter(Filter filter){ //(90) | |
this.filter.setNext(filter); //(91) | |
} //(92) | |
public void process(Email mail){ //(93) | |
System.out.println("%%%->\nNew message from " //(94) | |
+ mail.getFrom() + "\nMessage: " //(95) | |
+ mail.getMessage()); //(96) | |
filter.process(mail); //(97) | |
} //(98) | |
} //(99) | |
// ############################################## //(100) | |
class Test{ //(101) | |
public static EmailProcessor init(){ //(102) | |
EmailProcessor p = new EmailProcessor(); //(103) | |
Filter[] filters = { //(104) | |
new Filter(), //(105) | |
new FolderFilter("work","(.*)company(.*)"), //(106) | |
new PriorityFilter("(.*)money(.*)") }; //(107) | |
for(Filter f : filters) p.addFilter(f); //(108) | |
return p; //(109) | |
} //(110) | |
public static void main(String[] args){ //(111) | |
try{ //(112) | |
EmailProcessor p = init(); //(113) | |
Email m = new TextEmail( //(114) | |
"noone@gmail.com","You won a lot of money!"); //(115) | |
Email b = new TextEmail( //(116) | |
"boss@company.com","You got promoted!"); //(117) | |
Email h = new TextEmail( //(118) | |
"hater@facebook.com","I like you!"); //(119) | |
Email[] inbox = {m,b,h}; //(120) | |
for(Email mail : inbox) p.process(mail); //(121) | |
p.process(new Email(){ //(122) | |
public String getFrom(){ return "#@#"; } //(123) | |
public String getMessage(){return "?!?";}}); //(124) | |
}catch(Exception e){ //(125) | |
System.out.println(e.getMessage()); //(126) | |
} //(127) | |
} //(128) | |
} //(129) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment