|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Greetings Ruby fans
![]() From a couple of months I've been learning C++ but after I found out about Ruby... it's just GREAT! Right now im at a beginner level, trying to convert some simplier code to Ruby (it's easier for me to learn this way) Can somebody please me a little? ;> Ofcourse a working example would be also nice ![]() BTW sorry for my English... //--------------------------------------------------------------------------- #include<math.h> //WHAT TO DO WITH THOSE #include<iostream> #include<cstdio> //--------------------------------------------------------------------------- #pragma argsused using namespace std; class Drink{ double price; char name[20]; int amount,bought; public: Drink(double cen, int how_much, char* nn) { price=cen; amount=how_much; strcpy(name,nn); bought=0; }; double change(double x){ amount--; //HOW TO DO THIS bought++; return x-price; }; int amounti() {return amount;}; double pricea(){return price;}; int buy(){return bought;}; bool canbuy(){ if (amount==0) return false; else return true; }; void namea(){cout<<name;}; friend class Automat; //WHAT TO DO WIGHT THIS }; class Automat{ private: double tab[2][8],u,w,x; public: Automat(double pie,double dwa,double jed,double piec,double dwad,double dzi) { tab[0][0]=pie; tab[0][1]=dwa; tab[0][2]=jed; tab[0][3]=piec; tab[0][4]=dwad; tab[0][5]=dzi; tab[1][0]=5; tab[1][1]=2; tab[1][2]=1; tab[1][3]=0.5; tab[1][4]=0.2; tab[1][5]=0.1; }; void Coinsy(Drink *n,double s,int g){ x=s-n[g].price; for (int i = 0; i <=5; i++) { w=tab[0][i]; for (double j =w; j>0; j--) { if ((x-tab[1][i])> -0.0001) { tab[0][i]--; x=x-tab[1][i]; cout<<tab[1][i]<<endl; } } } }; void write(){ cout<<"Coins 5zl: "<<tab[0][0]<<endl; cout<<"Coins 2zl: "<<tab[0][1]<<endl; cout<<"Coins 1zl: "<<tab[0][2]<<endl; cout<<"Coins 50gr: "<<tab[0][3]<<endl; cout<<"Coins 20gr: "<<tab[0][4]<<endl; cout<<"Coins 10gr: "<<tab[0][5]<<endl; }; void add(double x){ if (x==5) tab[0][0]++; if (x==2) tab[0][1]++; if (x==1) tab[0][2]++; if (x==0.5)tab[0][3]++; if (x==0.2)tab[0][4]++; if (x==0.1)tab[0][5]++; } }; int main() { int q=1,y,i,g; double m=1,s=0; Automat p(15,15,20,25,25,25); Drink Drinke[3]= { Drink(1.7,0,"Coca-Cola"), Drink(2,0,"Sprite"), Drink(2.1,20,"Fanta") }; while(q!=0) { cout<<"What would you like to do?"<<endl; cout<<"Kupic Drink: press 1"<<endl; cout<<"Print Coins report: press 2"<<endl; cout<<"Print bought Drinks report: press 3"<<endl; cout<<"Exit: press 0"<<endl; cin>>q; switch (q) //I'M NOT QUITE SURE HOW TO USE SWITCH IN RUBY { case 1: { system("cls"); for (int j = 0; j <3; j++) { Drinke[j].namea(); cout<<": "<<Drinke[j].pricea()<<" nacisnij "<<j<<endl; } cin>>g; if (Drinke[g].canbuy()) { y=1; s=0; while (y!=0){ cout<<"Coins Input: "<<s<<endl<<" What to do: \n\t\t Input Coins: 0 \n\t\t Buy Drink: 1 \n\t\t Cancel: 2"<<endl; cout<<"Your choice: "; cin>>y; if (y==0) { cout<<"Input Coins. "; cin>>m; s+=m; p.add(m); } if ((y==1)&&(Drinke[g].change(s)<0)) {cout<<"No cash. Input Coins."<<endl;} else { cout<<"change: "<<endl; p.Coinsy(Drinke,s,g); } if (y==2) { break; } } } else cout<<"No such drink."<<endl; } case 2: p.write(); case 3: { system("cls"); //CAN YOU CLEAR SCREEN IN RUBY? cout<<"Drinks bought: "<<endl; for (int i=0; i <3; i++) { Drinke[i].namea(); cout<<": "<<Drinke[i].buy()<<endl; } cout<<endl; break; } } } getchar(); getchar(); return 0; } //--------------------------------------------------------------------------- -- Posted via http://www.ruby-forum.com/. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Mon, Jun 9, 2008 at 2:40 PM, Jed Kowalski <satanowicz@gmail.com> wrote:
> Greetings Ruby fans ![]() > From a couple of months I've been learning C++ but after I found out > about Ruby... it's just GREAT! > Right now im at a beginner level, trying to convert some simplier code > to Ruby (it's easier for me to learn this way) > Can somebody please me a little? ;> > Ofcourse a working example would be also nice ![]() You might have better luck asking for with specific sections of code, rather than asking us to rewrite a whole program. Try doing a best-guest translation to Ruby on your own, and asking about the parts you have trouble with. -- Avdi Home: http://avdi.org Developer Blog: http://avdi.org/devblog/ Twitter: http://twitter.com/avdi Journal: http://avdi.livejournal.com |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Avdi Grimm wrote:
> You might have better luck asking for with specific sections of > code, rather than asking us to rewrite a whole program. Try doing a > best-guest translation to Ruby on your own, and asking about the parts > you have trouble with. ok so, here's the first problem: def initialize (cen, ile, nn) @price=cen @amount=ile @name=nn @bought=0 end def change(x) amount-- bought++ return x-price //WHATS WRONG HERE? end and I get: gut.rb:13: void value expression return x-cena ^ gut.rb:13: syntax error, unexpected tIDENTIFIER, expecting kEND return x-cena -- Posted via http://www.ruby-forum.com/. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Jed Kowalski wrote:
> def change(x) > amount-- > bought++ > return x-price //WHATS WRONG HERE? > end > > and I get: > > gut.rb:13: void value expression > return x-cena I think a google search for "Instance Variables" should enlighten you ![]() |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Jed Kowalski wrote:
> Avdi Grimm wrote: >> You might have better luck asking for with specific sections of >> code, rather than asking us to rewrite a whole program. Try doing a >> best-guest translation to Ruby on your own, and asking about the parts >> you have trouble with. > > ok so, here's the first problem: > def initialize (cen, ile, nn) > @price=cen > @amount=ile > @name=nn > @bought=0 > end > > > def change(x) > amount-- > bought++ > return x-price //WHATS WRONG HERE? > end > > and I get: > > gut.rb:13: void value expression > return x-cena > ^ > gut.rb:13: syntax error, unexpected tIDENTIFIER, expecting kEND > return x-cena > Well, if you want to refer to the @amount, @bought, and @price instance variables in the change() method you have to use the correct names. Also Ruby doesn't have the -- and ++ methods. def change(x) @amount -= 1 @bought += 1 return x - @price end -- RMagick: http://rmagick.rubyforge.org/ RMagick 2: http://rmagick.rubyforge.org/rmagick2.html |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
-------- Original-Nachricht -------- > Datum: Tue, 10 Jun 2008 05:38:30 +0900 > Von: Jed Kowalski <satanowicz@gmail.com> > An: ruby-talk@ruby-lang.org > Betreff: Re: C++ 2 Ruby > Avdi Grimm wrote: > > You might have better luck asking for with specific sections of > > code, rather than asking us to rewrite a whole program. Try doing a > > best-guest translation to Ruby on your own, and asking about the parts > > you have trouble with. > > ok so, here's the first problem: > def initialize (cen, ile, nn) > @price=cen > @amount=ile > @name=nn > @bought=0 > end > > > def change(x) > amount-- > bought++ > return x-price //WHATS WRONG HERE? > end > > and I get: > > gut.rb:13: void value expression > return x-cena > ^ > gut.rb:13: syntax error, unexpected tIDENTIFIER, expecting kEND > return x-cena > > -- > Posted via http://www.ruby-forum.com/. Jed, you could do this: class MyClass # my most creative invention of a class name attr_accessor :price,:amount,:name,:bought # creates ways to get/set @price,@amount,... from MyClass def initialize (cen, ile, nn) @price=cen @amount=ile @name=nn @bought=0 end def change(x) @amount-=1 @bought+=1 x-self.price end end cen=500 ile=400 nn=4 obj=MyClass.new(cen,ile,nn) p obj.change(1) # -499 =1-500 Read about accessors here: http://www.ruby-doc.org/docs/UsersGu...accessors.html Best regards, Axel -- Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Axel Etzold wrote:
> Jed, you could do this: > > class MyClass # my most creative invention of a class name > attr_accessor :price,:amount,:name,:bought # creates ways to get/set > @price,@amount,... from MyClass > > def initialize (cen, ile, nn) > @price=cen > @amount=ile > @name=nn > @bought=0 > end > def change(x) > @amount-=1 > @bought+=1 > x-self.price > end > end > > cen=500 > ile=400 > nn=4 > obj=MyClass.new(cen,ile,nn) > p obj.change(1) # -499 =1-500 > > Read about accessors here: > http://www.ruby-doc.org/docs/UsersGu...accessors.html > > Best regards, > > Axel ufff... almoust done... I can compile the code but it doesn't want to work, juz like the variable q isn't seen properly, something with the 'case' instruction maybe? (BTW sorry for Polish texts) class Napoj def initialize (cen, ile, nn) @cena=cen @ilosc=ile @nazwa=nn @kupione=0 end def wydajreszte(x) @ilosc-=1 @kupione+=1 return x-cena end def ilosci() return ilosc end def cenaa() return cena end def kupno() return kupione end def czyjest() if ilosc==0 then return false else return true end end def nazwaa() puts nazwa end end class Automat def initialize (pie, dwa, jed, piec, dwad, dzi) @tab = [[pie, dwa, jed, piec, dwad, dzi],[5,2,1,0.5,0.2,0.1]] end def monety(n, s, g) x=s-n[g].cena for i in 1.. 5 w=tab[0][i] for w in w..0 if (x-tab[1][i])> -0.0001 then tab[0][i]-=1 x=x-tab[1][i] puts tab[1][i] end end end end def wypisz() puts "Monet 5zl: ", tab[0][0] puts "Monet 2zl: ", tab[0][1] puts "Monet 1zl: ", tab[0][2] puts "Monet 50gr: ", tab[0][3] puts "Monet 20gr: ", tab[0][4] puts "Monet 10gr: ", tab[0][5] end def dodaj(x) if x==5 then tab[0][0]+=1 end if x==2 then tab[0][1]+=1 end if x==1 then tab[0][2]+=1 end if x==0.5 then tab[0][3]+=1 end if x==0.2 then tab[0][4]+=1 end if x==0.1 then tab[0][5]+=1 end end end q=1 m=1 s=0 p = Automat.new(15,15,20,25,25,25) napoje = [ Napoj.new(1.7,0,"Coca-Cola"), Napoj.new(2,0,"Sprite"), Napoj.new(2.1,20,"Fanta") ] while q!=0 puts "Co chcesz zrobic?" puts "Kupic napoj: nacisnij 1" puts "Wydrukowac raport o monetach: nacisnij 2" puts "Wydrukowac raport o kupionych napojach: nacisnij 3" puts "Wyjsc: nacisnij 0" q = gets puts q case q when 1 for j in 0..2 napoje[j].nazwaa() puts ": "+napoje[j].cenaa()+" nacisnij "+j end g = gets if napoje[g].czyjest() then y=1 s=0 while y!=0 puts "Monety wrzucne: "+s+" Co chcesz zrobic: \n\t\t Wrzuc monete: 0 \n\t\t Kup napoj: 1 \n\t\t Anuluj: 2" puts "Twoj wybor: " y = gets if y==0 then puts "Wrzuc monete. " m = gets s+=m p.dodaj(m) end if y==1 && napoje[g].wydajreszte(s)<0 then puts "Brak srodkow. Wrzuc monete." else puts "reszta: " p.monety(napoje,s,g) end if y==2 then break end end else puts "Brak zadanego napoju." end when 2 p.wypisz() when 3 puts "Kupiles: " for i in 0..2 napoje[i].nazwaa() puts ": ", napoje[i].kupno() end end end -- Posted via http://www.ruby-forum.com/. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
-------- Original-Nachricht -------- > Datum: Tue, 10 Jun 2008 07:53:25 +0900 > Von: Jed Kowalski <satanowicz@gmail.com> > An: ruby-talk@ruby-lang.org > Betreff: Re: C++ 2 Ruby > Axel Etzold wrote: > > Jed, you could do this: > > > > class MyClass # my most creative invention of a class name > > attr_accessor :price,:amount,:name,:bought # creates ways to get/set > > @price,@amount,... from MyClass > > > > def initialize (cen, ile, nn) > > @price=cen > > @amount=ile > > @name=nn > > @bought=0 > > end > > def change(x) > > @amount-=1 > > @bought+=1 > > x-self.price > > end > > end > > > > cen=500 > > ile=400 > > nn=4 > > obj=MyClass.new(cen,ile,nn) > > p obj.change(1) # -499 =1-500 > > > > Read about accessors here: > > http://www.ruby-doc.org/docs/UsersGu...accessors.html > > > > Best regards, > > > > Axel > > ufff... almoust done... > I can compile the code but it doesn't want to work, juz like the > variable q isn't seen properly, something with the 'case' instruction > maybe? > (BTW sorry for Polish texts) > > class Napoj > > def initialize (cen, ile, nn) > @cena=cen > @ilosc=ile > @nazwa=nn > @kupione=0 > end > > def wydajreszte(x) > @ilosc-=1 > @kupione+=1 > return x-cena > end > def ilosci() return ilosc end > def cenaa() return cena end > def kupno() return kupione end > def czyjest() > if ilosc==0 then > return false > else return true > end > end > def nazwaa() puts nazwa end > > end > class Automat > > def initialize (pie, dwa, jed, piec, dwad, dzi) > @tab = [[pie, dwa, jed, piec, dwad, dzi],[5,2,1,0.5,0.2,0.1]] > end > def monety(n, s, g) > x=s-n[g].cena > for i in 1.. 5 > w=tab[0][i] > for w in w..0 > if (x-tab[1][i])> -0.0001 then > tab[0][i]-=1 > x=x-tab[1][i] > puts tab[1][i] > end > end > end > end > def wypisz() > puts "Monet 5zl: ", tab[0][0] > puts "Monet 2zl: ", tab[0][1] > puts "Monet 1zl: ", tab[0][2] > puts "Monet 50gr: ", tab[0][3] > puts "Monet 20gr: ", tab[0][4] > puts "Monet 10gr: ", tab[0][5] > end > def dodaj(x) > if x==5 then tab[0][0]+=1 end > if x==2 then tab[0][1]+=1 end > if x==1 then tab[0][2]+=1 end > if x==0.5 then tab[0][3]+=1 end > if x==0.2 then tab[0][4]+=1 end > if x==0.1 then tab[0][5]+=1 end > end > end > > > q=1 > m=1 > s=0 > p = Automat.new(15,15,20,25,25,25) > napoje = [ > Napoj.new(1.7,0,"Coca-Cola"), > Napoj.new(2,0,"Sprite"), > Napoj.new(2.1,20,"Fanta") ] > > > while q!=0 > puts "Co chcesz zrobic?" > puts "Kupic napoj: nacisnij 1" > puts "Wydrukowac raport o monetach: nacisnij 2" > puts "Wydrukowac raport o kupionych napojach: nacisnij 3" > puts "Wyjsc: nacisnij 0" > > q = gets > puts q > > case q > > when 1 > > for j in 0..2 > napoje[j].nazwaa() > puts ": "+napoje[j].cenaa()+" nacisnij "+j > end > g = gets > if napoje[g].czyjest() then > y=1 > s=0 > while y!=0 > puts "Monety wrzucne: "+s+" Co chcesz zrobic: \n\t\t > Wrzuc monete: 0 \n\t\t Kup napoj: 1 \n\t\t Anuluj: 2" > puts "Twoj wybor: " > y = gets > if y==0 then > puts "Wrzuc monete. " > m = gets > s+=m > p.dodaj(m) > end > if y==1 && napoje[g].wydajreszte(s)<0 then puts "Brak srodkow. > Wrzuc monete." > else > puts "reszta: " > p.monety(napoje,s,g) > end > if y==2 then break end > end > > > else puts "Brak zadanego napoju." > end > when 2 > p.wypisz() > when 3 > puts "Kupiles: " > for i in 0..2 > napoje[i].nazwaa() > puts ": ", napoje[i].kupno() > end > end > end > -- > Posted via http://www.ruby-forum.com/. Jed, if you ask for a variable using 'gets', this gives you a String. So you have to write mystring=gets case mystring when '1' # instead of when 1 # .... I would love to be able to speak Polish ... it's amazing that people who can still ask questions about anything ![]() Best regards, Axel -- Psssst! Schon vom neuen GMX MultiMessenger gehört? Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger |
|
![]() |
| Outils de la discussion | |
|
|