C //习题 2.4(3) 有3个数a, b, c,要求按大小顺序把它们输出。

C语言设计 (第四版) 谭浩强 习题2.4(3)

习题 2.4(3) 有3个数a, b, c,要求按大小顺序把它们输出。

IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。

 

代码块
方法1:使用条件语句
#include <stdio.h>
#include <stdlib.h>

typedef int Type;   //类型模板,可调整为其他类型

int main(){
	Type a, b, c;
	scanf_s("%d %d %d", &a, &b, &c);

	//以下代码块为从大到小排列
	printf("Arrange from large to small: ");
	if(a > b){
		if(a > c){
			if(b > c){
				printf("%d %d %d\n", a, b, c);
			}
			else{
				printf("%d %d %d\n", a, c, b);
			}
		}
		else{
			printf("%d %d %d\n", c, a, b);
		}
	}
	else{
		if(b > c){
			if(a > c){
				printf("%d %d %d\n", b, a, c);
			}
			else{
				printf("%d %d %d\n", b, c, a);
			}
		}
		else{
			printf("%d %d %d\n", c, b, a);
		}
	}

	//以下代码块为从小到大排列
	printf("Arrange from small to large: ");
	if(a < b){
		if(a < c){
			if(b < c){
				printf("%d %d %d\n", a, b, c);
			}
			else{
				printf("%d %d %d\n", a, c, b);
			}
		}
		else{
			printf("%d %d %d\n", c, a, b);
		}
	}
	else{
		if(a > c){
			if(b > c){
				printf("%d %d %d\n", c, b, a);
			}
			else{
				printf("%d %d %d\n", b, c, a);
			}
		}
		else{
			printf("%d %d %d\n", b, a, c);
		}
	}
	system("pause");
	return 0;
}
方法2:使用宏定义、类型模板、指针、函数的模块化设计
#include <stdio.h>
#include <stdlib.h>

#define N 3   //定义输入数的数量
#define FORMAT "%d"    //定义输出格式符,可调整为其他类型

typedef int Type;    //类型模板,可调整为其他类型

//输入函数
void input(Type *arr, int n){
	for(int i = 0; i < n; i++){
		scanf_s(FORMAT, &arr[i]);
	}
}

//由大到小排列函数
void largeToSmall(Type *arr, int n){
	Type temp;
	for(int i = 0; i < n - 1; i++){
		for(int j = i + 1; j < n; j++){
			if(arr[i] < arr[j]){
				temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
		}
	}
}

//由小到大排列函数
void smallToLarge(Type *arr, int n){
	Type temp;
	for(int i = 0; i < n - 1; i++){
		for(int j = i + 1; j < n; j++){
			if(arr[i] > arr[j]){
				temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
		}
	}
}

//输出函数
void output(Type *arr, int n){
	for(int i = 0; i < n; i++){
		printf(FORMAT, arr[i]);
		printf(" ");
	}
	printf("\n");
}

int main(){
	Type *arr = (Type*)malloc(N * sizeof(Type));
	input(arr, N);
	printf("Arrange from large to small: ");
	largeToSmall(arr, N);
	output(arr, N);
	printf("Arrange from small to large: ");
	smallToLarge(arr, N);
	output(arr, N);
	free(arr);
	system("pause");
	return 0;
}
方法3:使用条件表达式、函数的模块化设计。注:极简写法,不推荐,代码阅读理解会很困难,代码的原则之一,要容易理解。
#include <stdio.h>
#include <stdlib.h>

typedef int Type;

//由大到小排列函数
void largeToSmall(Type a, Type b, Type c){
	printf("Arrange from large to small: ");
	a > b ? (a > c ? (b > c ? printf("%d %d %d\n", a, b, c) : printf("%d %d %d\n", a, c, b)) : printf("%d %d %d\n", c, a, b)) : (b > c ? (a > c ? printf("%d %d %d\n", b, a, c) : printf("%d %d %d\n", b, c, a)) : printf("%d %d %d\n", c, b, a));
}

//由小到大排列函数
void smallToLarge(Type a, Type b, Type c){
	printf("Arrange from small to large: ");
	a < b ? (a < c ? (b < c ? printf("%d %d %d\n", a, b, c) : printf("%d %d %d\n", a, c, b)) : printf("%d %d %d\n", c, a, b)): (b < c ? (a < c ? printf("%d %d %d\n", b, a, c) : printf("%d %d %d\n", b, c, a)) : printf("%d %d %d\n", c ,b, a));
}

int main(){
	Type a, b, c;
	scanf_s("%d %d %d", &a, &b, &c);
	largeToSmall(a, b, c);
	smallToLarge(a, b, c);
	system("pause");
	return 0;
}
  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值