1.7 求2个或3个正整数中的最大数,用带有默认参数的函数实现
#include <iostream>
using namespace std;
int max(int a, int b){//求两个数的最大值
if(a > b)
return a;
else
return b;
}
int main()
{
int x, y ,z;
cout << "enter 3 integers:";
cin >> x >> y >> z;
cout << "The max integer is:" << max(x, max(y, z)) << endl;
return 0;
}
1.8 输入两个整数,将它们按由大到小的顺序输出。要求使用变量的引用
#include <iostream>
using namespace std;
void exchange(int &a, int &b)
{
int temp;
if(a < b)
{
temp = a;
a = b;
b = temp;
}
}
int main()
{
int x, y;
cout << "enter 2 integers:";
cin >> x >> y;
exchange(x, y);
cout << x << " > " << y << endl;
return 0;
}
1.9 对3个变量按由小到大顺序排序,要求使用变量的引用。
#include <iostream>
using namespace std;
void mySort(int &a, int &b, int &c){//要使a<b<c: 1.a<b 2.a<c 3.b<c
int temp;
if(a>b)//使a<b
{
temp=b;
b=a;
a=temp;
};
if(a>c)//使a<c
{
temp=c;
c=a;
a=temp;
};
if(b>c)//使b<c
{
temp=b;
b=c;
c=temp;
};
}
int main()
{
int x, y ,z;
cout << "enter 3 integers:";
cin >> x >> y >> z;
mySort(x, y, z);
cout << x << " < " << y << " < " << z << endl;
return 0;
}
1.10 编一个程序,将两个字符串连接起来, 结果取代第一个字符串。要求用string方法。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "stu", s2 = "dent";
cout<<"s1 = "<<s1<<endl;
cout<<"s2 = "<<s2<<endl;
s1 = s1 + s2;
cout<<"new s1 is "<< s1 <<endl;
return 0;
}
1.11 输入一个字符串,把其中的字符按逆序输出。如输入LIGHT,输出THGIL。要求用string方法。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
cout << "please input a string:" << endl;
cin >> str;
int len = str.length();//取str的长度
for(int i = len-1; i >=0; i--){
cout << str[i];
}
cout << " is reversed by " << str << "." << endl;
return 0;
}
1.12 有5个字符串,要求对它们由小到大顺序排列,用string方法。
#include<iostream>
#include<string>
using namespace std;
int main()
{
const int LEN = 5;
string strs[LEN]; //定义一个字符串数组。
cout << "Enter 5 strings:" << endl;
cin>>strs[0]>>strs[1]>>strs[2]>>strs[3]>>strs[4];
cout << "before sorted:" << endl;
for(int i=0;i<5;i++)
{
cout<<strs[i]<<"\t";
}
cout << endl;
string temp;
for(int i = 0; i < LEN - 1; i++) //通过冒泡排序进行排序
{
for(int j = 0; j < LEN - 1 - i; j++)
{
if(strs[j] > strs[j+1])//小数放在前
{
temp = strs[j];
strs[j] = strs[j+1];
strs[j+1] = temp;
}
}
}
cout << "after sorted:" << endl;
for(int i=0; i < LEN; i++)
{
cout << strs[i] << endl;
}
return 0;
}
1.13 用同一个函数名对N个数据进行从小到大排序,数据类型可以是整形、单精度型、双精度型. 用重载函数实现
#include<iostream>
using namespace std;
float mySort(float *a,int n)
{
float temp;
int i,j;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
for(i=0; i<n; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
int mySort(int *a,int n)
{
int t;
int i,j;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(a[j]>a[j+1])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
}
for(i=0; i<n; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
double mySort(double *a,int n)
{
double t;
int i,j;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(a[j]>a[j+1])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
}
for(i=0; i<n; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
int main()
{
int a[5]= {22,13,26,55,5};
float b[5]= {1.2f,3.4f,2.2f,5.4f,4.5f};
double c[5]= {1.2201,3.2121,0.3322,5.4433,2.1122};
mySort(a,5);
mySort(b,5);
mySort(c,5);
return 0;
}
1.14 用同一个函数名对n个数据进行从小到大排序 用函数模板
#include <iostream>
using namespace std;
template <typename T>//模板声明,其中T为类型参数
T mySort(T *a, int n){//定义一个通用函数,用T作虚拟的类型名
T t;
for(int i=0; i<n-1; i++)
{
for(int j=0; j<n-i-1; j++)
{
if(a[j]>a[j+1])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
}
for(int i=0; i < n; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
int main()
{
int a[5]= {22,13,26,55,5};
float b[5]= {1.2f,3.4f,2.2f,5.4f,4.5f};
double c[5]= {1.2201,3.2121,0.3322,5.4433,2.1122};
mySort(a,5);
mySort(b,5);
mySort(c,5);
return 0;
}