7.4 函数和二维数组

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;  

猜你喜欢

转载自blog.csdn.net/weixin_38858860/article/details/84171974
7.4
今日推荐