C++定義的標準流主要包括 cin、cout、cerr、clog。
cin 是由終端輸入數據,默認為鍵盤;
cout 為向終端輸出數據,默認為屏幕;cerr 是向終端輸出標準錯誤信息,默認為屏幕;
clog 為 cerr 的緩沖形式,默認為屏幕。
用博客鏈接 中的例子,使用 cin() 和 cout() 計算兩個日期之間的天數之差。
#include<iostream>#include<math.h>using namespace std;int f(int year,int month){ if (month<3) { return year-1; } else { return year; }}int g(int month){ if (month<3) { return month+3; } else { return month+1; }}int N_Value(int year,int month,int day){ int N = 1461*f(year,month)/4+153*g(month)/5+day; return N;}void main(){ int year1,mongth1,day1,year2,mongth2,day2,N1,N2,dN; cout<<"Please input integer year,mongth,day,the first date:"<<endl; cin>>year1>>mongth1>>day1; cout<<"Please input integer year,mongth,day,the seconddate:"<<endl; cin>>year2>>mongth2>>day2; N1 = N_Value(year1,mongth1,day1); N2 = N_Value(year2,mongth2,day2); dN = abs(N1-N2); PRintf("There are %d days between the two dates./n",dN);}結果:
新聞熱點
疑難解答