1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
Author: Gerald E. Wright JR
Assignment: Program 1
File: Driver.cpp
Instructor: Reza Sanati-Mehrizy
Class: CS 2420 Section: 001
Date: 11 May 2015
Description: Recursive and Iterative Fibonacci Sequence
I declare that the following source code was written solely by Me. I understand that
copying any source code, in whole or in part, constitutes cheating, and that I will
receive a zero on this project if I am found in violation of this policy.
*/
#include "File_Exception.h"
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
// File name Input Prototype
// Purpose: Get the name of the file to open
// Parameters: string
// Return: string
string fileName();
// Fibonacci Recursive Prototype
// Purpose: Handle the Fibonacci math
// Parameters: int
// Returns: int
int fibIn(int);
// Fibonacci Iterative Prototype
// Purpose: Handle the Fibonacci math
// Parameters: int
// Returns: int
int nonFib(int);
const int NUMBACK = 2, NUMCONST = 5, DIVISOR = 2, TAB1 = 5, TAB2 = 13, TAB3 = 8, TAB4 = 8, TAB5 = 13;
int numOut = 0, correctName =0;
int main()
{
cout << endl << endl << endl;
cout << "Author: Gerald E. Wright JR " << endl;
cout << "Assignment: Program 1" << endl;
cout << "Instructor: Reza Sanati-Mehrizy" << endl;
cout << "Class: CS 2420 Section: 001" << endl;
cout << "Date: 11 May 2015" << endl;
cout << "Description: Recursive and Iterative Fibonacci Sequence" << endl << endl;
system("PAUSE");
cout << endl;
system("CLS");
string name = fileName(), choice ="";
//Read Stream Declaration and file declaration.
ifstream dataIn;
dataIn.open(name);
//Test for fail
if (dataIn.fail())
{
cout << endl << "Error while opening the file, " << name << ", cannot continue." << endl;
cout << endl << "Application terminating" << endl;
system("PAUSE");
return 1;
}// end if
//recusrive code
string inNum = "";
system("CLS");
cout << " " << left << setw(TAB1) << "N" << setw(TAB2) << "Recursive" << setw(TAB3) << "Iterative" << endl;
cout << "==============================" << endl << endl;
while (!dataIn.eof())
{
try
{
getline(dataIn, inNum);
cout << " " << left << setw(TAB4) << inNum << setw(TAB5) << fibIn(stoi(inNum)) << nonFib(stoi(inNum)) << endl;
cout << "==============================" << endl;;
// See if there were any failures
if (dataIn.fail() && !dataIn.eof())
{
throw File_Exception(READ_ERROR);
}//end else if
else if (dataIn.eof())
{
throw File_Exception(END_OF_FILE);
return 0;
}//end else if
}//end try
catch (File_Exception fe) // This code handles the error situation
{
if (fe.getErrorCode() == READ_ERROR)
{
cout << "\nAn error occured while reading the file, unable to continue. ";
cout << "\nApplication is terminating";
dataIn.close();
system("PAUSE");
return 1;
}// End if
}// End catch
catch (invalid_argument ia)
{
cout << "\nAn error occured while reading the file, unable to convert to expected value. ";
cout << "\nApplication is terminating";
dataIn.close();
system("PAUSE");
return 1;
}// End catch
}//end while
//close data stream
dataIn.close();
cout << endl;
system("PAUSE");
return 0;
}//end main
string fileName()
{
string name = "";
cout << "Enter the name of the file you want to open => ";
cin >> name;
return name;
}
int fibIn(int _num)
{
int numIn = _num;
if (numIn <= 0)
{
numOut = 0;
}//end if
else if (numIn==1)
{
numOut = 1;
}//end else if
else
{
numOut = fibIn(numIn - NUMBACK) + fibIn(numIn - 1);
}//end else
return numOut;
}//end fibIn
//Iterative
int nonFib(int n)
{
if (n <= 0)
{
n = 0;
}
return (((1 / sqrt(NUMCONST))*(pow((1 + sqrt(NUMCONST)) / DIVISOR, n)) - ((1 / sqrt(NUMCONST))*(pow((1 - sqrt(NUMCONST)) / DIVISOR, n)))));
}//end nonFib
|