Blog Stats
  • Posts - 9
  • Articles - 0
  • Comments - 3
  • Trackbacks - 0

 

Sunday, March 4, 2012

Rock, Paper Scissors program - pseudo-code


Since I love case-statements, I decided to make the most out of them during the rock, paper, scissors program.

The gist of the pseudo-code I wrote up for the program is below.

/*
  CIS 217 Midterm
  Melanie Draves
  04 Mar 2012
  Pseudo-code

    Narrative dsecription
Program will simulate a game of Rock-Paper-Scissors.  To do this, the program will
generate a number at random from 1-3, which is to be the computer's selection.
Once user selects their choice, the answer and winner will be revealed.
Then the user can choose to start another game or exit the program.
    Selection
1 rock
2 paper
3 scissors

    Variables
Inputs
    userchoice  int 
 Outputs
    compchoice int (the random num)
 Temps
    outcome  int

    Pseudo-code
1    compchoice = generate random number
2    userchoice = call GetUserChoice();
3    outcome = call DetOutcome(compchoice, userchoice)
4    call DispWinner(outcome)
HALT

-----
method DetOutcome(int lcompchoice, luserchoice)
returns: int

loutcome int

 switch(lcompchoice)
  case 0:
   if (luserchoice < 1)
    then TIE     //loutcome = 1
    elseif (luserchoice > 1)
     then lcompchoice = WINNER //loutcome = 2
     else lcompchoice = LOSER       //loutcome = 3

  case 1:
   if (luserchoice = 1)
    then TIE    //loutcome = 4
    elseif (luserchoice > 1)
     then lcompchoice = LOSER    //loutcome = 5
     else lcompchoice = WINNER //loutcome = 6

  case 2:
   if (luserchoice > 1)
    then TIE    //loutcome = 7
    elseif (luserchoice < 1)
     then lcompchoice = LOSER       //loutcome = 8
     else lcompchoice = WINNER    //loutcome = 9
  default:

return(loutcome)

-----
method DispWinner(int loutcome)
return: void

msg1 string "TIE!, select Go to play again"
msg2 string "YOU WIN!, select Go to play again"
msg3 string "I WIN!, select Go to play again"

 show compchoiceLabel
 show userchoiceLabel
 switch(loutcome)
  case 1:
   display "Your Choice = Rock ; My Choice = Rock"
   display msg1
  case 2:
   display "Your Choice = Scissors; My Choice = Rock"
   display msg3
  case 3:
   display "Your Choice = Paper ; My Choice = Rock"
   display msg2
  case 4:
   display "Your Choice = Paper ; My Choice = Paper"
   display msg1
  case 5:
   display "Your Choice = Scissors ; My Choice = Paper"
   display msg2
  case 6:
   display "Your Choice = Rock ; My Choice = Paper"
   display msg3
  case 7:
   display "Your Choice = Scissors ; My Choice = Scissors"
   display msg1
  case 8:
   display "Your Choice = Rock ; My Choice = Scissors"
   display msg2
  case 9:
   display "Your Choice = Paper ; My Choice = Scissors"
   display msg3
  default:
return
*/

Joe's Automotive program - Pseudo-code


Pseudo-code for Joe's Automotive program

