c plus plus 第一章练习
来源:互联网 发布:kafka集群 端口 编辑:程序博客网 时间:2024/05/16 15:27
//P35第七题
#include<iostream>
using namespace std;
int max(int a,int b,int c=0)
{
if(b>a||a==b)
a=b;
if(c>a||a==c)
a=c;
return a;
}
int main()
{
int a,b,c,n;
cout<<"请输入进行比较的整数的个数"<<endl;
cin>>n;
if(n==3)
{
cout<<"请输入这3个数"<<endl;
cin>>a>>b>>c;
cout<<"这3个数中最大值为"<<max(a,b,c)<<endl;
}
if(n==2)
{
cout<<"请输入这2个数"<<endl;
cin>>a>>b;
cout<<"这个数中最大值为"<<max(a,b)<<endl;
}
if(!(n==2||n==3))
cout<<"ERROR"<<endl;
return 0;
}
运行的3种结果
//P35第八题
#include<iostream>
using namespace std;
void sort(int &a,int &b)
{ int temp;
if(b>a)
{
temp=a;
a=b;
b=temp;
}
else
return;
}
int main()
{
int a,b;
cout<<"请输入两个数进行由大到小排序"<<endl;
cin>>a>>b;
sort(a,b);
cout<<"排序完成"<<endl;
cout<<a<<','<<b<<endl;
}
结果:
//第9题
#include <iostream>
using namespace std;
int main()
{void sort(int x,int y,int z);
int x,y,z;
cin>>x>>y>>z;
sort(x,y,z);
return 0;
}
void sort(int x, int y, int z)
{
int temp;
if (x>y) {temp=x;x=y;y=temp;} //{ }内3个语句的作用是将x和y的值互换)
if (z<x) cout<<z<<','<<x<<','<<y<<endl;
else if (z<y) cout<<x<<','<<z<<','<<y<<endl;
else cout<<x<<','<<y<<','<<z<<endl;
}
//第10题
#include <iostream>
#include <string>
using namespace std;
int main()
{ string s1,s2;
cin>>s1>>s2;
cout<<"s1="<<s1<<endl;
cout<<"s2="<<s2<<endl;
s1=s1+s2;
cout<<"The new string is:"<<s1<<endl;
return 0;
}
//第11题
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string s;
cout<<"请输入一个字符串"<<endl;
cin>>s;
int l=s.length();
for(i=l-1;i>=0;i--)
cout<<s[i];
cout<<endl;
return 0;
}
//第12题
#include <iostream>
#include <string>
using namespace std;
void sort(string s[])
{int i,j;
string t;
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
if (s[i]>s[i+1])
{t=s[i];s[i]=s[i+1];s[i+1]=t;}
}
int main()
{ int i;
string str[5];
cout<<"请输入5个字符串"<<endl;
for(i=0;i<5;i++)
cin>>str[i];
void sort(string []);
sort(str);
cout<<"the sorted strings :"<<endl;
for(i=0;i<5;i++)
cout<<str[i]<<" ";
cout<<endl;
return 0;
}
//第13题
#include <iostream>
#include <string>
using namespace std;
int main()
{
long c[5]={10100,-123567,1198783,-165654, 3456};
int a[5]={1,9,0,23,-45};
float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };
void sort(int []);
void sort(float []);
void sort(long []);
sort(a);
sort(b);
sort(c);
return 0;
}
void sort(int a[])
{int i,j,t;
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
if (a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"thesorted numbers :"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
}
void sort(long a[])
{int i,j;
long t;
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
if (a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"the sorted numbers:"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
}
void sort(float a[])
{int i,j;
float t;
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
if (a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"the sorted numbers:"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
}
//第14题
#include <iostream>
#include <string>
using namespace std;
template <typename T>
void sort(T a[])
{int i,j,min;
T t;
for(i=0;i<5;i++)
{min=i;
for (j=i+1;j<5;j++)
if(a[min]>a[j]) min=j;
t=a[i]; a[i]=a[min]; a[min]=t;
}
cout<<"the sorted numbers:"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
}
int main()
{ int a[5]={1,8,0,22,-66};
float b[5]={3.1, 6.6, 5.5, 9.6, -2.3 };
long c[5]={10166,-123567, 119999,-165654, 9999};
sort(a);
sort(b);
sort(c);
return 0;
}
- c plus plus 第一章练习
- C Primer Plus 第一章
- C primer plus(第五版)编程练习第一章
- c plus plus 第3章练习
- c primer Plus第一章习题
- C Primer Plus第一章学习心得
- C Primer Plus笔记-第一章
- C primer plus---第一章:概览
- 【C++】primer plus 练习7.1
- 【C++】primer plus 练习7.2
- 【C++】primer plus 练习7.3
- 【C++】primer plus 练习7.5
- 【C++】primer plus 练习8.1
- C Primer Plus 练习p31
- C Primer Plus 练习 P59
- C Primer Plus 练习 P85
- c primer plus 练习一
- 《C Primer Plus》第一章 概览 课后习题
- OC与Swift混合开发技巧
- Android Date类型转String 与 String转Date类型
- win7下没有ntsd提供的程序支持
- Java多线程、并发编程知识点小结
- Android开发_电话拨号
- c plus plus 第一章练习
- MySql数据库学习
- 《Apache MINA 2.0 用户指南》第一章:入门
- JAVA学习笔记(四十)- 守护线程与中断线程
- merge sort vs. quicksort
- 离散数学实践:常用逻辑联结词计算
- 【SICP练习】135 练习3.66
- 未能找到类型或命名空间名称“DbContext”
- LNK2005错误 :error LNK2005: 已经在 libcmtd.lib(dbgheap.obj) 中定义