Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <string>
- #include <vector>
- #include <iostream>
- #include <fstream>
- using std::vector;
- using std::string;
- class ZlyNumerWejscia: public std::out_of_range
- {
- public:
- ZlyNumerWejscia(): std::out_of_range("Poza zakresem!"){}
- };
- class Bramka
- {
- protected:
- int liczba_wejsc;
- bool *stan_wejscia;
- public:
- Bramka(int l_wejsc): liczba_wejsc(l_wejsc)
- {
- stan_wejscia=new bool[liczba_wejsc];
- for (int i=0;i<liczba_wejsc;i++)
- {
- stan_wejscia[i]=false;
- }
- }
- virtual void Zeruj()
- {
- for (int i=0;i<liczba_wejsc;i++)
- {
- stan_wejscia[i]=false;
- }
- }
- virtual int LiczbaWejsc()
- {
- return liczba_wejsc;
- }
- virtual void UstawWejscie(int i, int x)
- {
- if (i<0 || i>liczba_wejsc)
- throw ZlyNumerWejscia();
- stan_wejscia[i]=x;
- }
- virtual bool Wejscie(int i)
- {
- if (i<0 || i>liczba_wejsc)
- throw ZlyNumerWejscia();
- return stan_wejscia[i];
- }
- virtual bool Wyjscie()=0;
- virtual string ZwrocNazwe()=0;
- };
- class NOT: public Bramka
- {
- public:
- NOT(): Bramka(1){}
- bool Wyjscie()
- {
- return !stan_wejscia;
- }
- string ZwrocNazwe()
- {
- return " NOT ";
- }
- };
- class OR: public Bramka
- {
- public:
- OR(int l_wejsc): Bramka(l_wejsc){}
- bool Wyjscie()
- {
- for (int i=0;i<liczba_wejsc;i++)
- {
- if (stan_wejscia[i]==1)
- {
- return true;
- }
- else
- return false;
- }
- }
- string ZwrocNazwe()
- {
- return " OR ";
- }
- };
- class AND: public Bramka
- {
- public:
- AND(int l_wejsc): Bramka(l_wejsc){}
- bool Wyjscie()
- {
- for (int i=0;i<liczba_wejsc;i++)
- {
- if (stan_wejscia[0]!=stan_wejscia[i])
- {
- return false;
- }
- else
- return true;
- }
- }
- string ZwrocNazwe()
- {
- return " AND ";
- }
- };
- class XOR: public Bramka
- {
- public:
- XOR(int l_wejsc): Bramka(l_wejsc){}
- bool Wyjscie()
- {
- for (int i=0;i<liczba_wejsc;i++)
- {
- if (stan_wejscia[0]!=stan_wejscia[i])
- {
- return true;
- }
- else
- return false;
- }
- }
- string ZwrocNazwe()
- {
- return " XOR ";
- }
- };
- int main()
- {
- vector<Bramka*> Nazwa;
- vector<Bramka*>::iterator it;
- string nazwa;
- int liczba, stan;
- std::ifstream p;
- p.open("bramki.txt");
- try
- {
- while (!p.eof())
- {
- p >> nazwa;
- if (nazwa == "NOT")
- {
- p >> stan;
- Nazwa.push_back(new NOT());
- it=Nazwa.end()-1;
- (*it)->UstawWejscie(0,stan);
- }
- if (nazwa == "OR")
- {
- p >> liczba;
- Nazwa.push_back(new OR(liczba));
- it=Nazwa.end()-1;
- for (int i=0;i<liczba;i++)
- {
- p >> stan;
- (*it)->UstawWejscie(i,stan);
- }
- }
- if (nazwa == "AND")
- {
- p >> liczba;
- Nazwa.push_back(new AND(liczba));
- it=Nazwa.end()-1;
- for (int i=0;i<liczba;i++)
- {
- p >> stan;
- (*it)->UstawWejscie(i,stan);
- }
- }
- if (nazwa == "XOR")
- {
- p >> liczba;
- Nazwa.push_back(new XOR(liczba));
- it=Nazwa.end()-1;
- for (int i=0;i<liczba;i++)
- {
- p >> stan;
- (*it)->UstawWejscie(i,stan);
- }
- }
- }
- for (it=Nazwa.begin(); it<Nazwa.end(); it++)
- {
- printf("%s %d ", (*it)->ZwrocNazwe().c_str(), (*it)->LiczbaWejsc());
- for (int i=0;i<(*it)->LiczbaWejsc(); i++)
- {
- printf("%d ", (*it)->Wejscie(i));
- }
- printf("%d ", (*it)->Wyjscie());
- printf("\n");
- }
- }
- catch (ZlyNumerWejscia &k)
- {
- printf(" ", k.what());
- }
- _getch();
- return 0;
- }
- NOT 0
- NOT 1
- OR 3 0 0 0
- OR 3 0 1 1
- OR 5 1 0 0 0 0
- OR 2 1 0
- AND 4 1 1 1 1
- AND 4 0 0 0 0
- AND 3 0 1 1
- XOR 4 1 0 1 0
- XOR 4 1 1 1 0
- XOR 3 0 0 0
Add Comment
Please, Sign In to add comment