2011-12: Intermediate
Import the following code sample (XMW920), there are a number of errors – fix these.
Most of the errors prevent the sample compiling so it should be relatively easy to identify these from the compiler error reports.
Click on these to take you to the offending line.There is also one other error that allows the code to run, but doesn’t work as expected, so you will need to test the code.
When all the errors are detected, the code works fine, but it is not ‘good’ code.
It jumps all-over-the-place using Goto statements and is very hard to follow.
The remainder of this challenge is to rewrite the code with a good structure using no Goto statements if possible, using any methods you think make the code clearer (possibly Arrays, While or Subroutines etc), also comment it.
Soluzione 1
Importa il codice seguente d’esempio (XMW920), ci sono diversi errori – correggili.
La maggior parte degli errori impediscono la semplice compilazione quindi dovrebbe essere relativamente facile individuarli a partire dagli errori riportati dal compilatore.
Clicca su ciascuno di essi per portarti alla linea corrispondente.
Originale
Goto start
clear:
TextWindow.Pause()
TextWindow.Clear()
Goto Start
start:
count = 0
enterNumber:
TextWindow.WriteLine("Enter a number")
count = count+1
If (count = 1) Then
num1 = TextWindow.ReadNumber()
ElseIf (count = 2) Then
num2 = TextWindow.ReadNumber()
Goto getOperations
EndIf
Gotoo enterNumber
getOperation:
TextWindow.WriteLine("Enter an operation (+,-,*,/)"
operation = Textwindow.Read()
If (operation <> "+" And operation <> "-" And operation <> "*" And operation <> "/") Then
Goto getOperation
Else
Goto dosum
Else
result:
TextWindow.WriteLine(num1+operation+num2+"="+result)
Goto clear
dosum:
If (operation = "+") Then
result = num1 + num2
ElseIf (operation = "-") Then
result = num1 - num2
ElseIf (operation = "/") Then
result = num1 * num2
ElseIf (operation = "*") Then
result = num1 * num2
EndIf
Goto result
Corretto
Goto start
clear:
TextWindow.Pause()
TextWindow.Clear()
Goto Start
start:
count = 0
enterNumber:
TextWindow.WriteLine("Enter a number")
count = count+1
If (count = 1) Then
num1 = TextWindow.ReadNumber()
ElseIf (count = 2) Then
num2 = TextWindow.ReadNumber()
Goto getOperation
EndIf
Goto enterNumber
getOperation:
TextWindow.WriteLine("Enter an operation (+,-,*,/")
operation = Textwindow.Read()
If (operation <> "+" And operation <> "-" And operation <> "*" And operation <> "/") Then
Goto getOperation
Else
Goto dosum
EndIf
result:
TextWindow.WriteLine(num1+operation+num2+"="+result)
Goto clear
dosum:
If (operation = "+") Then
result = num1 + num2
ElseIf (operation = "-") Then
result = num1 - num2
ElseIf (operation = "/") Then
result = num1 * num2
ElseIf (operation = "*") Then
result = num1 * num2
EndIf
Goto result
Soluzione 2
C’è un ulteriore errore che permette al codice di essere eseguito, ma non di funzionare come dovrebbe, quindi sarà necessario che tu metta alla prova il codice.
ElseIf (operation = "/") Then result = num1 / num2
Soluzione 3
Quando tutti gli errori saranno individuati il codice funzionerà correttamente ma non si tratta di un buon codice.
Salta dappertutto utilizzando l’istruzione GoTo ed è molto difficile da seguire.
L’ultima parte di questa sfida è riscrivere il codice con una buona struttura senza utilizzare, se possibile, l’istruzione GoTo, utilizzando qualsiasi metodo che ritieni necessario per semplificare il codice (per esempio Arrays, While, Sottoprogrammi, …), inoltre commentalo.
While "True"
pos=1
LeggiNumero()
pos=2
LeggiNumero()
LeggiOperazione()
Calcola()
Aspetta()
EndWhile
Sub LeggiNumero
TextWindow.Write("Numero" + pos + ": ")
numero[pos]=TextWindow.ReadNumber()
Endsub
Sub LeggiOperazione
TextWindow.Write("Operazione (+, -, *, /): ")
op=TextWindow.Read()
While (op <> "+" And op <> "-" And op <> "*" And op <> "/")
TextWindow.Write("Operazione (+, -, *, /): ")
op=TextWindow.Read()
EndWhile
Endsub
Sub Calcola
x1=numero[1]
x2=numero[2]
If (op = "+") Then
r=x1+x2
ElseIf (op = "-") Then
r=x1-x2
ElseIf (op = "*") Then
r=x1*x2
ElseIf (op = "/") Then
r=x1/x2
EndIf
TextWindow.WriteLine(x1+op+x2+"="+r)
EndSub
Sub Aspetta
TextWindow.Pause()
TextWindow.Clear()
EndSub