Androider学C/C++—(3)数学计算,循环,判断,函数,字符串

循环

while循环,for循环, do while循环,嵌套循环,循环控制语句-break, 循环控制语句-continue,循环控制语句-goto。

前面几个循环和控制语句我就不细说了,跟java一样,这里单独说下goto。

类似这么一个程序,当执行到goto HELLO时,它就返回到了上一句,这个HELLO是我自己定义的一个Flag,这是最简单的说法,本程序这么写就是死循环了,因为每次走到goto语句就会返回去。


    #include <iostream>
    using namespace std;

    int main()
    {
        HELLO:cout << " 我说Hello你说YO,Hello YO !Hello Yo!";
        goto HELLO;

        getchar();

        return 0;
    }

正确的写法是,必须要有一个跳出循环的条件类似下边:


    #include <iostream>
    using namespace std;

    int main()
    {

        //HELLO:cout << " 我说Hello你说YO,Hello YO !Hello Yo!";
        //goto HELLO;


        // 局部变量声明
        int a = 10;

        // do 循环执行
        LOOP:do
        {
            if (a == 15)
            {
                // 跳过迭代
                a = a + 1;
                goto LOOP;
            }
            cout << "a 的值:" << a << endl;
            a = a + 1;
        } while (a < 20);



        getchar();

        return 0;
    }

输出:


a 的值:10
a 的值:11
a 的值:12
a 的值:13
a 的值:14
a 的值:16
a 的值:17
a 的值:18
a 的值:19

然后给一个其他常用循环语法的示例:


    #include <iostream>
    using namespace std;

    void main() {

        int num = 1;

        //while 跟java一样
        while (true) {
            cout << "while i == " << num << endl;

            if (num == 10) {
                break;////跟java一样
            }
            num++;
        }

        //for 跟java一样
        for (size_t i = 0; i < num; i++){
            cout << "for i == " << i << endl;
        }


        //dowhile跟java一样  先do再while
        do {
            cout << "do-while num == " << num << endl;
        } while (false);

        //continue 跟java一样
        for (size_t i = 0; i < num; i++) {
            cout << "continue continue continue " << endl;
            continue;
            cout << "for i continue == " << i << endl;
        }


        getchar();


    }

输出:


    while i == 8
    while i == 9
    while i == 10
    for i == 0
    for i == 1
    for i == 2
    for i == 3
    for i == 4
    for i == 5
    for i == 6
    for i == 7
    for i == 8
    for i == 9
    do-while num == 10
    continue continue continue
    continue continue continue
    continue continue continue
    continue continue continue
    continue continue continue
    continue continue continue
    continue continue continue
    continue continue continue
    continue continue continue
    continue continue continue

判断

判断if else swich什么的都跟java一样,这里我就单看下c++的条件运算符 “?”


    #include<iostream>
    using namespace std;

    void main() {


        int var;
        int y = 9;

        var = (y < 10) ? 30 : 40;

        cout << var << endl;

        getchar();

    }

输出:30

函数

  • 函数是一组一起执行一个任务的语句
  • 每个 C++ 程序都至少有一个函数,即主函数 main()
  • 所有简单的程序都可以定义其他额外的函数。
  • 函数声明告诉编译器函数的名称、返回类型和参数。
  • 函数定义提供了函数的实际主体。
  • C++ 标准库提供了大量的程序可以调用的内置函数。例如,函数 strcat() 用来连接两个字符串,函数 memcpy() 用来复制内存到另一个位置。
  • 函数还有很多叫法,比如方法、子例程或程序,等等。

    #include<iostream>
    using namespace std;

    void sayHello();//函数声明,不声明编译不通过

    void main() {

        sayHello();

        getchar();
    }

    void sayHello() {
        cout << " hello " << endl;
    }

数学运算

这个主要就是看api了,需引入头文件


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

    void genRandom();

    int main()
    {
        // 数字定义
        short  s = 10;
        int    i = -1000;
        long   l = 100000;
        float  f = 230.47;
        double d = 200.374;

        // 数学运算
        cout << "sin(d) :" << sin(d) << endl;//该函数返回弧度角(double 型)的正弦。
        cout << "abs(i)  :" << abs(i) << endl;//该函数返回整数的绝对值。
        cout << "floor(d) :" << floor(d) << endl;//该函数返回弧度角(double 型)的正弦。
        cout << "sqrt(f) :" << sqrt(f) << endl;//该函数返回参数的平方根。
        cout << "pow( d, 2) :" << pow(d, 2) << endl;//假设第一个参数为 x,第二个参数为 y,则该函数返回 x 的 y 次方。


        //生成随机数
        genRandom();

        getchar();

        return 0;
    }

    //生成随机数
    //这里又引用到ctime头文件
    void genRandom() {
        int i, j;

        // 设置种子
        srand((unsigned)time(NULL));

        /* 生成 10 个随机数 */
        for (i = 0; i < 10; i++)
        {
            // 生成实际的随机数
            j = rand();
            cout << "随机数: " << j << endl;
        }
    }



输出:

    sin(d) :-0.634939
    abs(i)  :1000
    floor(d) :200
    sqrt(f) :15.1812
    pow( d, 2) :40149.7
    随机数: 3067
    随机数: 27850
    随机数: 13642
    随机数: 2541
    随机数: 29900
    随机数: 24649
    随机数: 9777
    随机数: 21840
    随机数: 14238
    随机数: 11042

C++字符串

一般俩种风格。另外一些api参考:http://www.runoob.com/cplusplus/cpp-strings.html


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

    void main() {

        cout << "*********************C风格***********************" << endl;
        //形态1:char greeting[6]
        char greeting[6] = { 'H', 'e', 'l', 'l', 'o', '\0' };
        cout << "Greeting message:";
        cout << greeting << endl;
        //输出:Greeting message: Hello


        cout << "--------------------------------------------------" << endl;
        //下边这几个则依赖新的头文件:#include <cstring>
        //api参考:http://www.runoob.com/cplusplus/cpp-strings.html
        char str1[11] = "Hello";
        char str2[11] = "World";
        char str3[11];
        int  len;

        // 复制 str1 到 str3
        strcpy(str3, str1);
        cout << "strcpy( str3, str1) : " << str3 << endl;

        // 连接 str1 和 str2
        strcat(str1, str2);
        cout << "strcat( str1, str2): " << str1 << endl;

        // 连接后,str1 的总长度
        len = strlen(str1);
        cout << "strlen(str1) : " << len << endl;

        cout << "*********************C++风格***********************" << endl;
        //依赖新的头文件 #include <string>

        string string1 = "Hello";
        string string2 = "World";
        string string3;
        int  length;

        // 复制 str1 到 str3
        string3 = string1;
        cout << "str3 : " << string3 << endl;

        // 连接 str1 和 str2
        string3 = string1 + string2;
        cout << "str1 + str2 : " << str3 << endl;

        // 连接后,str3 的总长度
        length = string3.size();
        cout << "str3.size() :  " << length << endl;


        getchar();

    }

输出:


    *********************C风格***********************
    Greeting message:Hello
    --------------------------------------------------
    strcpy( str3, str1) : Hello
    strcat( str1, str2): HelloWorld
    strlen(str1) : 10
    *********************C++风格***********************
    str3 : Hello
    str1 + str2 : Hello
    str3.size() :  10

一个字符串异常解决

error C4996: ‘strtok’: This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

参考:https://jingyan.baidu.com/article/49711c616b8a1ffa441b7cdc.html

Demo

本文Demo:https://github.com/zj614android/c-c-/tree/master/Lis3

猜你喜欢

转载自blog.csdn.net/user11223344abc/article/details/80513819