開始使用Visual C++
Download
Report
Transcript 開始使用Visual C++
開始使用Visual C++
CSIM, PU
建立第一個C++程式
由功能表點選【File/New】
點選Project,選取『Win32 Console Application』,並且設定Project
Name
Console-mode程式是一個以字元為主的應用程式,主要在DOS視窗中執
行
接著選取『An Empty Project』選項
完成設定後,按下工具列的『New Text File』,出現編輯視窗
編輯程式前,請先【存檔】,這樣編輯程式時才會有“關鍵字高亮度顯
示”等功能,以方便編輯
存檔時請自行設定檔案的副檔名(.cpp)
CSIM, PU
執行第一個C++程式
程式經過編譯之後,若沒有問題(可由build status視窗中看到
訊息),便可連結並建立執行檔,執行程式。
和.cpp檔案同一目錄中,會產生一Debug目錄,程式的執行
檔便放在此目錄中,可在DOS視窗中來執行此程式。
CSIM, PU
程式範例
// Test.cpp
// 程式的目的是在顯示器上顯示一行文字
// -------------------------------------#include <iostream.h>
// 程式主體部份
int main()
{
cout << "Hello, 您好!" // 主要的敘述
<< endl;
return 0;
}
CSIM, PU
程式範例
// 要求使用者輸入兩個整數,並把計算結果輸出(練習使用cout, cin)
#include <iostream>
int main()
{
int x; // 第一個輸入整數
int y; // 第二個輸入整數
int Sum; // 兩個整數的和
cout << "請輸入第一個整數\n";
cin >> x;
cout << "請輸入第二個整數\n";
cin >> y;
Sum = x + y;
cout << "這兩個整數的和是: " << Sum << endl;
return 0;
}
CSIM, PU
程式範例
CSIM, PU
// A program to check the size of data types
#include <iostream>
main()
{
cout << " Size of int is : "
<< sizeof(int)
<< " bytes" << endl;
cout << " Size of short is : " << sizeof(short)
<< " bytes" << endl;
cout << " Size of unsigned is: " << sizeof(unsigned) << " bytes" << endl;
cout << " Size of long is : " << sizeof(long)
<< " bytes" << endl;
cout << " Size of float is : "
<< sizeof(float)
<< " bytes" << endl;
cout << " Size of double is : " << sizeof(double) << " bytes" << endl;
cout << " Size of char is : " << sizeof(char)
<< " bytes" << endl;
cout<< " Size of 3.8 is : "
<< sizeof(3.8)
<< " bytes" << endl;
cout<< " Size of 3.8 + a is : " << sizeof(3.8 + a) << " bytes" << endl;
cout<< " Size of 3.8f is : "
<< sizeof(3.8f)
<< " bytes" << endl;
cout<< " Size of a is
:"
<< sizeof(a)
<< " bytes" << endl;
cout<< " Size of 3.8f + a is: " << sizeof(3.8f + a) << " bytes" << endl;
}
// x的3次方計算
#include <cmath>
#include <iostream>
main()
{
float x ;
cout << "請輸入一個數值:\n ";
cin >> x;
cout << "它的3次方是: "
<< pow(x,3) << endl ;
}
CSIM, PU
常用的數學函數
名稱
功能
輸出值的資料型態
abs(x)
絕對值
int
fabs(x)
絕對值
與x相同
exp(x)
double
pow(x,y)
次方
與x相同
sqrt(x)
開根號
與x相同
log(x)
取對數
double
log10(x)
以10為底取對數
Double
ceil(x)
>=x的最小整數
int
floor(x)
<=x的最大整數
int
CSIM, PU
C++的資料型態
基本資料型態
整數
int, short, long
浮點數 float, double, long double
字元
char
邏輯值 bool (bool b1,b2,b3; b1=true; b2=false; b3=1)
衍生資料型態
與位址相關的資料型態
指標
參照
有結構的資料型態
string
enum
aray
struct
union
class
CSIM, PU
函數
函數的語法有三個部分:
函數的宣告
建立函數的prototype,以告知編譯器本程式將使用的
函數名稱,以及進出這個函數的資料之型態和數量
函數的定義
函數的呼叫
CSIM, PU
範例程式
#include <iomanip>
//---------函數C2F的宣告-------------------------- float C2F(float);
int main()
{
float CTemp;
cout << " 攝氏 華氏 " << endl ;
cout << "-----------------" << endl ;
for ( int i = 1 ; i <= 10 ; i++ )
{
CTemp = 10.0*i;
cout << setw(5) << CTemp << " "
<< setw(5) << C2F(CTemp) << endl ;
}
cout << "-----------------" << endl ;
return 0;
}
CSIM, PU
//-------函數C2F的定義---------float C2F(float C)
{
float F;
F = C*9.0/5.0 + 32.0;
return F;
}
指標
指標的定義
float* ptf; //定義指標ptf, 較常用
float *ptf; //定義指標ptf,較少用
如果要同時定義兩個指標
float *p1,*p2; //p1,p2都是指標
float* p1,p2; //p1是指標, p2是float變數
CSIM, PU