top of page

第三章基本數學運算

3-1輸入

      1.輸入函數cin

         cin是一個讀取書熱設備輸入資料的函數,輸入直到按下enter為止,cin用的串列輸入符號剛好跟cout的<<相反是>>

      範例:

                int a;         //宣導變數a

                cin>>a;     //將輸入資料存為變數a

      2.多個cin輸入

          一個以上輸入變數時,中間以空白鍵做分隔,輸入完第一個變數中間至少要有一個空白鍵,在輸入第二個變數,但是若只有一個變數要輸入中間加了空白鍵,空白鍵後面資料將會被刪除,若cin多筆資料但輸入未達設定數就按enter,城市則會等到全部輸入完成才執行。

      範例:

               #include<iostream>

               using namespace std;

               int main(void)

               {

                         int a,b,c,d;                                    //宣告變數a b c d

                         cin>>a>>b>>c>>d;                     //輸入整數a b c d   

                         cout<<a<<b<<c<<d<<endl;      //輸出已輸入整數a b c d

                         system("pause");

                         return 0;

               }

                程式輸出結果:

                 1 2 3 4                   (輸入部分,輸入完後按下enter)

                 1234

                 請按任意鍵繼續. . .

3-2輸出格式

      1.控制輸出長度

             setw是指定輸出長度的函數,他會指定下一個字元輸出長度,若是字元輸出長度小於指定長度將會靠像最右邊前面補空白,setw函數在iomanip標頭檔當中,要使用setw要先插入此標頭檔,若輸出字元長度超過指定長度,會直接超過指定長度。

      範例:

               #include<iostream>

               #include<iomanip>                              //插入iomanip標頭檔 當中才有setw函數

               using namespace std;

               int main(void)

               {

                         int a=2000;                                    

                         int b=200000;

                         int c=20000000;

                         int d=2000000000;                    

                         cout<<a                             //普通輸出a

                         <<setw(9)<<b                   //給9個位置輸出b

                         <<setw(9)<<c                    //給9個位置輸出c

                         <<setw(9)<<d<<endl;      //給9個位置輸出d

                         system("pause");

                         return 0;

               }

                程式輸出結果:

                2000   200000 200000002000000000       (a輸出會靠左b前面會空3格c則會空1格d會直接補滿並且顯示完整)

                請按任意鍵繼續. . .

      2.設定有效位數

          setprecision是一個設定輸出位數的函數,他將決定下一個輸出字元長度,輸出的字元將自動向左對齊超過有效位數的部分將自動四捨五入,若大於有效位數,多餘空白的部分也將自動刪除,setprecision跟setw一樣包含在iomanip標頭檔中。

      範例:

               #include<iostream>

               #include<iomanip>                              //插入iomanip標頭檔 當中才有setprecision函數

               using namespace std;

               int main(void)

               {

                         double a=1.23456789;                  

                         cout<<setprecision(3)<<a<<endl;           //給3個位數輸出a

                         cout<<setprecision(5)<<a<<endl;           //給5個位數輸出a

                         cout<<setprecision(11)<<a;                    //給11個位數輸出a

                         cout<<setprecision(9)<<a<<endl;          //給9個位數輸出a

                         system("pause");

                         return 0;

               }

                程式輸出結果:

                1.23

                1.2346

                1.234567891.23456789

                請按任意鍵繼續. . .

      3.設定輸出旗號

          setiosflags是根據參數中的格式其好設定輸出資料格式,格式旗號是setiosflags中的參數,其格式如下表,若要使用兩個以上格式旗號,只需使用(|)此符號即可。

      範例:

               #include<iostream>

               #include<iomanip>                              //插入iomanip標頭檔 當中才有setprecision函數

               using namespace std;

               int main(void)

               {

                         double a=12.3456789;                  

                         cout<<setiosflags(ios::scientific)<<a<< endl;           //用科學記好輸出a

                         system("pause");

                         return 0;

               }

                程式輸出結果:

                1.2345678e+01

                請按任意鍵繼續. . .

      4.cout輸出成員

          可更改cout輸出格式的函數有:.width()、.precision()、.setf()、.unsetf()。

          .width()函數可更改cout輸出欄位寬度,相當於setw()的功能。                      範例:cout.width(欄位寬度)

          .percision()函數可更改cout有效輸出位數,等同於setprecision()的功能。   範例:cout.precision(有效位數)

          .setf()函數可以更改cout預設輸出格式,相同於setiosflags()的函數功能。    範例:cout.setf(ios::格式旗號)

          .unsetf()函數則是關閉成員格式。                                                                       範例:cout.setf(ios::格式旗號)

      範例:

               #include<iostream>

               #include<iomanip>                              //插入iomanip標頭檔 當中才有setprecision函數

               using namespace std;

               int main(void)

               {

                         int a=123456789;

                         double b=1.23456789;

                         double c=12.3456789;

                         cout.width(20);

                         cout<<a<<endl;;

                         cout.precision(5);

                         cout<<b<<endl;

                         cout.setf(ios::scientific);

                         cout<<c<<endl;

                         system("pause");

                         return 0;

               }

                程式輸出結果:

                                       123456789

                1.2346

                1.23457e+01

                請按任意鍵繼續. . .

bottom of page