maopao.cpp

来自「学生信息管理系统」· C++ 代码 · 共 47 行

CPP
47
字号
/**********************************************************
冒泡排序函数
原型:void paixu_maopao(double a[],const int n,const char cflag)
      a[]为数组名,n为你要对数组中的前多少个数进行排序,或你
	  的数组有多长,设定一下cflag为排序参数,有两种选择'<'和'>'
	  号,分别代码把数组中元素从小到大排序和从大到小排序
	                          written by 天涯浪子  08.5.16
***********************************************************/
#include "iostream"
/***************************************************
写模块时的测试函数
void main()
{
	void paixu_maopao(double a[],const int n,const char cflag);
	double a[LEN];
	int i,n;
	for(i=0;i<10;i++)
		cin >>a[i];
	n = 2;
	paixu_maopao(a,n,'<');
	for(i=0;i<10;i++)
		cout <<a[i] <<"   ";
}
******************************************************/
//冒泡排序函数模块
void paixu_maopao(double a[],const int n,const char cflag)
{
	double temp;
	int i,j;
	if((cflag !='<' && cflag !='>') || n<=0)//排序参数设置有误,输出提示
		printf("error: by <paixu_maopao(double a[],const int n,const char cflag)> function. The values of the parameters setting error\n");
	if(cflag =='<')  //要求从小到大排序
	{
	for(i=0;i<n-1;i++)
		for(j=i;j<n;j++)
			if(a[i]>a[j])
			{temp = a[i];a[i] = a[j];a[j] = temp;}
	}
	else if(cflag == '>')  //要求从大到小排序
	{
      for(i=0;i<n-1;i++)
		  for(j=i;j<n;j++)
			  if(a[i]<a[j])
			   {temp = a[i];a[i] = a[j];a[j] = temp;}
	}

}

⌨️ 快捷键说明

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