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

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...
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...
Term Project C++ Pet Class Problem Specification: Design a class named Pet, which should have the...
Term Project C++ Pet Class Problem Specification: Design a class named Pet, which should have the following fields: • name: The name field holds the name of a pet. • type: The type field holds the type of animal that a pet is. Example values are “Dog”, “Cat”, and “Bird”. • age: The age field holds the pet’s age. The Pet class should also have the following methods: • setName: The setName method stores a value in the name field....
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,...
Write in drjava Problem Design and implement these 4 files: A parent class called Plant with...
Write in drjava Problem Design and implement these 4 files: A parent class called Plant with name (eg Rose, Douglas Fir) and lifespan (could be in days, weeks, months or years) attributes Tree inherits from Plant and adds a height attribute Flower inherits from Plant and adds a color attribute A driver file to test the 3 classes above. The classes described in #1, 2 and 3 above should have the usual constructors (default and parameterized), get (accessor) and set...
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...
JAVA: You're given 3 files. Demo.java, SampleInterace.java, and OverflowException.java. Use your Demo class to implement SampleInterface,...
JAVA: You're given 3 files. Demo.java, SampleInterace.java, and OverflowException.java. Use your Demo class to implement SampleInterface, and the OverflowException class should handle any errors that may come from the addNum method. Demo.java: public class Demo implements SampleInterface { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated constructor stub } @Override public void addNum(int value) throws OverflowException { // TODO Auto-generated method stub } @Override public void removeNum(int value) { // TODO Auto-generated method stub...
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT