C语言如何从键盘输入任意3个数,按从小到大的顺序输出?

如题所述

代码1.

// 输入3个数,要求按从小到大顺序输出  
  
#include <stdio.h>  
  
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<stdio.h>
#include<string.h>
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<stdio.h> 
#include<string.h> 
#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<SIZE;i++) 
    { 
        fgets(*(pst+i),LEN,stdin);
     }
    printf("Befor sort:\n"); 
    
    for(i=0;i<SIZE;i++)
     { 
         fputs(*(pst+i),stdout);
     
     } 
     for(i=0;i<SIZE-1;i++)
          for(j=i+1;j<SIZE;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<SIZE;i++) 
       { 
           fputs(*(pst+i),stdout);
        }
       
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-12-07
代码1.
// 输入3个数,要求按从小到大顺序输出

#include <stdio.h>

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<stdio.h>
#include<string.h>
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<stdio.h>
#include<string.h>
#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<SIZE;i++)
{
fgets(*(pst+i),LEN,stdin);
}
printf("Befor sort:\n");

for(i=0;i<SIZE;i++)
{
fputs(*(pst+i),stdout);

}
for(i=0;i<SIZE-1;i++)
for(j=i+1;j<SIZE;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<SIZE;i++)
{
fputs(*(pst+i),stdout);本回答被网友采纳