Question

In: Computer Science

c++ The purpose of this project is to test your ability to use files, class design,...

c++

The purpose of this project is to test your ability to use files, class design, operator overloading, and Strings or strings effectively in program design

Create a program which will read a phrase from the user and create a framed version of it for printing. For example, the phrase "hello world"would result in:

   *********
   * hello *
   * world *
   *********

Whereas the phrase "the sky is falling"might be:

   ***********
   * the     *
   * sky     *
   * is      *
   * falling *
   ***********

Or:

   ***********
   *   the   *
   *   sky   *
   *   is    *
   * falling *
   ***********

Or even:

   ***********
   *     the *
   *     sky *
   *      is *
   * falling *
   ***********

Depending on whether or not the user asked for left, centered, or right justification — respectively — of the phrase's words within the frame. Note how the frame exactly fits the phrase based on the longest word within. Neat, eh?

The phrase "O | -+- | /-\"when centered would become:

    *******
    *  O  *
    *  |  *
    * -+- *
    *  |  *
    * /-\ *
    *******

(Okay, so it's a sad excuse for a stick figure, but hey! I've never claimed to be a great good decent artist, now have I? *phbbt*)

Also allow the user to specify what character you are to make the frame from. Typical choices would be @, #, *, +, x, X, o, or O. But let them tell you anything that will print just fine. (Hint: cctype has a function called isprint that tells if a character is printable.)

Give them the option of reading phrases from the keyboard or a file they specify (assume each line of the input stream contains a single phrase).

The user should also be able to choose if the framed phrase is printed on the screen or into a file they specify.

In case it isn't clear, you should create a 'frame the phrase' class. It should have overloaded operators for at leastinput and output. (Although concatenation might prove interesting, too...)

add a choice of frame types: single line, double line, or shaded. For instance:

    +---------+     +============+      +--------+    +=======+
    |         |     ||          ||      |        |*   ||     ||#
    |         |     ||          ||      |        |*   ||     ||#
    +---------+     +============+      +--------+*   +=======+#
                                         **********    #########

Solutions

Expert Solution

/******************************************************************************

   Program to read a phrase and display in a framed version of it.

*******************************************************************************/


#include <string>

#include <vector>

#include <string.h>

#include <stdio.h>

#include <iostream>

using namespace std;

#define MAX_NAME_LENGTH    200

int maxlength;

std::vector<std::string> split(std::string str,std::string sep){
  
   char* cstr=const_cast<char*>(str.c_str());
  
   char* current;
  
   std::vector<std::string> arr;
  
   current=strtok(cstr,sep.c_str());
  
   while(current!=NULL){
      
       arr.push_back(current);
      
       current=strtok(NULL,sep.c_str());
  
   }
  
   return arr;

}

int main()
{
  
   char name[MAX_NAME_LENGTH];
  
   std::vector<std::string> arr;
  
   cout<<"Enter the phrase:"<<endl;
  
   cin.getline(name,MAX_NAME_LENGTH);
  
   
   arr=split(name," ");
  
   for(int j=0;j<arr.size();j++){
      
       if(arr[j].length() > maxlength)
         
       maxlength= arr[j].length();
  
   }
   
   cout<<"############# Output ###############"<<endl;
   for(int k=0;k<=maxlength+2;k++)
      
       printf("%s","*");
  
   for(size_t i=0;i<arr.size();i++)
      
       printf("\n*%s*",arr[i].c_str());
  
   cout<<endl;  
  
   for(int k=0;k<=maxlength+2;k++)
      
       printf("%s","*");
  
   return 0;

}


Related Solutions

Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
Purpose The purpose of this assignment is to give you an opportunity to demonstrate your ability...
Purpose The purpose of this assignment is to give you an opportunity to demonstrate your ability to identify emerging ethical issues in business, interpret the multitude of perspectives inherent in your case study, and model appropriate behaviour by recommending specific solutions. How to Proceed Select a case. It can be one of the textbook cases that we have not discussed during the course. It can also come from the outside world, perhaps a case you have been following in the...
Use the following class for the following problem. The only purpose of the class is to...
Use the following class for the following problem. The only purpose of the class is to display a message both when the constructor is invoked and when the destructor is executed. class Trace { public: Trace(string n); ~Trace(); private: string name; }; Trace::Trace(string n) : name(n) { cout << "Entering " << name << "\n"; } Trace::~Trace() { cout << "Exiting " << name << "\n"; } Requirement: Extend the class Trace with a copy constructor and an assignment operator,...
This assignment will test your knowledge and skills in C++. Create a class named employee and...
This assignment will test your knowledge and skills in C++. Create a class named employee and have the data members of: Name ID Salary Create a class named manager that inherits the employee class and adds the data members: Managed_Employees (Array of up to 3 employees) Department Create methods to update each data member in the employee class. Create a method to print out the managed employees sorted by their salary in the manager class. (You can use one of...
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
Given the following specification, design a class diagram using PlantUML. To design the class diagram, use...
Given the following specification, design a class diagram using PlantUML. To design the class diagram, use abstract, static, package, namespace, association, and generalization on PlantUML Specification: A school has a principal, many students, and many teachers. Each of these persons has a name, birth date, and may borrow and return books. The book class must contain a title, abstract, and when it is available. Teachers and the principal are both paid a salary. A school has many playgrounds and rooms....
Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
The purpose of this assignment is to develop your ability to code and understand ArrayLists. We...
The purpose of this assignment is to develop your ability to code and understand ArrayLists. We will do this by taking a do-it-yourself (DIY) approach by building our own ArrayList-Like data structure called "ArrayBox". You must: Create a generic class called ArrayBox that uses an ARRAY to store its objects. Assume the initial size of the array is two(2). Your ArrayBox must automatically double the size of your elements array when it becomes full as described in class (see slides)....
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT