Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- // vashiot kod ovde
- class IceCream{
- private:
- char* ime;
- char sostav[100];
- float cena;
- int popust;
- public:
- IceCream(){
- popust = 0;
- ime = new char[0];
- }
- IceCream(char *name, char *ingr, int price){
- ime = new char[strlen(name)+1];
- strcpy(ime, name);
- strcpy(sostav, ingr);
- cena = price;
- popust = 0;
- }
- IceCream(char *name){
- ime = new char[strlen(name)+1];
- strcpy(ime, name);
- }
- IceCream(const IceCream &ic){
- ime = new char[strlen(ic.ime)+1];
- strcpy(ime, ic.ime);
- strcpy(sostav, ic.sostav);
- cena = ic.cena;
- popust = ic.popust;
- }
- IceCream& operator=(const IceCream &ic){
- if(this!=&ic){
- delete [] ime;
- ime = new char[strlen(ic.ime)+1];
- strcpy(ime, ic.ime);
- strcpy(sostav, ic.sostav);
- cena = ic.cena;
- popust = ic.popust;
- }
- return *this;
- }
- IceCream& operator++(){
- popust+=5;
- return *this;
- }
- IceCream& operator+(const char *niza){
- char* tmp;
- int b = strlen(this->ime)+strlen(niza)+4;
- tmp = new char[b];
- for(int i=0; i<strlen(this->ime); ++i){
- tmp[i] = this->ime[i];
- }
- delete [] ime;
- strcat(tmp, " + ");
- strncat(tmp, niza, strlen(niza));
- tmp[b] = '\0';
- ime = new char[b+1];
- strcpy(ime, tmp);
- //cout<<ime<<endl;
- delete [] tmp;
- this->cena+=10;
- return *this;
- }
- void setDiscount(int discount){
- if(discount>=0&&discount<=100){
- this->popust = discount;
- }
- }
- void setName(char *name){
- delete [] ime;
- ime = new char[strlen(name)+1];
- strcpy(ime, name);
- }
- float findPopust() const{
- return this->cena-(float)((this->cena/100)*(popust));
- }
- friend ostream& operator<<(ostream &out, const IceCream &ic);
- ~IceCream(){
- delete [] ime;
- }
- };
- ostream& operator<<(ostream &out, const IceCream &ic){
- out<<ic.ime<<": "<<ic.sostav<<" "<<ic.cena;
- if(ic.popust!=0&&ic.popust>0){
- out<<" ("<<ic.findPopust()<<")";
- }
- else
- cout<<" ";
- return out;
- }
- class IceCreamShop{
- private:
- char prodavnica[50];
- IceCream* ic;
- int brojSladoledi;
- public:
- IceCreamShop(char *name){
- strcpy(prodavnica, name);
- brojSladoledi = 0;
- ic = new IceCream[0];
- }
- IceCreamShop(const char* name, const char* ingr, int numberIce){
- strcpy(prodavnica, name);
- brojSladoledi = numberIce;
- ic = new IceCream[brojSladoledi];
- for(int i=0; i<brojSladoledi; i++){
- }
- }
- IceCreamShop(const IceCreamShop &ics){
- strcpy(prodavnica, ics.prodavnica);
- this->brojSladoledi = ics.brojSladoledi;
- ic = new IceCream[this->brojSladoledi];
- for(int i=0; i<brojSladoledi; ++i){
- this->ic[i] = ics.ic[i];
- }
- }
- IceCreamShop& operator=(const IceCreamShop &ics){
- if(this!=&ics){
- delete [] this->ic;
- strcpy(prodavnica, ics.prodavnica);
- this->brojSladoledi = ics.brojSladoledi;
- ic = new IceCream[this->brojSladoledi];
- for(int i=0; i<brojSladoledi; ++i)
- this->ic[i] = ics.ic[i];
- }
- return *this;
- }
- IceCreamShop& operator+=(const IceCream &icecream){
- IceCream *tmp;
- tmp = new IceCream[this->brojSladoledi];
- for(int i=0; i<brojSladoledi; ++i){
- tmp[i] = this->ic[i];
- }
- delete [] ic;
- ic = new IceCream[brojSladoledi+1];
- for(int i=0; i<brojSladoledi; ++i){
- this->ic[i] = tmp[i];
- }
- brojSladoledi+=1;
- ic[brojSladoledi-1] = icecream;
- return *this;
- }
- friend ostream& operator<<(ostream &out, const IceCreamShop &ics);
- ~IceCreamShop(){
- delete [] ic;
- }
- };
- ostream& operator<<(ostream &out, const IceCreamShop &ics){
- out<<ics.prodavnica<<endl;
- for(int i=0; i<ics.brojSladoledi; ++i){
- out<<ics.ic[i]<<endl;
- }
- return out;
- }
- // zabraneto e menuvanje na main funkcijata, osven dodavanje na kodot za spravuvanje so iskluchokot na soodvetno mesto
- int main() {
- char name[100];
- char ingr[100];
- float price;
- int discount;
- int testCase;
- cin >> testCase;
- cin.get();
- if(testCase == 1) {
- cout << "====== TESTING IceCream CLASS ======" << endl;
- cin.getline(name,100);
- cin.getline(ingr,100);
- cin >> price;
- cin >> discount;
- cout << "CONSTRUCTOR" << endl;
- IceCream ic1(name, ingr, price); //done
- ic1.setDiscount(discount); //done
- cin.get();
- cin.getline(name,100);
- cin.getline(ingr,100);
- cin >> price;
- cin >> discount;
- IceCream ic2(name, ingr, price); //done
- ic2.setDiscount(discount); //done
- cout << "OPERATOR <<" << endl;
- cout << ic1 << endl;
- cout << ic2 << endl;
- cout << "OPERATOR ++" << endl;
- ++ic1;
- cout << ic1 << endl;
- cout << "OPERATOR +" << endl;
- IceCream ic3 = ic2 + "chocolate";
- cout << ic3 << endl;
- } else if(testCase == 2) {
- cout << "====== TESTING IceCream CONSTRUCTORS ======" << endl;
- cin.getline(name,100);
- cin.getline(ingr,100);
- cin >> price;
- cout << "CONSTRUCTOR" << endl;
- IceCream ic1(name, ingr, price);
- cout << ic1 << endl;
- cout << "COPY CONSTRUCTOR" << endl;
- IceCream ic2(ic1);
- cin.get();
- cin.getline(name,100);
- ic2.setName(name);
- cout << ic1 << endl;
- cout << ic2 << endl;
- cout << "OPERATOR =" << endl;
- ic1 = ic2;
- cin.getline(name,100);
- ic2.setName(name);
- cout << ic1 << endl;
- cout << ic2 << endl;
- cin >> discount;
- ic1.setDiscount(discount);
- } else if(testCase == 3) {
- cout << "====== TESTING IceCreamShop ======" << endl;
- char icsName[50];
- cin.getline(icsName,100);
- cout << "CONSTRUCTOR" << endl;
- IceCreamShop ics(icsName); //done
- int n;
- cin >> n;
- cout << "OPERATOR +=" << endl;
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,100);
- cin.getline(ingr,100);
- cin >> price;
- IceCream ic(name, ingr, price);
- ics += ic;
- }
- cout << ics;
- } else if(testCase == 4) {
- cout << "====== TESTING IceCreamShop CONSTRUCTORS ======" << endl;
- char icsName[50];
- cin.getline(icsName,100);
- IceCreamShop ics(icsName);
- int n;
- cin >> n;
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,100);
- cin.getline(ingr,100);
- cin >> price;
- IceCream ic(name, ingr, price);
- ics += ic;
- } //како да се направи овој конструктор?
- IceCream x("FINKI fruits", "strawberry ice cream, raspberry ice cream, blueberry ice cream", 60);
- IceCreamShop icp = ics;
- ics+=x;
- cout << ics << endl;
- cout << icp << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement