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

默认分类 未结 1 942
___岁月_如歌_
___岁月_如歌_ 2023-03-17 11:29
1条回答
  • 2023-03-17 11:46

    代码1.

    //输入3个数,要求按从小到大顺序输出#includeintmain(){inta,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 ",a,b,c);return0;}

    代码2.

    输入3个字符串,按从小到大顺序输出。//先用程序对三个数进行从小到大排序,然后修改程序#include#includeintmain(){voidswap(char*pt1,char*pt2);chara[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 %s %s ",a,b,c);return0;}voidswap(char*pt1,char*pt2){chart[20];strcpy(t,pt1);strcpy(pt1,pt2);strcpy(pt2,t);//t=*pt1;*pt1=*pt2;*pt2=t;}

    代码3.

    #include#include#defineSIZE3#defineLEN50intmain(void){charstr[SIZE][LEN];char(*pst)[LEN]=str;chartemp[LEN];inti,j;printf("Pleaseenter3string. ");for(i=0;i

    代码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#includeint 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

    0 讨论(0)
提交回复