/*

 pseudo-code
Program: Joe's Automotive
Name:  Melanie Draves
Date:  Feb 26 2012

 Narrative Description:
Program calculate and display a customer's total charges incurred for a visit to an automotive establishment.
Calculations for regular services are based on the following schedule:
 Inspection 15.00
 Lube job 18.00
 Tire rotation 20.00
 Oil change 26.00
 Radiator flush 30.00
 Trnsmissn flush 80.00
 Muffler replace 100.00
 nonroutine svc 20.00 * Hours worked
Calculations to be made by the following methods:
 (value-returning)
 totoillube = OilLubeChrgs Method for oil change and/or lube job
 totflush = FlushChrgs  Method for radiator flush and/or transmission flush
 totmisc = MiscChrgs  Method for inspection, muffler replacement, and/or tire rotation
 totother = OtherChrgs  Method for services/labor (if applicable)
 tottax = 0.06 * amt of parts Method for sales tax charged on parts
 totfees = TotalChrgs  Method for total charges
 (void methods) called when the user click 'Clear' button
 ClrOilLube Method for clearing oil change and lube job check boxes  
 ClrFlushes Method for clearing radiator and transmission flushes check boxes
 ClrMisc  Method for clearing inspection, muffler replacement, tire rotation check boxes
 ClrOther Method for clearing parts and labor text boxes
 ClrFees  Method for clearing 'summary' labels
  
 Variables:
Inputs
 oil, lube - bool
 radflsh, tranflsh
 inspect, muffreplace, tirerotate
 
 amtparts -  decimal = 0
 hrslabor - int = 0
Outputs
 totother - decimal
 amtparts - decimal
 tottax - decimal
 totfees - decimal
Temps
 totoilube - decimal
 totflush - decimal
 totmisc - decimal

 pseudo-code:
MAIN
 init vars
  
 calc tot charges
  totoillube = OilLubeChrgs(oil, lube)
  totflush = FlushChrgs(radflsh, tranflsh)
  totmisc = MiscChrgs(inspect, muffreplace, tirerotate)
  totother = OtherChrgs(amtparts, hrslabor)
  tottax = 0.06 * amtparts
  totfees = TotalChrgs(totoillube, totflush, totmisc, totother, tottax)
 display totals
  disp totother
  disp amtparts
  disp tottax
  disp totfees
 clear form
  ClrOilLube
  ClrFlushes
  ClrMisc
  ClrOther
  ClrFees 
 HALT

-----
method OilLubeChrgs(loil, llube)
ret: ltotoillube
  
*/

Hospital Charges program - Pseudo-code


Before I program solutions, I like to write pseudo-code to plan out the steps that I intend to take!

The following is my plan for the Hospital Charges program

/*

  Pseudo-code
Program: Hospital Charges
Name:  Melanie Draves
Date:  Feb 26 2012

 Narrative Description:
Program will calculate the total cost for a patient's hospital stay.  Calculation variables include:
 number of days of hospital stay
 medication charges incurred
 surgical charges incurred
 lab charges incurred
 physical rehabilitation charges incurred
Program will calculate totals using the following methods:

 CalcStayCharges  = $350 * number of days of hospital stay
 CalcMiscCharges  = medication charges incurred + surgical charges incurred + lab charges incurred + medical rehabilitation charges incurred
 CalcTotalCharges = CalcStayCharges + CalcMiscCharges

 Variables:
Inputs
 numdays -  int
 medchrg -  real
 srgchrg -  real
 labchrg -  real
 rehbchrg - real
Outputs
 totcost -  real
Temps
 staychrg - real
 miscchrg - real


 Pseudo-code:
MAIN
 get inputs
 calc tot's
  staychrg = CalcStayCharges(numdays)
  miscchrg = CalcMiscCharges(medchrg, srgchrg, labchrg, rehbchrg)
  totcost  = CalcTotalCharges()
 display tot's
 HALT
-----
method CalcTotalCharges(real lstaychrg, real lmiscchrg)
ret ltotcost - real
 ltotcost = lstaychrg + lmiscchrg
 return(ltotcost)

-----
method CalcMiscCharges(real lmedchrg, real lsrgchrg, real llabchrg, real lrehbchrg)
ret: lmiscchrg - real
 lmiscchrg = lmedchrg + lsrgchrg + llabchrg + lrehbchrg
 return(lmiscchrg)
 
-----
method CalcStayCharges(int lnumdays)
ret: lstaychrg - real
 lstaychrg = 350 * lnumdays
 return(lstaychrg)

*/v

 

 

Copyright © MoDraves