2013年7月18日 星期四

CHAPTER 3 : 數學運算


3.1 鍵盤輸入



3.1.1 輸入函數
#include <iostream>
cin >> 變數;
  • cin 是讀入鍵盤資料,直到按下Enter為止
  • >> 是串列輸出符號(stream input),將串列資料移入指定變數中
  • cin 函數位於 iostream 中,使用時要先匯入標題檔
範例
int length;  //宣告整數變數length 
cin >> length;  //將輸入資料存入length 

3.1.2 多重輸入 cin
#include <iostream>
cin >> 變數1 >> 變數2 >> ... >> 變數3

  • 包含兩個變數以上,以Space作為每筆資料的間格
  • 若只有一個變數,而輸入資料中有空白,則空白後的資料會遺失
  • 若輸入第一筆資料後按Enter,系統會等待使用者輸入其他筆資料之後才會結束輸入

cin練習
#include <iostream>
#include <cstdlib>
using namespace std;

int main(int argc, char *[])
{
 char key;       //宣告字元變數key 
 cout << "請按任意鍵: ";    //輸出訊息字串 
 cin >> key;       //取得鍵盤輸入 
 cout << "輸入的按鍵是: " << key; //顯示訊息與輸入字元 
 
 system("PAUSE");
 return EXIT_SUCCESS;
}

=================================================================================

3.2 輸出格式化

3.2.1 設定輸出長度
#include <iomanip>
stew(指定長度)

  • 輸出字元小於指定長度,會向右對齊,前面補空白
  • 輸出字元大於指定長度,則不會有變化
  • setw 函數 位於 iomanip 中,使用時要先匯入標題檔

範例
int number = 1234;
cout << '(' << number << ")\n";            //輸出(1234)
cout << '(' << setw(3) << number << ")\n"; //輸出(1234)
cout << '(' << setw(5) << number << ")\n"; //輸出( 1234)

cout 與 setw() 練習
#include 
#include 
#include 
using namespace std;

int main(int argc, char *[])
{
 int n1 = 14, n2 = 21474836, n3 = 44;
 int n4 = -889162, n5 = 9, n6 = 524;
 
 cout << setw(12) << n1
   << setw(12) << n2
   << setw(12) << n3
   << endl;
   
 cout << setw(12) << n4
   << setw(12) << n5
   << setw(12) << n6
   << endl; 
    
 system("PAUSE");
 return EXIT_SUCCESS;
}
程式輸出







3.2.2 設定有效數字


#include <iomanip>
setrecision(有效位數)

  • 輸出字元自動向左對齊,超過有效位數部分四捨五入
  • 有效位數大於輸出字元,則不會有變化
  • setrecision 函數 位於 iomanip 中,使用時要先匯入標題檔

stew() 與 setrecision() 練習
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

const double PI = 3.141592653;

int main(int argc, char *[])
{
 cout << setprecision(10);    //設定 10 位有效數字 
 cout << setw(12) << PI * -1 << endl; //設定 12 空間輸出 PI*-1 
 cout << setw(12) << PI * 100 << endl; //設定 12 空間輸出 PI*100 
 cout << setw(12) << PI * 10000 << endl; //設定 12 空間輸出 PI*10000
    
 system("PAUSE");
 return EXIT_SUCCESS;
}
輸出結果







=================================================================================

3.3 輸入格式化

3.3.1 設定輸入長度

#include <iomanip>
setw(指定長度)

  • setw()可以指定輸出與輸入的長度
  • 若輸入字元小於指定長度,則不會有作用
  • 若輸入字元大於指定長度,則多餘部分會被去除

cin 與 setw() 練習
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{
 char string[4];     
 cout << "請輸入數字串: ";  
 cin >> setw(4) >> string;  
 cout << "輸入字串是:" << string <<  endl; 
 cout << string[3]; 
  
 system("PAUSE");
 return 0;
}

程式輸出







3.3.2 cin成員函數

cin.getline


cin.getline(變數,長度,'\n')

  • getline 是讀取鍵盤輸入資料包含空白,直到輸入Enter為止
  • 輸入長度大於指定的長度時,大於的部分會被刪掉

cin.getline()練習

