-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex4.rb
More file actions
74 lines (61 loc) · 2.17 KB
/
ex4.rb
File metadata and controls
74 lines (61 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Turma
@@turmas = []
attr_reader :alunos, :materia
def initialize(materia,numeroalunos)
@materia = materia
#@alunos = {} -- Alunos passa a ser um array:
@alunos = []
cont = 0
loop do
num_matricula = rand(1000..9999)
num_nota = rand(0..10.0)
##@alunos[matricula.to_sym] = nota -- Facilitou muito!
@alunos << {:matricula => num_matricula, :nota => num_nota}
cont += 1
break if cont == numeroalunos
end
@@turmas << self
end
def self.turmas
return @@turmas
end
def totalAlunos
return @alunos.length
end
def alunosAprovados
alunosaprovados = []
for aluno in @alunos
case
when aluno[:nota] >= 5
alunosaprovados.push(aluno[:matricula])
end
end
return alunosaprovados.length
end
end
puts "Quantas turmas? (Digite número entre 1 e 10)"
numeroturmas = 0
hash = {}
loop do
numeroturmas = gets.to_i
break if numeroturmas >= 1 && numeroturmas <= 10
puts "Número inválido."
end
materias = ["Cálculo 1", "Cálculo 2", "Cálculo 3", "Física 1", "Física 2", "Estruturas de Dados", "Algoritmos e Programação de Computadores", "Técnicas de Programação 1", "Sistemas Digitais", "Eletromagnetismo"]
num_aprovados = 0
num_total = 0
for materia in 1..numeroturmas
numeroalunos = rand(5..20)
turma = Turma.new(materias.sample.to_sym,numeroalunos)
if hash.key?(turma.materia) #Matéria Repetida
hash[turma.materia][:aprovados] += turma.alunosAprovados
hash[turma.materia][:total] += turma.totalAlunos
else #Matéria Nova
hash[turma.materia] = {:aprovados => turma.alunosAprovados, :total => turma.totalAlunos}
end
num_aprovados += turma.alunosAprovados
num_total += turma.totalAlunos
end
hash[:Total] = {:aprovados => num_aprovados, :total => num_total}
#30 minutos pra entender e fazer funcionar, porém finalmente consegui. Obrigado demais pela ajuda!
puts hash.map{ |materia, info| "#{materia}: #{((info[:aprovados].to_f / info[:total]) * 100).round(2)}% aprovados" }