Skip to content

Commit

Permalink
added source code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jesushilarioh committed Sep 2, 2017
1 parent 70dc493 commit 6df7e19
Show file tree
Hide file tree
Showing 551 changed files with 23,411 additions and 0 deletions.
19 changes: 19 additions & 0 deletions SourceCode/Appendix F/PrF-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Demonstrates a simple namespace
#include <iostream>
using namespace std;

namespace test
{
int x, y, z;
}

int main()
{
test::x = 10;
test::y = 20;
test::z = 30;
cout << "The values are:\n";
cout << test::x << " " << test::y
<< " " << test::z << endl;
return 0;
}
30 changes: 30 additions & 0 deletions SourceCode/Appendix F/PrF-2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Demonstrates two namespaces
#include <iostream>
using namespace std;

namespace test1
{
int x, y, z;
}

namespace test2
{
int x, y, z;
}

int main()
{
test1::x = 10;
test1::y = 20;
test1::z = 30;
cout << "The test1 values are:\n";
cout << test1::x << " " << test1::y
<< " " << test1::z << endl;
test2::x = 1;
test2::y = 2;
test2::z = 3;
cout << "The test2 values are:\n";
cout << test2::x << " " << test2::y
<< " " << test2::z << endl;
return 0;
}
17 changes: 17 additions & 0 deletions SourceCode/Appendix F/PrF-3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// A demonstration of the using namespace statement.
#include <iostream>
#include "nsdemo.h"
using namespace std;
using namespace demo;

int main()
{
testObject.x = 10;
testObject.y = 20;
testObject.z = 30;
cout << "The values are:\n"
<< testObject.x << " "
<< testObject.y << " "
<< testObject.z << endl;
return 0;
}
15 changes: 15 additions & 0 deletions SourceCode/Appendix F/nsdemo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file defines a namespace named demo.
// In the demo namespace a class named NsDemo
// is declared, and an instance of the class
// named testObject is defined.

namespace demo
{
class NsDemo
{
public:
int x, y, z;
};

NsDemo testObject;
}
17 changes: 17 additions & 0 deletions SourceCode/Appendix G/argdemo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This program demonstrates how to read
// command line arguments.
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
cout << "You entered " << (argc - 1);
cout << " command line arguments.\n";
if (argc > 1)
{
cout << "Here they are:\n";
for (int count = 1; count < argc; count++)
cout << argv[count] << endl;
}
return 0;
}
18 changes: 18 additions & 0 deletions SourceCode/Appendix G/sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This program takes two command line arguments,
// assumed to be numbers, and displays their sum.
#include <iostream>
#include <cmath> // Needed for atof
using namespace std;

int main(int argc, char *argv[])
{
double total = 0;

if (argc > 1)
{
for (int count = 1; count < argc; count++)
total += atof(argv[count]);
cout << total << endl;
}
return 0;
}
16 changes: 16 additions & 0 deletions SourceCode/Appendix J/Pet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Pet class implementation file
#include <string>
#include "Pet.h"
using namespace std;

// Constructor
Pet::Pet(string str)
{
name = str;
}

// getName member function
string Pet::getName() const
{
return name;
}
20 changes: 20 additions & 0 deletions SourceCode/Appendix J/Pet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Pet class specification file
#ifndef PET_H
#define PET_H
#include <string>
using namespace std;

class Pet
{
private:
string name; // To hold the pet's name

public:
// Constructor
Pet(string);

// Accessor function for name
string getName() const;
};

#endif
13 changes: 13 additions & 0 deletions SourceCode/Appendix J/PrJ-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include "Pet.h"
using namespace std;

int main()
{
// Create an instance of the Pet class.
Pet myPet("Fido");

// Display my pet's name.
cout << "My pet's name is " << myPet.getName() << endl;
return 0;
}
34 changes: 34 additions & 0 deletions SourceCode/Appendix J/PrJ-2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This program uses the Rectangle class, which is declared in
// the Rectangle.h file. The Rectangle class's member functions
// are defined in the Rectangle.cpp file. This program should
// be compiled with those files in a project.
#include <iostream>
#include "Rectangle.h" // Needed for Rectangle class
using namespace std;