#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
 char sentence[81];   //宣告字串變數
 cout << "請輸入字串: ";
 cin.getline(sentence, 81); //取得輸入文字
 cout << "輸入的字串是: " << sentence;
 cout << endl;
 
 system("PAUSE");
 return 0; 
}
程式輸出








cin.get()
cin.get(字元變數)
  • 若輸入緩衝器中已存在上一次輸入的資料,則cin.get()會直接讀取上次沒讀完的輸入。要避免這個問題要用cin.ignore()
=================================================================================

3.4 算術運算式

3.4.1 算數運算符號

% 取餘數

3.4.2 上限與下線溢位

  • overflows 指定一個超過該型態最大值的數
  • underflows 指定一個超過該型態最小值的數

overflows 與 underflows 練習

#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
 short n1 = 32767;   //short 最大值 
 cout << n1 << " + 1 = "; 
 n1 = n1 + 1;    //overflow
 cout << n1 << endl;
 
 unsigned short n2 = 65535; //unsigned short 最大值 
 cout << n2 << " + 1 = ";
 n2 = n2 + 1;    //overflow
 cout << n2 << endl;
 
 short n3 = -32768;   //short 最小值 
 cout << n3 << " - 1 = ";
 n3 = n3 - 1;    //underflow
 cout << n3 << endl;
 
 unsigned short n4 = 0;   //unsigned short 最小值 
 cout << n4 << " - 1 = ";
 n4 = n4 - 1;    //underflow
 cout << n4 << endl;
 
 system("PAUSE");
 return 0; 
}
程式輸出










3.4.3 轉換資料型態

指定型態(資料or變數)

由小轉大練習
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
 int n;
 
 short n1 = 32767;
 n = int(n1 + 1);
 cout << n1 << " + 1 = " << setw(6) << n << endl;  //stew()排版用
 
 unsigned short n2 = 65535;
 n = int(n2 + 1);
 cout << n2 << " + 1 = " << setw(6) << n << endl;
 
 short n3 = -32768;
 n = int(n3 - 1);
 cout << n2 << " - 1 = " << setw(6) << n << endl;
 
 unsigned short n4 = 0;
 n = int(n4 - 1);
 cout << n4 << setw(11) << " - 1 = " << setw(6) << n << endl;
 
 system("PAUSE");
 return 0; 
}
程式輸出









由大轉小

  • 較大型態轉為較小型太可能會遺失資料。


由大轉小練習
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
 int intVar = 5000;
 short shortVar = short(intVar);
 cout << "invVar = " << intVar << endl;
 cout << "shortVar = short(intVar) = " << shortVar << endl;       //遺失資料,導致數字錯誤 
 
 short shortLet = 65;
 char charLet = char(shortLet);           
 cout << "shoutLet = " << shortLet << endl;
 cout << "charLet = " << charLet << endl;       //將65以ASCII編碼所代表的字輸出    
 
 float floatNum = 70000.0f;              //小數預設值為double類型,需在後面加f才是float類型 
 int intNum = int(floatNum);
 short shortNum = short(floatNum);
 cout << "floatNum = " << floatNum << endl;
 cout << "intNum = int(floatNum) = " << intNum << endl;
 cout << "shortNum = short(floatunm) = " << shortNum << endl;  //遺失資料,導致數字錯誤 
 
 system("PAUSE");
 return 0; 
}
程式輸出











3.5 指定運算

3.5.1 單一指定

3.5.2 多重指定

int a, b, c, d;    //注意:宣告的時候不能使用多重指定 
a = b = c = d = 100;  //多重指定 

3.5.3 混合指定(combined assignment operators)
//若運算結果存回原本的變數時,可以使用混和運算,減少重複輸入 
//符號有 +=, -=, *=, /=, %= 

//例如 
a = a + 10 
a += 10

a = a - 10
a -= 10 
指定運算練習
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
 float a, b, c, d, x = 6;
 int f = 20;
 a = b = c = d = float(f);
 cout << "a = b = c = d = f = 20, x = 6"
    << "\na += x => a = " << (a += x)
    << "\nb -= x => b = " << (b -= x)
    << "\nc *= x => c = " << (c *= x)
    << "\nd /= x => d = " << (d /= x)
    << "\nf %= x => f = " << (f %= 6)
    << endl;
    
 system("PAUSE");
 return 0;
}
程式輸出










