sas.h

来自「小型图书馆管理系统」· C头文件 代码 · 共 276 行

H
276
字号
#ifndef SAS_H
#define SAS_H
#include"myfile.h"

//+---------------------------------------------------------------------------+
//|                         Numsort(书号排序)函数                           |
//+---------------------------------------------------------------------------+

int Partition1(int low,int high)
{ 
	BookData pivotkey=Book[low];
	
	while(low<high)
	{ 
		while(low<high && Book[high].getNum()>=pivotkey.getNum()) --high;
		
		Book[low]=Book[high];       //将比枢轴小的元素移到底端
		
		while(low<high && Book[low].getNum()<=pivotkey.getNum()) ++low;
		
		Book[high]=Book[low];       //将比枢轴大的元素移到顶端
	}
	Book[low]=pivotkey;        //枢轴记录还原到位
	return low;            //返回枢轴位置
}

void Numsort(int low ,int high)
{
	int povotloc;
	
	if(low<high)
	{
		povotloc=Partition1(low,high);
		Numsort(low,povotloc-1);           //对低子表分半排序
		Numsort(povotloc+1,high);           //对高子表分半排序
	}
}

//+---------------------------------------------------------------------------+
//|                         Titsort(书名排序)函数                           |
//+---------------------------------------------------------------------------+

int Partition2(int low,int high)
{ 
	BookData pivotkey=Book[low];
	
	while(low<high)
	{ 
		while(low<high && strcmp(Book[high].getTitle(),pivotkey.getTitle())>=0) --high;
		
		Book[low]=Book[high];
		
		while(low<high && strcmp(Book[low].getTitle(),pivotkey.getTitle())<=0) ++low;
		
		Book[high]=Book[low];
	}
	Book[low]=pivotkey;
	return low;
}

void Titsort(int low ,int high)
{
	int povotloc;
	
	if(low<high)
	{
		povotloc=Partition2(low,high);
		Titsort(low,povotloc-1);
		Titsort(povotloc+1,high);
	}
}

//+---------------------------------------------------------------------------+
//|                         Valsort(价值量排序)函数                         |
//+---------------------------------------------------------------------------+

int Partition3(int low,int high)
{ 
	BookData pivotkey=Book[low];
	
	while(low<high)
	{ 
		while(low<high && (Book[high].getCost()*Book[high].getQty())<=(pivotkey.getCost()*pivotkey.getQty())) --high;
		
		Book[low]=Book[high];
		
		while(low<high && (Book[low].getCost()*Book[low].getQty())>=(pivotkey.getCost()*pivotkey.getQty())) ++low;
		
		Book[high]=Book[low];
	}
	Book[low]=pivotkey;
	return low;
}

void Valsort(int low ,int high)
{
	int povotloc;
	
	if(low<high)
	{
		povotloc=Partition3(low,high);
		Valsort(low,povotloc-1);
		Valsort(povotloc+1,high);
	}
}

//+---------------------------------------------------------------------------+
//|                        Qtysort(库存量排序)函数                          |
//+---------------------------------------------------------------------------+

int Partition4(int low,int high)
{ 
	BookData pivotkey=Book[low];
	
	while(low<high)
	{ 
		while(low<high && Book[high].getQty()<=pivotkey.getQty()) --high;
		
		Book[low]=Book[high];
		
		while(low<high && Book[low].getQty()>=pivotkey.getQty()) ++low;
		
		Book[high]=Book[low];
	}
	Book[low]=pivotkey;
	return low;
}

void Qtysort(int low ,int high)
{
	int povotloc;
	
	if(low<high)
	{
		povotloc=Partition4(low,high);
		Qtysort(low,povotloc-1);
		Qtysort(povotloc+1,high);
	}
}

//+---------------------------------------------------------------------------+
//|                         Costsort(售价排序)函数                          |
//+---------------------------------------------------------------------------+

int Partition5(int low,int high)
{ 
	BookData pivotkey=Book[low];
	
	while(low<high)
	{ 
		while(low<high && Book[high].getCost()<=pivotkey.getCost()) --high;
		
		Book[low]=Book[high];
		
		while(low<high && Book[low].getCost()>=pivotkey.getCost()) ++low;
		
		Book[high]=Book[low];
	}
	Book[low]=pivotkey;
	return low;
}

void Costsort(int low ,int high)
{
	int povotloc;
	
	if(low<high)
	{
		povotloc=Partition5(low,high);
		Costsort(low,povotloc-1);
		Costsort(povotloc+1,high);
	}
}

//+---------------------------------------------------------------------------+
//|                         Busort(销售量排序)函数                          |
//+---------------------------------------------------------------------------+

int Partition6(int low,int high)
{ 
	BookData pivotkey=Book[low];
	
	while(low<high)
	{ 
		while(low<high && (Book[high].getTot()-Book[high].getQty())<=(pivotkey.getTot()-pivotkey.getQty())) --high;
		
		Book[low]=Book[high];
		
		while(low<high && (Book[high].getTot()-Book[high].getQty())>=(pivotkey.getTot()-pivotkey.getQty())) ++low;
		
		Book[high]=Book[low];
	}
	Book[low]=pivotkey;
	return low;
}

void Busort(int low ,int high)
{
	int povotloc;
	
	if(low<high)
	{
		povotloc=Partition6(low,high);
		Busort(low,povotloc-1);
		Busort(povotloc+1,high);
	}
}

//+---------------------------------------------------------------------------+
//|                       BaValsort(销售额排序)函数                         |
//+---------------------------------------------------------------------------+

int Partition7(int low,int high)
{ 
	BookData pivotkey=Book[low];
	
	while(low<high)
	{ 
		while(low<high && (Book[high].getCost()*(Book[high].getTot()-Book[high].getQty()))<=(pivotkey.getCost()*(pivotkey.getTot()-pivotkey.getQty()))) --high;
		
		Book[low]=Book[high];
		
		while(low<high && (Book[low].getCost()*(Book[low].getTot()-Book[low].getQty()))>=(pivotkey.getCost()*(pivotkey.getTot()-pivotkey.getQty()))) ++low;
		
		Book[high]=Book[low];
	}
	Book[low]=pivotkey;
	return low;
}

void BaValsort(int low ,int high)
{
	int povotloc;
	
	if(low<high)
	{
		povotloc=Partition7(low,high);
		BaValsort(low,povotloc-1);
		BaValsort(povotloc+1,high);
	}
}

//+---------------------------------------------------------------------------+
//|                         Search(书号查找)函数                            |
//+---------------------------------------------------------------------------+

int Search(int data,int start,int end)
{ 
	int mid=(start+end)/2;
	
	for(;;mid=(start+end)/2)
	{
		if(Book[mid].getNum()==data) return mid;  //查找成功,返回图书数组序号
		else if(Book[mid].getNum()>data) end=mid-1;
		else start=mid+1;
		if(start>end) return NULL;     //查找失败
	}
}

//+---------------------------------------------------------------------------+
//|                       Tisearch(书名查找)函数                            |
//+---------------------------------------------------------------------------+

int Tisearch(char *Title,int start,int end)
{ 
	int mid=(start+end)/2;
	
	for(;;mid=(start+end)/2)
	{
		if(strcmp(Book[mid].getTitle(),Title)==0) return mid;
		else if(strcmp(Book[mid].getTitle(),Title)>=0) end=mid-1;
		else start=mid+1;
		if(start>end) return NULL;
	}
}
#endif

⌨️ 快捷键说明

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