xt1-17-1.cpp

来自「谭浩强c++课后习题源码解析」· C++ 代码 · 共 58 行

CPP
58
字号
#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<<"the sorted 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;
}



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?