C++的小程序
时间:2020-01-08 19:06:31
收藏:0
阅读:92
#include <iostream> using namespace std; int main() { char p; cout << "请输入一段文本:\n"; while(cin.peek()!=‘\n‘)//挑取检查放回去,不会改变输入流 { p = cin.get(); cout << p; //cout << (p = cin.get() ); } cout << endl; return 0; }
#include <iostream> using namespace std; int main() { char buf[20]; cin.ignore(7);//忽视 cin.getline(buf,10);//获得一行 cout << buf << endl;//endl回车加清空缓冲区 return 0; } /*cin.ignore/cin.getline*/
#include <iostream> using namespace std; #define SIZE 50 int main() { //const int SIZE = 50;//相当于宏定义,微软喜欢用宏定义,头文件里面 char buf[SIZE]; cout << "请输入一段文本:"; cin.read(buf,20);//点对象里的动作,read,单纯CIN的时候有一个默认的函数供调用,讲类时再讲 cout << "字符串收集到的字符数为:" << cin.gcount() << endl; cout << "输入的文本信息是:"; cout.write(buf,20); cout << endl; return 0; } /*cin.gcount/cin.read*/
cout
#include <iostream> using namespace std; int main() { int width = 4; char str[20]; cout << "请输入一段文本:\n" ; cin.width(5); while(cin >> str) { cout.precision(width++); cout << str << endl; cin.width(5); } return 0; } /*cin.gcount/cin.read*/
#include <iostream> #include <math.h> using namespace std; int main() { double result = sqrt(3.0); cout << "对3开方保留小数点后0~9位,结果如下:\n" << endl; for(int i=0;i <= 9;i++) { cout.precision(i); cout << result << endl; } cout << "当前的输出精为:" << cout.precision() << endl; return 0; } /*cin.gcount/cin.read*/
评论(0)