3.6 數值函數

3.6.1 亂數函數

產生固定亂數
#include <cstdlib>  
#include <iostream>
using namespace std;

int main()
{
 cout << rand() << endl;  //亂數輸出 
 cout << rand() << endl;  //亂數輸出 
 cout << rand() << endl;  //亂數輸出 
 
 system("PAUSE");
 return 0;
}

// rand 函數會產生 0 到 RAND_MAX 之間的整數亂數
// rand 函數包含於 cstdlib 標題檔中,使用時須插入
程式輸出








產生種子亂數
//strand 函數是依據種子數來設定亂數的起點,必須至於 rand 前。
//不同的種子數產生不同的亂數樣式,相同種子亂數樣式會相同。
//種子數是無號整數 宣告使用 unsigned seed 
//strand 函數包含於 cstdlib 標題檔中

#include <cstdlib>  
#include <iostream>
using namespace std;

int main()
{
 unsigned seed;
 cout << "請輸入種子數: ";
 cin >> seed;    //輸入種子數
 srand(seed);    //設定亂數種子數
 cout << rand() << endl;  //亂數輸出 
 cout << rand() << endl;  //亂數輸出 
 cout << rand() << endl;  //亂數輸出
 
 system("PAUSE");
 return 0;
}

產生隨機亂數
//time 函數包含於 ctime 標題檔中
#include <cstdlib>  
#include <iostream>
#include <ctime>                          
using namespace std;

int main()
{
 srand(time(NULL));   //以時間函數為種子數 
 cout<< rand() << endl;  //輸出亂數 
 cout<< rand() << endl;  //輸出亂數 
 cout<< rand() << endl;  //輸出亂數 
 
 system("PAUSE");
 return 0;
}
程式輸出2次
















調整亂數範圍
上限 + rand() % (上限 - 下限 + 1)
#include <cstdlib>  
#include <iostream>
#include <ctime>     
using namespace std;

int main()
{
 srand(time(NULL));                              //以時間函數為種子數
 cout << 1 + rand() % (10 - 1 + 1) << endl;      //輸出1~10之間的亂數 
 cout << 450 + rand() % (500 - 450 + 1) << endl; //輸出450~500之間的亂數
 cout << 1 + rand() % (5 - 1 + 1) << endl;  //輸出1~5之間的亂數 
   
 system("PAUSE");
 return 0;
}
程式輸出







3.6.2 三角函數

C++ 的數學函式庫是用弧度的。要處理度數,必須自行轉換。

#include <cstdlib>  
#include <iostream>
#include <cmath>     //三角函數包含於cmath標頭檔中 
using namespace std;

int main()
{
 x = 30 * (3.14159 / 180) //將30度轉成弧度 
 double a = sin(x);   //三角函數輸出是double類型 
 double b = cos(x);
 double c = tan(x);
 
 system("PAUSE");
 return 0;
}

3.6.3 指數與對數
#include <cstdlib>  
#include <iostream>
#include <cmath>     //指數對數包含於cmath標頭檔中 
using namespace std;

int main()
{
 double a = log(2);  //ln(2)
 double b = log10(2); //log2 (以10為底數)
 double c = exp(2);  //e^2
 
 cout << a << endl
   << b << endl
   << c << endl;
 
 system("PAUSE");
 return 0;
}

冪次與開方
#include <cstdlib>  
#include <iostream>
#include <cmath>     //冪次與開方包含於cmath標頭檔中 
using namespace std;

int main()
{
 double a = pow(2, 3);  //a=2^3
 double b = sqrt(3);    //b=√3
 
 cout << a << endl
   << b << endl;

 system("PAUSE");
 return 0;
}


小數進位與切除小數
#include   
#include 
#include      //ceil,floor函數包含於cmath標頭檔中 
using namespace std;

int main()
{
 double a = ceil(3.33);  //ceil 正數有小數進位
 double b = floor(3.33);  //floor 正數有小數刪除 
 double c = ceil(-3.33);  //ceil 負數有小數刪除 
 double d = floor(-3.33); //floor 負數有小數-1
  
 cout << a << endl
   << b << endl
   << c << endl
   << d << endl;

 system("PAUSE");
 return 0;
}
輸出結果








