(原创)谭浩强C++程序设计学习笔记:第3章 程序设计初步

怎样设计步骤,怎样保证它的正确性和具有较高的效率,就是:算法需要解决的问题。

一个面向过程的程序应包括以下两方面内容:
        (1) 对数据的描述。在程序中要指定数据的类型和数据的组织形式,即数据结构 (data structure)。
        (2) 对操作的描述。即操作步骤,也就是算法 (algorithm)。

程序 = 算法 + 数据结构

作为程序设计人员,必须认真考虑和设计数据结构和操作步骤 (即算法)。

算法是处理问题的一系列的步骤。算法必须具体地指出在执行时每一步应当怎样做。

广义地说,为解决一个问题而采取的方法和步骤,就称为 “算法”。

计算机算法可分为两大类别:数值算法和非数值算法。数值算法的目的是求数值解。非数值算法包括的面十分广泛,最常见的是用于事务管理领域。

算法的表示:

    - 自然语言<容易产生歧义性,一般不采用>

    - 流程图<形象直观,不易维护更改>

    - 伪代码<形象直观,易于维护>,例如:

    - 用计算机语言表示算法(即程序)

每一个程序单位有以下组成:

    - 预处理命令

    - 声明部分

    - 函数

// test1
// 一个c++程序
#include <iostream>
using namespace std;
int a = 3;
int main() {
    float b;
    b = 4.5;
    cout << a << b << endl;
    return 0;
}
// 输出:3 4.5

如果一个变量在函数内声明,此变量是局部变量,它的有效范围是从该行开始到本函数结束。

程序应该包括数据描述(由声明语句来实现)和数据操作(由执行语句来实现)。数据描述主要包括数据类型的声明、函数和变量的定义、变量的初始化等。数据操作的任务是对已提供的数据进行加工。

C++语句一般分一下4种:

    - 声明语句

    - 执行语句

        - 控制语句:if else, for, while, do-while, continue, break, goto, switch, return;

        - 函数和流对象语句:如sort(); cout << "" << endl;

        - 表达式语句,如 i = i + 1;  // 分号不能少  -- 一个语句必须在最后出现分号

    - 空语句(;)

    - 复合语句( {} )

赋值语句

    赋值语句是由赋值表达式加上一个分号构成。

    

输入和输出

   

    需要使用预处理命令:#include <iostream>

    在输入流与输出流中使用控制符

   注意的是:如果使用了控制符,在程序单位的开头除了要加 iostream 头文件外,还要加 iomanip 头文件。

    

// test2
// 各行小数点对齐
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    double a = 123.456, b = 3.14159, c = -3214.67;
    cout << setiosflags(ios::fixed) << setiosflags(ios::right) << setprecision(2);
    cout << setw( 10 ) << a << endl;
    cout << setw( 10 ) << b << endl;
    cout << setw( 10 ) << c << endl;
    return 0;
}
// 输出:
//   123.46
//     3.14
// -3214.67

    先统一设置定点形式输出、取两位小数、右对齐。这些设置对其后的输出均有效 (除非重新设置),而 setw 只对其后一个输出项有效,因此必须在输出 a,b,c 之前都要写 setw(10)。

putchar 函数的作用是向终端输出一个字符。

