2009-06-11 57 views
5

我已經把它轉過來了,所以你不會幫我作弊。只是想知道,如果這看起來正確:僞代碼檢查。需要轉讓的驗證

分配: 輸入員工的姓名和工資的列表,並確定 均值(平均)工資,以及上述工資和 低於平均值的數量。

的計劃: 允許低於平均

計數值的姓名和工資 計算平均 排序值高於平均值 計數值輸入
//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 

回答

1

的注意事項有關計算CountDwn

你「減1調和」將取決於For循環中實現語言究竟是如何工作的,(一)生成「 (b)產生一個「索引超出範圍」的錯誤,或者(c)減去一個IFF,最後的工資等於平均數。 (另外,您的代碼不包含Calculation中的End For,但我認爲它應該緊接在該函數的第一個End If之後。)

代替計算從​​CountDwn(畢竟,每一個工資可能等於平均)的,我建議把它放置在循環:

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

5

看起來不錯。我必須建議的唯一事情就是在閱讀輸入名稱/銷售時使用do-while結構。正如你可以看到你有同樣的邏輯循環開始前,並在循環:

Write, "Enter Employee name and Salary." 
Write, "Enter *,0 when done." 
Input Name(K), Salary(K) 

而且,僞代碼將無法編譯,因爲你調用計算,但該程序被調用的計算;)

感謝您的建議。不是真的 與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

我真的不認爲像do ...這樣的指令(true),在代碼中有一個break語句,是教會一個新人的好方式。代碼應該是自我記錄的,這裏的執行流程很難遵循。 – jkeys 2009-06-14 04:32:44

0
FINAL ALGORITHM 

START 
OUTPUT "Enter the number of parcels" 
INPUT NUMBEROFPARCELS 
INTEGER PRICE = 0 
INTEGER PARCELWEIGHT [1:NUMBEROFPARCELS] 
INTEGER TOTALPRICE = 0 

FOR PARCELLOOP = 1 TO NUMBEROFPARCELS 
    INTEGER REJECT = 0 
    INTEGER ACCEPT = 0 
    INTEGER ACCEPTWEIGHT = 0 
    INTEGER REJECTEDPARCELS = 0 

    OUTPUT "Enter the weight of the parcel in kg" 
    INPUT WEIGHT 
    IF (WEIGHT < 1) THEN 
     REJECT = REJECT + 1 
     OUTPUT "The weight of the parcel should be atleast 1kg" 
    ELSE 
     IF (WEIGHT > 10) THEN 
      REJECT = REJECT + 1 
      OUTPUT "The weight of the parcel should be less than 10kg" 
    ENDIF 
    IF (WEIGHT > 1) THEN 
     IF (WEIGHT < 10) THEN 
      PARCELWEIGHT[PARCELLOOP] = WEIGHT 
     ENDIF 
    ENDIF 


    OUTPUT "Enter the first dimension of the parcel in cm" 
    INPUT DIMENSION1 
    IF (DIMENSION1 > 80) THEN 
     REJECT = REJECT + 1 
     OUTPUT "Each dimension of the parcel should be less than 80" 
    ENDIF 

    OUTPUT "Enter the second dimension of the parcel in cm" 
    INPUT DIMENSION2 
    IF (DIMENSION2 > 80) THEN 
     REJECT = REJECT + 1 
     OUTPUT "Each dimension of the parcel should be less than 80" 
    ENDIF 

    OUTPUT "Enter the third dimension of the parcel in cm" 
    INPUT DIMENSION3 
    IF (DIMENSION3 > 80) THEN 
     REJECT = REJECT + 1 
     OUTPUT "Each dimension of the parcel should be less than 80" 
    ENDIF 

    TOTALDIMENSION = DIMENSION1 + DIMENSION2 + DIMENSION3 
    IF (TOTALDIMENSION > 200) THEN 
     REJECT = REJECT + 1 
     OUTPUT "The size of the parcel should be less than 200cm" 
    ENDIF 

    IF (REJECT > 0) THEN 
     OUTPUT "Your parcel has been rejected for the reasons above" 
     REJECTEDPARCELS = REJECTEDPARCELS + 1 
    ENDIF 

    IF (REJECT = 0)THEN 
     OUTPUT "Your parcel has been accepted" 
     ACCEPT = ACCEPT + 1 
     ACCEPTWEIGHT = ACCEPTWEIGHT + WEIGHT 
    END IF 

    INTEGER PARCELSACCEPTED = ACCEPT 
    INTEGER TOTALWEIGHT = ACCEPTWEIGHT 
    INTEGER PARCELSREJECTED = REJECTEDPARCELS 

    OUTPUT "The number of parcels accepted is " PARCELSACCEPTED " and the total weight of the parcels is " TOTALWEIGHT 
    OUTPUT "The number of parcels rejected is " PARCELSREJECTED 
NEXT PARCELLOOP 

FOR PRICELOOP = 1 TO NUMBEROFPARCELS 
    IF (PARCELWEIGHT[PARCELLOOP] < 5) THEN 
     PRICE = PRICE + 10 
     TOTALPRICE = TOTALPRICE +PRICE 
    END IF 

    IF (PARCELWEIGHT[PARCELLOOP] > 5) THEN 
     PRICE = ((PARCELWEIGHT[PARCELLOOP] - 5)*0.10)/100 
     TOTALPRICE = TOTALPRICE +PRICE 
    END IF 

    OUTPUT "The price of the parcel is " PRICE 
NEXT PRICELOOP 

OUTPUT "The total price of all the parcels is " TOTALPRICE 
STOP