第七章——函数

  1. 数组传值
#include<iostream>
using namespace std;

int sum(int arr[],int n);
const int Size=8;

int main()
{
    int cookies[Size]={1,2,4,8,16,32,64,128};
    int s=0,s1=0,s2=0;
    s=sum(cookies,Size);
    cout<<"Total cookies eaten:"<<s<<endl;
    s1=sum(cookies,3);
    cout<<"First three people ate:"<<s1<<endl;
    s2=sum(cookies+4,4);                              //传地址
    cout<<"Last four people ate:"<<s2<<endl;
    return 0;
}

int sum(int arr[],int n)
{
    int i,s=0;
    cout<<sizeof arr<<endl;   //arr 的大小为4这是指针的大小 因为传的是地址,即指针
    cout<<arr<<endl;
    for(i=0;i<n;i++)
      s=s+arr[i];
    return s;
}

2.数组填充(根据输入数目的多少),数组显示和修改数组。

#include<iostream>
using namespace std;

const int MAX=5;
int fill_array(double a[],int limit);
void show_array(const double a[],int n);
void revalue(double a[],double r,int n);

int main()
{
    double arr[MAX];
    double factor;
    int size=fill_array(arr,MAX);
    show_array(arr,size);              //数组传递一律用数组名,不管是否为const
    cout<<"Enter revalue factor:";
    cin>>factor;
    revalue(arr,factor,size);
    cout<<"the new array is:\n";
    show_array(arr,size);
    cout<<"Done\n";
    return 0;
}

int fill_array(double a[],int limit)
{
    int i;
    for(i=0;i<limit;i++)
    {
      cout<<"Enter value #"<<i+1<<":";
      cin>>a[i];
      if(a[i]<0)
          break;
    }
    return i;
}
void show_array(const double a[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        cout<<a[i]<<" ";
    }
}
void revalue(double a[],double r,int n)
{
    int i;
    for(i=0;i<n;i++)
       a[i]=a[i]*r;
}

3.int sum_arr(const int *begin,const int *end)
{
const int *pt;
int total=0;
for(pt=begin;pt!=end;pt++)
total=total+*pt;
return total;
}

在主函数调用的时候,改用 sum=sum_arr(cookies,cookies+arrSize); 传参均为指针。
4.函数和字符串

#include<iostream>
using namespace std;

unsigned int c_in_str(const char *str,char ch);  //不记得要用char *str了

int main()
{
    char mmm[15]="minimum";//char *s1 的s1,指针指向常量字符串时,它的内容是不可以被修改的,否则在运行时会报错。char s2[]的s2 是数组对应着一块内存区域,其地址和容量在生命期里不会改变,只有数组的内容可以改变
    char *wail="ululate";       
    unsigned int ms=c_in_str(mmm,'m');       //形参为char *,实参为字符串的名称
    unsigned int us=c_in_str(wail,'u');
    cout<<ms<<" m in "<<mmm<<endl;
    cout<<us<<" u in "<<wail<<endl;
    return 0;
}


unsigned int c_in_str(const char *str,char ch)//可以换为str[] 下面仍然可以用*str
{
    int count=0;
    while(*str)             //重要!或为while(*str!='\0')  
    {
        if(*str==ch)
            count++;
        str++;
    }
    return count;
}

5.返回字符串指针的函数

#include<iostream>
using namespace std;

char *strbuilder(char ch,int n);

int main()
{
    char c;
    int times;
    char *p;
    cout<<"Enter the char:";
    cin>>c;
    cout<<"Enter the times repeat:";
    cin>>times;
    p=strbuilder(c,times);
    cout<<p<<endl;
   // delete []p;
    p=strbuilder('+',20);
    cout<<p<<"-date-"<<p<<endl;
    return 0;
}

char *strbuilder(char ch,int n)
{
   int i;
   char *pt=new char[n+1];  //n+1 存储最后一个空 
   for(i=0;i<n;i++)
   {   *pt=ch;
       pt++;
   }
   *(pt+1)='\0';
   pt=pt-n;
   return pt;
}


将for循环换为
while(n>0)
{
    pt[n]=c;
    n--;
}  从后往前进行,效率更高!

6.如果需要多个字符串,可以声明一个string对象数组,而不是二维char数组。

#include<iostream>
#include<string>
using namespace std;

const int SIZE=5;
void display(string s[],int n);

int main()
{
    string list[SIZE];
    int i;
    cout<<"Enter five strings:\n";
    for(i=0;i<SIZE;i++)
    {  
      cout<<i+1<<"th string:";
      getline(cin,list[i]);    //??此处有问题,对cin的用法掌握的不好。多个回车?
    }
    display(list,SIZE);
    return 0;
}

void display(string s[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {   cout<<i+1<<"th string:";
        cout<<s[i]<<endl;
    }
}

7.函数指针

#include<iostream>
using namespace std;

double betsy(int);
double pam(int);
void estimate(int lines,double (*pf)(int));

int main()
{
    int code;
    cout<<"How many lines of code do you need?";
    cin>>code;
    cout<<"Here's Betsy's estimate:\n";
    estimate(code,betsy);
    cout<<"Here's Pam's estimate:\n";
    estimate(code,pam);
    return 0;
}

double betsy(int lns)
{
    return 0.05*lns;
}

double pam(int lns)
{
    return 0.03*lns+0.0004*lns*lns;
}

void estimate(int lines,double (*pf)(int))
{
    cout<<(*pf)(lines)<<" hours"<<endl;            //Betsy和Pam函数的返回值 返回到这里
}

8.`

#include<iostream>
#include<cctype>
using namespace std;

const int MAX=10;
int  Fill_array(double arr[],int n);
void Show_array(double arr[],int n);
void Reverse_array(double arr[],int n);

int main()
{
    double array[MAX];
    cout<<"Enter num to the array:\n";
    int n=Fill_array(array,MAX);
    cout<<"show the array:\n";
    Show_array(array,n);
    cout<<"Now,reverse the array!";
    Reverse_array(array,n);
    cout<<"The result is:\n";
    Show_array(array,n);
    return 0;
}

int Fill_array(double arr[],int n)
{
   for(int i=0;i<n;i++)
   {
       cin>>arr[i];
     if(cin.fail()) //判断不是数字的方法不能用isdigit()只能判断是否是“0~9” 而且对象为char 
           break;
   }
   return i;
 //  cout<<"i is:"<<i<<endl;
}
void Show_array(double arr[],int n)
{

  for(int i=0;i<n;i++)
    cout<<arr[i]<<" ";
  cout<<endl;
}
void Reverse_array(double arr[],int n)  //此函数有问题? 解决:一定是n-1啊啊啊 !!                          
{
   int i,j,t=0;
   for(i=0,j=n-1;i<n/2;i++,j--)
   {
       t=arr[i];                                
       arr[i]=arr[j];
       arr[j]=t;
   }
   for(i=0;i<n;i++)
     cout<<arr[i]<<" ";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值