我已经把它转过来,所以你不会帮我欺骗。只是想知道这看起来是否合适:
作业: 输入员工姓名和工资列表,并确定 平均(平均)工资以及上面和下面的工资数量 低于平均值。
计划: 允许输入姓名和工资 计算平均值 排序值 计数值高于平均值 计算低于平均值
的值//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
答案 0 :(得分:5)
看起来不错。我唯一要建议的是,在读取名称/销售的输入时使用do-while结构。正如您所看到的,在循环开始之前,您在循环中具有相同的逻辑:
Write, "Enter Employee name and Salary."
Write, "Enter *,0 when done."
Input Name(K), Salary(K)
此外,伪代码将无法编译,因为您正在调用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)
这不是一个很大的区别,但更多的是品味问题。我个人觉得它更容易阅读,因为代码是以这样的方式编写的,你很容易意识到你应该输入一些内容,检查输入并根据输入做一些事情。
在你的while循环中,Set CountM等在第一个输入之后(在文本流中),但在输入的其余部分之前,这意味着你必须回顾循环的顶部才能理解它做了什么在循环中之前的“回合”之后。现在这只是一个小循环,但如果它是30行(上帝禁止),你必须向上滚动才能看到发生了什么。如果你知道我的意思:)
答案 1 :(得分:1)
关于计算CountDwn
:
你的“减去1来协调”将取决于For
循环在实现语言中的工作原理,(a)生成“未声明的变量”类型错误,(b)生成“索引输出”范围“错误,或(c)减去一个IFF 最后工资与平均值完全相等。 (另外,您的代码在End For
中不包含Calculation
,但我认为它应该紧接在该函数中的第一个End If
之后。)
而不是从CountDwn
计算CountUp
(毕竟,每一个工资 等于平均值),我建议将其包含在循环中:< / p>
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
请注意,CountUp + CountDwn
不一定等于CountM
。
答案 2 :(得分:0)
self.state