Sample Finals

Sample 1:

//
Point.h:
#ifndef POINT_H
#define POINT_H

class Point
{
private:
    double xcoord, ycoord;
public:
    Point();
    Point(double x, double y);
    bool operator==(const Point& p);
    double operator-(const Point& b);
};

#endif // POINT_H

Point.cpp:

#ifndef POINT_H
#define POINT_H

class Point
{
private:
    double xcoord, ycoord;
public:
    Point();
    Point(double x, double y);
    bool operator==(const Point& p);
    double operator-(const Point& b);
};

#endif // POINT_H

main.cpp:

#include <iostream> //Required for cout
#include "Point.h"
using namespace std;
int main()
{
//Declare and initialize objects.
Point p1;
Point p2(1.5, -4.7);
//Test operators
if( p1 == p2)
{
cout << "p1 is equal to p2" << endl;
}
else
{
cout << "Distance between p1 and p2 is " << p1 - p2 << endl;
}
return 0;
}


Sample 2:

//Decoding message
#include <iostream>
#include <fstream>
#define FILENAME "decode.txt"
#define ARRAYSIZE 500

using namespace std;

int main()
{
    ifstream script;
    int k = 0;
    string decoding[ARRAYSIZE];
    string sentence;
    string decode;
    script.open(FILENAME);
    if (script.fail())
    {
        cout <<"Error opening input file." << endl;
        cout << "Enter words instead?" << endl;
        getline(cin, sentence);
        for (int i = 0; i <= sentence.length(); i++)
        {
            if (sentence[i] == ' ')
            {
                k++;
            }
            else
            {
               decoding[k] += sentence[i];
            }
        }
        k = k + 1;
        for (int j = 0; j < k; j++)
        {
            decode = decoding[j];
            cout << decode[1];
        }
        cout << endl;
    }
    else
    {
      while (!script.eof())
      {
          script >> decoding[k];
          cout << decoding[k] << " ";
          k++;
      }
    }
    cout << "\n\nThere are " << k << " words" << endl;
    cout << endl;
    for (int j = 0; j < k; j++)
    {
        decode = decoding[j];
        cout << decode[1];
    }
    script.close();
    return 0;
}

Comments

Popular posts from this blog

Lab 13/ HW 13

Lab 21/ HW 21