
一.实验内容及要求
11、求2个数或3个正整数中的最大数,用带默认参数的函数实现。
13、求3个变量按由小到大顺序排序,要求使用变量的引用。
15、编写程序,输入一个字符串,把其中的字符按逆序的输出。
17、编写一个程序,用同一个函数名对n个数据进行从小到大排序,数据类型可以是整型、单精度型、双精度型。用重载函数实现。
18、使用函数模板实现十七题。
二.实验步骤
11、程序代码:
#include using namespace std; int max(int a,int b,int c=0) //正整数 { int m; m=(a>b?a:b); m=(m>c?m:c); return m; } int main() { int a,b,c; cout<<”Input 2 integer:”; cin>>a>>b; cout<<”max=”< cin>>a>>b>>c; cout<<”max=”< } 运行结果: Input 2 integer:35 66 max=66 Input 3 integer:23 78 50 max=78 Press any key to continue 13、程序代码: #include using namespace std; void swap(int &x,int &y) { int temp; temp=x; x=y; y=temp; } void sort(int &i,int &j,int &k) { if (i>j) swap(i,j); if (i>k) swap(i,k); if (j>k) swap(j,k); } int main() { int a,b,c; cout<<"Please enter 3 integers:"< sort(a,b,c); cout<<"Sorted order is:"< } 运行结果: Please enter 3 integers: 35 45 20 Sorted order is: 20 35 45 Press any key to continue 15、程序代码: #include #include using namespace std; void Convert(string &str) { int i,n; char ch; n=str.length(); for(i=0;i ch=str[i]; str[i]=str[n-i-1]; str[n-i-1]=ch; } } void main() { string str; cout<<"Enter string: "; cin>>str; Convert(str); cout<<"string=: "< 运行结果: Enter string: abcdef string=: fedcba Press any key to continue 17、程序代码: #include using namespace std; void Input(int a[],int n) { for(int i=0;i } void Output(int a[],int n) { for(int i=0;i void Sort(int a[],int n) { int i,j; int t; for(i=0;i { t=a[i]; a[i]=a[j]; a[j]=t; } } void Input(double a[],int n) {
