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

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

  代码实现:

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

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

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

  代码如下:

 #include <iostream>
using namespace std;
void sortTwoIntegerByDesc(int &num1,int &num2);
int main()
{
int number1,number2;
cout<<"please input two integer:(Numbers are separated by spaces)"<<endl;
cin>>number1>>number2;
sortTwoIntegerByDesc(number1,number2);
return ;
}
void sortTwoIntegerByDesc(int &num1,int &num2)
{
if(num1 > num2)
{
cout<<num1<<"..."<<num2<<endl;
}
else
{
cout<<num2<<"..."<<num1<<endl;
}
}

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

  代码如下:

 #include <iostream>
#include <string>
using namespace std;
int main()
{
string str1,str2;
cout<<"please input str1:(write over by pressing enter)"<<endl;
cin>>str1;
cout<<"please input str2:(write over by pressing enter)"<<endl;
cin>>str2; str1 = str1 + str2;
cout<<str1; return ;
}

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

  代码如下:

 #include <iostream>
#include <algorithm>
using namespace std;
void mySort(int *arr,int n)//数组的首地址和排序元素的个数
{
sort(arr,arr+n);
for(int i=;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
void mySort(float *arr,int n)
{
sort(arr,arr+n);
for(int i=;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
void mySort(double *arr,int n)
{
sort(arr,arr+n);
for(int i=;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
int main()
{
// int array[5],n;
// n=sizeof(array)/sizeof(int); float array[],n;
n=sizeof(array)/sizeof(float); // double array[5],n;
// n=sizeof(array)/sizeof(double); cout<<"输入待排序的数:"<<endl;
for(int i=;i<n;i++)
{
cin>>array[i];
}
mySort(array,n);
return ;
}

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

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

  代码如下:

 #include <iostream>
using namespace std;
template<typename T>
T paixu(T *arr,int n)//数组的首地址和排序元素的个数
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(arr[i]<arr[j])
{
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i=;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
int main()
{
int array[],n;
n=sizeof(array)/sizeof(int);
cout<<"输入待排序的数:"<<endl;
for(int i=;i<n;i++)
{
cin>>array[i];
}
paixu(array,n);
return ;
}

C++例题练习(1)的更多相关文章

  1. BIT 树状数组 详解 及 例题

    (一)树状数组的概念 如果给定一个数组,要你求里面所有数的和,一般都会想到累加.但是当那个数组很大的时候,累加就显得太耗时了,时间复杂度为O(n),并且采用累加的方法还有一个局限,那就是,当修改掉数组 ...

  2. STL模板中的map的使用与例题

    最近的计分赛,记得自己的都只是过了两题.遇到了两次map,自己在寒假看了一点的map,只知道在字符串匹配的时候可以用的到.但是自己对map的使用还是不够熟练使用,这回在第一次和第二次的计分赛中都遇到可 ...

  3. C语言经典例题100

    C语言经典例题100 来源 http://www.fishc.com 适合初学者 ----------------------------------------------------------- ...

  4. 图的全局最小割的Stoer-Wagner算法及例题

    Stoer-Wagner算法基本思想:如果能求出图中某两个顶点之间的最小割,更新答案后合并这两个顶点继续求最小割,到最后就得到答案. 算法步骤: --------------------------- ...

  5. lca入门———树上倍增法(博文内含例题)

    倍增求LCA: father[i][j]表示节点i往上跳2^j次后的节点 可以转移为 father[i][j]=father[father[i][j-1]][j-1] 整体思路: 先比较两个点的深度, ...

  6. acm常见算法及例题

    转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题  初期:一.基本算法:     (1)枚举. (poj17 ...

  7. [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

  8. C语言中的经典例题用javascript怎么解?(一)

    C语言中的经典例题用javascript怎么解?(一) 一.1+2+3+……+100=?        <script type="text/javascript">  ...

  9. 数据库留言板例题:session和cookie区别

    session和cookie区别: <?php session_start(); //session_start();必须写在所有的php代码前边 ?> <!DOCTYPE html ...

  10. mysql连接查询经典小例题

    mysql连接查询: Mysql连接查询支持多表连接 对同一张表可以重复连接多次(别名在多次连接同一张表时很重要) 例题1: 下面有2张表 teams表 比赛结果表:result 问题: 得出一张表: ...

随机推荐

  1. linux中ctime,mtime,atime的区别

    st_atime Time when file data was last accessed. Changed by  the            following   functions:    ...

  2. 关于 android的 渲染器 Shader

    因为公司在 自定义的画图上面比较苛刻(各种要求= =),最后又是改来改去的.反正是 Shader起到很大作用,特此记录一下下.在achartengine的基础上没有能满足他们= = androd 提供 ...

  3. pip error: command 'gcc' failed with exit status 1

    SWIG/_m2crypto_wrap.c:127:20: 致命错误:Python.h:没有那个文件或目录     #include <Python.h>                  ...

  4. ALV可输入状态下输入金额字段变小数的问题

    http://blog.163.com/mxb_sap@yeah/blog/static/10335262520167109022155/ 小数位数两位    当我在给ALV上给该字段输入整数 '12 ...

  5. 使用 jsPlumb 绘制拓扑图 —— 异步载入与绘制的实现

    本文实现的方法能够边异步载入数据边绘制拓扑图. 有若干点须要说明一下: 1.  一次性获取全部数据并绘制拓扑图. 请參见文章: <使用 JsPlumb 绘制拓扑图的通用方法> ; 本文实现 ...

  6. 汉语转拼音pinyin4j

    分享一个将汉语转成拼音的工具包:pinyin4j-2.5.0.jar,下载地址:http://download.csdn.net/detail/abc_key/7629141 使用例如以下代码 imp ...

  7. xps13 关机充电 右边的usb口

    bios里设置了 usb powershare但关机的时候还是不能充电 度娘了一下,发现不解决问题,只能放狗了.果然谷歌里搜到答案,是windows的电源策略. I figured it out. A ...

  8. [React Native] Build a Separator UI component

    In this lesson we'll create a reusable React Native separator component which manages it's own style ...

  9. android的进度条使用

    android的进度条 1.实现的效果 2.布局代码 先写一个my_browser.xml文件 存放WebView <?xml version="1.0" encoding= ...

  10. yum -------包安装库

    elrepo:   http://elrepo.org/tiki/tiki-index.php CentOSPlus:    http://wiki.centos.org/zh/AdditionalR ...