Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ============================================ Жичани инструменти =============================================================
- #include <iostream>
- #include <cstring>
- using namespace std;
- class ZicanInstrument {
- protected:
- char ime[20];
- int brojZici;
- int osnovnaCena;
- public:
- ZicanInstrument (const char *ime="",int brojZici=0,int osnovnaCena=0)
- {
- strcpy(this->ime,ime);
- this->brojZici = brojZici;
- this->osnovnaCena = osnovnaCena;
- }
- ~ZicanInstrument () {}
- bool operator== (ZicanInstrument &z1)
- {
- if (z1.brojZici == brojZici)
- return true;
- else
- return false;
- }
- virtual int cena ()=0;
- };
- class Mandolina : public ZicanInstrument {
- protected:
- char forma[20];
- public:
- Mandolina (const char *ime="",int brojZici=0,int osnovnaCena=0,const char *forma="") : ZicanInstrument (ime,brojZici,osnovnaCena)
- {
- strcpy(this->forma,forma);
- }
- ~Mandolina () {}
- int cena ()
- {
- if (!strcmp(forma,"Neapolitan"))
- {
- return osnovnaCena + osnovnaCena * 0.15;
- }
- else
- return osnovnaCena;
- }
- friend ostream &operator<< (ostream &o,const Mandolina &m)
- {
- o<<m.ime<<" "<<m.brojZici<<" "<<m.osnovnaCena<<" "<<m.forma<<endl;
- return o;
- }
- };
- class Violina: public ZicanInstrument {
- protected:
- float golemina;
- public:
- Violina (const char *ime="",int brojZici=0,int osnovnaCena=0,float golemina=0) : ZicanInstrument (ime,brojZici,osnovnaCena)
- {
- this->golemina = golemina;
- }
- ~Violina () {}
- int cena ()
- {
- if (golemina == 0.25)
- return osnovnaCena + osnovnaCena * 0.1;
- else if (golemina == 1.00)
- return osnovnaCena + osnovnaCena * 0.2;
- else
- return osnovnaCena;
- }
- friend ostream &operator<< (ostream &o,const Violina &v)
- {
- o<<v.ime<<" "<<v.brojZici<<" "<<v.osnovnaCena<<" "<<v.golemina<<endl;
- }
- };
- void pecatiInstrumenti (ZicanInstrument &zi,ZicanInstrument **i,int n)
- {
- for (int k=0;k<n;k++)
- {
- if (zi == *(i[k]))
- {
- cout<< i[k] -> cena() << endl;
- }
- }
- }
- int main() {
- char ime[20];
- int brojZici;
- float cena;
- char forma[20];
- cin >> ime >> brojZici >> cena >> forma;
- Mandolina m(ime, brojZici, cena, forma);
- int n;
- cin >> n;
- ZicanInstrument **zi = new ZicanInstrument*[2 * n];
- for(int i = 0; i < n; ++i) {
- cin >> ime >> brojZici >> cena >> forma;
- zi[i] = new Mandolina(ime, brojZici, cena, forma);
- }
- for(int i = 0; i < n; ++i) {
- float golemina;
- cin >> ime >> brojZici >> cena >> golemina;
- zi[n + i] = new Violina(ime, brojZici, cena, golemina);
- }
- pecatiInstrumenti(m, zi, 2 * n);
- for(int i = 0; i < 2 * n; ++i) {
- delete zi[i];
- }
- delete [] zi;
- return 0;
- }
- ================================================== Numbers ===============================================================
- #include<iostream>
- using namespace std;
- class Number {
- public:
- virtual double doubleValue() = 0;
- virtual int intValue() = 0;
- virtual void print() = 0;
- bool operator==(Number& n)
- {
- return (this->doubleValue() == n.doubleValue());
- }
- };
- class Integer : public Number{
- private:
- int broj;
- public:
- Integer(int broj)
- {
- this->broj = broj;
- }
- int intValue()
- {
- return broj;
- }
- double doubleValue()
- {
- return double(broj);
- }
- void print()
- {
- cout << broj;
- }
- };
- class Double : public Number{
- private:
- double broj;
- public:
- Double(double broj)
- {
- this->broj = broj;
- }
- int intValue()
- {
- return int(broj);
- }
- double doubleValue()
- {
- return broj;
- }
- void print()
- {
- cout << broj;
- }
- };
- class Numbers{
- private:
- Number** niza;
- int brElem;
- public:
- Numbers()
- {
- niza = new Number*[0];
- brElem = 0;
- }
- Numbers(const Numbers& n)
- {
- brElem = n.brElem;
- niza = new Number*[brElem];
- for (int i = 0; i < n.brElem; i++)
- {
- niza[i] = n.niza[i];
- }
- }
- Numbers& operator=(const Numbers& n)
- {
- brElem = n.brElem;
- niza = new Number*[brElem];
- for (int i = 0; i < n.brElem; i++)
- {
- niza[i] = n.niza[i];
- }
- return *this;
- }
- ~Numbers()
- {
- delete[] niza;
- }
- Numbers& operator+=(Number* n)
- {
- bool add = true;
- for (int i = 0; i < brElem; i++)
- {
- if (*niza[i] == *n)
- {
- add = false;
- }
- }
- if (add)
- {
- Number** nova = new Number*[brElem + 1];
- for (int i = 0; i < brElem; i++)
- {
- nova[i] = niza[i];
- }
- if (add)
- {
- nova[brElem] = n;
- brElem++;
- }
- delete[] niza;
- niza = nova;
- }
- return *this;
- }
- void statistics()
- {
- cout << "Count of numbers: " << brElem << endl;
- double allSum = 0;
- for (int i = 0; i < brElem; i++)
- {
- allSum += niza[i]->doubleValue();
- }
- cout << "Sum of all numbers: " << allSum << endl;
- int countIntegers = 0;
- int intSum = 0;
- for (int i = 0; i < brElem; i++)
- {
- if (dynamic_cast<Integer*>(niza[i]))
- {
- countIntegers++;
- intSum += niza[i]->intValue();
- }
- }
- cout << "Count of integer numbers: " << countIntegers << endl;
- cout << "Sum of integer numbers: " << intSum << endl;
- int countDoubles = 0;
- double doubleSum = 0;
- for (int i = 0; i < brElem; i++)
- {
- if (dynamic_cast<Double*>(niza[i]))
- {
- countDoubles++;
- doubleSum += niza[i]->doubleValue();
- }
- }
- cout <<"Count of double numbers: " << countDoubles << endl;
- cout <<"Sum of double numbers: " << doubleSum << endl;
- }
- void integersLessThan(Integer n)
- {
- bool none = true;
- for (int i = 0; i < brElem; i++)
- {
- if (dynamic_cast<Integer*>(niza[i]))
- {
- if (niza[i]->intValue() < n.intValue())
- {
- none = false;
- cout << "Integer: " << niza[i]->intValue() << endl;
- }
- }
- }
- if (none) cout << "None" << endl;
- }
- void doublesBiggerThan(Double n)
- {
- bool none = true;
- for (int i = 0; i < brElem; i++)
- {
- if (dynamic_cast<Double*>(niza[i]))
- {
- if (niza[i]->doubleValue() > n.doubleValue())
- {
- none = false;
- cout << "Double: " << niza[i]->doubleValue() << endl;
- }
- }
- }
- if (none) cout << "None" << endl;
- }
- };
- int main() {
- int n;
- cin>>n;
- Numbers numbers;
- for (int i=0;i<n;i++){
- int type;
- double number;
- cin>>type>>number;
- if (type==0){//Integer object
- Integer * integer = new Integer((int) number);
- numbers+=integer;
- }
- else {
- Double * doublee = new Double(number);
- numbers+=doublee;
- }
- }
- int lessThan;
- double biggerThan;
- cin>>lessThan;
- cin>>biggerThan;
- cout<<"STATISTICS FOR THE NUMBERS\n";
- numbers.statistics();
- cout<<"INTEGER NUMBERS LESS THAN "<<lessThan<<endl;
- numbers.integersLessThan(Integer(lessThan));
- cout<<"DOUBLE NUMBERS BIGGER THAN "<<biggerThan<<endl;
- numbers.doublesBiggerThan(Double(biggerThan));
- return 0;
- }
- ================================================ Тајни пораки ===============================================================
- #include<cstring>
- #include<iostream>
- using namespace std;
- class Secret {
- public:
- virtual double simpleEntropy() const = 0;
- virtual int total() const = 0;
- };
- class DigitSecret : public Secret{
- private:
- int digits [100] ;
- int length;
- public:
- DigitSecret(const int * digits=0, int length=0){
- this->length=length;
- for (int i=0;i<length;i++)
- this->digits[i]=digits[i];
- }
- int total()const {
- return length;
- }
- double simpleEntropy() const {
- double different=0.0;
- int count = 0;
- for (int i=0;i<length;i++){
- count = 0;
- for (int j=0;j<length;j++){
- if ((i!=j)&&(digits[i]==digits[j]))
- count++;
- }
- if (count==0)
- different+=1.0;
- }
- return different/total();
- }
- friend ostream &operator << (ostream &out, const DigitSecret &ds){
- for (int i=0;i<ds.total();i++)
- out<<ds.digits[i];
- out<<" Simple entropy: " << ds.simpleEntropy() << " Total: "<<ds.total();
- return out;
- }
- };
- class CharSecret : public Secret{
- private:
- char chars [100] ;
- int length;
- public:
- CharSecret(const char * chars=""){
- for (int i=0;i<strlen(chars);i++)
- this->chars[i]=chars[i];
- this->length=strlen(chars);
- }
- int total() const {
- return length;
- }
- double simpleEntropy() const {
- double different=0.0;
- int count = 0;
- for (int i=0;i<length;i++){
- count = 0;
- for (int j=0;j<length;j++){
- if ((i!=j)&&(chars[i]==chars[j]))
- count++;
- }
- if (count==0)
- different+=1.0;
- }
- return different/total();
- }
- friend ostream &operator << (ostream &out, const CharSecret &cs){
- for (int i=0;i<cs.total();i++)
- out<<cs.chars[i];
- out<<" Simple entropy: " << cs.simpleEntropy() << " Total: "<<cs.total();
- return out;
- }
- };
- bool operator ==(const Secret &s1, const Secret &s2){
- return s1.total()==s2.total()&&s1.simpleEntropy()==s2.simpleEntropy();
- }
- bool operator !=(const Secret &s1, const Secret &s2){
- return s1.total()!=s2.total() || s1.simpleEntropy()!=s2.simpleEntropy();
- }
- void process (Secret ** secrets, int n){
- double max = -1;
- int idx = -1;
- for (int i=0;i<n;i++){
- if (secrets[i]->simpleEntropy()>max){
- max=secrets[i]->simpleEntropy();
- idx=i;
- }
- }
- DigitSecret * ds = dynamic_cast<DigitSecret *>(secrets[idx]);
- if (ds!=0){
- cout<<*ds;
- }
- CharSecret * cs = dynamic_cast<CharSecret *>(secrets[idx]);
- if (cs!=0){
- cout<<*cs;
- }
- }
- void printAll(Secret ** secrets, int n){
- for (int i=0;i<n;i++){
- DigitSecret * ds = dynamic_cast<DigitSecret *>(secrets[i]);
- if (ds!=0){
- cout<<*ds<<endl;
- }
- CharSecret * cs = dynamic_cast<CharSecret *>(secrets[i]);
- if (cs!=0){
- cout<<*cs<<endl;
- }
- }
- }
- int main() {
- int n;
- cin >> n;
- if(n == 0) {
- cout << "Constructors" << endl;
- int numbers [] = {1,2,3,4,5};
- DigitSecret ds(numbers,5);
- CharSecret cs("abcabc");
- cout << "OK" << endl;
- } else if(n == 1) {
- cout << "operator <<" << endl;
- int numbers [] = {1,2,3,4,5};
- DigitSecret ds(numbers,5);
- CharSecret cs("abcabc");
- cout << ds << endl;
- cout << cs << endl;
- } else if(n == 2) {
- cout << "== and !=" << endl;
- int numbers [] = {1,2,3,4,5};
- DigitSecret ds(numbers,5);
- CharSecret cs("abcabc");
- CharSecret css("abcabc");
- cout << (ds == cs) << endl;
- cout << (cs != ds) << endl;
- cout << (cs == css) << endl;
- cout << (cs != css) << endl;
- } else if(n == 3) {
- cout << "Secret processor" << endl;
- int numbers1 [] = {1,2,3,4,5,6,4,3,2,1,1,2,3,4,5};
- DigitSecret ds1(numbers1,15);
- int numbers2 [] = {1,2,3,4,5,0,0,0,5,5,4,4,3,3,2};
- DigitSecret ds2(numbers2,15);
- int numbers3 [] = {1,0,9,4,3,8,4,0,9,3,1,4,3,2,1,4,4,3,7,2};
- DigitSecret ds3(numbers3,20);
- CharSecret cs1("fhdaieowujkfjdsafjdsakjhueiqoyroq");
- CharSecret cs2("nvzcfsadrqipqhfjdfncxoqw");
- CharSecret cs3("uyoihfdsanmqeqrzvdhfeqyrq");
- Secret** s = new Secret*[6];
- s[0] = &ds1;
- s[1] = &ds2;
- s[2] = &ds3;
- s[3] = &cs1;
- s[4] = &cs2;
- s[5] = &cs3;
- process(s,6);
- delete [] s;
- }
- else if (n==4){
- cout << "Print all secrets" << endl;
- int numbers1 [] = {1,2,3,4,5,5,4,3,2,1,1,2,3,4,5};
- DigitSecret ds1(numbers1,15);
- int numbers2 [] = {1,2,3,4,5,0,0,0,5,5,4,4,3,3,2};
- DigitSecret ds2(numbers2,15);
- int numbers3 [] = {1,0,9,4,3,8,4,0,9,3,1,4,3,2,1,4,4,3,7,2};
- DigitSecret ds3(numbers3,20);
- CharSecret cs1("fhdaieowujkfjdsafjdsakjhueiqoyroq");
- CharSecret cs2("nvzcfsadrqipqhfjdfncxoqw");
- CharSecret cs3("uyoihfdsanmqeqrzvdhfeqyrq");
- Secret** s = new Secret*[6];
- s[0] = &ds1;
- s[1] = &ds2;
- s[2] = &ds3;
- s[3] = &cs1;
- s[4] = &cs2;
- s[5] = &cs3;
- printAll(s,6);
- delete [] s;
- }
- return 0;
- }
- ====================================================== Shapes =================================================================
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Shape{
- protected:
- int a;
- public:
- Shape(){}
- Shape(int a){
- this->a=a;
- }
- virtual double plostina() {}
- virtual void pecati() {}
- virtual int getType() {}
- };
- class Square : public Shape{
- public:
- Square(int a) : Shape(a){}
- double plostina(){
- return (double)a*a;
- }
- void pecati(){
- cout<<"Kvadrat so plostina = "<<plostina()<<endl;
- }
- int getType(){
- return 1;
- }
- };
- class Krug : public Shape{
- public:
- Krug(int a) : Shape(a){}
- double plostina(){
- return 3.14 * a * a;
- }
- void pecati(){
- cout<<"Krug so plostina = "<<plostina()<<endl;
- }
- int getType(){
- return 2;
- }
- };
- class Triagolnik : public Shape{
- public:
- Triagolnik(int a) : Shape(a){}
- double plostina(){
- return (sqrt(3)/4) * (double)a * a;
- }
- void pecati(){
- cout<<"Triagolnik so plostina = "<<plostina()<<endl;
- }
- int getType(){
- return 3;
- }
- };
- void checkNumTypes(Shape** niza, int n){
- int square=0, krug=0, triagolnik=0;
- for(int i=0;i<n;++i){
- if(niza[i]->getType()==1)
- square++;
- if(niza[i]->getType()==2)
- krug++;
- if(niza[i]->getType()==3)
- triagolnik++;
- }
- cout<<"Broj na kvadrati vo nizata = "<<square<<endl;
- cout<<"Broj na krugovi vo nizata = "<<krug<<endl;
- cout<<"Broj na triagolnici vo nizata = "<<triagolnik<<endl;
- }
- int main(){
- int n;
- cin >> n;
- Shape **niza;
- niza=new Shape*[50];
- int classType;
- int side;
- for(int i = 0; i < n; ++i){
- cin >> classType;
- cin >> side;
- if(classType==1)
- niza[i]=new Square(side);
- if(classType==2)
- niza[i]=new Krug(side);
- if(classType==3)
- niza[i]=new Triagolnik(side);
- }
- for(int i = 0; i < n; ++i){
- niza[i]->pecati();
- }
- checkNumTypes(niza, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement