
ertyiopos
Members-
ΜΗΝΥΜΑΤΑ FORUM
16 -
ΜΕΛΟΣ
-
ΤΕΛ. ΕΠΙΣΚΕΨΗ
Τύπος περιεχομένου
Forums
Ειδήσεις
Reviews
Gallery
Αγγελίες
Gadgets
Οτιδήποτε δημοσιεύεται από ertyiopos
-
λοιπόν έκανα το εξής: void Ball::hit() { if( hidden == true ) //Elenxei an i mpala einai idi eksafanismeni { cout << "You cannot hit a hidden ball." << endl; return; } srand(time(NULL)); int i = 1 + rand()%5; //Dialegei tixaia ena ari8mo gia tin pi8anotita i mpala //Na eksafanistei.Dialegei apo to 1-5 gia na min simvenei polli sixna if( i == 5 ) //an dialextike to 5 tote i mpala eksafanizete allios paremenei faneri! { hidden = true; cout << "Ball disappeared!" << endl; return; } cout << "Tsaf!" << endl; //Xtipima tis mpalas(simvenei gia ka8e mpala pou xtipiete) if( durability == 0 ) //An antoxi = 0 tote eina f8armeni cout << "The ball is about to break!" << endl; else if( durability < 0 ) //An ksanaxtipi8ei enw itan f8armeni(antoxi = 0) tote cout << "Plof!" << endl; //8a paei -1 ara plof! } void Basket::hit() { cout << "Basket ball is going to be hit!" << endl; durability--; } kai twra pia stin main kalo prwta: ball1->hit(); ball1->Ball::hit(); kai leitourgei opws to proigoumeno! einai apodekto omws?
-
Τελικά αυτό που πόσταρα μπορώ να το κάνω??
-
Άμα βάλω τον κώδικα που είναι ίδιος στην virtual συνάρτηση που είναι δηλωμένη στηn κλάση ball και στις άλλες απλώς να βάλω αυτό που αλλάζει τότε όταν καλώ την hit για την basket θα καλείται και η hit της κλάσης ball?? βασικά παρατήρησα οτι μπορώ να κάνω αυτό: #include <iostream> #include <cstdlib> using namespace std; class Ball { public: virtual void hit() { cout << "Program "; } }; class Basket:public Ball { public: void hit() { Ball b; b.hit(); cout << "Worked!" << endl; } }; int main(int argc, char *argv[]) { Basket b; Tennis t; Ping_pong p; Ball *ball1 = &b; ball1->hit(); return 0; } λειτουργεί αλλά είναι σωστό?? Το έκανα αυτό για να βάλω οτι είναι ίδιο στην συναρτήση στην κλάση ball και στις αλλές συναρτήσεις να έχει μόνο αυτό που αλλάζει.
-
Καλησπέρα έχω μια εργάσια στην c++ η οποία θέλει να εφαρμόσουμε ένα απλό προγραμματάκι απλώς θέλει να δείξουμε οτι μπορούμε να χειριστόυμε καλά τις virtual συναρτήσεις! Το πρόβλημα είναι το εξής: έχω κάνει το πρόγραμμα το οποίο είναι να φτιάξεις 3 μπάλες basket,tennis,ping-pong και ο παίχτης να επιλέγει μια μπάλα τυχαία κάθε φόρα και σε κάθε χτύπημα η μπάλα να χάνει αντοχή ή να εξαφανίζεται (μαλλον αυτό συμβαίνει αν αυτός που την χτυπάει είναι άμπαλος! ) οι υπόλυπες να κάνουν rest και αύτο να γίνεται για Κ κύκλους που δίνονται απο την γραμμη εντολής. Το πρόβλημα είναι ότι εγώ είχα σταμάτησει να παρακολόυθω λόγω άλλων μαθημάτων το μάθημα και έτσι έμαθα για τις virtual συναρτήσεις απο ένα tutorial Buckys C++ Programming Tutorials - 56 - virtual Functions - YouTube ...το θέμα είναι οτι έτσι όπως έχω υλοποιήσει το πρόγραμμα δεν μ φαίνετε σωστό γιατί το τι κάνει κάθε συνάρτηση το έχω υλοποιήση σε κάθε κλάση ξεχώριστα και δεν καταλαβαίνω γιατι πρέπει να χρησημοποιήσω virtual...θέλώ να μ πείτε αν στο πρόγραμμα υλοποιώ σωστά τις virtual συνάρτησεις και αν χρειάζομαι να προσθέσω τπτ άλλο ευχαριστώ εκ των προτέρων! το πρόγραμμα είναι το εξής: //class.h class Ball { protected: int durability; int value; //arxiki timi! bool hidden; public: virtual void hit(); virtual void Set(int); virtual void rest(); }; class Basket:public Ball { public: void hit(); void Set(int); void rest(); }; class Tennis:public Ball { public: void hit(); void Set(int); void rest(); }; class Ping_pong:public Ball { public: void hit(); void Set(int); void rest(); }; //class.cpp #include <iostream> #include <cstdlib> #include <ctime> #include "class3.h" using namespace std; void Ball::hit() {} void Ball::Set(int) {} void Ball::rest() {} void Basket::Set(int a) { durability = a; value = a; hidden = false; } void Tennis::Set(int a) { durability = a; value = a; hidden = false; } void Ping_pong::Set(int a) { durability = a; value = a; hidden = false; } void Basket::hit() { cout << "Basket ball is going to be hit!" << endl; if( hidden == true ) { cout << "You cannot hit a hidden ball" << endl; return; } srand(time(0)); int i = 1 + rand()%5; if( i == 5 ) { hidden = true; cout << "Ball disappeared!" << endl; return; } durability--; cout << "Tsaf!" << endl; if( durability == 0 ) cout << "The ball is about to break!" << endl; else if( durability < 0 ) cout << "Plof!" << endl; } void Tennis::hit() { cout << "Tennis ball is going to be hit!" << endl; if( hidden == true ) { cout << "You cannot hit a hidden ball" << endl; return; } srand(time(0)); int i = 1 + rand()%5; if( i == 5 ) { hidden = true; cout << "Ball disappeared!" << endl; return; } durability -= 5; cout << "Tsaf!" << endl; if( durability == 0 ) cout << "The ball is about to break!" << endl; else if( durability < 0 ) cout << "Plof!" << endl; } void Ping_pong::hit() { cout << "Ping-pong ball is going to be hit!" << endl; if( hidden == true ) { cout << "You cannot hit a hidden ball" << endl; return; } srand(time(0)); int i = 1 + rand()%5; if( i == 5 ) { hidden = true; cout << "Ball disappeared!" << endl; return; } durability--; cout << "Tsaf!" << endl; if( durability == 0 ) cout << "The ball is about to break!" << endl; else if( durability < 0 ) cout << "Plof!" << endl; } void Basket::rest() { cout << "Basket is " << durability << endl; } void Tennis::rest() { durability += 3; if( durability > value ) durability = value; cout << "Tennis is " << durability << endl; } void Ping_pong::rest() { durability++; if( durability > value ) durability = value; cout << "Ping_pong is " << durability << endl; } //main.cpp #include <iostream> #include <cstdlib> #include <ctime> #include "class3.h" using namespace std; int main(int argc, char *argv[]) { int L1,L2,L3,i,K,option; Basket b; Tennis t; Ping_pong p; Ball *ball1 = &b; Ball *ball2 = &t; Ball *ball3 = &p; L1 = atoi(argv[1]); L2 = atoi(argv[2]); L3 = atoi(argv[3]); K = atoi(argv[4]); ball1->Set(L1); ball2->Set(L2); ball3->Set(L3); srand(time(0)); for( i=0; i < K; i++ ) { option = 1 + rand()%3; switch(option) { case 1: ball1->hit(); ball2->rest(); ball3->rest(); break; case 2: ball2->hit(); ball1->rest(); ball3->rest(); break; case 3: ball3->hit(); ball1->rest(); ball2->rest(); break; } } return 0; }
-
ωραιιιιος!! Αυτό που θέλω να κάνω είναι οι μουσικές καρέκλες. Με την ht αύτο που θέλω είναι π.χ μπένουν μέσα οι αριθμοί 1,2,3,4 και μέτα απο ένα hit να γίνει 2,3,4,1 δηλαδή να μεταφερθούν όλα μια φορά προς τα αρίστερα φαινότανε να δουλεύει πάντως η συνάρτηση hit του bluezy. Γιατί αυτές τις 2 γραμμές κώδικα τις λές υπόπτες? Σε εύχαριστω πολύ για την βοήθεια βέβεα την τετάρτη επρεπε να παραδώσω την εργασία όποτε την έδωσα με ότι είχα κάνει ελπίζω απλώς να είμαι τυχερός και να μου δουλέψει για να δούν οτι δεν τρέχει κάτι :D!!
-
είναι λιγο μεγάλο...λοιπόν αυτή είναι η main2.cpp: #include <iostream> #include <cstdlib> #include <ctime> #include "class2.h" using namespace std; int main( int argc, char* argv[] ) { int K,i,times=0,N; N = atoi(argv[1]); node players; srand(time(0)); for( i = 0; i < N; i++ ) players.create_chain(); cout << "The game begins!!" << endl; while( 1 ) { if( players.size == 1 ) break; K = 1 + rand()%10; while( times < K ) { cout << "Drum hit!" << endl; players.hit(); times++; } times = 0; cout << "Drum stoped!" << endl; players.stop_tune(); cout << "The current players is:" << endl; players.print_chain(); } if( players.game_over() == true ) players.winner(); return 0; } αυτή είναι η class2.cpp: #include <iostream> #include <cstdlib> #include <ctime> #include "class2.h" using namespace std; node::node():size(0),head(0),tail(0) { } void node::create_chain() { node *temp = new node; cout << "Give the player id: "; cin >> temp -> id; temp -> next = 0; if ( head != NULL && tail != NULL ) { tail->next = temp; tail = temp; } else { head = temp; tail = temp; } size++; } void node::print_chain() { node *h = head; while ( h != NULL ) { cout << h->id << endl; h = h->next; } } void node::hit() { int number = head->id; head->id = head->next->id; node *current = head->next; while(current!=tail) { current->id = current->next->id; current = current->next; } current->id = number; } void node::stop_tune() { node *h = head; node *temp = head; int i = 1; srand(time(0)); int selected = 1 + rand()%size;//take a random position while ( h != NULL ) { if ( i == selected )//if we reach the position tha was selected { break; } else if ( h == temp ) { h = h->next; } else { temp = temp->next; h = temp->next; } i++;//increase position } if ( h == NULL ) { cout << "Not found" << endl; return; } else { temp -> next = temp -> next -> next; cout << "The player with id " << h->id << " in position " << i << " deleted!" << endl; delete h; } size--; cout << "The current size of chain is: " << size << endl; } int node::game_over() { node *h = head; int i = 0; while( h != NULL ) { h = h->next; i++; } if( i == 1) { cout << "Game Over!" << endl; return true; } else { cout << "Game is not over!" << endl; return false; } } void node::winner() { cout << "The winner is the player with id " << head->id << "!" << endl; } και τέλος η class2.h: class node{ public: node(); int size; void create_chain(); void print_chain(); void hit(); void stop_tune(); int game_over(); void winner(); private: int id; node *next; node *head; node *tail; };
-
έχω βρεί οτι γίνετε στην παρακάω συναρτηση γιατί όταν δεν την χρησιμοποώστο πρόγραμμα δεν ου εμφάνιζει αυτο το μύνημα: void node::resume() { node *h = head; int number = head->id; head->id = head->next->id; node *current = head->next; while(current!=tail){ current->id = current->next->id; current = current->next; } current->id = number; } δε βρήσκω καποιό λάθος όμως για κοιτάξτε την και εσείς! εντωμεταξύ την πόσταρε ο bluezy
-
Νομίζω οτι κατάλαβα. Μηπώς μπόρεις να μου απαντήσεις τι είναι αυτό που πέρνω απο τον compiler σαν λάθος? *** glibc detected *** a.out: double free or corruption (fasttop): 0x09206008 *** ======= Backtrace: ========= /lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xac9ee2] /usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0x8ee51f] a.out[0x8048d0a] a.out[0x804897c] /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xa6d4d3] a.out[0x8048801] ======= Memory map: ======== 0059b000-005b7000 r-xp 00000000 08:01 1067252 /lib/i386-linux-gnu/libgcc_s.so.1 005b7000-005b8000 r--p 0001b000 08:01 1067252 /lib/i386-linux-gnu/libgcc_s.so.1 005b8000-005b9000 rw-p 0001c000 08:01 1067252 /lib/i386-linux-gnu/libgcc_s.so.1 0070f000-00710000 r-xp 00000000 00:00 0 [vdso] 00845000-0091d000 r-xp 00000000 08:01 85360 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16 0091d000-0091e000 ---p 000d8000 08:01 85360 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16 0091e000-00922000 r--p 000d8000 08:01 85360 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16 00922000-00923000 rw-p 000dc000 08:01 85360 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16 00923000-0092a000 rw-p 00000000 00:00 0 00a54000-00bf8000 r-xp 00000000 08:01 1067462 /lib/i386-linux-gnu/libc-2.15.so 00bf8000-00bfa000 r--p 001a4000 08:01 1067462 /lib/i386-linux-gnu/libc-2.15.so 00bfa000-00bfb000 rw-p 001a6000 08:01 1067462 /lib/i386-linux-gnu/libc-2.15.so 00bfb000-00bfe000 rw-p 00000000 00:00 0 00e26000-00e46000 r-xp 00000000 08:01 1067013 /lib/i386-linux-gnu/ld-2.15.so 00e46000-00e47000 r--p 0001f000 08:01 1067013 /lib/i386-linux-gnu/ld-2.15.so 00e47000-00e48000 rw-p 00020000 08:01 1067013 /lib/i386-linux-gnu/ld-2.15.so 00eea000-00f14000 r-xp 00000000 08:01 1067299 /lib/i386-linux-gnu/libm-2.15.so 00f14000-00f15000 r--p 00029000 08:01 1067299 /lib/i386-linux-gnu/libm-2.15.so 00f15000-00f16000 rw-p 0002a000 08:01 1067299 /lib/i386-linux-gnu/libm-2.15.so 08048000-0804a000 r-xp 00000000 00:15 4164234 /home/users1/std10218/a.out 0804a000-0804b000 r--p 00001000 00:15 4164234 /home/users1/std10218/a.out 0804b000-0804c000 rw-p 00002000 00:15 4164234 /home/users1/std10218/a.out 09206000-09227000 rw-p 00000000 00:00 0 [heap] b770f000-b7712000 rw-p 00000000 00:00 0 b7721000-b7726000 rw-p 00000000 00:00 0 bfd8b000-bfdac000 rw-p 00000000 00:00 0 [stack] Abort (core dumped) μου το βγάζει κάποιες φορές και κάποιες άλλες μου εκτέλει κανονικά το πρόγραμμα..εχω μπερδευτεί.
-
έτσι των δήλωσα τον constructor class node { public: node(int); ~node(); } και στο .cpp αρχείο είναι node::node(int x):size(x) { head = 0; tail = 0; }
-
όχι τον κράτησα. Πρέπει μέσα στα άγκιστρα να μην υπάρχει τπτ?
-
Καλησπέρα και πάλι μου δημιουργήθηκε το ακόλουθο πρόβλημα θέλω να φτίαξω τον δημιουργό της κλάσεις μου να παιρνει μια μεταβλητή π.χ: node::node( int x ): size(x) { } αυτό είναι στην main: N = atoi(argv[1]); node players(N): νομίζω σωστά τα έχω αλλά ο compiler μου βγάζει πρόβλημα στην συνάρτηση insert και λέει no matching function call to node::node() η insert είναι αυτή: void node::insert() { node *temp = new node; cout << "Give the player id: "; cin >> temp -> id; temp -> next = 0; if ( head != NULL && tail != NULL ) { tail->next = temp; tail = temp; } else { head = temp; tail = temp; } } τι πρέπει να φτιάξω???
-
ευχαριστώ πολύ!!
-
Ναι το έκανα έτσι και δούλεψε ευχαριστώ πολύ!! Όσο για το αλλό που λέγαμε δηλαδή να φτιαξω μια συνάρτηση που να μεταφέρει όλα κατα 1 πίσω δήλαδη να βάζω 1,2,3,4 και να μου βγάζει 2,3,4,1 σκέφτηκα το εξής πρόγραμμα void node::resume() { node *h = head, *current = head, *temp = NULL; while ( current != NULL ) { h = h->next; next = h->next; prev = temp; temp = current; current = current->next; } } το h το έχω βάλει να είναι ίσο με τον επομενό του γιατί σκέφτηκα ότι θα πρεπεί το next να είναι ίσο με τον επόμενο του επόμενου του αφόυ αμα μπεί για την πρώτη θέση ο επόμενος της θα πρέπει να είναι το 3 αφόυ στη θέση πρώτη τώρα πια είναι το 2. Μετά ο προηγούμενος της πρώτης θέσης είναι πάντα ίσος με NULL και ετσί έφτιαξα μια μεταβλητή temp που στην αρχή θα είναι ΝULL αλλά στη επομενη επανάληψη θα αλλάζει.Τέλος το current το κάνω ίσο με το επομενό του αφου η πρώτη θέση θα πρέπει να έχει το 2. Δεν μου λειτουργεί καθόλου όμως και δεν μπορώ να σκεφτώ καποιό καλύτερο τρόπο.Μπορείτε να μου γράψετε κάποια συνάρτηση που να κάνει αυτό που θέλω?
-
Προσπάθησα να την κάνω διπλά συνδεδεμένη και έκανα class node { private: int id; node *next; node *prev; node *head; node *tail; public: node(); void insert(); void print(); }; και επείδη δεν ξέρω πώς ακριβώς είναι η διπλά συνδεδεμένη υπέθεσα οτι το insert θα είναι κάπως έτσι: void node::insert() { node * temp = new node; cout << "Value: " ; cin >> temp->id; temp -> next = 0; temp -> prev = 0; if( head!= NULL && tail!= NULL ) { tail -> next = temp; tail -> prev = tail; tail = temp; } else { head = temp; tail = temp; } } το temp θα μπεί στον επόμενο του ήδη υπάρχον tail άρα ο προήγουμενος του temp θα είναι το υπάρχον tail και μετά το καινουργιο tail θα είναι το temp (έτσι το σκέφτηκα ελπίζω να είναι σωστό) αλλά όταν πάω να εκτυπώσω απ πίσω πρως τα μπρος χρησημοποιώντας τον ακόλουθο κώδικα void node::print(){ node *h = tail; while (h!= NULL){ cout << h->id << endl; h = h->prev; } } βάζοντας σαν values π.χ 1,2,3 μου εμφανίζει μόνο το 3 και σταματάει γιατί όμως? Αφού το καινούργιο h θα ειναι ο προηγούμενος δεν θα έπρεπε να βγάλει το 2? ( νομίζω οτι στο Insert έχω κάνει λάθος και δεν μπαίνει τπτ στο prev ) χρειάζομαι τα φώτα σας plz
-
δηλαδή να φτιάξω μια κλάση που να έχει μια struct μέσα? Έτσι όπως το έχω φτιάξει (χωρι struct) το ίδιο δεν είναι? αφου κάνει οτι κάνει και μια λίστα..
-
Καλησπέρα σας! Έχω φτιάξει το ακόλουθο πρόγραμμα (λίστα) σε c++ #include <iostream> using namespace std; class node{ public: node(); void insert(); void print(); private: int value; int size=0; node *next; node *head; node *tail; }; node::node(){ head = 0, tail = 0; } int main(){ node ll; while (1){ cout << "1. Insert\n2. Print All "; int opt; cin >> opt; switch(opt){ case 1: ll.insert(); break; case 2: ll.print(); break; }//end switch }//end while return 0; }//end main void node::insert(){ node *temp = new node; cout << "Value: "; cin >> temp->value; temp -> next = 0; //insert at tail. if (head != NULL && tail != NULL)\ { tail->next = temp; tail = temp; } else { head = temp; tail = temp; } size++; } void node::print(){ node *h = head; while (h!= NULL){ cout << h->value << endl; h = h->next; } cout << "Size is: " << size << endl; } (Ελπίζω να έφτιαξα καλά την στοίχιση του προγράμματος ) Θέλω να φτιάξω μια συνάρτηση που θα μεταφέρει όλα τα values της λίστας μια θέση προς τα πίσω. π.χ: πείτε οτι δίνω στην αρχή 1234 θελώ μετά την χρησιμοποίηση τις συνάρτησης να γίνετε 234. Όποια λύση ευπρόσδεκτη ευχαριστώ εκ των προτέρων!