C++面向对象程序设计 笔记整理(1)——编程基础

字符与字符串


大小写转换

void lower_or_upper_case(char c)
{
    int transferred_c;
    if ( ('a'<=c) && (c<='z') )
    {
        transferred_c = (int)c - ( (int)'b' - (int)'B' );
        cout << "The lower/upper case is: " << (char)transferred_c << endl;
    }
    else if ( ('A'<=c) && (c<='Z') )
    {
        transferred_c = (int)c + ( (int)'a' - (int)'A' );
        cout << "The lower/upper case is: " << (char)transferred_c << endl;
    }
    else
        cout << "您输入的不是字母" << endl;

}
int main()
{
    char letter;
    cout << "Please enter a letter: ";
    cin >> letter;
    lower_or_upper_case(letter);

    char c = 'A';

    cout << c << endl;
}

程序的控制结构


例题0: 鸡兔同笼

鸡、兔共有98个头,386只脚,求鸡、兔各多少只。 使用遍历方法求所有解

int main()
{
    for (int i = 0; i <= 98; i++)    // i: chicken
    {
        int j = 98 - i;    // j: rabbit
        if ((2*i + 4*j) == 386)
        {
            cout << "chicken: " << i << " rabbit: " << j << endl;
        }
    }

    return 0;
}

例题1:

π/4 = 1 - 1/3 + 1/5 - 1/7 + ... 直到最后一项的绝对值小于10^-6为止

int main()
{
    double sum = 0;
    double temp = 1;
    int i = 1;

    while (abs(temp) >= 1e-6)
    {
        temp = pow(-1.0, i+1) / (2*i -1);
        cout << fixed << setprecision(0) << pow(-1.0, i+1) << "/" << (2*i -1) << ": ";
        cout << scientific << setprecision(1) << temp << endl;
        i++;
        sum += temp;
    }

    cout << endl;
    cout << sum << endl;
    system("pause");
    return 0;
}

例题2: 阶乘数列求和

S = 1!+2!+...+n!

int factorial(int k)    //阶乘函数
{
    int product = 1;
    for (int i = 1; i <=k; i++)
        product *= i;
    return product;
}

int main()
{
    int sum = 0, n;
    cout << "please enter a number: ";
    cin >> n;

    for (int i=1; i<=n; i++)
    {
        sum += factorial(i);
    }

    cout << "The result is: " << sum << endl;

    return 0;
}

例题3: 菲波那切数列

S = 1+1+2+3+5+8+13+21+...

int sum_fibonacci_sequence()
{
    static int a = 0, b = 1;
    int t = a;
    a = b;
    b = t + b;
    //cout << a << " " << b << endl;
    return a;
}

int main()
{
    int result = 0, n;
    cout << "please enter n: ";
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int an = sum_fibonacci_sequence();
        result += an;
        cout << "an is: " << an << endl;
    }
    cout << result << endl;

    return 0;
}

指针


使用指针动态生成数组

int length;
    cout << "input x length: ";
    cin >> length;
    int* a;
    a = new int[length];
    a[0] = 25;
    cout << a[0] << endl;
    delete[] a;        // 释放内存

递归


使用递归来计算阶乘

long factorial(long n)
{
    cout << "cur n == " << n << endl;
    if (n < 0)
        return -1;
    else if (n == 0 || n == 1)    // 函数终止条件, 小事化了
        return 1;
    else
    {
        long ret;
        ret = n * factorial(n-1);
        cout << "cur ret == " << ret << endl;
        return ret;    // 大事化小, 调用自身进行递归, 想办法把表达式变成 f(n) = g( f(n-1) , n )
    }
}

常用模块


向量 vector

#include <vector>
int main()
{
    vector<int> x;

    x.push_back(1);
    x.push_back(2);
    int y = 345;
    x.push_back(y);  // 动态插入成员
    cout << x.size() << endl;  // 获取长度
    x.pop_back();        // 删除最后一个成员

    for (int k=0; k<x.size(); k++)
    {
        cout << x[k] << endl;
    }

    return 0;
}
(0)

相关推荐