作者: tmxkok516

  • GESP C++编程一级标准编程实例

    1. 变量与数据类型实例

    整数变量的使用

    #include <iostream>
    using namespace std;
    
    int main() {
        int a = 10;
        cout << "a=" << a << endl;
        return 0;
    }

    浮点变量的使用

    #include <iostream>
    using namespace std;
    int main() {
        float a = 3.14;
        cout << "a=" << a << endl;
        return 0;
    }

    字符变量的使用

    #include <iostream>
    using namespace std;
    int main() {
        char ch = 'A';
        cout << "字符变量的值: " << ch << endl;
        return 0;
    }

    2. 运算符与表达式实例

    算术运算符实例

    #include <iostream>
    using namespace std;
    int main() {
        int a = 10, b = 3;
        cout << "加法: " << a + b << endl;
        cout << "减法: " << a - b << endl;
        cout << "乘法: " << a * b << endl;
        cout << "除法: " << a / b << endl;
        cout << "取余: " << a % b << endl;
        return 0;
    }

    关系运算符实例

    #include <iostream>
    using namespace std;
    int main() {
        int a = 10, b = 3;
        cout << "a > b: " << (a > b) << endl;
        cout << "a < b: " << (a < b) << endl;
        cout << "a >= b: " << (a >= b) << endl;
        cout << "a <= b: " << (a <= b) << endl;
        cout << "a == b: " << (a == b) << endl;
        cout << "a != b: " << (a != b) << endl;
        return 0;
    }

    逻辑运算符实例

    #include <iostream>
    using namespace std;
    int main() {
        bool p = true, q = false;
        cout << "p && q: " << (p && q) << endl;
        cout << "p || q: " << (p || q) << endl;
        cout << "!p: " << (!p) << endl;
        return 0;
    }

    3. 输入输出语句实例

    标准输入语句示例

    #include <iostream>
    using namespace std;
    int main() {
        int num;
        cout << "请输入一个整数: ";
        cin >> num;
        cout << "你输入的整数是: " << num << endl;
        return 0;
    }

    标准输出语句示例

    #include <iostream>
    using namespace std;
    int main() {
        cout << "这是一个标准输出语句示例。" << endl;
        return 0;
    }

    4. 顺序结构程序实例

    简单数学计算程序

    #include <iostream>
    using namespace std;
    int main() {
        int a = 5, b = 3;
        int result = a * a + b * b;
        cout << "a^2 + b^2 的结果是: " << result << endl;
        return 0;
    }

    数据处理程序

    #include <iostream>
    using namespace std;
    int main() {
        int num1, num2;
        cout << "请输入两个整数: ";
        cin >> num1 >> num2;
        int sum = num1 + num2;
        int average = sum / 2;
        cout << "两数之和: " << sum << endl;
        cout << "两数平均值: " << average << endl;
        return 0;
    }

    5. 选择结构程序实例

    if语句应用实例

    #include <iostream>
    using namespace std;
    int main() {
        int num;
        cout << "请输入一个整数: ";
        cin >> num;
        if (num > 0) {
            cout << "你输入的是正数。" << endl;
        }
        return 0;
    }

    if – else语句应用实例

    #include <iostream>
    using namespace std;
    int main() {
        int num;
        cout << "请输入一个整数: ";
        cin >> num;
        if (num > 0) {
            cout << "你输入的是正数。" << endl;
        } else {
            cout << "你输入的是负数或零。" << endl;
        }
        return 0;
    }

    6. 循环结构程序实例

    for循环实例

    #include <iostream>
    using namespace std;
    int main() {
        for (int i = 1; i <= 5; i++) {
            cout << i << endl;
        }
        return 0;
    }

    while循环实例

    #include <iostream>
    using namespace std;
    int main() {
        int i = 1;
        while (i <= 5) {
            cout << i << endl;
            i++;
        }
        return 0;
    }

    do – while循环实例

    #include <iostream>
    using namespace std;
    int main() {
        int i = 1;
        do {
            cout << i << endl;
            i++;
        } while (i <= 5);
        return 0;
    }

    7. 函数的使用实例

    自定义函数示例

    #include <iostream>
    using namespace std;
    int add(int a, int b) {
        return a + b;
    }
    int main() {
        int result = add(3, 5);
        cout << "两数之和: " << result << endl;
        return 0;
    }

    函数调用实例

    #include <iostream>
    using namespace std;
    int square(int num) {
        return num * num;
    }
    int main() {
        int num = 4;
        int result = square(num);
        cout << num << " 的平方是: " << result << endl;
        return 0;
    }

    8. 综合应用实例

    计算圆的面积

    #include <iostream>
    using namespace std;
    const double PI = 3.14159;
    int main() {
        double radius;
        cout << "请输入圆的半径:";
        cin >> radius;
        double area = PI * radius * radius;
        cout << "圆的面积是:" << area << endl;
        return 0;
    }

    求三个数中的最大值

    #include <iostream>
    using namespace std;
    int main() {
        int a, b, c;
        cout << "请输入三个整数:";
        cin >> a >> b >> c;
        int max_num = a;
        if (b > max_num) {
            max_num = b;
        }
        if (c > max_num) {
            max_num = c;
        }
        cout << "三个数中的最大值是:" << max_num << endl;
        return 0;
    }

    输出三个数中的最大值

    #include <iostream>  
    using namespace std;  
    int main() {  
        int a, b, c;  
        cin >> a >> b >> c;  
        cout << max(a, max(b, c));  
        return 0;  
    }  

    输出Hello World

    #include <iostream>
    using namespace std;
    int main() {
        cout << "Hello World!";
        return 0;
    }

    两数相加

    #include <iostream>
    using namespace std;
    int main() {
        int a, b;
        cin >> a >> b;
        cout << a + b;
        return 0;
    }

    判断奇偶数

    #include <iostream>
    using namespace std;
    int main() {
        int num;
        cin >> num;
        if(num % 2 == 0) cout << "Even";
        else cout << "Odd";
        return 0;
    }

    打印1-10

    #include <iostream>
    using namespace std;
    int main() {
        for(int i=1; i<=10; i++){
            cout << i << " ";
        }
        return 0;
    }

    计算平均数

    #include <iostream>
    using namespace std;
    int main() {
        float a,b,c;
        cin >> a >> b >> c;
        cout << (a+b+c)/3;
        return 0;
    }

    判断闰年

    #include <iostream>
    using namespace std;
    int main() {
        int year;
        cin >> year;
        if((year%4==0 && year%100!=0) || year%400==0)
            cout << "Yes";
        else
            cout << "No";
        return 0;
    }

    计算阶乘

    #include <iostream>
    using namespace std;
    int main() {
        int n,sum=1;
        cin >> n;
        for(int i=1;i<=n;i++) sum *= i;
        cout << sum;
        return 0;
    }

    数字逆序

    #include <iostream>
    using namespace std;
    int main() {
        int num;
        cin >> num;
        while(num>0){
            cout << num%10;
            num /=10;
        }
        return 0;
    }

    判断质数

    #include <iostream>
    using namespace std;
    int main() {
        int n;
        cin >> n;
        for(int i=2;i*i<=n;i++){
            if(n%i==0){
                cout << "No";
                return 0;
            }
        }
        cout << "Yes";
        return 0;
    }

    斐波那契数列

    #include <iostream>
    using namespace std;
    int main() {
        int n,a=0,b=1;
        cin >> n;
        for(int i=0;i<n;i++){
            cout << a << " ";
            int temp = a;
            a = b;
            b = temp + b;
        }
        return 0;
    }

    求绝对值

    输入一个整数,输出其绝对值。

    输入样例:-8

    输出样例:8

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main() {
        int num;
        cin >> num;
        int absNum = abs(num);
        cout << absNum << endl;
        return 0;
    }

    计算三角形周长

    输入三个整数表示边长(保证能构成三角形),输出周长。

    输入样例:3 4 5

    输出样例:12

    #include <iostream>
    using namespace std;
    int main() {
        int a, b, c;
        cin >> a >> b >> c;
        int perimeter = a + b + c;
        cout << perimeter << endl;
        return 0;
    }

    统计字符数量

    输入一个字符和一个字符串,统计字符在字符串中出现的次数。

    输入样例:a banana

    输出样例:3

    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
        char target;
        string str;
        cin >> target >> str;
        int count = 0;
        for (char c : str) {
            if (c == target) {
                count++;
            }
        }
        cout << count << endl;
        return 0;
    }

    数组元素求和

    输入5个整数存入数组,输出它们的和。

    输入样例:1 2 3 4 5

    输出样例:15

    #include <iostream>
    using namespace std;
    int main() {
        int arr[5];
        int sum = 0;
        for (int i = 0; i < 5; i++) {
            cin >> arr[i];
            sum += arr[i];
        }
        cout << sum << endl;
        return 0;
    }

    字母大小写转换

    输入一个字母,输出其大小写形式(如A→a,b→B)。

    输入样例:D

    输出样例:d

    #include <iostream>
    using namespace std;
    int main() {
        char ch;
        cin >> ch;
        if (ch >= 'A' && ch <= 'Z') {
            ch = ch + 32;
        } else if (ch >= 'a' && ch <= 'z') {
            ch = ch - 32;
        }
        cout << ch << " using namespace std";
        return 0;
    }

    华氏度转摄氏度

    #include <iostream>  
    using namespace std;  
    int main() {  
        float f;  
        cin >> f;  
        cout << 5.0 / 9 * (f - 32);  
        return 0;  
    } 

    求1到n的和

    #include <iostream>  
    using namespace std;  
    int main() {  
        int n, sum = 0;  
        cin >> n;  
        for(int i=1; i<=n; i++) sum += i;  
        cout << sum;  
        return 0;  
    }  

    计算矩形面积

    #include <iostream>  
    using namespace std;  
    int main() {  
        int length, width;  
        cin >> length >> width;  
        cout << length * width;  
        return 0;  
    }  

    反向输出数字

    #include <iostream>  
    using namespace std;  
    int main() {  
        int n, reversed = 0;  
        cin >> n;  
        while(n > 0) {  
            reversed = reversed * 10 + n % 10;  
            n /= 10;  
        }  
        cout << reversed;  
        return 0;  
    }  

    求两数的最大公约

    #include <iostream>  
    using namespace std;  
    int main() {  
        int a, b;  
        cin >> a >> b;  
        while(b != 0) {  
            int temp = b;  
            b = a % b;  
            a = temp;  
        }  
        cout << a;  
        return 0;  
    }  

    数据统计程序示例(计算一组数的平均值和总和)

    #include <iostream>
    using namespace std;
    int main() {
        int n;
        cout << "请输入数字的个数: ";
        cin >> n;
        int sum = 0;
        for (int i = 0; i < n; i++) {
            int num;
            cout << "请输入第 " << i + 1 << " 个数字: ";
            cin >> num;
            sum += num;
        }
        double average = static_cast<double>(sum) / n;
        cout << "总和: " << sum << endl;
        cout << "平均值: " << average << endl;
        return 0;
    }

    输出九九乘法表

    #include <iostream>  
    using namespace std;  
    int main() {  
        for(int i=1; i<=9; i++) {  
            for(int j=1; j<=i; j++) {  
                cout << j << "*" << i << "=" << i*j << " ";  
            }  
            cout << endl;  
        }  
        return 0;  
    }  

    小型游戏程序示例(猜数字游戏)

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main() {
        srand(time(0));
        int secretNumber = rand() % 100 + 1;
        int guess;
        int attempts = 0;
        cout << "欢迎来到猜数字游戏!我已经想好了一个1到100之间的数字,你可以开始猜啦。" << endl;
        do {
            cout << "请输入你的猜测: ";
            cin >> guess;
            attempts++;
            if (guess > secretNumber) {
                cout << "猜大了,再试试。" << endl;
            } else if (guess < secretNumber) {
                cout << "猜小了,再试试。" << endl;
            } else {
                cout << "恭喜你,猜对了!你一共用了 " << attempts << " 次尝试。" << endl;
            }
        } while (guess != secretNumber);
        return 0;
    }
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main() {
        srand(static_cast<unsigned int>(time(nullptr)));
        int secretNumber = rand() % 100 + 1;
        int guess;
        cout << "欢迎来到猜数字游戏!我已经想好了一个1到100之间的数字,你可以开始猜啦。" << endl;
        do {
            cout << "请输入你的猜测: ";
            cin >> guess;
    
            if (guess > secretNumber) {
                cout << "猜大了,再试试。" << endl;
            } else if (guess < secretNumber) {
                cout << "猜小了,再试试。" << endl;
            } else {
                cout << "恭喜你,猜对了!" << endl;
            }
        } while (guess != secretNumber);
    
        return 0;
    }

  • 世界,您好!

    欢迎使用 WordPress。这是您的第一篇文章。编辑或删除它,然后开始写作吧!

error: Content is protected !!