|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 这是她 于 2020-4-27 20:59 编辑
Heard melodies are sweet,but those unheard are sweeter.
函函函函函函函函函数来喽
- #include <iostream>
- const int Max = 10;
- //函数原型---是一条语句必须以分号结束;不要求提供函数名
- void uparameter(int a);
- int parameter(int b);
- void mutiparameter(int c,char d);
- int fill_array(double ar[],int n);//传递常规变量--->函数将使用该变量的拷贝;传递数组--->函数使用原来的数组(传递的是数组的地址不是数组的内容)
- void show_array(const double ar[],int n);
- void revalue(double r,double ar[],int n);
- int sum_arr(const int * begin,const int * end);
- using namespace std;
- int main ()
- {
- int swan,crab,penguin,magple;
- char snake;
- double goose[Max];
- int tree[Max] = {34,36,7,35,367,38,96,73};
-
- cout << "Please enter a number of swan : " << endl;
- cin >> swan;
-
- uparameter(swan);//调用函数---没有返回值的函数(void函数)
- //swan是一个形参--传递个函数的值
-
- cout << "Please enter a number of crab :" << endl;
- cin >> crab;
- penguin = parameter(crab);//调用函数---有返回值的函数(将生成一个值,并将他返回给调用函数);对于返回值的类型不能是数组
- cout << "Value : penguin " << penguin << endl;
-
- //含有多个参数的函数
- cout << "Please enter a number of penguin : " << endl;
- cin >> penguin;
- cout << "Enter a character : " << endl;
- cin >> snake;
- mutiparameter(penguin,snake);
-
- //函数和数组
- int size = fill_array(goose,Max);
- show_array(goose,size);
- if (size > 0)
- {
- cout << "Enter revaluation factor : ";
- double factor;
- while (!(cin >> factor))
- {
- cin.clear();
- while (cin.get() != '\n')
- continue;
- cout << "Bad input;Please enter a number : ";
- }
- revalue(factor,goose,size);
- show_array(goose,size);
- }
- cout << "Done.\n";
- cin.get();
- cin.get();
-
- int sum = sum_arr(tree,tree + Max);
- cout << "Value : tree : " << sum << endl;
- sum = sum_arr(tree,tree + 4);
- cout << "Value : 0-3 : " << sum << endl;
- sum = sum_arr(tree + 4,tree + 7);
- cout << "Value : 3-6 : " << sum << endl;
-
- return 0;
- }
- //函数的定义---没有返回值的函数
- void uparameter(int a)//a是形参--接受传递值
- {
- for (int i = 0;i < a;i++)//i是局部变量,是在diplomatic这个函数中的变量
- cout << "constructive!";
- cout << endl;
- }
- //函数的定义---有返回值的函数
- int parameter(int b)
- {
- return b * b * b;//返回值的类型要与定义函数的类型相同
- }
- //多个参数的函数
- void mutiparameter(int c,char d)//使用','分隔参数
- {
- for (int i = 0;i < c;i++)
- cout << "amiable " << d;
- cout << endl;
- }
- //填充数组
- int fill_array(double ar[],int n)//接受两个参数:数组名和指定了要读取的最大元素数
- {
- double temp;
- int i;
- for (i = 0;i < n;i++)
- {
- cout << "Enter value $ " << (i+1) << ": ";
- cin >> temp;
- if (!cin)
- {
- cin.clear();
- while(cin.get() != '\n')
- continue;
- cout << "Bad input;input process terminated.\n";
- break;
- }
- else if (temp < 0)
- break;
- ar[i] = temp;
- }
- return i;
- }
- //显示数组及用const保护数组
- void show_array(const double ar[],int n)//为了函数防止无意中修改数组的内容,在声明形参的时候使用关键字const
- {
- for (int i = 0;i < n;i++)
- {
- cout << "goose $" << (i+1) << " : ";
- cout << ar[i] << endl;
- }
- }
- //修改数组——需要给函数提供三个参数:因子、数组和元素数目;该函数不需要返回值
- void revalue(double r,double ar[],int n)
- {
- for (int i = 0;i < n;i++)
- ar[i] *= r;
- }
- //使用数组区间的函数
- 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;
- }
复制代码
渣渣一个 各位大佬手下留情
你的建议,我的改变
|
|