C++例题练习(1)

环境:Dev-C++( Version:5.6.1)

一.求2个或3个正整数中的最大数,用带有默认参数的函数实现

  代码实现:

 1 #include <iostream>
 2 using namespace std;
 3 int max(int num1,int num2,int num3=0);
 4 int main()
 5 {
 6     int firstNum,secondNum,thirdNum = 0;
 7     int maxNumber;
 8     cout<<"please input two integer number:(Numbers are separated by spaces)"<<endl;
 9     cin>>firstNum>>secondNum;
10     maxNumber = max(firstNum,secondNum);
11     cout<<"max number is :"<<maxNumber<<endl;
12     
13     cout<<"please input three integer number:(Numbers are separated by spaces)"<<endl;
14     cin>>firstNum>>secondNum>>thirdNum;
15     maxNumber = max(firstNum,secondNum,thirdNum);
16     cout<<"max number is :"<<maxNumber<<endl;
17     
18     return 0;
19 }
20 int max(int num1,int num2,int num3)
21 {
22     int tempMax = 0;
23     tempMax = num1 > num2?num1:num2;
24     tempMax = tempMax > num3?tempMax:num3;
25     return tempMax; 
26 }
  PS:这个例题可以使用函数重载来实现,不过对于相同的逻辑,要写两个函数,累觉不爱
  使用带默认参数的函数时,需要注意:如果函数的定义在函数调用之前,则在函数的定义中给出默认参数的默认值;如果函数的定义在函数的调用之后,但在函数的调用之前有声明时,则必须在函数的声明中给出默认参数的默认值(此时在函数定义时能否给函数定义处带默认值,则要依据具体的编译器,如Dev-c++是不允许的)

  一个函数不能即作为重载函数,有作为含有默认参数的函数。因为会出现二义性,编译器不能准确的调用相应的函数

.输入两个整数,将它们按由大到小的顺序输出,要求使用变量的引用

  代码如下:

 1 #include <iostream>
 2 using namespace std;
 3 void sortTwoIntegerByDesc(int &num1,int &num2);
 4 int main()
 5 {
 6     int number1,number2;
 7     cout<<"please input two integer:(Numbers are separated by spaces)"<<endl;
 8     cin>>number1>>number2;
 9     sortTwoIntegerByDesc(number1,number2);
10     return 0;
11 }
12 void sortTwoIntegerByDesc(int &num1,int &num2)
13 {
14     if(num1 > num2)
15     {
16         cout<<num1<<"..."<<num2<<endl;
17     }
18     else 
19     {
20         cout<<num2<<"..."<<num1<<endl;
21     }
22 }

三.编写一个程序,将两个字符串连接起来,结果取代第一个字符串,要求用string方法

  代码如下:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 int main()
 5 {
 6     string str1,str2;
 7     cout<<"please input str1:(write over by pressing enter)"<<endl;
 8     cin>>str1;
 9     cout<<"please input str2:(write over by pressing enter)"<<endl;
10     cin>>str2;
11     
12     str1 = str1 + str2;
13     cout<<str1;
14     
15     return 0;
16 }

 四.编写一个程序,用同一个函数名对n个数据进行从小到大排序,数据类型可以是整型,单精度型,双精度型,要求重载函数实现

  代码如下:

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 void mySort(int *arr,int n)//数组的首地址和排序元素的个数
 5 {
 6     sort(arr,arr+n); 
 7     for(int i=0;i<n;i++)
 8     {
 9         cout<<arr[i]<<" ";
10     }
11 } 
12 void mySort(float *arr,int n)
13 {
14     sort(arr,arr+n); 
15     for(int i=0;i<n;i++)
16     {
17         cout<<arr[i]<<" ";
18     }
19 }
20 void mySort(double *arr,int n)
21 {
22     sort(arr,arr+n); 
23     for(int i=0;i<n;i++)
24     {
25         cout<<arr[i]<<" ";
26     }
27 }
28 int main()
29 {
30 //    int array[5],n;
31 //    n=sizeof(array)/sizeof(int);
32 
33     float array[5],n;
34     n=sizeof(array)/sizeof(float);
35     
36 //    double array[5],n;
37 //    n=sizeof(array)/sizeof(double);
38     
39     cout<<"输入待排序的数:"<<endl;
40     for(int i=0;i<n;i++)
41     {
42         cin>>array[i];
43     }
44     mySort(array,n);
45     return 0;
46 }

  PS:偷了下懒,关于排序直接用了c++的内置函数sort,实现细节参考下一小题

 五.使用函数模板实现第4个例题

  代码如下:

 1 #include <iostream>
 2 using namespace std;
 3 template<typename T>
 4 T paixu(T *arr,int n)//数组的首地址和排序元素的个数
 5 {
 6     for(int i=0;i<n;i++)
 7     {
 8         for(int j=0;j<n;j++)
 9         {
10             if(arr[i]<arr[j])
11             {
12                 T temp = arr[i];
13                 arr[i] = arr[j];
14                 arr[j] = temp;
15             }
16         }
17     } 
18     for(int i=0;i<n;i++)
19     {
20         cout<<arr[i]<<" ";
21     }
22 } 
23 int main()
24 {
25     int array[5],n;
26     n=sizeof(array)/sizeof(int);
27     cout<<"输入待排序的数:"<<endl;
28     for(int i=0;i<n;i++)
29     {
30         cin>>array[i];
31     }
32     paixu(array,n);
33     return 0;
34 }
posted @ 2015-10-13 16:22  阿赖耶云  阅读(1368)  评论(0编辑  收藏  举报