求一个c++程序,用重载函数实现对若干个数据从小到大的排序

求一个c++程序,用重载函数实现对若干个数据从小到大的排序,第1张

void sort(int a[100])

{

int iA =0;

for(int i =0 ;i<100;i++)

{

for(int j =0;j<100 - i;j++)

if(a[i]>a[j])

{

iA =a[j];

a[j] = a[i];

a[i] = iA;

}

}

}

void sort(char a[26])

{

char cA;

for(int i =0 ;i<100;i++)

{

for(int j =0;j<100 - i;j++)

if(a[i]>a[j])

{

cA =a[j];

a[j] = a[i];

a[i] = cA;

}

}

}

void float(float[100])

{

float fA =0;

for(int i =0 ;i<100;i++)

{

for(int j =0;j<100 - i;j++)

if(a[i]>a[j])

{

iA =a[j];

a[j] = a[i];

a[i] = iA;

}

}

}

首先,在我的编译器(visual studio 2008)上编译,连接,都没有问题

正是因为没有问题,我才郁闷(应该有问题的,要不就是visual studio 2008太正能了----能辨别最优匹配调用)

有很多人建议:在函数参数列表内最好不用数组形式,即arr[]

而用 arr,原因很简单, arr才是本质---传递进来的是一个指针(地址)而非数组~我认为这个建议很好,起码在你的程序里显得非常有用:

template<class T>

void showarray(T arr[],int n);

template<class T>

void showarray(T arr[],int n);

这是你的两个模板函数,

其实他们的真面目是:

template<class T>

void showarray(T arr,int n);

template<class T>

void showarray(T arr,int n);

为了表明把变量和类型分开,我用了T arr, 而不是你所喜欢的T arr(C风格)。

再看看,为什么会出现穆棱两可(ambiguous call to overloaded function

)的情况:很明显了吧

对于T(double const pd,pd是指向double类型的一个常指针)

这种情况而言:

其实可以解释成是(T)-------T可以是T,T也是一种类型

也可以解释成(T)---------T也是一种类型

前者要调用的是

template<class T>

void showarray(T arr,int n);

后者要调用的是

template<class T>

void showarray(T arr,int n);

这就要看编译器怎么对待这个问题了:

60中则表示:“两种都可以,我不知道你要调用哪一个,所以我指出来错误,你改正一下”

2008则认为:“虽然两种都可以,但是我认为pd配

template<class T>

void showarray(T arr,int n);

things配

template<class T>

void showarray(T arr,int n);

非常合适,所以我替你选了!

不知道你理解没?我睡觉了~

#include <iostream>

using namespace std;

void sort(int &a,int &b)

{

int temp;

if (a>b)

{

temp = a;

a = b;

b = temp;

}

}

void sort(float &a,float &b,float &c)

{

int temp[3];

if (b>a)

{

temp[0] = a;

temp[1] = b;

if (c>b)

temp[2] = c;

else

{

if (c<a)

{

temp[0] = c;

temp[1] = a;

temp[2] = b;

}

else

{

temp[1] = c;

temp[2] = b;

}

}

}

else

{

temp[0] = b;

temp[1] = a;

if (a>c)

{

if (b>c)

{

temp[0] = c;

temp[1] = b;

temp[2] = a;

}

else

{

temp[1] = c;

temp[2] = a;

}

}

else

temp[2] = c;

}

a = temp[0];

b = temp[1];

c = temp[2];

}

int main()

{

int a,b;

float c,d,e;

cout<<"请输入两个整数:"<<endl;

cin>>a>>b;

sort(a,b);

cout<<"排序之后:"<<a<<"\t"<<b<<"\t"<<endl;

cout<<"请输入三个浮点数:"<<endl;

cin>>c>>d>>e;

sort(c,d,e);

cout<<"排序之后:"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<endl;

return 0;

}

#include

using namespace std;

int main()

{ int a[5]={1,9,0,23,-45};

float b[5]={24, 76, 55, 66, -23 };

long int  c[5]={10100,-123567, 1198783,-165654, 3456};

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<<"the sorted numbers :"<<endl;

for(i=0;i<5;i++)

cout<<a[i]<}

C++是C语言的继承,它既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可以进行以继承和多态为特点的面向对象的程序设计。C++擅长面向对象程序设计的同时,还可以进行基于过程的程序设计,因而C++就适应的问题规模而论,大小由之。

C++不仅拥有计算机高效运行的实用性特征,同时还致力于提高大规模程序的编程质量与程序设计语言的问题描述能力。

在C++中,类是支持数据封装的工具,对象则是数据封装的实现。C++通过建立用户定义类支持数据封装和数据隐藏。

在面向对象的程序设计中,将数据和对该数据进行合法 *** 作的函数封装在一起作为一个类的定义。对象被说明为具有一个给定类的变量。每个给定类的对象包含这个类所规定的若干私有成员、公有成员及保护成员。完好定义的类一旦建立,就可看成完全封装的实体,可以作为一个整体单元使用。类的实际内部工作隐藏起来,使用完好定义的类的用户不需要知道类是如何工作的,只要知道如何使用它即可。

参考资料:

c++ 百度百科

#include<iostream>

using namespace std;

void sort( double x,double y );

void sort( double x,double y,double z );

int main()

{ sort( 56, 79 );

sort( 05, 308, 59 );

}

void sort(double x,double y)

{ if ( x>y ) cout << x << '\t' << y << endl;

else cout << y << '\t' << x << endl;

}

void sort( double x,double y,double z )

{ double t;

if( y<z ) { t = y; y = z; z = t; }

if( x<z ) { t = x; x = z; z = t; }

if( x<y ) { t = x; x = y y = t; }

cout << x << '\t' << y << '\t' << z << '\t' << endl;

#include <iostream>

using namespace std;

class complex {

protected:

double real;

double image;

public:

complex(double r=0,double i=0):real(r),image(i){}

void set(double r,double i){real=r; image=i;}

double Getreal() {return real;}

double Getimage() {return image;}

friend istream &operator>>(istream &in, complex &z);

friend ostream &operator<<(ostream &out,complex &z);

};

istream &operator>>(istream &in, complex &z) {

cout<<"输入实部:"<<endl;

in>>zreal;

cout<<"输入虚部:"<<endl;

in>>zimage;

return in;

}

ostream &operator<<(ostream &out,complex &z) {

out<<zreal<<"+"<<zimage<<"i"<<endl;

return out;

}

int main()

{

complex t1;

complex t2;

cin>>t1;

cin>>t2;

cout<<t1;

cout<<t2;

return 0;

}

试一下,protect的问题不知道是不是VC的问题,VS下运行没有问题,你的operator定义友元函数时不要在 *** 作符前加空格

我这个程序也不能运行吗?应该不会吧。。。

以上就是关于求一个c++程序,用重载函数实现对若干个数据从小到大的排序全部的内容,包括:求一个c++程序,用重载函数实现对若干个数据从小到大的排序、c++程序中使用函数模板重载问题、c++程序怎么用函数重载,实现两个整数和三个浮点数的排序,并按照从小到大的顺序将排序结果输出等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9415670.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存