System Learning C + + (2) __c++

Source: Internet
Author: User

The first part is C + + some basic knowledge, has not involved in paragraph code, and are some scattered knowledge points, the second part, involving the knowledge of functions, the code involved in the knowledge point will be a little more.

Exercise 6.4: Write a function that interacts with the user, asking the user to enter a number to calculate the factorial that generated the number. Call the function in the main function.

Program implementation:

#include <iostream>

using namespace std;

int factorial (int i)
{
    int factor = 1;
    if (i==0 | | i==1) return
        1;
    else
    {while
        (i>1)
            factor *= i--;
        Return factor
    }
}

int main ()
{
    int i;
    while (true)
    {
        cout << ' Please input a number: ';
        Cin >> I;
        Try
        {
            if (i<0)
                throw Runtime_error ("it should be greater than");
            cout << factorial (i) << Endl;
        }
        catch (Runtime_error e)
        {
            cout << e.what () << Endl;
        }
        cout << "Again?" Enter y or N: ";
        char c;
        CIN >> C;
        if (!cin| | c== ' n ')  //If no characters are entered (direct enter) or n break is entered
            ;
    }

Run Result:

Exercise 6.6: Describe the difference between a formal parameter, a local variable, and a local static variable. Write a program that uses three of these forms.

Code implementation:

#include <iostream>

using namespace std;

The void fun1 (int i)  //I is the formal parameter
{
    i++;
    cout << i << "";
    static int num=0;  Num is a local static variable at the end of the  program will be destroyed.
    num++;
    cout << num << endl;
}

int main ()
{
    int j=10;//j is local variable for
    (int x=0;x<5;++x)
    {
        fun1 (j);
    }
    return 0;
}

Run Result:

Exercise 6.7: Write a function that returns 0 when it is first invoked, and then returns a value of 1 at a later time.

Code implementation:

#include <iostream>

using namespace std;

You can use static local variables to implement
int Countcall ()
{
    static int count = 0;
    return count++;
}

int main ()
{
    //call 10 for
    (int i=0;i<10;i++)
    {
        cout <<  i+1 <<  Call: "<< countcall () << Endl;
    }
}

Run Result:

Exercise 6.10: Write a function that uses a pointer parameter to exchange the value of two integers.

Code implementation:

#include <iostream>
using namespace std;

The classic topic of pointer passing value.
void Exchange (int *p,int *q)
{
    int t;
    t = *p;
    *p = *q;
    *q = t;
};

int main ()
{
    int a=1,b=2;
    int *p=&a, *q = &b;
    cout << "Before:" << a << "" << B <<endl;
    Exchange (P,Q);
    cout << "After:" << a << "" << b << Endl;
    return 0;
}

Run Result:

Exercise 6.12: Use a reference instead of a pointer to exchange a value of two integers.

The question can be compared with the previous one.
Code implementation:

#include <iostream>

using namespace std;

void change (int &a, int &b)
{
    int t=0;;
    t = A;
    A = b;
    b = t;
}

int main ()
{
    int a = 1,b=2;
    cout << "Before:" << a << "" << B <<endl;
    Change (a,b);
    cout << "After:" << a << "" <<b <<endl;
    return 0;
}

Run Result:

Exercise 6.42: Give the second parameter of the Make_plural function (see page 201) The default argument ' s ', using the new version of the function to output the singular and plural forms of the word sucess and failure.

Analysis: Look at the make_pural function, the feeling should be given a default value of the first three parameters.
Code implementation:

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

String Make_plural (size_t ctr, const string &word,
    const string &ending = "s")
{return
    (ctr > 1)? Word + Ending:word;
}

int main (int argc, char *argv[])
{
    cout << "The Single Form is:" << make_plural (1, "success") <& Lt "" << make_plural (1, "failure") <<endl;
    cout << "The plural form is:" << make_plural (2, "Success", "es") << "<< make_plural (2," failure ") <<endl;
}

Run Result:

Exercise 6.47: A program that uses recursive output vector content to conditionally output information related to the execution process.

Analysis: Recursion can be considered in conjunction with the vector of the method function as a recursive condition, output information according to this section, you can consider #ifndef ndebug.

Program implementation:

#include <iostream>
#include <vector>

using namespace std;

Using recursion to output the elements of a vector object
//Open and close compiler

void Printvec (vector<int> vec)
{
#ifndef ndebug
    cout < < "function Name:" << __function__ <<endl;
    cout << "vector size is:" << vec.size () <<endl;
#endif
    if (!vec.empty ())
    {
        int tmp = Vec.back ();  Remove the last element of the VEC
        vec.pop_back ();  At this time VEC removes the last element
        Printvec (VEC);
        cout << tmp << "";
    }
}

int main (int argv, char *argc[])
{
    int a[] = {1,2,3,4,5};
    Vector<int> VEC (a,a+5);
    Printvec (VEC);
    return 0;
}

Run Screenshots:

Exercise 6.51: Write the 4 versions of function f, so that each of them outputs a single message that can be distinguished.

Analysis: Mainly depends on the function of the number of parameters and types of differences

Program implementation:

#include <iostream>

using namespace std;

void f (double,int)
{
    cout << "Call F1" <<endl;
}
void f (int)
{
    cout << "Call F2" << Endl;
}

void f (int, int)
{
    cout << "Call F3" << Endl;
}

void f (double, double)
{
    cout << ' call F4 ' <<endl;
}

int main (int argv, char *argc)
{
    F (2.56,42);
    f (a);
    f (42,0);
    f (2.56,3.14);
}

Not to be continued ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.