1、编写一个程序,使用 sizeof 运算符打印基本类型、一些指针类型和你选择的一些枚举类型的大小。
以下是一个 C++ 程序示例,可实现打印基本类型、一些指针类型和枚举类型的大小:
#include <iostream>
// 定义一个枚举类型
enum Color { RED, GREEN, BLUE };
enum Weekday { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };
int main() {
// 打印基本类型的大小
std::cout << "Size of char: " << sizeof(char) << " bytes" << std::endl;
std::cout << "Size of short: " << sizeof(short) << " bytes" << std::endl;
std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;
std::cout << "Size of long: " << sizeof(long) << " bytes" << std::endl;
std::cout << "Size of float: " << sizeof(float) << " bytes" << std::endl;
std::cout << "Size of double: " << sizeof(double) << " bytes" << std::endl;
std::cout << "Size of bool: " << sizeof(bool) << " bytes" << std::endl;
// 打印指针类型的大小
std::cout << "Size of int*: " << sizeof(int*) << " bytes" << std::endl;
std::cout << "Size of char*: " << sizeof(char*) << " bytes" << std::endl;
std::cout << "Size of double*: " << sizeof(double*) << " bytes" << std::endl;
// 打印枚举类型的大小
std::cout << "Size of Color: " << sizeof(Color) << " bytes" << std::endl;
std::cout << "Size of Weekday: " << sizeof(Weekday) << " bytes" << std::endl;
return 0;
}
这个程序定义了两个枚举类型 Color 和 Weekday ,并使用 sizeof 运算符打印了基本类型、指针类型和枚举类型的大小。
2、编写一个程序,打印出字母 ‘a’ 到 ‘z’、数字 ‘0’ 到 ‘9’ 及其整数值。对其他可打印字符也进行同样操作。再用十六进制表示法重复上述操作。
可使用 C++ 语言编写程序实现该功能。示例代码如下:
#include <iostream>
#include <iomanip>
int main() {
std::cout << "十进制表示:" << std::endl;
// 打印字母 'a' 到 'z' 及其整数值
for (char c = 'a'; c <= 'z'; ++c) {
std::cout << c << " : " << static_cast<int>(c) << std::endl;
}
// 打印数字 '0' 到 '9' 及其整数值
for (char c = '0'; c <= '9'; ++c) {
std::cout << c << " : " << static_cast<int>(c) << std::endl;
}
// 打印其他可打印字符及其整数值
for (char c = 33; c <= 126; ++c) {
if ((c < '0' || c > '9') && (c < 'a' || c > 'z')) {
std::cout << c << " : " << static_cast<int>(c) << std::endl;
}
}
std::cout << "\n十六进制表示:" << std::endl;
std::cout << std::hex;
// 打印字母 'a' 到 'z' 及其十六进制整数值
for (char c = 'a'; c <= 'z'; ++c) {
std::cout << c << " : " << static_cast<int>(c) << std::endl;
}
// 打印数字 '0' 到 '9' 及其十六进制整数值
for (char c = '0'; c <= '9'; ++c) {
std::cout << c << " : " << static_cast<int>(c) << std::endl;
}
// 打印其他可打印字符及其十六进制整数值
for (char c = 33; c <= 126; ++c) {
if ((c < '0' || c > '9') && (c < 'a' || c > 'z')) {
std::cout << c << " : "

最低0.47元/天 解锁文章
371

被折叠的 条评论
为什么被折叠?



