📜  C++程序的输出|套装25(微距)(1)

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

C++程序的输出 | 套装25(微距)

本次套装包括了以下25个关于C++程序的输出的小问题。

1. 输出 Hello World
#include <iostream>

int main() {
  std::cout << "Hello World!" << std::endl;
  return 0;
}

输出:

Hello World!
2. 输出变量
#include <iostream>

int main() {
  int num = 10;
  std::cout << "The value of num is " << num << std::endl;
  return 0;
}

输出:

The value of num is 10
3. 输出多个变量
#include <iostream>

int main() {
  int num1 = 5;
  int num2 = 10;
  std::cout << "The values of num1 and num2 are " << num1 << " and " << num2 << std::endl;
  return 0;
}

输出:

The values of num1 and num2 are 5 and 10
4. 输出计算结果
#include <iostream>

int main() {
  int num1 = 5;
  int num2 = 10;
  int sum = num1 + num2;
  std::cout << "The sum of num1 and num2 is " << sum << std::endl;
  return 0;
}

输出:

The sum of num1 and num2 is 15
5. 输出带小数点的变量
#include <iostream>
#include <iomanip>

int main() {
  double num = 3.14159265;
  std::cout << std::fixed << std::setprecision(2) << "The value of num is " << num << std::endl;
  return 0;
}

输出:

The value of num is 3.14
6. 输出科学计数法表示的数字
#include <iostream>
#include <iomanip>

int main() {
  double num = 12345.6789;
  std::cout << std::scientific << std::setprecision(3) << "The value of num is " << num << std::endl;
  return 0;
}

输出:

The value of num is 1.235e+04
7. 输出不同进制表示的数字
#include <iostream>

int main() {
  int num = 16;
  std::cout << "Decimal: " << num << std::endl;
  std::cout << "Hexadecimal: 0x" << std::hex << num << std::endl;
  std::cout << "Octal: 0" << std::oct << num << std::endl;
  return 0;
}

输出:

Decimal: 16
Hexadecimal: 0x10
Octal: 020
8. 输出字符
#include <iostream>

int main() {
  char letter = 'A';
  std::cout << "The letter is " << letter << std::endl;
  return 0;
}

输出:

The letter is A
9. 输出字符串
#include <iostream>

int main() {
  std::string greeting = "Hello World!";
  std::cout << greeting << std::endl;
  return 0;
}

输出:

Hello World!
10. 输出布尔值
#include <iostream>

int main() {
  bool is_true = true;
  bool is_false = false;
  std::cout << std::boolalpha << "is_true: " << is_true << std::endl;
  std::cout << std::boolalpha << "is_false: " << is_false << std::endl;
  return 0;
}

输出:

is_true: true
is_false: false
11. 输出指针
#include <iostream>

int main() {
  int num = 10;
  int *ptr = &num;
  std::cout << "The value of num is " << *ptr << std::endl;
  std::cout << "The memory address of num is " << ptr << std::endl;
  return 0;
}

输出:

The value of num is 10
The memory address of num is 0x7ffeed7482dc
12. 输出字符数组
#include <iostream>

int main() {
  char str[6] = "Hello";
  std::cout << "The string is " << str << std::endl;
  return 0;
}

输出:

The string is Hello
13. 输出二维数组
#include <iostream>

int main() {
  int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
  std::cout << "The array is:" << std::endl;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
      std::cout << arr[i][j] << " ";
    }
    std::cout << std::endl;
  }
  return 0;
}

输出:

The array is:
1 2 3 
4 5 6
14. 输出枚举值
#include <iostream>

enum Day { SUN, MON, TUE, WED, THU, FRI, SAT };

int main() {
  Day today = SUN;
  std::cout << "Today is " << today << std::endl;
  return 0;
}

输出:

Today is 0
15. 输出结构体
#include <iostream>
#include <string>

struct Person {
  std::string name;
  int age;
};

int main() {
  Person person;
  person.name = "John";
  person.age = 30;
  std::cout << "Name: " << person.name << std::endl;
  std::cout << "Age: " << person.age << std::endl;
  return 0;
}

输出:

Name: John
Age: 30
16. 输出类的对象
#include <iostream>
#include <string>

class Person {
 public:
  std::string name;
  int age;

  void Display() {
    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
  }
};

int main() {
  Person person;
  person.name = "John";
  person.age = 30;
  person.Display();
  return 0;
}

输出:

Name: John
Age: 30
17. 输出带标记的文本
#include <iostream>

int main() {
  std::cout << "The value of x is: " << std::endl;
  std::cout.width(10);
  std::cout << std::left << "x" << std::endl;
  std::cout << "The value of y is: " << std::endl;
  std::cout.width(10);
  std::cout << std::left << "y" << std::endl;
  return 0;
}

输出:

The value of x is: 
x         
The value of y is: 
y         
18. 输出固定长度的文本
#include <iostream>
#include <iomanip>

int main() {
  std::cout << std::setw(10) << std::left << "Hello" << std::endl;
  std::cout << std::setw(10) << std::left << "World" << std::endl;
  return 0;
}

输出:

Hello     
World     
19. 输出时间
#include <iostream>
#include <ctime>

int main() {
  time_t now = time(nullptr);
  std::cout << "Current date and time: " << ctime(&now) << std::endl;
  return 0;
}

输出:

Current date and time: Fri Mar 26 15:23:10 2021
20. 输出文件流
#include <iostream>
#include <fstream>

int main() {
  std::ofstream file("output.txt");
  file << "Hello World!" << std::endl;
  file.close();
  return 0;
}

在程序目录下生成 output.txt 文件,文件内容为:

Hello World!
21. 输出异常信息
#include <iostream>
#include <exception>

int main() {
  try {
    throw std::runtime_error("An error occurred");
  } catch (const std::exception& e) {
    std::cerr << "Error: " << e.what() << std::endl;
  }
  return 0;
}

输出:

Error: An error occurred
22. 输出命令行参数
#include <iostream>

int main(int argc, char **argv) {
  for (int i = 0; i < argc; i++) {
    std::cout << "Argument " << i << ": " << argv[i] << std::endl;
  }
  return 0;
}

如果命令行调用该程序时输入:

./a.out hello world

则输出:

Argument 0: ./a.out
Argument 1: hello
Argument 2: world
23. 输出环境变量
#include <iostream>
#include <cstdlib>

int main() {
  const char* env_var = std::getenv("LANG");
  if (env_var) {
    std::cout << "LANG: " << env_var << std::endl;
  } else {
    std::cout << "LANG environment variable not set" << std::endl;
  }
  return 0;
}

输出:

LANG: en_US.UTF-8
24. 输出当前工作目录
#include <iostream>
#include <filesystem>

int main() {
  std::filesystem::path current_dir = std::filesystem::current_path();
  std::cout << "Current directory: " << current_dir << std::endl;
  return 0;
}

输出:

Current directory: /Users/user/Projects
25. 输出程序运行时间
#include <iostream>
#include <chrono>

int main() {
  auto start_time = std::chrono::high_resolution_clock::now();
  // 在这里添加需要测量运行时间的代码
  auto end_time = std::chrono::high_resolution_clock::now();
  auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
  std::cout << "Program execution time: " << duration.count() << "ms" << std::endl;
  return 0;
}

输出:

Program execution time: 1234ms