7.4 函式和二維陣列
阿新 • • 發佈:2018-11-17
7.5 函式和c-風格字串
C-風格字串由一系列字元組成,以空值字元結尾。
將字串作為引數時意味著傳遞的是地址,但可以使用const來禁止對字串引數進行修改。
7.5.1 將C-風格字串作為引數的函式
假設要將字串作為引數傳遞給函式,則表示字串的方式有三種:
char陣列;
用引號括起的字串常量(也稱字串字面值);
被設定為字串的地址的char指標。
#include <iostream> usigned int c_in str{const char *str,char ch}; int main() { using namespace std; char mmm[15]="minimum"; //string in an array //some systems require preceding char with static to enable array initialization char *wail="ululate"; //wail points-to string unsigned int ms=c_in_str(mmm,'m'); unsigned int us =c_in_str(wail,'u'); cout<<ms<<"m characters in "<<mmm<<endl; cout<<us<<"u characters in "<<wail<<endl; return 0; } //this function counts the number of ch characters in the string atr unsigned int c_in_str(const char *str.char ch) { unsigned int count=0; while (*str) //quit when *str is'/0' { if (*str == ch) count ++; str++; //move pointer to next char } return count; }
travel_time sum(travel_time t1,travel_time t2) { travel_time toal; 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"; }
#include <iostream> #include<cmath> //structure templates struct polar { double distance; //distance from origin double angle; //direcction from origin }; struct rect { double x; //horizontal distance from origin double y; //vertical distance from origin }; //prototypes void rect_to_plar(const rect *pxy,polar *pda); void show_polar(const polar *pda); int main() { using namespace std; rect rpace; polar pplace; cout<<"Enter the x and y values:" while (cin >> rplace.x>>rplace.y) { rect_to polar(&rplace,&pplace); //pass addresses show_polar(&applace); //pass addresses cout<<"Next two number (q to quit):" } cout<<"Done.\n"; return 0; } //show polar coordinates,corverting angle to degrees void show_polar (const polar *pda) { using namespace std; const double Rad_to_deg=57.29577951; cout<<"distance ="<<pda->distance; cout<<",angle="<<plda->angle *Rad_to_deg; cout<<"degrees\n"; } //convert rectangular to polar coordinates 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); }
7.7函式和string物件
7.8 函式和array物件
類物件是基於結構的,因此結構程式設計方面的有些考慮因素也適用於類。例如,可按值將物件傳遞給函式,在這種情況下,函式處理的是原始物件的副本。另外,也可將傳遞指向物件的指標,這讓函式能夠操作原始物件。
#include <iostream>
#include <array>
#include <string>
//constant data
const int Seasons =2;
const std::array<std::string,Season>Snames={"Spring","Summer","Fall","Winter"};
//function to modify array object
void fill(std::array<double,Seasons> *pa);
//function that uses array object without modifying it
void show (std::array<double,Seasons> da);
int main()
{
std::array<double,Seasons> expenses;
fill(&expenses);
show(expenses);
return 0;
}
void fill (std:;array<double,Seasons> *pa)
{
using namepace std;
for (int i=0;i<Seasons;i++)
{
cout<<"enter"<<Sname[i]<<"expenses:";
cin>>(*pa)[i];
}
}
void show(std::array<double,Seasons> da)
{
using namespace std;
double total=0.0;
cout<,"\nEXPENSES\n";
for (int i-=0;i<Seasons;i++)
{
cout<<Sname[i]<<":$"<<da[i]<<endl;
total +=da[i];
}
cout <<"Total Expenses:$"<<total<<endl;
}
7.9 遞迴
7.10函式指標
#include <iostream>
//various notations,same signatures
const double *f1(const double ar[],int n);
const double *f2(const double [],int);
const double *f3(const double *,int);
int main(){
using namespace std;
double av[3]=[1112.3,1542.6,2227.9);
//pointer to a function
const double *(*p1)(const double *,int)=f1;
auto p2=f2;