Discussion: C++ 2 Ruby
Afficher un message
Vieux 10/06/2008, 09h33   #8
Axel Etzold
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: C++ 2 Ruby


-------- 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

  Réponse avec citation
 
Page generated in 0,10818 seconds with 9 queries