代码1.
// 输入3个数,要求按从小到大顺序输出
#include
int main()
{
int a,b,c,t;
printf("请输入三个数:");
scanf("%d%d%d",&a,&b,&c);
if(a > b)
{
t = a;
a = b;
b = t;
}
if(a > c)
{
t = a;
a = c;
c = t;
}
if(b > c)
{
t = b;
b = c;
c = t;
}
printf("从小到大的顺序是:%d %d %d\n",a,b,c);
return 0;
}
代码2.
输入3个字符串,按从小到大顺序输出。 //先用程序对三个数进行从小到大排序,然后修改程序
#include
#include
int main()
{void swap(char *pt1,char *pt2);
char a[20],b[20],c[20];
char *p1,*p2,*p3;
printf("请输入三个字符串:");
gets(a);
gets(b);
gets(c);
//或用scanf("%s,%s,%s",a,b,c);
p1=&a[0];p2=&b[0];p3=&c[0];//三个指针分别指向三个字符数组
if(strcmp(*p1,*p2)>0)swap(p1,p2);//if(strcmp(a,b)>0)swap(a,b); //比较两个字符串的大小,为什么用前一句的时候会出现警告呢
if(strcmp(a,c)>0)swap(a,c);//if(strcmp(*p1,*p3)>0)swap(*p1,*p3);
if(strcmp(b,c)>0)swap(b,c);// if(strcmp(*p2,*p3)>0)swap(*p2,*p3);
printf("由小到大排列:%s\n%s\n%s\n",a,b,c);
return 0;
}
void swap(char *pt1,char *pt2)
{ char t[20];
strcpy(t,pt1);
strcpy(pt1,pt2);
strcpy(pt2,t);
//t=*pt1;*pt1=*pt2;*pt2=t;
}
代码3.
#include
#include
#define SIZE 3
#define LEN 50
int main(void)
{
char str[SIZE][LEN];
char (*pst)[LEN]=str;
char temp[LEN];
int i,j;
printf("Please enter 3 string.\n");
for(i=0;i{
fgets(*(pst+i),LEN,stdin);
}
printf("Befor sort:\n");
for(i=0;i{
fputs(*(pst+i),stdout);
}
for(i=0;ifor(j=i+1;j {
if(strcmp(*(pst+i),*(pst+j)) == 1)
{
strcpy(temp,*(pst+i));
strcpy(*(pst+i),*(pst+j));
strcpy(*(pst+j),temp);
}
}
printf("After sort:\n");
for(i=0;i{
fputs(*(pst+i),stdout);
}
}
#include "stdio.h"
main()
{
int x,y,z,t;
scanf("%d%d%d",&x,&y,&z);
if (x>y)
{
t=x;x=y;y=t;
} /*交换x,y的值*/
if(x>z)
{
t=z;z=x;x=t;
}/*交换x,z的值*/
if(y>z)
{
t=y;y=z;z=t;
}/*交换z,y的值*/
printf("small to big: %d %d %d\n",x,y,z);
}
#include
//输入三个数,按从小到大顺序输出
int main(void)
{
int a, b, c, min, median, max;
scanf("%d%d%d", &a,&b,&c);
if (a>b)
{
if (a>c)
{
max = a;
if (b>c)
{
median = b;
min = c;
}
else
{
median = c;
min = b;
}
}
else
{
max = c;
median = a;
min = b;
}
}
else if (b>c)
{
max = b;
if(a>c)
{
median = a;
min = c;
}
else
{
median = c;
min = a;
}
}
printf("%d < %d < %d\n", min, median, max);
return 0;
}
main()
{
int s[3],i,j,a;
printf("请输入4个整数(各数之间用空格隔开):");
for(i=0;i<3;i++)
{
scanf("%d",&s[i]);
}
for(i=0;i<3;i++)
{
for(j=i;j<3;j++)
{
if(s[j] {
a=s[j];
s[j]=s[i];
s[i]=a;
}
}
}
printf("\n");
for(i=0;i<3;i++)
{
printf("%d ",s[i]);
}
}