int main()
{
Rectangle box; // Define an instance of the Rectangle class
double rectWidth; // Local variable for width
double rectLength; // Local variable for length

// Get the rectangle's width and length from the user.
cout << "This program will calculate the area of a\n";
cout << "rectangle. What is the width? ";
cin >> rectWidth;
cout << "What is the length? ";
cin >> rectLength;

// Store the width and length of the rectangle
// in the box object.
box.setWidth(rectWidth);
box.setLength(rectLength);

// Display the rectangle's data.
cout << "Here is the rectangle's data:\n";
cout << "Width: " << box.getWidth() << endl;
cout << "Length: " << box.getLength() << endl;
cout << "Area: " << box.getArea() << endl;

return 0;
}
62 changes: 62 additions & 0 deletions SourceCode/Appendix J/Rectangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Implementation file for the Rectangle class.
#include "Rectangle.h" // Needed for the Rectangle class
#include <iostream> // Needed for cout
#include <cstdlib> // Needed for the exit function
using namespace std;

//***********************************************************
// setWidth sets the value of the member variable width. *
//***********************************************************

void Rectangle::setWidth(double w)
{
if (w >= 0)
width = w;
else
{
cout << "Invalid width\n";
exit(EXIT_FAILURE);
}
}

//***********************************************************
// setLength sets the value of the member variable length. *
//***********************************************************

void Rectangle::setLength(double len)
{
if (len >= 0)
length = len;
else
{
cout << "Invalid length\n";
exit(EXIT_FAILURE);
}
}

//***********************************************************
// getWidth returns the value in the member variable width. *
//***********************************************************

double Rectangle::getWidth() const
{
return width;
}

//*************************************************************
// getLength returns the value in the member variable length. *
//*************************************************************

double Rectangle::getLength() const
{
return length;
}

//************************************************************
// getArea returns the product of width times length. *
//************************************************************

double Rectangle::getArea() const
{
return width * length;
}
20 changes: 20 additions & 0 deletions SourceCode/Appendix J/Rectangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Specification file for the Rectangle class.
#ifndef RECTANGLE_H
#define RECTANGLE_H

// Rectangle class declaration.

class Rectangle
{
private:
double width;
double length;
public:
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLength() const;
double getArea() const;
};

#endif
33 changes: 33 additions & 0 deletions SourceCode/Appendix J/RectangleData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This program uses the Rectangle class's specification
// and implementation files.
#include <iostream>
#include "Rectangle.h" // Contains the class declaration.
using namespace std;

int main()
{
Rectangle box; // Define an instance of the class.
float rectWidth, // Local varibale for width.
rectLength; // Local variable for length.

// Get the rectangle's width and length from the user.
cout << "This program will calculate the area of a\n";
cout << "rectangle. What is the width? ";
cin >> rectWidth;
cout << "What is the length? ";
cin >> rectLength;

if (!box.setWidth(rectWidth)) // Store the width.
cout << "Invalid value for width.\n";
else if (!box.setLength(rectLength)) // Store the length.
cout << "Invalid value for length.\n";
else
{
// Display the rectangle's data.
cout << "Here is the rectangle's data:\n";
cout << "Width: " << box.getWidth() << endl;
cout << "Length: " << box.getLength() << endl;
cout << "Area: " << box.getArea() << endl;
}
return 0;
}
31 changes: 31 additions & 0 deletions SourceCode/Appendix K/PrK-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This program asks for sales figures for 3 days. The total
// sales is calculated and displayed in a table.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double day1, day2, day3, total;

cout << "Enter the sales for day 1: ";
cin >> day1;
cout << "Enter the sales for day 2: ";
cin >> day2;
cout << "Enter the sales for day 3: ";
cin >> day3;

total = day1 + day2 + day3;

cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);

cout << "\nSales Figures\n";
cout << "-------------\n";
cout << "Day 1: " << setw(8) << day1 << endl;
cout << "Day 2: " << setw(8) << day2 << endl;
cout << "Day 3: " << setw(8) << day3 << endl;
cout << "Total: " << setw(8) << total << endl;

return 0;
}
14 changes: 14 additions & 0 deletions SourceCode/Appendix K/PrK-2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This program uses cin's width member function.
#include <iostream>
using namespace std;

int main()
{
char word[5];

cout << "Enter a word: ";
cin.width(5);
cin >> word;
cout << "You entered " << word << endl;
return 0;
}
Loading

0 comments on commit 6df7e19

Please sign in to comment.