c++ 标准输入输出流基础

本文详细介绍了C++中的标准输入输出流,包括cin、cout和cerr的使用,以及各种格式设置。此外,还讲解了如何进行文件操作,如打开、读写和关闭文件,并提到了错误处理。文章还涉及字符输入、字符串流和类继承的概念,特别是封装和继承在类层次结构中的作用。
摘要由CSDN通过智能技术生成

有三个重要的对象

cin   cout    cerr

以及一些manipulator有的带参数,有的不带参数。

/*
 * This program demonstrates various options for floating-point output
 * by displaying three different constants (pi, the speed of light in meters/second,
 * and the fine-structure constant). These constants
 * are chosen because they illustrate a range of exponent scales.
 */

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

const double PI = 3.1415926535897;
const double SPEED_OF_LIGHT = 2.99792458e+8;
const double FINE_STRUCTURE = 7.2573525e-3;

void printPrecisionTable();

int main() {
    cout << uppercase << right;
    cout << "Default format:" << endl << endl;
    printPrecisionTable();
    cout << endl << "Fixed format:" << fixed << endl << endl;
    printPrecisionTable();
    cout << endl << "Scientific format:" << scientific << endl << endl;
    printPrecisionTable();
    return 0;
}

void printPrecisionTable() {
    cout << "prec |     pi      | speed of light    | fine-structure" << endl;
    cout << "-------------------------------------------------------" << endl;
    for (int prec = 0; prec <= 6; prec +=2) {
        cout << setw(4) << prec << "  |";
        cout << " " << setw(12) << setprecision(prec) << PI << " |";
        cout << " " << setw(16) << setprecision(prec) << SPEED_OF_LIGHT << " |";
        cout << " " << setw(14) << setprecision(prec) << FINE_STRUCTURE << endl;
    }
}

Default format:


prec | pi | speed of light | fine-structure

-------------------------------------------------------

0 | 3 | 3E+08 | 0.007

2 | 3.1 | 3E+08 | 0.0073

4 | 3.142 | 2.998E+08 | 0.007257

6 | 3.14159 | 2.99792E+08 | 0.00725735


Fixed format:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值