📅  最后修改于: 2023-12-03 14:59:52.326000             🧑  作者: Mango
本次套装包括了以下25个关于C++程序的输出的小问题。
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
输出:
Hello World!
#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
#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
#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
#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
#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
#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
#include <iostream>
int main() {
char letter = 'A';
std::cout << "The letter is " << letter << std::endl;
return 0;
}
输出:
The letter is A
#include <iostream>
int main() {
std::string greeting = "Hello World!";
std::cout << greeting << std::endl;
return 0;
}
输出:
Hello World!
#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
#include <iostream>
int main() {
int num = 10;
int *ptr = #
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
#include <iostream>
int main() {
char str[6] = "Hello";
std::cout << "The string is " << str << std::endl;
return 0;
}
输出:
The string is Hello
#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
#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
#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
#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
#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
#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
#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
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("output.txt");
file << "Hello World!" << std::endl;
file.close();
return 0;
}
在程序目录下生成 output.txt 文件,文件内容为:
Hello World!
#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
#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
#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
#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
#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