3.6.6 取絕對值
#include <cstdlib>  
#include <iostream>
#include <cmath>     //fabs函數包含於cmath標頭檔中 
using namespace std;

int main()
{
 int a = fabs(3);   //程式輸出3 
 int b = fabs(-133);   //程式輸出133 
  
 cout << a << endl
   << b << endl;

 system("PAUSE");
 return 0;
}


=================================================================================

3.7 習題:

選擇題

  1. 在C++程式中,使用cin或cout函數,需在前置處理區插入 #include<iostream> 標頭檔
  2. 在C++程式中,算數運算符號+、-、*、/、% 等,不須在前置處理區加入任何檔
  3. 在C++程式中,要使用數學函數、指數對數、冪次開方、取整函數、取絕對值函數,需在前置處理區加上  #include<cmath> 的標題檔
  4. C++敘述int r = 3 * 2 + 14 % 5 - 4;,則 r = 6
  5. C++敘述float s = 15 / 2 + 12 * (3 - 5) ;,則 s = -17
  6. pow(x, 3)是x^3的C++等效函數
  7. sqrt(100)是√100的等效函數
  8. 產生亂數的C++函數是rand()
  9. 輸出0到9之間亂數的C++敘述是
srand(time(NULL));
cout << 0 + rand() % (9 - 0 + 1);


實做題


  1. 寫一個C++程式,由鍵盤輸入半徑(r),計算顯示圓周長與圓面積。
#include <cstdlib>  
#include <iostream>
#include <cmath>    
using namespace std;

const double PI = 3.1415926;

int main()
{ 
 double r;
 double p;
 double a;
 
 cout << "請輸入半徑: ";
 cin >> r;
 cout << "圓周長 = " << (2 * PI * r) << endl
   << "圓面積 = " << (PI * pow(r, 2)) << endl;

 system("PAUSE");
 return 0;
}

2.寫一個程式求 tan28° * tan62° + (tan35° )^2 - (sec35° )^2
#include <cstdlib>  
#include <iostream>
#include <cmath>    
using namespace std;

const double A = 3.145926 / 180;   //角度要換成弧度

int main()
{ 
 double i;
 double j = 1 / cos(35*A);    //sec35°
 
 i = tan(28*A) * tan(62*A) + pow(tan(35*A), 2) - pow(j, 2);
 
 cout << i;
  
 system("PAUSE");
 return 0;
}

3.寫一個程式,產生1至99之間的整數亂數
#include <cstdlib>  
#include <iostream>
#include <ctime>    
using namespace std;


int main()
{ 
 
 srand(time(NULL));
 cout << 1 + rand() % (99 - 1 + 1);
  
 system("PAUSE");
 return 0;
}


4.寫一個程式,計算本利和,輸出畫面如下

年利率: 3.25%
期數: 12
本金: $10000.00
利息: $329.89
本利和: $10329.89

#include <cstdlib>  
#include <iostream>
#include <cmath> //pow
#include <iomanip> //setw  
using namespace std;


int main()
{ 
 double i = 0.0325;  //年利率 
 int j = 12;       //期數 
 double k = 10000.00; //本金 
 double l = 329.89;  //利息 
 
 cout << "年利率:" << setw(6) << i * 100 << "%" << endl
   << "期數:" << setw(6) << j << endl 
   << "本金:" << setw(5) << '$' << k << endl
   << "利息" << setw(6) << '$' << l << endl
   << "本利和:" << setw(3) << '$' << (k * pow((1 + (i / j)), j)) << endl;

 system("PAUSE");
 return 0;
}


5. 寫一個程式,產生100~999的亂數,按下Enter後加總輸出

#include <iostream>
#include <cstdlib> //system(),rand()
#include <ctime>     //time()
#include <iomanip> //setw()
using namespace std;

int main()
{
 int i;
 int j;
 
 srand(time(NULL));
 i = 100 + rand() % (999 - 100 + 1);   //賦值是 =  
 j = 100 + rand() % (999 - 100 + 1);
 
 cout << setw(10) << i << endl
   << '+' << setw(9) << j << endl
   << "----------";
 cin.get();         //讓程式按下Enter才會執行下去 
 cout << setw(10) << i + j << endl;
   
 system("PAUSE");
 return 0;
}




沒有留言:

張貼留言