环境: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. 74LS164 for stm32 源码下载

      在单片机系统中, 如果并行口的IO资源不够,而串行口又没有其他的作用, 那么我们可以用74LS164来扩展并行IO口,节约单片机资源.       74LS164是一个串行输入并行输出的移位寄存器 ...

  2. lazyload 图片延迟加载

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Linux基本操作 9----- 认识与学习bash

    一 认识bash这个shell 1 管理整个计算机硬件的其实就是操作系统的内核,这个内核是需要被保护的,所以我们一般用户就只能通过shell来跟内核通信,以让内核达到我们所想打到的工作. 2 只要能够 ...

  4. Oracle中的User与Schema

    Oracle中有两个概念容易混淆──user和schema,本随笔记录并摘抄了一些促进理解这连个概念的理解方法,希望有助于分清这两个概念. user是控制权限的,而schema则是一个容器,非所有者如 ...

  5. ModSecurity--web应用防火墙

    Introducing ModSecurity IIS 2.7.2 Stable Release ★★★★★ ★★★★ ★★★ ★★ ★   swiatFebruary 11, 20130 0 0 0 ...

  6. OC/Swift第三方添加出错解决方法

    (未经同意,不得转载!) ------------------------华丽分割线-----------------------

  7. 【36】绝不重新定义继承而来的non-virtual方法

    1.绝不重新定义继承而来的non-virtual方法,为什么? 首先想想,non-virtual方法是干什么的?也就是说,它的使用场景.父类的non-virtual方法,其实就是告诉子类,继承实现,子 ...

  8. HDU 5002 Tree LCT 区间更新

    Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  9. 匿名内部类new Runnable()

    匿名内部类(Anonymous Inner Class),在创建实例的同时给出类的定义,所有这些在一个表达式中完成. Java code? 1 2 3 4 Runnable rn = new Runn ...

  10. kernel debuging

    http://blog.csdn.net/XscKernel/article/category/1276234