// Filename: PASSADDR.CPP
// Passing an array means that the called function
// can change the same variable as in the calling function
#include <iostream.h>
#include <string.h>
void ChangeIt(char userName[30]); // Prototype
void main()
{
char userName[30];
cout << "What is your name? ";
cin.getline(userName, 30);
// Send the name to changeIt() by address
ChangeIt(userName);
cout << "After a return to main(), here is your name: ";
cout << userName;
return;
}
//********************************************************
void ChangeIt(char userName[30])
{
int endPlace;
userName[0] = '#'; // Change first letter in name
endPlace = strlen(userName) - 1; // Find location of
// final letter in name
userName[endPlace] = '#';
return; // Return to main() - optional
}
===================================
// Filename: PASSRET.CPP
// Program that passes by value and by address.
// This program also demonstrates prototypes and
// how to return a value from one function to
// the calling function.
#include <iostream.h>
void GetUserName(char userName[50]);
int ComputeRetire(int age);
void main()
{
char userName[50];
int age = 0;
int yearsToRetire = 0;
GetUserName(userName); // Fill the array in the function
cout << "How old are you, " << userName << " ?";
cin >> age;
yearsToRetire = ComputeRetire(age);// Pass and return values
cout << "You have " << yearsToRetire
<< " years until retirement at 65" << endl;
return;
}
//**********************************************************
void GetUserName(char userName[50]) // 50 is optional here
{
cout << "What is your name? ";
cin.get(userName,50);
return; // No need to return anything
}
//**********************************************************
int ComputeRetire(int age)
{
return (65 - age); // Return the number of years until 65
}
=============================
// Filename: PAYFULL.CPP
// Calculate pay so that correct overtime is computed
// based solely on the number of hours worked.
#include <iostream.h>
void main()
{
int hours;
double rate = 5.65;
double pay;
double doubleTime=0.0, timeHalf=0.0, regular = 0.0;
cout << "How many hours did the employee work? ";
cin >> hours;
// Make sure the user entered a reasonable amount
if ((hours < 1) || (hours > 80))
{ cout << "There's no way the employee worked that much!" << endl;
cout << "Please rerun the program with the correct hours." << endl;
return; // Return early to the IDE
}
// Only gets here if the hours are reasonable
if (hours > 50) // Some double-time if true
{ doubleTime = 2 * rate * (hours - 50); // Double for all
// hours over 50
timeHalf = 1.5 * rate * 10; // Ten of time-and-a-half
regular = 40 * rate;
}
if ((hours > 40) && (hours <= 50)) // Some time-and-a-half if true
{ timeHalf = 1.5 * rate * (hours - 40);
regular = 40 * rate; }
if (hours <= 40)
{ regular = hours * rate; }
// Add to get total pay
pay = regular + timeHalf + doubleTime;
// Print results
cout.precision(2);
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout << endl << "The employee who worked " << hours
<< " hours earned:" << endl;
cout << "$" << regular << " of regular pay" << endl;
cout << "$" << timeHalf << " of time and a half" << endl;
cout << "$" << doubleTime << " of double time" << endl;
cout << "Giving a total of $" << pay << " total pay." << endl;
return;
}
================================
// File name: PERSON.CPP
// A simple class for handling age of a person
//
#include <iostream.h>
#include <string.h>
// Class declaration
class Person
{
// Public - available like a struct member
public:
// Set up data
int AskForPerson();
void SetRetirementAge(int year);
// Get the name, but const char * stops the
// pointer being used to change the name
// and the following const after GetName means
// that the class will not be changed by calling
// this function
const char * GetName() const;
int GetAge() const;
int YearsToRetirement() const;
// Private - only member functions of this class
// can see the following members
private:
char name[30];
int age;
int retirementAge;
};
int MoreNames();
void AskForInput(char * name,int & age);
void main()
{
// Array to hold up to 20 people - initialized to 0 pointers
Person * people[20] = {0};
int more = 1;
int count = 0;
while (more)
{
more = MoreNames(); // Ask whether more input
if (more)
{
people[count] = new Person;
if (people[count]->AskForPerson())
count++;
else
{
cout << "** Invalid input - rejected **" << endl;
delete people[count]; // Tidy up problem
}
}
}
//
// Output collected data
//
cout << endl << endl
<< "Name Age"
<< " Years to Retirement" << endl;
for (int i = 0; i < count; i++)
{
cout << people[i]->GetName();
for (int j = strlen(people[i]->GetName()); j < 31; j++)
cout << ' ';
cout << people[i]->GetAge();
if (people[i]->YearsToRetirement())
cout << " " << people[i]->YearsToRetirement() << endl;
else
cout << " " << "Retired" << endl;
}
for (i = 0;i < count; i++) // Tidy up dynamic data
delete people[i];
}
//************************************************************
//
// Global functions
//
int MoreNames()
{
char temp;
cout << endl << endl << "Do you want to add more names? ";
cin >> temp;
cin.ignore();
if (temp == 'y' || temp == 'Y')
return 1;
else
return 0;
}
//************************************************************
//
// Person functions
//
int Person::AskForPerson()
{
cout << endl << "Name: ";
cin.getline(name,30);
if (strlen(name) == 0)
return 0;
cout << "Age : ";
cin >> age;
cin.ignore();
if (age < 0 || age > 120)
return 0;
cout << "Retirement age [0 for default]: ";
cin >> retirementAge;
if (retirementAge <= 0 || retirementAge > 120)
retirementAge = 65;
return 1;
}
const char * Person::GetName() const
{
return name;
}
int Person::GetAge() const
{
return age;
}
int Person::YearsToRetirement() const
{
if (age >= retirementAge)
return 0;
else
return retirementAge - age;
}
=======================
// File name: PERSON1.CPP
// A simple class for handling age of a person
//
#include <iostream.h>
#include <string.h>
// Class declaration
class Person
{
public:
int AskForPerson();
void SetRetirementAge(int year);
const char * GetName() const;
int GetAge() const;
int YearsToRetirement() const;
void CleanUp();
private:
char* name;
int age;
int retirementAge;
};
int MoreNames();
void AskForInput(char * name,int & age);
void main()
{
// Array to hold up to 20 people - initialized to 0 pointers
Person * people[20] = {0};
int more = 1;
int count = 0;
while (more)
{
more = MoreNames(); // Ask whether more input
if (more)
{
people[count] = new Person;
if (people[count]->AskForPerson())
count++;
else
{
cout << "** Invalid input - rejected **" << endl;
delete people[count]; // Tidy up problem
}
}
}
//
// Output collected data
//
cout << endl << endl
<< "Name Age"
<< " Years to Retirement" << endl;
for (int i = 0; i < count; i++)
{
cout << people[i]->GetName();
for (int j = strlen(people[i]->GetName()); j < 31; j++)
cout << ' ';
cout << people[i]->GetAge();
if (people[i]->YearsToRetirement())
cout << " " << people[i]->YearsToRetirement() << endl;
else
cout << " " << "Retired" << endl;
}
for (i = 0;i < count; i++) // Tidy up dynamic data
{
people[i]->CleanUp();
delete people[i];
}
}
//************************************************************
//
// Global functions
//
int MoreNames()
{
char temp;
cout << endl << endl << "Do you want to add more names? ";
cin >> temp;
cin.ignore();
if (temp == 'y' || temp == 'Y')
return 1;
else
return 0;
}
//************************************************************
//
// Person functions
//
int Person::AskForPerson()
{
char tempName[80]; // Temporary place to hold name
cout << endl << "Name: ";
cin.getline(tempName,80);
if (strlen(tempName) == 0)
return 0;
cout << "Age : ";
cin >> age;
cin.ignore();
if (age < 0 || age > 120)
return 0;
cout << "Retirement age [0 for default]: ";
cin >> retirementAge;
if (retirementAge <= 0 || retirementAge > 120)
retirementAge = 65;
//
// Allocate the name
//
name = new char[strlen(tempName) + 1];
strcpy(name,tempName);
//
//
//
return 1;
}
const char * Person::GetName() const
{
return name;
}
int Person::GetAge() const
{
return age;
}
int Person::YearsToRetirement() const
{
if (age >= retirementAge)
return 0;
else
return retirementAge - age;
}
void Person::CleanUp()
{
delete name;
}
Tidak ada komentar:
Posting Komentar