[ create a new paste ] login | about

Link: http://codepad.org/sMth9YdP    [ raw code | output | fork ]

ninwa - C++, pasted on Apr 10:
/* Select exercises from Deitel and Deitel's "C++ How To Program, 3rd Ed." 
   Segregated into functions for my sanity. */

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <climits>
using namespace std;

class Employee
{
	public: 
		string name;
		double hours;
		double rate;

		Employee(string n, double h, double r) : 
		name(n), hours(h), rate(r) {} 
};

void Ex2_18()
{
	vector<Employee> employeeList; 
	employeeList.push_back(Employee("Tom Smith",40,22.75));
	employeeList.push_back(Employee("Aaron Karr", 45, 23.80));
	employeeList.push_back(Employee("Roger Wilco", 50, 40.20));

	/* Header */
	cout << left << setw(20) << "Name" << setw(10) << "Earnings" << endl;
	cout << setfill('-') << setw(30) << "" << endl << setfill(' ');

	for( unsigned i = 0; i < employeeList.size(); i++ )
	{
		/* Because typing sucks. */
		double &rate = employeeList[i].rate;
		double &hours = employeeList[i].hours;

		/* Skip employees with no earnings. */
		if( 0 == (hours * rate))
			continue;

		/* Overtime. */
		double ot = hours - 40;

		/* Calculate total earnings. */
		double earned = ((ot > 0) ? ((40 * rate) + (ot + (rate * 1.5))) : (hours * rate));

		cout << left << setw(20) << employeeList[i].name << setw(10) <<  earned << endl;
	}
}


vector<int*> Ex2_37_Encrypt(vector<int*> codes, unsigned codeLength = 4)
{
	cout << "Encrypted data "<< endl;
	for( unsigned i = 0; i < codes.size(); i++ ){ 
		std::swap(codes[i][0], codes[i][2]);
		std::swap(codes[i][1], codes[i][3]);

		for( unsigned j = 0; j < codeLength; j++ ){
			codes[i][j] += 7;
			codes[i][j] %= 10;
			cout << codes[i][j] << " ";
		}
		cout << endl;
	}

	return codes;
}

vector<int*> Ex2_37_Decrypt(vector<int*> codes, unsigned codeLength = 4)
{
	cout << "Decrypted data " << endl;
	for( unsigned i = 0; i < codes.size(); i++ ){
		std::swap(codes[i][0], codes[i][2]);
		std::swap(codes[i][1], codes[i][3]);

		for( unsigned j = 0; j < codeLength; j++ ){
			codes[i][j] += 3;
			if(codes[i][j] > 10) codes[i][j] -= 10;
			cout << codes[i][j] << " ";
		}
		cout << endl;
	}

	return codes;
}

int main(int argv, char* argc)
{
	// 2.18
	// Develop a C++ program that will determine the gross pay for each of several
	// employees. The company pays a "straight-time" for the first 40 hours worked 
	// by each employee and pays "time-and-a-half" for all hours worked in excess
	// of 40 hours. You are given a list of employees (actually, I wasn't, but...,) 
	// their hours worked, and their rate of pay. Use this information to display
	// their earnings neatly.
	Ex2_18();


	// 2.21 
	// Write an application that determines the highest number out of a list of numbers.
	int list[] = {1,3,4,1,2,19,14};
	cout << *(std::max_element(list,list+7)) << " is the highest number. " << endl;

	// 2.22
	// Write the same application, except find the highest two numbers int the list.
	int *hp = std::max_element(list,list+7);
	int t = *hp;
	cout << *hp << " is the highest number. " << endl;
	*hp = INT_MIN;
	cout << *(std::max_element(list,list+7)) << " is the second highest. " << endl;
	*hp = t;

	// 2.37
	// A company wants to transmit data over the telephone, but they are
	// concerned that their phones may be tapped. All of their data are transmitted 
	// as four-digit integers. They have asked you to write a program that encrypts their
	// data so that it can be transmitted more securely. Your program should read a four-digit integer and
	// encrypt it as follows: Replace each digit by (the sum of that digit plus seven) modulus 10. 
	// Then swap the first digit with the third, swap the second digit with the fourth. 
	// Write both methods for encryption and decryption.

	vector<int*> data;
	int codes[][4] = {{1,2,3,4},{2,4,1,8},{2,4,1,2}};
	data.push_back(codes[0]);
	data.push_back(codes[1]);
	data.push_back(codes[2]);

	Ex2_37_Decrypt(Ex2_37_Encrypt(data,4));

	// 2.48
	// Make a bar-graph or histogram out of a list of numbers.
	cout << "Histogram: " << endl << setfill('*');
	for( unsigned i = 0; i < 7; i++ )
		cout << setw(list[i]) << "" << endl;

	return EXIT_SUCCESS;
}


Output:
Name                Earnings  
------------------------------
Tom Smith           910       
Aaron Karr          992.7     
Roger Wilco         1678.3    
19 is the highest number. 
19 is the highest number. 
14 is the second highest. 
Encrypted data 
0 1 8 9 
8 5 9 1 
8 9 9 1 
Decrypted data 
1 2 3 4 
2 4 1 8 
2 4 1 2 
Histogram: 
*
***
****
*
**
*******************
**************


Create a new paste based on this one


Comments: