Ψευδοκώδικας έλεγχο. Ανάγκες επικύρωσης για την ανάθεση

ψήφοι
5

Έχω ήδη μετατραπεί αυτό έτσι δεν θα με βοηθούν να εξαπατήσει. Απλά αναρωτιέμαι αν αυτό φαίνεται σωστό:

Η ανάθεση: Εισαγωγή ένας κατάλογος ονομάτων των εργαζομένων και των μισθών, και προσδιορίζεται το μέσο (μέσος όρος) μισθού, καθώς και τον αριθμό των μισθών πάνω και κάτω από τη μέση.

Το σχέδιο: Να επιτρέπεται η είσοδος των ονομάτων και των μισθών Υπολογίστε μέσες τιμές Ταξινόμηση Count τιμές πάνω από τη μέση Count τιμές κάτω Mean

//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
Δημοσιεύθηκε 12/06/2009 στις 00:02
πηγή χρήστη
Σε άλλες γλώσσες...                            


3 απαντήσεις

ψήφοι
5

Φαίνεται εντάξει. Το μόνο που έχω να προτείνω, είναι να χρησιμοποιήσετε μια δομή do-ενώ κατά την ανάγνωση των εισροών στο όνομα / salery. Όπως μπορείτε να δείτε έχετε την ίδια λογική πριν από την έναρξη της βρόχο, και στο βρόχο:

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

Επίσης, ο κώδικας ψευδο δεν θα συγκεντρώνουν, από τη στιγμή που καλείτε Υπολογίστε αλλά η ρουτίνα καλείται Υπολογισμός?)

Ευχαριστώ για τις προτάσεις. Δεν έχει πραγματικά ακόμη εξοικειωμένοι με το Do-Αν. Τι θα ήταν αυτό μοιάζει; Ι αν και ίσως κάτι για την είσοδο θα πρέπει να αλλάξει στο βρόχο, αλλά δεν ήταν σίγουρος πώς.

Θα μπορούσε να είναι κάπως έτσι:

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 σας το σύνολο COUNT εκ κλπ έρθει μετά (στη ροή του κειμένου) την πρώτη είσοδο, αλλά πριν από το υπόλοιπο της εισόδου, το οποίο σημαίνει ότι πρέπει να κοιτάξουμε πίσω στην κορυφή του βρόχου να καταλάβει ότι κάνει κάτι μετά από την προηγούμενη «γύρο» στο βρόχο. Τώρα αυτό είναι μόνο ένα μικρό βρόχο, αλλά αν ήταν μήκους 30 σειρές (Θεός φυλάξοι) που θα πρέπει να μετακινηθείτε προς τα επάνω για να δείτε τι συμβαίνει. Αν καταλαβαίνεις τι εννοώ :)

Απαντήθηκε 12/06/2009 στις 00:12
πηγή χρήστη

ψήφοι
1

Μια σημείωση σχετικά με τον υπολογισμό CountDwn:

Σας «αφαιρούμε 1 να συμφιλιώσει» θα, ανάλογα με το πώς ακριβώς Forλειτουργεί βρόχο στη γλώσσα εφαρμογή, (α) να δημιουργήσει ένα «αδήλωτη μεταβλητή» σφάλμα -τύπου, (β) να δημιουργήσει ένα «δείκτη από τη σειρά» σφάλμα, ή (γ ) αφαιρέσουμε την μία IFF η τελευταία μισθός ήταν ακριβώς ίσο με το μέσο όρο. (Επίσης, ο κωδικός σας δεν περιλαμβάνει End Forτο 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

Σημειώστε ότι CountUp + CountDwnδεν είναι απαραίτητα ίσο με CountM.

Απαντήθηκε 12/06/2009 στις 00:50
πηγή χρήστη

ψήφοι
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
Απαντήθηκε 13/11/2016 στις 04:29
πηγή χρήστη

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more