C程序40 字符串排序

题目:字符串排序。

代码

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define N 3/* 设定要输入的字符串个数,可更改 */

int cmp(char *p1,char *p2) /* 字符串比较函数 */
{
	int i=0;
	while (*(p1+i)==*(p2+i))
	if (*(p1+i++)=='\0') return 0; /* 这里是先判断*(p1+i)=='\0',然后再是i++ */
	return (*(p1+i)-*(p2+i));
}

void sort(char *a[N]) /*排序函数*/
{
	char *temp;
	int i,j;
	for(i=0;i<N-1;i++)/* 选择排序 */
	for(j=i+1;j<N;j++)
	if(cmp(a[i],a[j])<0)
	{
		temp=a[i];/* 交换的是字符串的地址,不是字符串的内容,可更改 */
		a[i]=a[j];
		a[j]=temp;
    }
}
int main(void )
{
		int i;
		char s[N][81],*p[N];/* 设定每个字符串不超过80字节 */
		printf("Please input %d strings one by one:\n",N);
		for(i=0;i<N;i++)
		{gets(s[i]);
		p[i]=s[i];
}
		sort(p);
		printf("The sequence after sort is:\n");
		for(i=0;i<N;i++)
		printf("%s\n",p[i]);
		getch();
		return 0;
} 

运行结果

在这里插入图片描述

#include "graphics.h" #include "stdio.h" #include "dos.h" #include "conio.h" #include "bios.h" #include "stdlib.h" #include "math.h" #ifdef __cplusplus #define __CPPARGS ... #else #define __CPPARGS #endif #define VK_LEFT 0x4b00 #define VK_RIGHT 0x4d00 #define VK_DOWN 0x5000 #define VK_UP 0x4800 #define VK_HOME 0x4700 #define VK_END 0x4f00 #define VK_SPACE 0x3920 #define VK_ESC 0x011b #define VK_ENTER 0x1c0d #define VK_PGUP 0x4900 #define VK_PGDOWN 0x5100 #define TIMER 0x1c void drawblock(int,int,int); void nedr(void); void fk(int,int,int,int); void rewr(int,int,int,int); void rrwf(int,int); int is_bd(int,int,int); int is_line(void); void interrupt ( *oldhandler)(__CPPARGS); void KillTimer(void); void SetTimer(void interrupt (*IntProc)(__CPPARGS)); void rscore(int); int fkt[]={0,4,8,12,14,16,18}; int cj[]={0,100,300,900,1500}; long grc[]={0,3000,60000,90000,120000,150000,180000,210000,240000,270000}; int cjb[]={0,0,0,0,0,0,0,0,0,0}; char cjc[]="score: 0"; char grad[]="grade:0"; struct shape { short xy[8]; short color; short next; }; struct shape shapes[19]= { /* {x1,y1,x2,y3,x3,y3,x4,y4,color,next} */ {1,1,2,1,3,1,3,2,LIGHTBLUE,1}, /* □□□□ */ {2,2,3,0,3,1,3,2,LIGHTBLUE,1}, /* □■□□ */ {1,1,1,2,2,2,3,2,LIGHTBLUE,1}, /* □■□□ */ {2,0,2,1,2,2,3,0,LIGHTBLUE,-3}, /* □■■□ */ {1,1,1,2,2,1,3,1,LIGHTCYAN,1}, /* □□□□ */ {2,0,3,0,3,1,3,2,LIGHTCYAN,1}, /* □■■□ */ {1,2,2,2,3,1,3,2,LIGHTCYAN,1}, /* □■□□ */ {2,0,2,1,2,2,3,2,LIGHTCYAN,-3}, /* □■□□ */ {2,1,3,0,3,1,3,2,LIGHTRED,1}, /* □□□□ */ {1,2,2,1,2,2,3,2,LIGHTRED,1}, /* □□□□ */ {1,0,1,1,1,2,2,1,LIGHTRED,1}, /* □■□□ */ {1,0,2,0,2,1,3,0,LIGHTRED,-3}, /* ■■■□ */ {2,1,2,2,3,0,3,1,DARKGRAY,1}, /* □□□□ */ {1,0,2,0,2,1,3,1,DARKGRAY,-1}, /* □□□□ */ /* □■■□ */ /* ■■□□ */ {2,0,2,1,3,1,3,2,MAGENTA,1}, /* □□□□ */ {1,2,2,1,2,2,3,1,MAGENTA,-1}, /* □□□□ */ /* ■■□□ */ /* □■■□ */ {0,1,1,1,2,1,3,1,BROWN,1}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值