C++的指针属于比较难理解的内容,本章节中有相当一部分在讲述指针、数组的关系与运用。
7.1 函数基本知识
下属代码演示了一个接受两个参数的函数
#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]。
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- azee.cn 版权所有 赣ICP备2024042794号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务