?? 萬能.cpp
字號:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <conio.h>
#define MAXSIZE 100
typedef int RedType;
typedef struct SqList
{
RedType INT[MAXSIZE+1];
int length;
}SqList;
SqList L;
void TIME() //獲得系統時間
{
static char *week[]={"一","二","三","四","五","六","日"};
time_t t;
struct tm *tp;
t=time(NULL);
tp=localtime(&t);
printf("\t ─────────────────────\n");
printf("\t\t 現在是:%d年%d月%d日",tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday);
printf(" %d:%d:%d ",tp->tm_hour,tp->tm_min,tp->tm_sec,tp->tm_wday);
printf("星期%s\n",week[(tp->tm_wday)-1]);
}
void PRINT(SqList *L) //打印排序結果
{
int i;
printf("\t\t");
for(i=1;i<=L->length;i++)
printf("%d ",L->INT[i]);
printf("\n");
}
void STINSORT(SqList *L) //直接插入排序
{
int i,j;
for(i=2;i<=L->length;i++) //依次插入INT[2],…,INT[n]
{
L->INT[0]=L->INT[i]; //以INT[0]為哨兵
j=i-1;
while(L->INT[j]>L->INT[0])
{
L->INT[j+1]=L->INT[j];
j--;
}
L->INT[j+1]=L->INT[0];
}
}
void BIS_INT(SqList *L) //折半插入排序
{
int i,j,low,high,mid;
for(i=2;i<=L->length;++i)
if(L->INT[i]<L->INT[i-1])
{
L->INT[0] = L->INT[i];
high=i-1; //認為在r[1]和r[i-1]之間已經有序
low=1;
while(low<=high) //對有序表進行折半查找
{
mid=(low+high)/2;
if(L->INT[0]<L->INT[mid])
high=mid-1;
else
low=mid+1;
}
for(j=i-1;j>=high+1;--j)
L->INT[j+1]=L->INT[j];
L->INT[high+1]=L->INT[0];
}
}
void SHELL(SqList *L) //希爾排序
{
int i,j,d;
d=L->length/2; //d為增量
while(d>=1) //最后一次的增量一定為1
{
for(i=d+1;i<=L->length;i++)
{
L->INT[0]=L->INT[i];
j=i-d;
while(L->INT[j]>L->INT[0]&&j>0)
{
L->INT[j+d]=L->INT[j];
j=j-d;
}
L->INT[j+d]=L->INT[0];
}
d=d/2;
}
}
void QUICK(SqList *L,int low,int high) //快速排序
{
int i,j,temp;
i=low;
j=high;
temp=L->INT[low]; //以INT[0]為基準
while(i<j) //從兩端往中間進行掃描直到i=j為止
{
while(i<j&&temp<=L->INT[j]) //右端進行掃描
j--;
if(i<j)
{
L->INT[i]=L->INT[j];
i++;
}
while(i<j&&L->INT[i]<=temp) //左端進行掃描
i++;
if(i<j)
{
L->INT[j]=L->INT[i];
j--;
}
}
L->INT[i]=temp;
if(low<i)
QUICK(L,low,i-1); //對左區間進行排序
if(i<high)
QUICK(L,j+1,high); //對右區間進行排序
}
void SELECT(SqList *L) //選擇排序
{
int i,j,small;
for(i=1;i<=L->length-1;i++) //做第i趟排序(1≤i≤length-1)
{
small=i; //保存小址
for(j=i+1;j<=L->length;j++) //在當前無序區INT[i..length]中選最小的記錄INT[small]
{
if(L->INT[j]<L->INT[small])
small=j; //small記下目前找到的最小關鍵字所在的位置
}
if(small!=i) //交換值
{L->INT[0]=L->INT[i];L->INT[i]=L->INT[small];L->INT[small]=L->INT[0];}
}
}
void BUBBLE(SqList *L) //冒泡優化排序
{
int i,j,flag=1; //i為掃描次數,flag標記為是否交換
for(i=0;i<=L->length-1&&flag==1;i++)
{
flag=0;
for(j=0;j<L->length-i;j++)
{
if(L->INT[j]>L->INT[j+1])
{
flag=1;
L->INT[0]=L->INT[j];
L->INT[j]=L->INT[j+1];
L->INT[j+1]=L->INT[0];
}
}
}
}
void BUILDHEAP(SqList *L,int k,int m) //建立堆
{
int i,x;
x=L->INT[k];
for(i=2*k;i<=m;i=i*2)
{
if(i<m&&L->INT[i]<L->INT[i+1])
i++;
if(x>L->INT[i]) break; //x應插入在位置k上
L->INT[k]=L->INT[i];
k=i;
}
L->INT[k]=x; //插入
}
void HEAPSORT(SqList *L) //堆排序
{
int i,n,temp;
n=L->length;
for(i=n/2;i>0;i--)
BUILDHEAP(L,i,n);
for(i=n;i>1;i--)
{
temp=L->INT[1];
L->INT[1]=L->INT[i];
L->INT[i]=temp;
BUILDHEAP(L,1,i-1); //將INT[1..n-1] 重新調整為大頂堆
}
}
void RAND(SqList *L) //隨機生成數字
{
int i;
L->length=(rand()%(14))+2; //長度<=15,數值>=2的隨機數
for(i=0;i<L->length;i++)
{
//rand((unsigned)time(NULL)); //長度為65535以內的隨機數
L->INT[i]=(rand()%(100))+1; //為0-100的隨機數
}
}
void INIT(SqList *L) //初始化排序的數據
{
int i;
char c;
loop:;
TIME();
printf("\t ─────────────────────\n");
printf("\n\t\t\t請輸入待排序數據的數量【>=2】: ");
while (scanf("%d", &L->length)!=1) //檢測是否為正確輸入數
{
while ((c=getchar())!='\n');
printf("\n\t\t【×】Error:錯誤的輸入,請按任意鍵重新輸入→\n\t\t\t\t");
getch();
system("cls");
goto loop;
}
if(L->length<2||L->length>MAXSIZE)
{
printf("\n\t\t\t\t 【×】Error:\n\t\t待排序數據數目小于2或超出最大輸入量,請按任意鍵重新輸入→\n\t\t\t\t");
getch();
system("cls");
goto loop;
}
printf("\n");
for(i=1;i<=L->length;i++)
{
printf("\t\t\t 請輸入第%d個數據: ",i);
lable:;
while (scanf("%d",&L->INT[i])!=1) //檢測是否為正確輸入數
{
while ((c=getchar())!='\n');
printf("\n【×】Error:數據輸入出錯,請重新輸入→");
goto lable;
}
}
printf("\n\n\n\n\t\t\t數據初始化成功,按任意鍵繼續→\n");
getch();
system("cls");
}
void PRIN() //格式化輸出─
{
int i;
printf("\t\t");
for(i=0;i<L.length;i++)
printf("──");
printf("\n");
}
int MENUE()
{
int input_data;
char c;
TIME();
printf("\t ╭─────────────────────╮\n");
printf("\t | 各種排序的基本操作實現 |\n");
printf("\t |─────────────────────|\n");
printf("\t | 1、手動(Hand)輸入數據 |\n");
printf("\t |─────────────────────|\n");
printf("\t | 2、隨機(Rand)生成數據 |\n");
printf("\t |─────────────────────|\n");
printf("\t | 3、退 出 ... |\n");
printf("\t ╰─────────────────────╯\n");
printf("\t 請正確選擇:");
while(scanf("%d", &input_data)!=1) //檢測是否為正確輸入數
{
while ((c=getchar())!='\n');
return input_data;
}
fflush(stdin);
return input_data;
}
void SUB_MENUE()
{
char c;
for(;;){
TIME();
printf("\t ╭─────────────────────╮\n");
printf("\t | 各種排序的基本操作實現 |\n");
printf("\t |─────────────────────|\n");
printf("\t | 1、重新(Again)輸入數據 |\n");
printf("\t | 2、折半(Binary)排序 |\n");
printf("\t | 3、直接(Straight)排序 |\n");
printf("\t | 4、希爾(Shell)排序 |\n");
printf("\t | 5、快速(Quick)排序 |\n");
printf("\t | 6、選擇(Select)排序 |\n");
printf("\t | 7、冒泡(Bubble)排序 |\n");
printf("\t | 8、堆 (Heap)排序 |\n");
printf("\t | 9、隨機(Rand)生成 |\n");
printf("\t | Q、退 出 ... |\n");
printf("\t ╰─────────────────────╯\n");
printf("\t 【共%d個數據】如下:\n",L.length);
PRIN();
PRINT(&L);
PRIN();
printf("\t 請選擇:");
scanf("%s",&c);
switch(c)
{
case '1':
system("cls");
INIT(&L);
system("cls");
break;
case '2':
BIS_INT(&L);
printf("\n\t\t\t 排序成功!!!\n\t\t\t ");
system("pause");
system("cls");
break;
case '3':
STINSORT(&L);
printf("\n\t\t\t 排序成功!!!\n\t\t\t ");
system("pause");
system("cls");
break;
case '4':
SHELL(&L);
printf("\n\t\t\t 排序成功!\n\t\t\t ");
system("pause");
system("cls");
break;
case '5':
QUICK(&L,1,L.length);
printf("\n\t\t\t 排序成功!\n\t\t\t ");
system("pause");
system("cls");
break;
case '6':
SELECT(&L);
printf("\n\t\t\t 排序成功!\n\t\t\t ");
system("pause");
system("cls");
break;
case '7':
BUBBLE(&L);
printf("\n\t\t\t 排序成功!\n\t\t\t ");
system("pause");
system("cls");
break;
case '8':
HEAPSORT(&L);
printf("\n\t\t\t 排序成功!\n\t\t\t ");
system("pause");
system("cls");
break;
case '9':
RAND(&L);
printf("\n\t\t\t 隨機生成成功!\n\t\t\t ");
system("pause");
system("cls");
break;
case 'q':
case 'Q':
system("cls");
printf("\n\n\n\n\t\t\t 謝 謝 使 用 , 再 見!!!\n");
printf("\n\t\t\t 任 意 鍵 退 出!\n\t\t\t");
getch();
exit(0);
break;
default :
printf("\n\t\t\t\t 【×】Error:\n\t\t\t 輸入有誤,請重新選擇!!!\n");
getch();
system("cls");
break;
}
}
}
int main(void)
{
int input_data;
//kbhit();
//puts("輸入任意字符繼續:");
//while (!kbhit()) /* do nothing */ ;
//puts("\r\n你按下了一個鍵!\r\n");
do
{
input_data=MENUE();
switch(input_data)
{
case 1:
system("cls");
INIT(&L);
SUB_MENUE();
break;
case 2:
system("cls");
RAND(&L);
SUB_MENUE();
break;
case 3:
system("cls");
printf("\n\n\n\n\t\t\t 謝 謝 使 用 , 再 見!!!\n");
printf("\n\t\t\t 任 意 鍵 退 出!\n\t\t\t");
getch();
exit(0);
break;
default:
printf("\n\t\t\t\t 【×】Error:\n\t\t\t 輸入有誤,請重新選擇!!!\n");
getch();
system("cls");
break;
}
}while(input_data!=3);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -