跳转至内容

编程基础/条件示例

来自维基教科书,开放的书籍,开放的世界

伪代码

[编辑 | 编辑源代码]
Function Main
    Declare String choice
    
    Assign choice = GetChoice()
    If Choice = "C" Or Choice = "c"
        Call ProcessCelsius()
    False:
        If Choice = "F" Or Choice = "f"
            Call ProcessFahrenheit()
        False:
            Output "You must enter C to convert to Celsius or F to convert to Fahrenheit!"
        End
    End
End

Function CalculateCelsius (Real fahrenheit)
    Declare Real celsius
    
    Assign celsius = (fahrenheit - 32) * 5 / 9
Return Real celsius

Function CalculateFahrenheit (Real celsius)
    Declare Real fahrenheit
    
    Assign fahrenheit = celsius * 9 / 5 + 32
Return Real fahrenheit

Function DisplayResult (Real temperature, String fromScale, Real result, String toScale)
    Output temperature & "° " & fromScale & " is " & result & "° " & toScale
End

Function GetChoice
    Declare String choice
    
    Output "Enter F to convert to Fahrenheit or C to convert to Celsius:"
    Input Choice
Return String choice

Function GetTemperature (String scale)
    Declare Real temperature
    
    Output "Enter " & scale & " temperature:"
    Input temperature
Return Real temperature

Function ProcessCelsius
    Declare Real temperature
    Declare Real result
    
    Assign temperature = GetTemperature("Celsius")
    Assign result = CalculateCelsius(temperature)
    Call DisplayResult(temperature, "Fahrenheit", result, "Celsius")
End

Function ProcessFahrenheit
    Declare Real temperature
    Declare Real result
    
    Assign temperature = GetTemperature("Fahrenheit")
    Assign result = CalculateFahrenheit(temperature)
    Call DisplayResult(temperature, "Celsius", result, "Fahrenheit")
End
Enter C to convert to Celsius or F to convert to Fahrenheit:
 c
Enter Fahrenheit temperature:
 100
100° Fahrenheit is 37.7777777777778° Celsius

Enter C to convert to Celsius or F to convert to Fahrenheit:
 f
Enter Celsius temperature:
 100
100° Celsius is 212° Fahrenheit

Enter C to convert to Celsius or F to convert to Fahrenheit:
 x
You must enter C to convert to Celsius or F to convert to Fahrenheit.

流程图

[编辑 | 编辑源代码]

Conditions flowchart Conditions ProcessCelsius flowchart Conditions ProcessFahrenheit flowchart Conditions GetChoice flowchart Conditions GetTemperature flowchart Conditions CalculateCelsius flowchart Conditions CalculateFahrenheit flowchart Conditions DisplayResult flowchart

参考文献

[编辑 | 编辑源代码]
华夏公益教科书