编程计算从键盘上输入的单词个数,并从中找出最长的单词, 输出单词个数和最长单词和它的长度CTRL+z结束输入 include <iostream.h>//11-2-21 cpp void maino d char buf[80] maxWord[ 20]; int curLen, maxLen =0, cnt o cout<<"请输入一句话:”; while (cin > buf)t curLen strlen (buf);cnt ++i if(curLen maxEnt maxLen curLen: strcpy (maxWord bufi cout<<end<<"单词个数:”<<cnt<<end cout<<"最长单词:”<≤ maxWord <<",长度:”< maxlen<<enc國囻
编程计算从键盘上输入的单词个数,并从中找出最长的单词, 输出单词个数和最长单词和它的长度 CTRL+z 结束输入 #include <iostream.h> // 11-2-21.cpp void main() { char buf[80], maxWord[20]; int curLen, maxLen = 0, cnt = 0; cout << “请输入一句话:”; while(cin >> buf) { curLen = strlen(buf); cnt ++; if(curLen > maxLen) { maxLen = curLen; strcpy(maxWord, buf); } } cout << endl << “单词个数:” << cnt << endl; cout << “最长单词:” << maxWord << “,长度:” << maxLen << endl; }
112标准流对象 cout 类 stream的定义: class ostream: virtual public ios t public: ostream (streambuf x)i ostream flush; ostream put(char) ostream write(const signed char * int, ) i ostream write(const unsigned char int, )i streampos tellg; ostream operator <<(short) ostream operator <<(int)i ostream operator <<(float Si
11.2 标准流对象 cout 类ostream的定义: class ostream:virtual public ios { public: ostream(streambuf *); ostream & flush(); ostream & put(char); ostream & write(const signed char *, int, ); ostream & write(const unsigned char *, int, ); …… streampos tellg(); ostream & operator << (short); ostream & operator << (int); ostream & operator << (float); …… };
112标准流对亲cout 心 在 ostream输出流类中定义有对左移操作符<<重载的一组公用 成员函数,函数的具体声明格式为: ostream& operator<<(简单类型标识符); cou<<"sum=<<100 对象引用 简单类型标识符除了与在 istream流类中声明右移操作符重载函 数给出的所有简单类型标识符相同以外,还增加一个void*类 型,用于输出任何指针(但不能是字符指针,因为它将被作为字 符串处理,即输出所指向存储空间中保存的一个字符串)的值 由于左移操作符重载用于向流中输出表达式的值,所以又称为插 入操作符。另外,要注意输出表达式中运算符的优先级如果低于 <,那么要括号把表达式括起来。如: cout <<i>j?i;j<< endi cout <<(>j?i:j<< endl;
11.2 标准流对象 cout 在ostream输出流类中定义有对左移操作符<<重载的一组公用 成员函数,函数的具体声明格式为: ostream & operator <<(简单类型标识符); cout << “Sum=“ << 100; 简单类型标识符除了与在istream流类中声明右移操作符重载函 数给出的所有简单类型标识符相同以外,还增加一个void * 类 型,用于输出任何指针(但不能是字符指针,因为它将被作为字 符串处理,即输出所指向存储空间中保存的一个字符串)的值。 由于左移操作符重载用于向流中输出表达式的值,所以又称为插 入操作符。另外,要注意输出表达式中运算符的优先级如果低于 <<,那么要括号把表达式括起来。 如: cout << i>j?i:j << endl; cout << (i>j?i:j) << endl; 对象引用
112标准流对象c0ut 直接利用 stream类中的函数进行输出: include <iostream.h>//11-2-3. cpp void maino d char *s =This is a string. int x= 10, * p= &x cout put( s) put(); cout < end: cout. write(S+10, 3); cout < endli cout<<X<<" Address:”<<p≤<end 结果是:TA str 10 Address: 0x0012FF78
11.2 标准流对象 cout 直接利用ostream类中的函数进行输出: #include <iostream.h> // 11-2-3.cpp void main() { char *s = “This is a string.”; int x = 10, *p = &x; cout.put(*s).put(‘A’); cout << endl; cout.write(s+10, 3); cout << endl; cout << x << “ Address:” << p << endl; } 结果是:TA str 10 Address:0x0012FF78
11.2标准流对cerr和clog cer: cerr类似标准错误文件。cer与cout的差别在于: (1)cerr是不能重定向的; (2)cerr不能被缓冲,它的输出总是直接传达到标准输 出设备上。 cerr<< Error"<<n" clog: clg是不能重定向的,但是可以被缓冲。 clog < Error"<<n
11.2 标准流对象 cerr和clog cerr: cerr类似标准错误文件。cerr与cout的差别在于: (1)cerr是不能重定向的; (2)cerr不能被缓冲,它的输出总是直接传达到标准输 出设备上。 cerr << “Error” << “\n”; clog: clog是不能重定向的,但是可以被缓冲。 clog << “Error” << “\n”;