Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<cstring>
- using namespace std;
- class SMS
- {
- protected:
- int cena;
- char *sodrzina;
- char broj[20];
- public:
- SMS(char *Broj,char *Sodrzina, int Cena)
- {// sms[i] = new RegularSMS(tel, msg, cena, roam);
- sodrzina= new char[strlen(Sodrzina)+1];
- strcpy(sodrzina,Sodrzina);
- cena=Cena;
- strcpy(broj,Broj);
- }
- SMS(){};
- virtual ~SMS()
- {
- delete [] sodrzina;
- }
- virtual float SMS_cena()=0;
- friend ostream& operator<< (ostream &out, SMS &c)
- {
- return out<<c.broj<<": "<<c.sodrzina<<" - "<<c.SMS_cena()<<endl;
- }
- bool operator < (SMS &c)
- {
- return this->SMS_cena()<c.SMS_cena();
- }
- };
- class RegularSMS : public SMS
- {
- private:
- bool roaming;
- public:
- RegularSMS(char *Broj,char *Sodrzina, int Cena,bool Roaming) : SMS(Broj,Sodrzina,Cena)
- {
- sodrzina= new char[strlen(Sodrzina)+1];
- strcpy(sodrzina,Sodrzina);
- cena=Cena;
- strcpy(broj,Broj);
- roaming=Roaming;
- }
- float SMS_cena()
- {
- int dolzina=strlen(sodrzina);
- int brojPoraki=1;
- if(dolzina>16)
- {
- brojPoraki=dolzina/16;
- }
- else
- brojPoraki=1;
- if(roaming==true)
- {
- return brojPoraki*cena + (brojPoraki*cena) * 1.2;
- }
- else
- return brojPoraki*cena + (brojPoraki*cena) * 0.18;
- }
- };
- class SpecialSMS : public SMS
- {
- private:
- bool humanitarni;
- public:
- SpecialSMS(char *Broj,char *Sodrzina, int Cena,bool Humanitarni) : SMS(Broj,Sodrzina,Cena)
- {
- sodrzina= new char[strlen(Sodrzina)+1];
- strcpy(sodrzina,Sodrzina);
- cena=Cena;
- strcpy(broj,Broj);
- humanitarni=Humanitarni;
- }
- float SMS_cena()
- {
- if(humanitarni == true)
- {
- return cena;
- }
- else
- {
- return (float)cena+cena*0.8;
- }
- }
- };
- void najskapaSMS(SMS** sms, int n)
- { float suma=0;
- int index=0;
- float max=-99;
- for(int i=0;i<n;i++)
- {
- suma+=sms[i]->SMS_cena();
- if(max<sms[i]->SMS_cena())
- {
- max=sms[i]->SMS_cena();
- index=i;
- }
- }
- cout<< "Najskapa poraka e :" << suma << endl;;
- cout<< *sms[index];
- }
- int main(){
- char tel[20], msg[1000];
- int cena;
- float price;
- bool roam, hum;
- SMS **sms;
- int n;
- int tip;
- int testCase;
- cin >> testCase;
- if (testCase == 1){
- cout << "====== Testing RegularSMS class ======" << endl;
- cin >> n;
- sms = new SMS *[n];
- for (int i = 0; i < n; i++){
- cin >> tel;
- cin.get();
- cin.getline(msg, 1000);
- cin >> cena;
- cin >> roam;
- cout << "CONSTRUCTOR" << endl;
- sms[i] = new RegularSMS(tel, msg, cena, roam);
- cout << "OPERATOR <<" << endl;
- cout << *sms[i];
- }
- cout << "OPERATOR <" << endl;
- cout << "Rezultat od sporedbata e: " << endl;
- if (*sms[0] < *sms[1])
- cout << *sms[0];
- else
- cout << *sms[1];
- }
- if (testCase == 2){
- cout << "====== Testing SpecialSMS class ======" << endl;
- cin >> n;
- sms = new SMS *[n];
- for (int i = 0; i < n; i++){
- cin >> tel;
- cin.get();
- cin.getline(msg, 1000);
- cin >> cena;
- cin >> hum;
- cout << "CONSTRUCTOR" << endl;
- sms[i] = new SpecialSMS(tel, msg, cena, hum);
- cout << "OPERATOR <<" << endl;
- cout << *sms[i];
- }
- cout << "OPERATOR <" << endl;
- cout << "Rezultat od sporedbata e: " << endl;
- if (*sms[0] < *sms[1])
- cout << *sms[0];
- else
- cout << *sms[1];
- }
- if (testCase == 3){
- cout << "====== Testing method najskapaSMS() ======" << endl;
- cin >> n;
- sms = new SMS *[n];
- for (int i = 0; i<n; i++){
- cin >> tip;
- cin >> tel;
- cin.get();
- cin.getline(msg, 1000);
- cin >> cena;
- if (tip == 1) {
- cin >> roam;
- sms[i] = new RegularSMS(tel, msg, cena, roam);
- }
- else {
- cin >> hum;
- sms[i] = new SpecialSMS(tel, msg, cena, hum);
- }
- }
- najskapaSMS(sms, n);
- }
- for (int i = 0; i<n; i++) delete sms[i];
- delete[] sms;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement