📜  C++程序的输出| 13套(1)

📅  最后修改于: 2023-12-03 14:40:00.123000             🧑  作者: Mango

C++程序的输出 | 13套

本篇文章为程序员们提供了13套C++程序的输出示例,旨在帮助程序员们更好地理解C++程序的输出方式。以下是13套C++程序的输出示例(代码片段已按Markdown格式标明):

1. 在控制台输出Hello World!
#include <iostream>

int main() {
    std::cout << "Hello World!\n";
    return 0;
}
2. 输出数字、字符串和单个字符
#include <iostream>

int main() {
    int num = 123;
    std::string str = "Hello World!";
    char ch = 'a';
    std::cout << num << "\n";
    std::cout << str << "\n";
    std::cout << ch << "\n";
    return 0;
}
3. 格式化输出整型和浮点型
#include <iostream>
#include <iomanip>

int main() {
    int num = 123;
    float fnum = 1.23;
    std::cout << std::fixed << std::setprecision(2);
    std::cout << "num = " << std::setw(5) << num << "\n";
    std::cout << "fnum = " << fnum << "\n";
    return 0;
}
4. 输出多个数字和字符串
#include <iostream>

int main() {
    int num1 = 123;
    int num2 = 456;
    std::string str1 = "Hello";
    std::string str2 = "World";
    std::cout << num1 << ", " << num2 << "\n";
    std::cout << str1 << " " << str2 << "!\n";
    return 0;
}
5. 输出特殊字符
#include <iostream>

int main() {
    std::cout << "The backslash: \\\n";
    std::cout << "The double quote: \"\n";
    std::cout << "The single quote: '\n";
    std::cout << "The newline character:\n";
    return 0;
}
6. 输出数组内容
#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    for(int i=0; i<n; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << "\n";
    return 0;
}
7. 输出二维数组内容
#include <iostream>

int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int rows = sizeof(arr)/sizeof(arr[0]);
    int cols = sizeof(arr[0])/sizeof(arr[0][0]);
    for(int i=0; i<rows; i++) {
        for(int j=0; j<cols; j++) {
            std::cout << arr[i][j] << " ";
        }
        std::cout << "\n";
    }
    return 0;
}
8. 输出指针变量的值以及地址
#include <iostream>

int main() {
    int num = 123;
    int* ptr = &num;
    std::cout << "Value of num: " << num << "\n";
    std::cout << "Address of num: " << &num << "\n";
    std::cout << "Value of ptr: " << ptr << "\n";
    std::cout << "Address of ptr: " << &ptr << "\n";
    return 0;
}
9. 输出一个简单的计算器
#include <iostream>

int main() {
    float num1, num2;
    char op;
    std::cout << "Enter first number: ";
    std::cin >> num1;
    std::cout << "Enter operator (+, -, *, /): ";
    std::cin >> op;
    std::cout << "Enter second number: ";
    std::cin >> num2;
    float result;
    switch(op) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            result = num1 / num2;
            break;
        default:
            std::cout << "Invalid operator\n";
            return 0;
    }
    std::cout << "Result: " << result << "\n";
    return 0;
}
10. 输出一个简单的猜数字游戏
#include <iostream>
#include <cstdlib>
#include <ctime>

int main() {
    srand(time(0)); // 设置随机数种子
    int num = rand() % 100 + 1; // 生成1~100的随机数
    int guess;
    int tries = 0;
    std::cout << "Guess the number (1-100)\n";
    do {
        std::cout << "Enter your guess: ";
        std::cin >> guess;
        tries++;
        if(guess < num) {
            std::cout << "Too low!\n";
        } else if(guess > num) {
            std::cout << "Too high!\n";
        }
    } while(guess != num);
    std::cout << "Congratulations, you guessed the number in " << tries << " tries!\n";
    return 0;
}
11. 输出一个简单的倒序打印程序
#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    std::cout << "Original array: ";
    for(int i=0; i<n; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << "\nReverse array: ";
    for(int i=n-1; i>=0; i--) {
        std::cout << arr[i] << " ";
    }
    std::cout << "\n";
    return 0;
}
12. 输出一个简单的自动售货机程序
#include <iostream>

int main() {
    float balance = 10.0;
    float price = 1.5;
    std::cout << "Welcome to the vending machine!\n";
    std::cout << "Cost per item: " << price << " dollars\n";
    std::cout << "Current balance: " << balance << " dollars\n";
    char choice;
    do {
        std::cout << "Would you like to buy an item? (y/n) ";
        std::cin >> choice;
        if(choice == 'y') {
            if(balance < price) {
                std::cout << "Insufficient balance\n";
            } else {
                balance -= price;
                std::cout << "Item purchased!\n";
                std::cout << "Current balance: " << balance << " dollars\n";
            }
        } else if(choice != 'n') {
            std::cout << "Invalid choice\n";
        }
    } while(choice != 'n');
    std::cout << "Thank you for using the vending machine!\n";
    return 0;
}
13. 输出一个简单的聊天程序
#include <iostream>
#include <string>

int main() {
    std::string name;
    std::cout << "Hello! What is your name?\n";
    std::getline(std::cin, name);
    std::cout << "Nice to meet you, " << name << "!\n";
    std::cout << "What is your favorite color?\n";
    std::string color;
    std::getline(std::cin, color);
    std::cout << "Really? " << color << " is my favorite color too!\n";
    std::cout << "What is your favorite food?\n";
    std::string food;
    std::getline(std::cin, food);
    std::cout << "I love " << food << " too!\n";
    std::cout << "It was nice chatting with you, " << name << "!\n";
    return 0;
}

以上便是13套C++程序的输出示例,希望对程序员们有所帮助!