// test3
// 输出单个字符
#include <iostream>
using namespace std;
int main() {
    char a, b, c;
    a = 'B';
    b = 'O';
    c = 'Y';
    putchar( a );
    putchar( b );
    putchar( c );
    putchar( '
' );
    
    putchar( 66 );
    putchar( 79 );
    putchar( 89 );
    putchar( 10 );
    return 0;
}
// 输出:
// BOY
// BOY

getchar 函数(字符输入函数): 此函数的作用是从终端(或系统隐含指定的输入设备)输入一个字符。

// test4
// 输入单个字符
#include <iostream>
using namespace std;
int main() {
    char c;
    c = getchar();
    putchar( c + 32 );
    putchar( '
' );
    return 0;    
}
// 输入:A
// 输出:a

getchar( ) 只能接收一个字符。

用 printf 函数进行输出,用 scanf 函数进行输入的。

// test5
// 用 scanf 和 printf 函数进行输入和输出
#include <iostream>
using namespace std;
int main() {
    int a;
    float b;
    char c;
    scanf( "%d %f %c", &a, &b, &c );
    printf( "a = %d, b = %f, c = %c", a, b, c );
    return 0;
}
// 输入:12 67.98 A
// 输出:a = 12, b = 67.980003, c = A
// test6
// 求一元二次方程式的根(ax^2 + bx + c = 0 )( 它们的值满足 b^2 - 4ac >= 0 )
#include <iostream>
#include <cmath>
using namespace std;
int main() {
    float a, b, c, x1, x2;
    cin >> a >> b >> c;
    x1 = ( -b + sqrt( b * b - 4 * a * c ) ) / ( 2 * a );
    x2 = ( -b - sqrt( b * b - 4 * a * c ) ) / ( 2 * a );
    cout << "x1 = " << x1 << end;
    cout << "x2 = " << x2 << end;
    return 0;
}
// 输入:4.5 8.8 2.4
// 输出:
// x1 = -0.327612
// x2 = -1.17794

可以看到:顺序结构的程序中的各执行语句是顺序执行的。这种程序最简单,最容易理解。

if 语句提供条件选择。

C++ 增加了逻辑型数据。逻辑型常量只有两个,即 false(假) 和 true(真) 。逻辑型变量要用类型标识符 bool 来定义,它的值只能是 true 和 false 之一。

由于逻辑变量是用关键字 bool 来定义的,因此又称为布尔变量。逻辑型常量又称为布尔常量。所谓逻辑型,就是布尔型。

在编译系统处理逻辑型数据时,将 false 处理为0,将 true 处理为1。

如果其值是 0 就认为是 “假” ,如果其值是 非0 就认为是 “真”。

逻辑运算结果不是 0就是 1,不可能是其他数值。

if 语句是用来判定所给定的条件是否满足,根据判定的结果(真或假)决定执行给出的两种操作之一。

// test7
// 求三角形的面积
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
    double a, b, c;
    cout << "please enter a, b, c: ";
    cin >> a >> b >> c;
    if ( a + b + c && b + c > a && c + a > b ) {
        double s, area;
        s = ( a + b + c ) / 2;
        area = sqrt( s * ( s - a ) * ( s - b ) * ( s - c ) );
        cout << setiosflags( ios::fixed ) << setprecision( 4 );
        cout << "area = " << area << endl;
    }
    else {
        cout << "it is not a trilateral!" << dendl;
    }
    return 0;
}
// 输出:please enter a, b, c:
// 2.45 3.67 4.89
// area = 4.3565

// test8
// 输入一个字符,如果是大写,则转换为小写
#include <iostream>
using namespace std;
int main() {
    char ch;
    cin >> ch;
    ch = ( ch >= 'A' && ch <= 'Z' ) ? ( ch + 32 ) ? ch;
    cout << ch << endl;
    return 0;
}

switch 语句是多分支选择语句,用来实现多分支选择结构。当选择多时,用 switch 会比用 if-else 效率更高。

// test9
// 判断某一年是否为闰年
#include <iostream>
using namespace std;
int main() {
    int year;
    bool leap;
    cout << "please enter year: ";
    cint << year;
    if ( year % 4 == 0 ) {
        if ( year % 100 == 0 ) {
            if ( year % 400 == 0 ) {
                leap = true;
            }
            else {
                leap = false;
            }            
        }
        else {
            leap = true;
        }
    }
    else {
        leap = false;
    }
    if ( leap ) {
        cout << year << "is";
    }
    else {
        cout << year << "is not";
    }
    cout << "a leap year." << endl;
    return 0;
}
// test10
// 运输折扣
#include <iostream>
using namespace std;
int main() {
    int c, s;
    float p, w, d, f;
    cout << "please enter p, w, s: ";
    cin >> p >> w >> s;
    if ( s >= 3000 ) {
        c = 12;
    }
    else {
        c = s / 250;
    }
    switch( c ) {
        case 0:
            d = 0;
            break;
        case 1: 
            d = 2;
            break;
        case 2:
        case 3:
            d = 5;
            break;
        case 4:
        case 5:
        case 6:
        case 7:
            d = 8;
            break;
        case 8:
        case 9:
        case 10:
        case 11:
            d = 10;
            break;
        case 12:
            d = 15;
            break;
    }
    f = p * w * s * ( 1 - d / 100.0 );
    cout << "freight = " << f << endl;
    return 0;
}
// 输出:please enter p, w, s: 100 20 300
// freight = 588000

循环结构和循环语句

    while:先判断表达式,后执行语句。

// test11
// 求1+2+3+...+100
#include <iostream>
using namespace std;
int main() {
    int i = 1, sum = 0;
    while( i <= 100 ) {
        sum += i;
        ++i;
    }
    cout << "sum = " << sum << endl;
    return 0;
} 

    do-while:先执行一次循环体,再判断表达式是否为真,根据判断结果判断是否需要继续执行循环体。

// test12
// 用do-while语句求1+2+...+100
#include <iostream>
using namespace std;
int main() {
    int i = 1, sum = 0;
    do {
        sum += i;
        ++i;
    } while( i <= 100 );  // 注意分号
    cout << "sum = " << sum << endl;
    return 0;
}

    

    for:   for( 表达式 1;表达式 2;表达式 3 ) 语句

        (1) 先求解表达式 1。

        (2) 求解表达式 2,若其值为真 (值为非 0),则执行 for 语句中指定的内嵌语句,然后执行下面第 (3) 步。若为假 (值为 0),则结束循环,转到第 (5) 步。

        (3) 求解表达式 3。

       (4) 转回上面第 (2) 步骤继续执行。

       (5) 循环结束,执行 for 语句下面的一个语句。

break 语句和 continue 语句

    break: 使流程跳出 switch 结构;  使流程从循环体内跳出循环体,即提前结束循环,接着执行循环体下面的语句。

    continue:结束本次循环,即跳过循环体中下面尚未执行的语句,接着进行下一次是否执行循环的判定。

    continue 语句和 break 语句的区别是: continue 语句只结束本次循环,而不是终止整个循环的执行。而 break 语句则是结束整个循环过程,不再判断执行循环的条件是否成立。

// test13
// 求π得近似值
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
    int s = 1;
    double n = 1, t = 1, pi = 0;
    while( ( fabs(t) ) > 1e-7 ) {
        pi = pi + t;
        n = n + 2;
        s = -s;
        t = s / n;
    }
    pi = pi * 4;
    cout << "pi = " << endl;
    return 0;
}
// test14
// 求Fibonacci数列前40个数
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    long f1, f2;
    int i;
    for ( i = 1; i <= 20; ++i ) {
        cout << f1 << f2;
        if ( i % 2 == 0 ) {
            cout << endl;
        }
        f1 = f1 + f2;
        f2 = f2 + f1;
    }
    return 0;
}
// test15
// 找出100~200间的全部素数
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
    int m, k, i, n = 0;
    bool prime;
    for ( m = 101; m <= 200; m = m + 2 ) {
        prime = true;
        k = int( sqrt( m ) );
        for ( i = 2; i < k; ++i ) {
            if ( m % i == 0 ) {
                prime = false;
                break;
            }
        }
        if ( prime ) {
            cout << m;
            ++n;
        }
        if ( n % 10 == 0 ) {
            cout << endl;
        }
    }
    cout << endl;
    return 0;
}
// test16
// 译密码
#include <iostream>
using namespace std;
int main() {
    char c;
    while( ( c = getchar() ) != '
' ) {
        if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) {
            c = c + 4;
            if ( c > 'Z' && c <= 'Z' + 4 || c > 'z' ) {
                c = c -26;
            }
        }
        cout << c;
    }
    cout << endl;
    return 0;
}
// 运行:
// I am going to Beijing!
// M eq ksmrk xs Fimnmrk!

 

    

    

原文地址:https://www.cnblogs.com/wodehao0808/p/12912178.html