C++ Computer Level 2 Operation Questions (6)

Hits: 0

basic operation

#include<iostream>
#include<cmath>
using namespace std;

class Point{
    double x,y;
public:
    // ERROR **********found**********
    Point(double m=0.0, double n=0.0):m(x), n(y){}
    friend double distanceBetween(const Point& p1, const Point& p2);
};
// ERROR **********found********** 
double Point::distanceBetween( const Point& p1, const Point& p2){ //returns the distance between two points 
    double dx =p1.x-p2.x;
     double dy=p1.y-p2.y;
     return sqrt(dx*dx+dy*dy);
}

int main(){
    Point f1(0,0), f2(1,1);
    // ERROR **********found**********
    cout<<distanceBetween(Point f1, Point f2)<<endl;
    return 0;
}

Basic Operation Answers

#include<iostream>
#include<cmath>
using namespace std;

class Point{
    double x,y;
public:
    // ERROR **********found**********
    Point(double m=0.0, double n=0.0):x(m), y(n){}
    friend double distanceBetween(const Point& p1, const Point& p2);
};
// ERROR **********found********** 
double  distanceBetween ( const Point& p1, const Point& p2 ) { //returns the distance between two points 
    double dx=p1. x-p2.x;
     double dy=p1.y-p2.y;
     return sqrt(dx*dx+dy*dy);
}

int main(){
    Point f1(0,0), f2(1,1);
    // ERROR **********found**********
    cout<<distanceBetween(f1, f2)<<endl;
    return 0;
}

simple application

#include<iostream>
using namespace std;

class  Switchable {     //A device with two states of on and off 
    bool is_on;      //true means "on", false means "off" 
public :
    Switchable(): is_on( false ){}
     void  switchOn () { is_on= true ; }     //Set to "on" state 
    //**********found********* * 
    void  switchOff () {_________________________________} //Set to "off" state 
    bool  isOn () { return is_on; }  //Return to device status 
    //**********found******* *** 
    virtual  const  char * getDeviceName () __________ ;    //A pure virtual function that returns the device name
};

class  Lamp :  public Switchable{
 public :
     //Return the device name, used to override the pure virtual function in the base class 
    const  char * getDeviceName () { return  "Lamp" ; }
};

class  Button {         //Button 
    Switchable *device;  //Device controlled by button 
public :
     //**********found********** 
    Button(Switchable &dev): _______________{}   //Initialize the device with the address of the parameter variable 
    bool  isOn () { return device->isOn(); }     //Button state 
    void  push () {                          //Press the button to change the state 
        if (isOn()) device->switchOff( );
         //************found********** 
        else ___________________________
    }
};

int main(){
    Lamp lamp;
    Button button (lamp) ;
     cout << "The state of the lamp:" <<(lamp.isOn()? "On" : "Off" )<< endl ;
     cout << "The state of the button:" <<(button. isOn()? "On" : "Off" )<< endl ;

    button.push(); //click the button

    cout << "Lamp status:" <<(lamp.isOn()? "On" : "Off" )<< endl ;
     cout << "Button status:" <<(button.isOn()? "On " : "off" )<< endl ;

    return 0;
}

Simple operation answer

#include<iostream>
using namespace std;

class  Switchable {     //A device with two states of on and off 
    bool is_on;      //true means "on", false means "off" 
public :
    Switchable(): is_on( false ){}
     void  switchOn () { is_on= true ; }     //Set to "on" state 
    //**********found********* * 
    void  switchOff () {is_on= false ;} //set to "off" state 
    bool  isOn () { return is_on; }  //return device status 
    //**********found**** ****** 
    virtual  const  char * getDeviceName () = 0 ;    // pure virtual function that returns the device name
};

class  Lamp :  public Switchable{
 public :
     //Return the device name, used to override the pure virtual function in the base class 
    const  char * getDeviceName () { return  "Lamp" ; }
};

class  Button {         //Button 
    Switchable *device;  //The device controlled by the button 
public :
     //**********found********** 
    Button(Switchable &dev): device(&dev ){}     //Initialize the device with the address of the parameter variable 
    bool  isOn () { return device->isOn(); }     //Button state 
    void  push () {                          //Press the button to change the state 
        if (isOn()) device- >switchOff();
         //**********found********** 
        else device->switchOn();

    }
};

int main(){
    Lamp lamp;
    Button button (lamp) ;
     cout << "The state of the lamp:" <<(lamp.isOn()? "On" : "Off" )<< endl ;
     cout << "The state of the button:" <<(button. isOn()? "On" : "Off" )<< endl ;

    button.push(); //click the button

    cout << "Lamp status:" <<(lamp.isOn()? "On" : "Off" )<< endl ;
     cout << "Button status:" <<(button.isOn()? "On " : "off" )<< endl ;

    return 0;
}

Comprehensive application of the original title

//main.cpp
#include "MiniString.h"
#pragma warning (disable:4996)

ostream& operator <<(ostream& o,MiniString& s)
{
    int len=strlen(s.str);
    for(int i=0;i<len;i++)
        cout<<s.str[i];
    return o;
}

