KUMPULAN PROGRAM C++ TUGAS AKHIR MATA KULIAH PBP D I S U S U N OLEH
1. FRANSESCO AGNES RANUBAYA NIM : 2008 120 48
2. SAMSUL
NIM : 2008 120 3. KORNELIUS RIKI
NIM : 2008 120
AKADEMI MANAJEMEN KOMPUTER DAN INFORMATIKA AMKI
KETAPANG 2009
A. PROGRAM C++ PELAYANAN RUMAH SAKIT
Konsep:
- program C++ berdiagram objek.
- jenis program ada INPUT --> OUTPUT.. #include <iostream>
#include <iomanip> using namespace std; // Function prototypes char getChoice();
double patientCharges(int, double, double, double); // In-patient double patientCharges(double, double); // Out-patient
int main() {
char patientType; // I=in-patient, O=out-patient
int days; // Number of days of <strong class="highlight">hospital</strong> stay double roomRate, // Daily room rate
medication, // Total medication charges services, // Total for tests and other services totalCharges; // Total of all charges
// Input and validate patient type
cout << "This <strong class="highlight">program</strong> will compute patient <strong class="highlight">hospital</strong> charges.\n";
cout << "Enter I for in-patient or O for out-patient: "; patientType=getChoice();
//Process the menu selection switch (patientType)
{
//Inpatient case 'I':
case 'i': cout << "The number of days spent in the <strong class="highlight">hospital</strong>: "; cin >> days;
cout << "The daily rate: "; cin >> roomRate;
cout << "Charges for <strong class="highlight">hospital</strong> service: "; cin >> services;
cout << "Hospital medicaion charges; "; cin >> medication;
cout << patientCharges (days, roomRate, services, medication) << endl; totalCharges=(days*roomRate) + services + medication;
//Outpatient case 'O':
case 'o': cout << "Charges for <strong class="highlight">hospital</strong> services: "; cin >> services;
cout << "Hospital medication charges: "; cin >> medication;
cout << patientCharges (services, medication) << endl; totalCharges= services + medication;
}
// Display the billing statment
cout << fixed << showpoint << setprecision(2) << endl << endl; cout << "******************************\n";
if (patientType == 'I' && patientType == 'i')
cout << "Room charges $" << setw(8) << days*roomRate << endl; if (services > 0.0)
cout << "Lab & Services $" << setw(8) << services << endl; if (medication > 0.0)
cout << "Medication $" << setw(8) << medication << endl; cout << "Total charges $" << setw(8) << totalCharges << endl; cout << "******************************\n";
return 0;
}// End of main function
//************************************... // Definition of function getChoice *
// Accepts and returns user's validated menu choice. * //************************************... char getChoice()
{
char letter; // Holds user's letter choice // Get the user's choice
cin >> letter;
// Validate the choice
while (letter != 'I' && letter != 'i' && letter != 'O' && letter != 'o') {
cin >> letter; }
// Return the choice return letter;
}
/*************************************... * patientCharge *
* This function is called by main to calculate and return * * total patient charges for in-patients *
**************************************...
double patientCharges(int days, double rate, double med, double serv) {
return (days*rate) + med + serv;
}// end overload function patientCharges
/*************************************... * patientCharge *
* This function is called by main to calculate and return * * total patient charges for out-patients *
**************************************... double patientCharges(double med, double serv) {
return med + serv;
}// end overload function patientCharges
B. Program berstruktur pengulangan dengan counter:
Petunjuk:
1. Gunakan langkah-langkah pembuatan template program seperti pada program sederhana I (“Hello World!) yang lalu , tetapi gunakan Project_name yang berbeda,misalnya Program5
2. Gantilah blok program fungsi main ( ingat,yang dimaksud blok adalah barisan perintah diantara { dan } dari fungsi main),dengan yang berikut:
int counter; /* number of grade to be entered next */ int grade; /* grade value */
int total; /* sum of grades input by user */ int average; /* average of grades */
/* initialization phase */ total = 0; /* initialize total */
counter = 1; /* initialize loop counter */
/* processing phase */
while ( counter <= 10 ) { /* loop 10 times */ printf( "Enter grade: " ); /* prompt for input */ scanf( "%d", &grade ); /* read grade from user */ total = total + grade; /* add grade to total */ counter = counter + 1; /* increment counter */ } /* end while */
/* termination phase */
average = total / 10; /* integer division */
printf( "Class average is %d\n", average ); /* display result */ return 0; /* indicate program ended successfully */
Output Program: Enter grade: 98 Enter grade: 76 Enter grade: 71 Enter grade: 87 Enter grade: 83 Enter grade: 90 Enter grade: 57 Enter grade: 79 Enter grade: 82 Enter grade: 94 Class average is 81
C. Program berstruktur pengulangan dengan sentinel:
Petunjuk:
1. Gunakan langkah-langkah pembuatan template program sama seperti di atas, tetapi gunakan Project_name yang berbeda,misalnya Program6
2. Gantilah blok program fungsi main,dengan yang berikut: int counter; /* number of grades entered */
int grade; /* grade value */ int total; /* sum of grades */
float average; /* number with decimal point for average */
/* initialization phase */ total = 0; /* initialize total */
counter = 0; /* initialize loop counter */
/* processing phase */ /* get first grade from user */
printf( "Enter grade, -1 to end: " ); /* prompt for input */ scanf( "%d", &grade ); /* read grade from user */
/* loop while sentinel value not yet read from user */ while ( grade != -1 ) {
total = total + grade; /* add grade to total */ counter = counter + 1; /* increment counter */
/* get next grade from user */
printf( "Enter grade, -1 to end: " ); /* prompt for input */ scanf("%d", &grade); /* read next grade */ } /* end while */
/* termination phase */
/* if user entered at least one grade */ if ( counter != 0 ) {
/* calculate average of all grades entered */
average = ( float ) total / counter; /* avoid truncation */ /* display average with two digits of precision */ printf( "Class average is %.2f\n", average ); } /* end if */
else { /* if no grades were entered, output message */ printf( "No grades were entered\n" );
} /* end else */
Output Program:
Enter grade, -1 to end: 75 Enter grade, -1 to end: 94 Enter grade, -1 to end: 97 Enter grade, -1 to end: 88 Enter grade, -1 to end: 70 Enter grade, -1 to end: 64 Enter grade, -1 to end: 83 Enter grade, -1 to end: 89 Enter grade, -1 to end: -1 Class average is 82.50 Enter grade, -1 to end: -1 No grades were entered
D. Program berstruktur kondisi switch dan pengulangan dengan sentinel.Sentinel yang digunakan adalah karakter EOF yaitu apabila user menekan tombol Ctrl+Z
Petunjuk:
1. Gunakan langkah-langkah pembuatan template program sama seperti program sebelumnya, tetapi gunakan Project_name yang berbeda,misalnya Program7 2. Gantilah blok program fungsi main,dengan yang berikut:
int grade; /* one grade */ int aCount = 0; /* number of As */ int bCount = 0; /* number of Bs */ int cCount = 0; /* number of Cs */ int dCount = 0; /* number of Ds */ int fCount = 0; /* number of Fs */ printf( "Enter the letter grades.\n" );
printf( "Enter the EOF character to end input.\n" ); /* loop until user types end-of-file key sequence */ while ( ( grade = getchar() ) != EOF ) {
/* determine which grade was input */ switch ( grade ) { /* switch nested in while */ case 'A': /* grade was uppercase A */ case 'a': /* or lowercase a */
++aCount; /* increment aCount */ break; /* necessary to exit switch */ case 'B': /* grade was uppercase B */ case 'b': /* or lowercase b */
++bCount; /* increment bCount */ break; /* exit switch */
case 'C': /* grade was uppercase C */ case 'c': /* or lowercase c */
++cCount; /* increment cCount */ break; /* exit switch */
case 'D': /* grade was uppercase D */ case 'd': /* or lowercase d */
++dCount; /* increment dCount */ break; /* exit switch */
case 'F': /* grade was uppercase F */ case 'f': /* or lowercase f */
++fCount; /* increment fCount */ break; /* exit switch */
case '\n': /* ignore newlines, */ case '\t': /* tabs, */
case ' ': /* and spaces in input */ break; /* exit switch */
default: /* catch all other characters */ printf( "Incorrect letter grade entered." ); printf( " Enter a new grade.\n" );
break; /* optional; will exit switch anyway */ } /* end switch */
} /* end while */
/* output summary of results */
printf( "\nTotals for each letter grade are:\n" );
printf( "A: %d\n", aCount ); /* display number of A grades */ printf( "B: %d\n", bCount ); /* display number of B grades */ printf( "C: %d\n", cCount ); /* display number of C grades */ printf( "D: %d\n", dCount ); /* display number of D grades */ printf( "F: %d\n", fCount ); /* display number of F grades */ return 0; /* indicate program ended successfully */
Output Program: Enter the letter grades.
Enter the EOF character to end input.
a b c C A d f C E
Incorrect letter grade entered. Enter a new grade. D
A b ^Z
Totals for each letter grade are: A: 3
B: 2 C: 3 D: 2 F: 1
E. Program C+ + untuk segitiga Pascal
#include <iostream.h> #include <vector.h> int main(){
int n,i,j,tmp1,tmp2;
vector <unsigned long long> arr; bool odd; cin>>n; arr.push_back(0); arr.push_back(1); if (n>=1) cout<<arr[1]<<’\n’; for (i=2;i<=n;i++){ odd=i%2; tmp1=0; for (j=1;j<=i/2;j++){ tmp2=arr[j]; arr[j]=tmp1+arr[j]; tmp1=tmp2; }
for (j=1;j<=i/2;j++) cout<<arr[j]<<’ ‘; if (odd) {
arr.push_back(2*tmp1); cout<<arr[i/2+1]<<’ ‘; }
for (j=i/2;j>=1;j–) cout<<arr[j]<<’ ‘; cout<<’\n’;
return 0; }