In: Computer Science
C++ program, include comments stating what each part of code does please. I want to be able to understand it so I'll be more knowledgeable in the future. The program is multiple files(fibonacci.h file, fibonacci.cpp file, main.cpp file and loops_simple_data_test.cpp). After the directions I also included any starter code or comments left by my professor within the files to aide us.
Directions:
In folder 04_loops_simple_data write prototype and definition for string value - return function get_fibonacci with an int parameter that returns the fibonacci sequence up to that number. Write the required unit test(s) in folder 04_loops_simple_data_test. Main program flow: Program runs until user opts out. For each loop prompt user for a number, use number as function argument, call get_fibonacci function and display the output.
fibonacci.h
/* | |
Write prototype for string value-return function get_fibonacci with an int | |
parameter that returns the fibonacci sequence up to that number. | |
*/ |
fibonacci.cpp
/* | |
Write prototype for string value - return function get_fibonacci with an int | |
parameter that returns the fibonacci sequence up to that number. | |
DO NOT USE A RECURSIVE FUNCTION | |
*/ |
main.cpp
//Write include statements | |
//Write using statements | |
/* | |
Program runs until user opts out. | |
For each loop prompt user for a number, use number as function argument, | |
call get_fibonacci function and display the output. | |
*/ | |
int main() | |
{ | |
return 0; | |
} |
loops_simple_data_test.cpp
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file | |
#include "catch.hpp" | |
//Write includes | |
/* | |
Write test case for get fibonacci function with values | |
10 result "0, 1, 1, 2, 3, 5, 8" | |
and | |
5 result "0, 1, 1, 2, 3, 5" | |
*/ |
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks
fibonacci.h
#pragma once
#include<string>
/*
Write prototype for string value-return function
get_fibonacci with an int
parameter that returns the fibonacci sequence up to
that number.
*/
class fibonacci
{
public:
std::string get_fibonacci(int);
};
fibonnacci.cpp
#include "fibonacci.h"
std::string fibonacci::get_fibonacci(int num){
std::string series="";
int first=1,second=1,temp;
if(num<0){
return series;
}else if(num==0){
series="0 ";
}
else if(num==1){
series="0 1 1";
}
else{
series="0 1 1";
temp=first+second;
while(temp<=num){
series=series+"
"+std::to_string(temp);
first=second;
second=temp;
temp=first+second;
}
}
return series;
}
Main.cpp
// fibonacciSequence.cpp : Defines the entry point for the
console application.
//
#include "fibonacci.h"
#include "iostream"
using namespace std;
int getNumber(){
int num;
cin>>num;
cin.clear();
cin.ignore();
return num;
}
char getChoice(){
char ch;
cin>>ch;
cin.clear();
cin.ignore();
return ch;
}
void main(){
fibonacci obj;
int num;
char choice;
while(true){
cout<<"Please give number
:";
num=getNumber();
cout<<obj.get_fibonacci(num)<<endl;
cout<<"Do you want to check
again? (y) :";
choice=getChoice();
if(choice!='y'){
break;
}
}
system("pause");
}
loops_simple_data_test.cpp
// tests-main.cpp
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "fibonacci.h"
TEST_CASE("Testing Fibnacci") {
fibonacci testObject;
SECTION("First section, works") {
REQUIRE(obj.get_fibonacci(5)=="0 1 1 2 3 5");
}
SECTION("Second section, works") {
REQUIRE(obj.get_fibonacci(10)=="0 1 1 2 3 5 8");
}
SECTION("Second section, fails") {
REQUIRE(obj.get_fibonacci(10)=="1 1 2 3");
}
}
Output: