zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然C++语言学习笔记:C++ 实例

    C++ 实例 - 输出 "Hello, World!"
    #include <iostream>
    using namespace std;
     
    int main() 
    {
        cout << "Hello, World!";
        return 0;
    }
    C++ 实例 - 标准输入输出
    #include <iostream>
    using namespace std;
     
    int main()
    {    
        int number;
     
        cout << "输入一个整数: ";
        cin >> number;
     
        cout << "输入的数字为: " << number;    
        return 0;
    }
    C++ 实例 - 实现两个数相加
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int firstNumber, secondNumber, sumOfTwoNumbers;
        
        cout << "输入两个整数: ";
        cin >> firstNumber >> secondNumber;
     
        // 相加
        sumOfTwoNumbers = firstNumber + secondNumber;
     
        // 输出
        cout << firstNumber << " + " <<  secondNumber << " = " << sumOfTwoNumbers;     
     
        return 0;
    }
    C++ 实例 - 求商及余数
    #include <iostream>
    using namespace std;
     
    int main()
    {    
        int divisor, dividend, quotient, remainder;
     
        cout << "输入被除数: ";
        cin >> dividend;
     
        cout << "输入除数: ";
        cin >> divisor;
     
        quotient = dividend / divisor;
        remainder = dividend % divisor;
     
        cout << "商 = " << quotient << endl;
        cout << "余数 = " << remainder;
     
        return 0;
    }
    C++ 实例 - 查看 int, float, doublechar 变量大小
    #include <iostream>
    using namespace std;
     
    int main() 
    {    
        cout << "char: " << sizeof(char) << " 字节" << endl;
        cout << "int: " << sizeof(int) << " 字节" << endl;
        cout << "float: " << sizeof(float) << " 字节" << endl;
        cout << "double: " << sizeof(double) << " 字节" << endl;
     
        return 0;
    }
    C++ 实例 - 交换两个数
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int a = 5, b = 10, temp;
     
        cout << "交换之前:" << endl;
        cout << "a = " << a << ", b = " << b << endl;
     
        temp = a;
        a = b;
        b = temp;
     
        cout << "
    交换之后:" << endl;
        cout << "a = " << a << ", b = " << b << endl;
     
        return 0;
    }
    C++ 实例 - 判断一个数是奇数还是偶数
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int n;
     
        cout << "输入一个整数: ";
        cin >> n;
     
        if ( n % 2 == 0)
            cout << n << " 为偶数。";
        else
            cout << n << " 为奇数。";
     
        return 0;
    }
    C++ 实例 - 判断元音/辅音
    #include <iostream>
    using namespace std;
     
    int main()
    {
        char c;
        int isLowercaseVowel, isUppercaseVowel;
     
        cout << "输入一个字母: ";
        cin >> c;
     
        // 小写字母元音
        isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
     
        // 大写字母元音
        isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
     
        // if 语句判断
        if (isLowercaseVowel || isUppercaseVowel)
            cout << c << " 是元音";
        else
            cout << c << " 是辅音";
     
        return 0;
    }
    C++ 实例 - 判断三个数中的最大数
    #include <iostream>
    using namespace std;
     
    int main()
    {    
        float n1, n2, n3;
     
        cout << "请输入三个数: ";
        cin >> n1 >> n2 >> n3;
     
        if(n1 >= n2 && n1 >= n3)
        {
            cout << "最大数为: " << n1;
        }
     
        if(n2 >= n1 && n2 >= n3)
        {
            cout << "最大数为: " << n2;
        }
     
        if(n3 >= n1 && n3 >= n2) {
            cout << "最大数为: " << n3;
        }
     
        return 0;
    }
    C++ 实例 - 求一元二次方程的根
    #include <iostream>
    #include <cmath>
    using namespace std;
     
    int main() {
     
        float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
        cout << "输入 a, b 和 c: ";
        cin >> a >> b >> c;
        discriminant = b*b - 4*a*c;
        
        if (discriminant > 0) {
            x1 = (-b + sqrt(discriminant)) / (2*a);
            x2 = (-b - sqrt(discriminant)) / (2*a);
            cout << "Roots are real and different." << endl;
            cout << "x1 = " << x1 << endl;
            cout << "x2 = " << x2 << endl;
        }
        
        else if (discriminant == 0) {
            cout << "实根相同:" << endl;
            x1 = (-b + sqrt(discriminant)) / (2*a);
            cout << "x1 = x2 =" << x1 << endl;
        }
     
        else {
            realPart = -b/(2*a);
            imaginaryPart =sqrt(-discriminant)/(2*a);
            cout << "实根不同:"  << endl;
            cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
            cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
        }
     
        return 0;
    }
    C++ 实例 - 计算自然数之和
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int n, sum = 0;
     
        cout << "输入一个正整数: ";
        cin >> n;
     
        for (int i = 1; i <= n; ++i) {
            sum += i;
        }
     
        cout << "Sum = " << sum;
        return 0;
    }
    C++ 实例 - 判断闰年
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int year;
     
        cout << "输入年份: ";
        cin >> year;
     
        if (year % 4 == 0)
        {
            if (year % 100 == 0)
            {
                // // 这里如果被 400 整数是闰年
                if (year % 400 == 0)
                    cout << year << " 是闰年";
                else
                    cout << year << " 不是闰年";
            }
            else
                cout << year << " 是闰年";
        }
        else
            cout << year << " 不是闰年";
     
        return 0;
    }
    C++ 实例 - 求一个数的阶乘
    #include <iostream>
    using namespace std;
     
    int main()
    {
        unsigned int n;
        unsigned long long factorial = 1;
     
        cout << "输入一个整数: ";
        cin >> n;
     
        for(int i = 1; i <=n; ++i)
        {
            factorial *= i;
        }
     
        cout << n << " 的阶乘为:"<< " = " << factorial;    
        return 0;
    }
    C++ 实例 - 创建各类三角形图案
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int rows;
     
        cout << "输入行数: ";
        cin >> rows;
     
        for(int i = 1; i <= rows; ++i)
        {
            for(int j = 1; j <= i; ++j)
            {
                cout << "* ";
            }
            cout << "
    ";
        }
        return 0;
    }
    #include <iostream>
    using namespace std;
     
    int main()
    {
        char input, alphabet = 'A';
     
        cout << "输入最后一个大写字母: ";
        cin >> input;
     
        for(int i = 1; i <= (input-'A'+1); ++i)
        {
            for(int j = 1; j <= i; ++j)
            {
                cout << alphabet << " ";
            }
            ++alphabet;
     
            cout << endl;
        }
        return 0;
    }
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int rows;
     
        cout << "输入行数: ";
        cin >> rows;
     
        for(int i = rows; i >= 1; --i)
        {
            for(int j = 1; j <= i; ++j)
            {
                cout << "* ";
            }
            cout << endl;
        }
        
        return 0;
    }
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int rows;
     
        cout << "输入行数: ";
        cin >> rows;
     
        for(int i = rows; i >= 1; --i)
        {
            for(int j = 1; j <= i; ++j)
            {
                cout << j << " ";
            }
            cout << endl;
        }
     
        return 0;
    }
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int space, rows;
     
        cout <<"输入行数: ";
        cin >> rows;
     
        for(int i = 1, k = 0; i <= rows; ++i, k = 0)
        {
            for(space = 1; space <= rows-i; ++space)
            {
                cout <<"  ";
            }
     
            while(k != 2*i-1)
            {
                cout << "* ";
                ++k;
            }
            cout << endl;
        }    
        return 0;
    }
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int rows, count = 0, count1 = 0, k = 0;
     
        cout << "输入行数: ";
        cin >> rows;
     
        for(int i = 1; i <= rows; ++i)
        {
            for(int space = 1; space <= rows-i; ++space)
            {
                cout << "  ";
                ++count;
            }
     
            while(k != 2*i-1)
            {
                if (count <= rows-1)
                {
                    cout << i+k << " ";
                    ++count;
                }
                else
                {
                    ++count1;
                    cout << i+k-2*count1 << " ";
                }
                ++k;
            }
            count1 = count = k = 0;
     
            cout << endl;
        }
        return 0;
    }
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int rows;
     
        cout << "输入行数: ";
        cin >> rows;
     
        for(int i = rows; i >= 1; --i)
        {
            for(int space = 0; space < rows-i; ++space)
                cout << "  ";
     
            for(int j = i; j <= 2*i-1; ++j)
                cout << "* ";
     
            for(int j = 0; j < i-1; ++j)
                cout << "* ";
     
            cout << endl;
        }
     
        return 0;
    }
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int rows, coef = 1;
     
        cout << "Enter number of rows: ";
        cin >> rows;
     
        for(int i = 0; i < rows; i++)
        {
            for(int space = 1; space <= rows-i; space++)
                cout <<"  ";
     
            for(int j = 0; j <= i; j++)
            {
                if (j == 0 || i == 0)
                    coef = 1;
                else
                    coef = coef*(i-j+1)/j;
     
                cout << coef << "   ";
            }
            cout << endl;
        }
     
        return 0;
    }
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int rows, number = 1;
     
        cout << "输入行数: ";
        cin >> rows;
     
        for(int i = 1; i <= rows; i++)
        {
            for(int j = 1; j <= i; ++j)
            {
                cout << number << " ";
                ++number;
            }
     
            cout << endl;
        }
     
        return 0;
    }
    C++ 实例 - 求两数的最大公约数
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int n1, n2;
     
        cout << "输入两个整数: ";
        cin >> n1 >> n2;
        
        while(n1 != n2)
        {
            if(n1 > n2)
                n1 -= n2;
            else
                n2 -= n1;
        }
     
        cout << "HCF = " << n1;
        return 0;
    }
    C++ 实例 - 求两数最小公倍数
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int n1, n2, max;
     
        cout << "输入两个数: ";
        cin >> n1 >> n2;
        
        // 获取最大的数
        max = (n1 > n2) ? n1 : n2;
     
        do
        {
            if (max % n1 == 0 && max % n2 == 0)
            {
                cout << "LCM = " << max;
                break;
            }
            else
                ++max;
        } while (true);
        
        return 0;
    }
    C++ 实例 - 实现一个简单的计算器
    #include <iostream>
    using namespace std;
     
    int main()
    {
        char op;
        float num1, num2;
     
        cout << "输入运算符:+、-、*、/ : ";
        cin >> op;
     
        cout << "输入两个数: ";
        cin >> num1 >> num2;
     
        switch(op)
        {
            case '+':
                cout << num1+num2;
                break;
     
            case '-':
                cout << num1-num2;
                break;
     
            case '*':
                cout << num1*num2;
                break;
     
            case '/':
                cout << num1/num2;
                break;
     
            default:
                // 如果运算符不是 +, -, * 或 /, 提示错误信息
                cout << "Error!  请输入正确运算符。";
                break;
        }
     
        return 0;
    }
    猴子吃桃问题
    一只小猴子一天摘了许多桃子,第一天吃了一半,然后忍不住又吃了一个;第二天又吃了一半,再加上一个;后面每天都是这样吃。到第10天的时候,小猴子发现只有一个桃子了。问小猴子第一天共摘了多少个桃子。
    #include <iostream>
    using namespace std;
    int main()
    {
        int x, y, n;
        for (x=1, n=0; n<9; y=(x+1)*2, x=y, n++);
            cout<<"第一天共摘的桃子数量为 "<<x<<endl;
        return 0;
    }
  • 相关阅读:
    Spring IoC详解
    Hibernate 和Mybatis的区别
    Nand Flash 驱动框架
    Nor Flash 驱动框架
    USB驱动框架
    输入子系统框架
    module_init 内核调用过程
    平台设备驱动框架
    LCD驱动框架
    嵌入式-开篇
  • 原文地址:https://www.cnblogs.com/tszr/p/12150241.html
Copyright © 2011-2022 走看看