#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<cctype>
#include<exception>
#include"windows.h"
using namespace std;
int main()
{
char *ch = "china";
cout << "Address:"<<(void *)ch << endl;//要获取字符串的地址可以用void *强制转换
cout.put(ch[1]).put(ch[2])<<"***put()\n";//用put()输出一个字符
cout.write(ch,strlen(ch))<<"**write()\n"<<flush;//用write()输出一个字符串
int n = 12;
cout <<dec<< n << "="<<oct<<n<<"="<<hex<<n<<endl;//或者利用hex(cout);
string str[][4] = { { "January", "Feburary", "March", "April" }, { "May", "June", "July", "August" }
, { "September", "Octomber", "November", "December" } };
int init_width = cout.width();//返回初始宽度
ios_base::fmtflags init_style = cout.setf(ios_base::left, ios_base::adjustfield);
ios_base::fmtflags init_decimal = cout.setf(ios_base::dec, ios_base::basefield);
ios_base::fmtflags init_float = cout.setf(ios_base::fixed, ios_base::floatfield);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
//cout.width(12);
cout.fill(' ');
cout << "|"<<setw(12)<<str[i][j];
}
cout << "|";
cout << endl;
}
double x = 3.14;
cout.precision(3);//默认格式下是最终结果的位数,对于科学计数法和定点表示法中它是小数点后位数
cout << "sqrt(3.14)=" << sqrt(x) << endl;
cout.setf(ios_base::showpoint);
//showpoint,boolalpha,showbase,uppercase,showpos均是类ios_base中定义的常数
cout << "6.0/3=" << 6.0/3 << endl;
cout.setf(init_style, ios_base::adjustfield);
cout.setf(init_float, ios_base::floatfield);
cout << "sqrt(3.14)=|";
cout.width(12);
cout.precision(10);
cout.setf(ios_base::showpos);
cout<< sqrt(x) << endl;
cout.unsetf(ios_base::showpos);
cout << sqrt(x) << endl;
cout << setw(10) << setprecision(8) << setfill('*') << sqrt(2) << endl;
int num,sum=0;
cin.exceptions(ios_base::failbit);
cout << "please input a num:";
try{
while (cin >> num)
{
sum += num;
}
}
catch (ios_base::failure &f)
{
cout << f.what() << endl;
cout << "This is a horror.\n";
}
cout << "The last input is:" << num << endl;
cout << "Sum=" << sum << endl;
if (!cin.eof()&&cin.fail())//读取失败因为数据不匹配
{
cin.clear();
while (!isspace(cin.get())) //读取下一个数据
continue;
int next;
cin >> next;
cout << "The next number is: " << next << endl;
while (cin.get() != '\n') //清空输入
continue;
}
else
{
cout << "Sorry,I can't go on.\n";
exit(0);
}
cout << "Let's to test cin about char.\n";
int count = 0;
char c;
cin>>c;
while (c != '#')//不能采用'\n'来作为输入结束的标识,因为cin>>会自动跳过空格,换行符和回车
{
cout << c;
count++;
cin >> c;
}
cout << "\ncount=" << count << endl;
while (cin.get() != '\n') //清空输入
continue;
count = 0;
while (cin.get(c))//读取到文件尾,windows可以利用ctrl+Z模拟文件尾输入
{
if (c == '\n') break;
cout << c;
count++;
}
cout << "\ncount=" << count << endl;
count = 0;
char ch1;
ch1= cin.get(); //cin.get()返回值是int型
while (ch1 != '\n')
{
cout << ch1;
count++;
ch1 = cin.get();
}
cout << "\ncount=" << count << endl;
int ch2;
while ((ch2 = cin.get()) != EOF)
{
if (ch2 == '\n') break;
cout << ch2;
}
cout << "\nDone!\n";
char s[20];
cout << "please input a string.\n";
cin.getline(s, 20, '#');//getline()丢弃标志符'#'
char nex1 = cin.get();
cout << "String: " << s << ",Next char: " << nex1 << endl;
cout << "please input another string.\n";
cin.get(s, 20, '#');//get()保留标志符'#'
char nex2 = cin.get();
cout << "String: " << s << ",Next char: " << nex2 << endl;
cin.ignore(20, '\n');//ignore()一般用来丢弃剩下的输入流中字符
char flag;
cin >> flag;
cout << "flag=" << flag << endl;
if (cin.peek()!='\n')//peek()用来查看下一个字符并返回它,并不取出输入流的数据
cin.ignore(20, '\n');
//read()创建的字符串并不自动在末尾加'\0',putback()将一个字符加到输入流中
system("pause");
return 0;
}
程序运行结果如下