cin.get(),cout.put(),cin.eof()的应用案例

// 处理一个流中的各个字符
// Counts the number of characters in a line and lines in a stream 
// 对一行中的字符数和流中的行数进行计数
// cin.get(),cout.put(),cin.eof() 这三个函数是非常有用的。
// cin.eof() 在cin.get()得到文件结束字符时才返回true. 有下面两种情况:
// 1,文件结束了  2,输入文件结束字符
// 一生万物。能处理一个字符,配合循环,就能处理任意个字符。很伟大的思想。
// 从第一个砖头开始盖楼,再高的楼也会被盖好。
// 时间:2012年2月14日,情人节哎。。
// 环境:VC6.0
#include<iostream >
#include<string >
using namespace std;


#define ENDFILE "CTRL-Z"     // Windows用这个表示文件结束
//#define ENDFILE "CTRL-D"  // UNIX 用这个表示文件结束


//   main函数
int main()
{     
const char NWLN = '\n'; 


char next;  // 字符暂存变量
int charCount; // 统计字符数
int lineCount; // 统计行数


cout << "Enter a string or press " << ENDFILE << " to stop:";
cin.get(next);
lineCount = 0;
while ( !cin.eof() )
{
// 统计一行有多少字符
charCount = 0;
while ( NWLN != next )
{
cout.put( next ); // 回显这个字符。
// 可以添加保存到文件的语句。
charCount++;
cin.get(next);
}


        // 显示这行有多少个字符
cout.put(NWLN);
lineCount++;   // 行数增加
cout << "The number of characters in " << lineCount << " is " << charCount << endl;


// 提示输入下一行字符
cout << "Enter a string or press " << ENDFILE << " to stop:";
   cin.get(next);
} // end outer while


cout << "the number of processed lines is " << lineCount << endl;


return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值