MiniString& MiniString::replace( int pos, int n, char *p )
{
    MiniString temp (str) ;  //Create a copy to save the previous string information 
    delete [] str;           //Free the string array 
    int lt= strlen (temp.str),lp= strlen (p);
     int len=lt- n+lp+ 1 ;       //Calculate the size of the space required to store the result string 
    str= new  char [len];        //Apply for the character array required to store the result 
    //Respectively put the first pos characters in temp.str and the characters in p Copy lp characters to str 
    //********333******** 
    //********666******** 
    //put temp. Copy the remaining string after str into str 
    for ( int k=pos+n; k<lt; k++)
        str[pos+lp++]=temp.str[k];
    str[len-1]='
//main.cpp
#include "MiniString.h"
#pragma warning (disable:4996)
ostream& operator <<(ostream& o,MiniString& s)
{
int len=strlen(s.str);
for(int i=0;i<len;i++)
cout<<s.str[i];
return o;
}
MiniString& MiniString::replace( int pos, int n, char *p )
{
MiniString temp (str) ;  //Create a copy to save the previous string information 
delete [] str;           //Free the string array 
int lt= strlen (temp.str),lp= strlen (p);
int len=lt- n+lp+ 1 ;       //Calculate the size of the space required to store the result string 
str= new  char [len];        //Apply for the character array required to store the result 
//Respectively put the first pos characters in temp.str and the characters in p Copy lp characters to str 
//********333******** 
//********666******** 
//put temp. Copy the remaining string after str into str 
for ( int k=pos+n; k<lt; k++)
str[pos+lp++]=temp.str[k];
str[len-1]='\0';
return *this;
}
int main()
{
MiniString mStr("String handling C++ style.");
char iStr[]="STL Power";
cout << "Replace char* into MiniString:\n";
mStr.replace(7, 8, iStr);
cout << mStr << endl;
writeToFile("");
return 0;
}
'; return *this; } int main() { MiniString mStr("String handling C++ style."); char iStr[]="STL Power"; cout << "Replace char* into MiniString:\n"; mStr.replace(7, 8, iStr); cout << mStr << endl; writeToFile(""); return 0; }

//MinString.h
#include <iostream>
#include <string>
#pragma warning (disable:4996)
using namespace std;

class MiniString
{
public:
    MiniString( const  char * s )
    {
        str = new char[strlen(s)+1];
        strcpy(str,s);
    }
    ~MiniString() { delete [] str; }
     //delete n characters starting at pos, then insert string p at pos 
    MiniString& replace ( int pos, int n, char *p) ;    
     char * getStr ()  const  { return str; }
     friend ostream& operator <<(ostream& o,MiniString& s);      //output string str 
private :
     char *str;
};

void writeToFile(const char *);

=====================================================================

Comprehensive application answers

#include "MiniString.h"
#pragma warning (disable:4996)

ostream& operator <<(ostream& o,MiniString& s)
{
    int len=strlen(s.str);
    for(int i=0;i<len;i++)
        cout<<s.str[i];
    return o;
}

MiniString& MiniString::replace( int pos, int n, char *p )
{
    MiniString temp (str) ;  //Create a copy to save the previous string information 
    delete [] str;           //Free the string array 
    int lt= strlen (temp.str),lp= strlen (p);
     int len=lt- n+lp+ 1 ;       //Calculate the size of the space required to store the result string 
    str= new  char [len];        //Apply for the character array required to store the result 
    //Respectively put the first pos characters in temp.str and the characters in p Copy lp characters to str 
    //********333******** 
    strncpy (str,temp.str,pos);   //Copy the first pos characters in temp.str to character array str 
    for ( int i=pos,j= 0 ; i<pos+lp; i++) {str[i]=p[j]; j++;}   //Copy lp characters in p to character in the array str





    //********666******** 
    //Copy the remaining string after temp.str into str 
    for ( int k=pos+n; k<lt; k++)
        str[pos+lp++]=temp.str[k];
    str[len-1]='
#include "MiniString.h"
#pragma warning (disable:4996)
ostream& operator <<(ostream& o,MiniString& s)
{
int len=strlen(s.str);
for(int i=0;i<len;i++)
cout<<s.str[i];
return o;
}
MiniString& MiniString::replace( int pos, int n, char *p )
{
MiniString temp (str) ;  //Create a copy to save the previous string information 
delete [] str;           //Free the string array 
int lt= strlen (temp.str),lp= strlen (p);
int len=lt- n+lp+ 1 ;       //Calculate the size of the space required to store the result string 
str= new  char [len];        //Apply for the character array required to store the result 
//Respectively put the first pos characters in temp.str and the characters in p Copy lp characters to str 
//********333******** 
strncpy (str,temp.str,pos);   //Copy the first pos characters in temp.str to character array str 
for ( int i=pos,j= 0 ; i<pos+lp; i++) {str[i]=p[j]; j++;}   //Copy lp characters in p to character in the array str
//********666******** 
//Copy the remaining string after temp.str into str 
for ( int k=pos+n; k<lt; k++)
str[pos+lp++]=temp.str[k];
str[len-1]='\0';
return *this;
}
int main()
{
MiniString mStr("String handling C++ style.");
char iStr[]="STL Power";
cout << "Replace char* into MiniString:\n";
mStr.replace(7, 8, iStr);
cout << mStr << endl;
writeToFile("");
return 0;
}
'; return *this; } int main() { MiniString mStr("String handling C++ style."); char iStr[]="STL Power"; cout << "Replace char* into MiniString:\n"; mStr.replace(7, 8, iStr); cout << mStr << endl; writeToFile(""); return 0; }

Leave a Reply

Your email address will not be published.