自己设计和编写5个(用成员函数和操控符的程序各至少5个,2类程序可以实现同样的功能)以上的小程序测试使用ios类的所有成员函数和使用(所有)操控符进行格式化输入/输出的方

时间: 2024-02-15 09:05:17 浏览: 23
案 程序1:使用成员函数实现输入和输出 ```c++ #include <iostream> #include <string> using namespace std; class ios_test { public: void input() { cout << "请输入字符串:"; getline(cin, str); } void output() { cout << "输出的字符串为:" << str << endl; } private: string str; }; int main() { ios_test t; t.input(); t.output(); return 0; } ``` 程序2:使用成员函数实现格式化输出 ```c++ #include <iostream> #include <iomanip> using namespace std; class ios_test { public: void output() { cout << "使用setw()实现对齐" << endl; cout << setw(10) << left << "left" << setw(10) << right << "right" << endl; cout << setw(10) << internal << "internal" << setw(10) << setfill('*') << "setfill" << endl; cout << "使用setprecision()实现精度控制" << endl; cout << setprecision(2) << 3.1415926 << endl; cout << setprecision(5) << 3.1415926 << endl; cout << "使用setiosflags()实现控制位的设置" << endl; cout << setiosflags(ios::fixed) << setprecision(2) << 3.1415926 << endl; cout << setiosflags(ios::scientific) << setprecision(2) << 3.1415926 << endl; } }; int main() { ios_test t; t.output(); return 0; } ``` 程序3:使用操控符实现输入和输出 ```c++ #include <iostream> #include <string> using namespace std; class ios_test { public: void input() { cout << "请输入字符串:"; cin >> str; } void output() { cout << "输出的字符串为:" << str << endl; } private: string str; }; int main() { ios_test t; t.input(); t.output(); return 0; } ``` 程序4:使用操控符实现格式化输出 ```c++ #include <iostream> #include <iomanip> using namespace std; class ios_test { public: void output() { cout << "使用setw()实现对齐" << endl; cout << setw(10) << left << "left" << setw(10) << right << "right" << endl; cout << setw(10) << internal << "internal" << setw(10) << setfill('*') << "setfill" << endl; cout << "使用setprecision()实现精度控制" << endl; cout << fixed << setprecision(2) << 3.1415926 << endl; cout << fixed << setprecision(5) << 3.1415926 << endl; cout << "使用setiosflags()实现控制位的设置" << endl; cout << fixed << setprecision(2) << 3.1415926 << endl; cout << scientific << setprecision(2) << 3.1415926 << endl; } }; int main() { ios_test t; t.output(); return 0; } ``` 程序5:使用成员函数和操控符实现输入和输出 ```c++ #include <iostream> #include <string> using namespace std; class ios_test { public: void input() { cout << "请输入字符串:"; getline(cin, str); } void output() { cout << "输出的字符串为:" << str << endl; } friend ostream& operator<<(ostream& os, const ios_test& t) { os << "输出的字符串为:" << t.str << endl; return os; } friend istream& operator>>(istream& is, ios_test& t) { cout << "请输入字符串:"; getline(is, t.str); return is; } private: string str; }; int main() { ios_test t1, t2; t1.input(); t1.output(); cin >> t2; cout << t2; return 0; } ```

最新推荐

recommend-type

Oracle实验六 PL/SQL程序设计

以bs用户登录BOOKSALES数据库,利用PL/SQL程序编写下列功能模块。 SQL&gt;CONN bs/bs@booksales (1)创建一个函数,以客户号为参数,返回该客户订购图书的价格总额。 create or replace function sumprice( id ...
recommend-type

用python+pyqt5手工编写一个含交互界面的简易的词法分析器

交互界面可以用QtDesigner快速编写,或者自己写,我采用的是用可视化界面创建了一个大致的框架,然后再手动添加其他需要的控件,或者微调一些属性。 我简单设计的交互界面如图所示: 有话说: 这边以C和C++为输入...
recommend-type

C语言程序设计实现区号查询系统C语言程序设计实现

用C语言编程的区号查询系统,要求实现区号查询系统中,添加新记录、删除记录、显示记录信息、按城市查找信息和退出系统等功能。 ①录入有关城市的名称和区号。 ②显示所有城市的信息。 ③通过输入城市名称查找对应...
recommend-type

用C编写一个送给女朋友的情人节小程序 可爱!

非常可爱的情人节小程序!文章为大家分享了用C编写一个送给女朋友的小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

基于STM32单片机流水灯仿真与程序设计

用STM32一个端口接8个发光二极管,编写程序,实现从左到右轮流点亮8个二极管(即流水灯) 二、仿真电路设计 2.1、环境介绍 仿真采用Proteus 8.9 SP2安装链接 2.2、电路设计 第一步:在Proteus中的[P]选择所需要的...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。