伪代码检查.需要验证分配

goo*_*ike 5 vb.net pseudocode

我已经把它变成了这样你就不会帮我欺骗.只是想知道这看起来是否正确:

作业:输入员工姓名和工资清单,并确定平均(平均)工资以及平均值之上和之下的工资数量.

计划:允许输入姓名和工资计算平均排序值计算高于平均计数值低于平均值的值

//This program will allow a user to input an employee name and salary
//The output will contain the mean salary 
//as well as the number of salaries above and below the mean
//
//Arrays Used:
//Name(K) = Array for employee names
//Salary(K) = Array for salaries
//
//Variables Used:
//Mean = Mean of all employees Salaries
//UpMean = Number of Employees making more than the mean
//DwnMean = Number of Employees making less than the mean
//Sum = Sum of all salaries
//CountM = Counter for Mean
//CountUp = Counter for # of salaries above mean
//CountDwn = Counter for # of salaries below mean

Main
    Call WelcomeMessage
    Call InputData
    Call Calculate
    Call OutputData
End Program

WelcomeMessage
    Write, “Beginning the Salary Program” 
End WelcomeMessage

InputData
    Declare Name(100) Of Strings
    Declare Salary(100) Of Real
    Declare Mean, UpMean, DwnMean As Real
    Set Sum = 0
    Set CountM = 0
    Set CountUp = 0
    Set CountDwn = 0
    Write, "Enter Employee name and Salary."
    Write, "Enter *,0 when done."
    Input Name(K), Salary(K)
    While Name(K) <> "*"
        Set CountM = CountM + 1
        Set Sum = Sum + Salary
        Write, "Enter Employee name and Salary."
        Write, "Enter *,0 when done."
        Input Name(K), Salary(K)
    End While
End InputData

Calculation
    //Here Mean is found
    Set Mean = Sum / CountM
    //Here Number of Employees making more than the mean is found
    For K = Step 1 to CountM
        If Salary(K) > Mean Then
            Set CountUp = CountUp + 1
        End If
    //Here Number of Employees making more than the mean is found
    Set CountDwn = CountM - CountUp
    //The above algorythm doesn't account for the possibility 
    //of someone making exactly the average so subtract 1 to reconcile
    If Salary(K) = Mean Then
            Set CountDwn = CountDwn - 1
    End If
End Calculation

OutputData
    Write, "There were,"  CountM, "salaries entered."
    Write, "The mean salary is:", Mean
    Write, "There are", CountUp, "employees who make more than the average"
    Write, "There are", CountDwn, "employees who make less than the average"
End OutputData
Run Code Online (Sandbox Code Playgroud)

ral*_*nja 5

看起来不错.我唯一要建议的是,在读取名称/销售的输入时使用do-while结构.正如您所看到的,在循环开始之前和循环中您具有相同的逻辑:

Write, "Enter Employee name and Salary."
Write, "Enter *,0 when done."
Input Name(K), Salary(K)
Run Code Online (Sandbox Code Playgroud)

此外,伪代码将无法编译,因为您正在调用Calculate但该例程称为Calculation;)

谢谢你的建议.Do-While并不熟悉.那会是什么样的?我可能也许关于输入的东西应该在循环中改变但是不确定如何.

它可能看起来像这样:

Do 
    Write, "Enter Employee name and Salary."
    Write, "Enter *,0 when done."
    Input Name(K), Salary(K)
    If Name(K) <> "*"
        Set CountM = CountM + 1
        Set Sum = Sum + Salary
    Else
        BreakLoop
    End If
End While (true)
Run Code Online (Sandbox Code Playgroud)

这不是一个很大的区别,但更多的是品味问题.我个人觉得它更容易阅读,因为代码是以这样的方式编写的,你很容易意识到你应该输入一些内容,检查输入并根据输入做一些事情.

在你的while循环中,Set CountM等在第一个输入后(在文本流中),但在输入的其余部分之前,这意味着你必须回顾循环的顶部以了解它在之前的事情之后做了什么循环中"圆".现在这只是一个小循环,但如果它是30行(上帝禁止),你必须向上滚动才能看到发生了什么.如果你明白我的意思 :)


Ben*_*ank 1

关于计算的注意事项CountDwn

您的“减去 1 来协调”将根据循环For在实现语言中的工作原理,(a) 生成“未声明的变量”类型错误,(b) 生成“索引超出范围”错误,或者 (c ) 减去一个 IFF最后的工资正好等于平均水平。(另外,您的代码不包含End Forin Calculation,但我认为它应该紧接End If在该函数中的第一个之后。)

CountDwn我建议将其包含在循环中,而不是计算CountUp(毕竟,每一份工资都可能等于平均水平):

Calculation
    //Here Mean is found
    Set Mean = Sum / CountM

    For K = Step 1 to CountM
        //Here Number of Employees making more than the mean is found
        If Salary(K) > Mean Then
            Set CountUp = CountUp + 1
        End If

        //Here Number of Employees making less than the mean is found
        If Salary(K) < Mean Then
            Set CountDwn = CountDwn + 1
        End If
    End For
End Calculation
Run Code Online (Sandbox Code Playgroud)

请注意CountUp + CountDwn不一定等于CountM