C++的指針屬于比較難了解的内容,本章節中有相當一部分在講述指針、數組的關系與運用。
7.1 函數基本知識
- 提供函數定義。分為兩類函數有傳回值的函數和無傳回值的函數,無傳回值的函數為void函數。
上述函數中int n意味調用函數cheers()時,需要将一個int類型參數傳遞給函數。void cheers(int n) { for (int i = 0; i < n; i++) cout<<"cheers=:"; cout<<endl; }
- 提供函數原型。調用函數之前需要在main函數之前聲明函數。
void fufu(float a, b)//此為不正确聲明變量方式。 void fufu(float a, float b)//正确聲明方式。
- 調用函數。函數中定義的局部變量如x, n, i等,在另外一個函數中也定義這樣的同名變量,不會帶來沖突。
下屬代碼示範了一個接受兩個參數的函數
#include <iostream>
using namespace std;
void n_chars(char, int);
int main()
{
int times;
char ch;
cout << "enter a character:";
cin >> ch;
cout << "\n";
while (ch!='q')
{
cout << "enter a integer:";
cout << "enter a character:";
cout << "\n";
cin >> times;
if (!cin)
{
cout << "alpha only!!!! please enter again." << endl;
cin.clear();
cin.ignore();//清空讀取緩存
continue;
}
n_chars(ch, times);
cout << "enter another character or press q to quit." << endl;
cin >> ch;
}
cout << "the value of times is " << times << endl;
return 0;
}
void n_chars(char c, int n)//不停輸出c的值,直到n==0
{
while (n-- > 0)
{
cout << c;
}
cout << endl;
}
形參跟局部變量的差別。
彩票中獎機率計算程式。
#include <iostream>
using namespace std;
long double probability(unsigned numbers, unsigned picks);
int main()
{
double total, choices;
long double P;
cout << "enter the total number of choices on the game card and\n"
<< "the number of picks allowed." << endl;
while ((cin>>total)&&(cin>>choices)&&(choices<=total))
{
P = probability(total, choices);
cout << "you have a chance in " << P << "of winning." << endl;
cout << "please enter anohter two numbers, or press non-digit to quit." << endl;
}
return 0;
}
long double probability(unsigned numbers, unsigned picks)//不停輸出c的值,直到n==0
{
long double R = 1.0;
unsigned n;
unsigned p;
for ( n = numbers, p =picks ; p > 0; n--,p--)
{
R = R * (n / p);
}
return 1/R;
}
7.3.1 C++函數如何使用指針來處理數組
數組名即為指針,指向數組中第一個元素的位址
#include <iostream>
using namespace std;
const int s = 8;
int cookies[s];
int parameter;
int sum_arr(int arr[], int n);
int main()
{
int cookies[s] = { 1,2,4,8,16,32,64,128 };
cout << cookies << " = arry address." << sizeof(cookies) << " = sizeof cookies." << endl;
int sum = sum_arr(cookies, s);
cout << sum << "cookies eaten." << endl;
sum = sum_arr(cookies, 3);
cout << sum << "first three ate." << endl;
sum = sum_arr(cookies+4, 4);
cout << sum << "last four ate." << endl;
while (1)
{
}
return 0;
}
int sum_arr(int arr[], int n)//arr為指向數組的指針,sizeof(arr)輸出指針的大小,此處為4。
{
int total = 0;
cout << arr << " = arr, " << sizeof(arr) << "= sizeof arr." << endl;
for (int i = 0; i < n; i++)
{
total = total + arr[i];
}
return total;
}
上述代碼中 main函數中cookies就是指向cookies數組的指針。
#include <iostream>
int c_in_str(const char * str, char ch);
int main()
{
using namespace std;
char mmm[15] = "minimum";
const char *wail = "ululate";
int ms = c_in_str(mmm, 'm');
int us = c_in_str(wail, 'u');
cout << ms << " m characters in " << mmm << endl;
cout << us << " u characters in " << wail << endl;
getchar();
getchar();
return 0;
}
int c_in_str(const char * str, char ch)
{
int count = 0;
while (*str)
{
if (*str==ch)
count++;
str++;
}
return count;
}
上述代碼中,const char *wail 如果不加const就無法編譯通過;
#include <iostream>
char * buildstr(char c, int n);
int main()
{
using namespace std;
char ch;
int times;
cout << "enter an character:";
cin >> ch;
cout << "enter an integer:";
cin >> times;
char *ps = buildstr(ch, times);
cout << ps << endl;
delete[] ps;
ps = buildstr('+', 20);
cout << ps << "-done-" << ps << endl;
delete[] ps;
getchar();
getchar();
return 0;
}
char * buildstr(char c, int n)
{
char *pstr = new char[n + 1];
pstr[n] = '\0'; //将字元串最後一個字元改成空值
while (n-- > 0)
{
pstr[n] = c;
}
return pstr;
}
傳回字元串的函數就必須在函數之前加上 “*” ,不加的話程式就不能編譯完成,因為函數return的是一個指針
#include <iostream>
struct travel_time
{
int hours;
int mins;
};
const int mins_per_hr = 60;
travel_time sum(travel_time t1, travel_time t2);
void show_time(travel_time t);
int main()
{
using namespace std;
travel_time day1 = { 5, 45 };
travel_time day2 = { 4, 55 };
travel_time trip = sum(day1, day2);
travel_time day3 = { 4, 32 };
cout << "three day total: ";
show_time(sum(trip, day3));
getchar();
return 0;
}
//将兩次時間加到一起:A:1小時20份 B:1小時15分鐘 則A+B為2小時35分鐘
travel_time sum(travel_time t1, travel_time t2)
{
travel_time total;
total.mins = (t1.mins + t2.mins) % mins_per_hr;
total.hours = t1.hours + t2.hours + (t1.mins + t2.mins) / mins_per_hr;
return total;
}
void show_time(travel_time t)
{
using namespace std;
cout << t.hours << "hours, " << t.mins << "minutes\n";
}
struct的應用 struct名稱 變量名 就是命名方式 。travel_time sum(travel_time t1, travel_time t2)表示sum函數接受兩個travel_time參數,并且傳回的是travel_time數值。
#include <iostream>
//聲明polar rect 結構
struct polar
{
double distance;
double angle;
};
struct rect
{
double x;
double y;
};
void rect_to_polar(const rect *pxy, polar *pda);
void show_polar(const polar *pda);
int main()
{
using namespace std;
rect rplace;
polar pplace;
cout << "enter the x and y:";
while (cin>> rplace.x,cin >> rplace.y)
{
rect_to_polar(&rplace, &pplace);
show_polar(&pplace);
cout << "next x and y(no_digits to quit)";
}
cout << "done." << endl;
return 0;
}
//直角坐标系轉換為極坐标系 void函數無傳回值的需求 無return
//從rect結構中用指針讀取數值并進行運算,用polar指針讀取運算結果并賦予distance和angle
void rect_to_polar(const rect *pxy, polar *pda)
{
using namespace std;
pda->distance = sqrt(pxy->x*pxy->x + pxy->y*pxy->y);
pda->angle = atan2(pxy->y, pxy->x);
}
void show_polar(const polar *pda)
{
using namespace std;
const double Rad_to_deg = 57.29577951;
cout << "distance: " << pda->distance << endl;
cout << "angle: " << pda->angle*Rad_to_deg << endl;
}
用指針讀取struct類型資料,很精巧。
#include <iostream>
#include <string>
using namespace std;
const int Size = 5;
void display(const string sa[], int n);
int main()
{
using namespace std;
string list[Size];
cout << "enter you string list, " << Size << " favorite astronomical sights." << endl;
for (int i = 0; i < Size; i++)
{
cout << i + 1 << ":";
getline(cin, list[i]);
}
cout << "display your list:"<<endl;
display(list, Size);
getchar();
return 0;
}
void display(const string sa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << i + 1 << ":" << sa[i] << endl;
}
}
在上述代碼中list[size]的實際結構為list[fdaf,gdfdg,llkj,jiefjd,jifafe]。