C++学习笔记(自用)

基本格式:

#include <iostream>

using namespace std(空间名);



int main(){

       cout << "Hello World";

       return 0;

}

函数和算法:

cin >> // 输入以空格分隔  

cout << // 输出:

#include <iostream>
#include <iomanip>

using namespace std;

void printNumbers(int x, int y, int z, ios::fmtflags base, bool showBase = false, bool uppercase = false) {
    if (showBase) {
        cout.setf(ios::showbase);
    } else {
        cout.unsetf(ios::showbase);
    }

    if (uppercase) {
        cout.setf(ios::uppercase);
    } else {
        cout.unsetf(ios::uppercase);
    }

    cout.setf(base, ios::basefield);
    cout << x << ' ' << y << ' ' << z << endl;
}

int main() {
    int x = 30, y = 300, z = 1024;

    // 十进制输出
    printNumbers(x, y, z, ios::dec);

    // 设置基指示符和大写字母输出
    printNumbers(x, y, z, ios::dec, true, true);

    // 取消基指示符和大写字母输出
    printNumbers(x, y, z, ios::dec);

    // 八进制输出
    printNumbers(x, y, z, ios::oct);

    // 设置基指示符和大写字母输出(八进制)
    printNumbers(x, y, z, ios::oct, true, true);

    // 取消基指示符和大写字母输出
    printNumbers(x, y, z, ios::oct);

    // 恢复十进制输出
    printNumbers(x, y, z, ios::dec);

    // 十六进制输出
    printNumbers(x, y, z, ios::hex);

    // 设置基指示符和大写字母输出(十六进制)
    printNumbers(x, y, z, ios::hex, true, true);

    // 取消基指示符和大写字母输出
    printNumbers(x, y, z, ios::hex);

    // 恢复十进制输出
    printNumbers(x, y, z, ios::dec);

    return 0;
}
#include <iostream>
#include <iomanip>

using namespace std;

// 输出单行数字,根据给定的进制显示
void printNumbers(int x, int y, int z, ios::fmtflags baseFlags = ios::dec, bool showBase = false, bool uppercase = false) {
    if (showBase) {
        cout.setf(ios::showbase);
    } else {
        cout.unsetf(ios::showbase);
    }

    if (uppercase) {
        cout.setf(ios::uppercase);
    } else {
        cout.unsetf(ios::uppercase);
    }

    cout.setf(baseFlags, ios::basefield);
    cout << x << ' ' << y << ' ' << z << endl;
}

int main() {
    int x = 30, y = 300, z = 1024;

    // 十进制输出
    printNumbers(x, y, z);

    // 八进制输出
    pri
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值