用C语言实现输入3个整数,按由小到大的顺序输出。 函数原型:void swap(int *p1, int *p2) 输出格式: Please input three integers: 34,21,25↙ The ascending order is 21,25,34.输入3个整数,按由小到大的顺序输出。 函数原型:void swap(int *p1, int *p2) 输出格式: Please input three integers: 34,21,25↙ The ascending order is
时间: 2024-02-29 21:51:19 浏览: 120
好的,以下是用C语言实现输入3个整数,按由小到大的顺序输出的代码:
```c
#include <stdio.h>
void swap(int *p1, int *p2);
int main() {
int a, b, c;
printf("Please input three integers: ");
scanf("%d,%d,%d", &a, &b, &c);
if (a > b) {
swap(&a, &b);
}
if (a > c) {
swap(&a, &c);
}
if (b > c) {
swap(&b, &c);
}
printf("The ascending order is %d,%d,%d.", a, b, c);
return 0;
}
void swap(int *p1, int *p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
```
这个程序会先要求用户输入三个整数,然后通过使用 `swap` 函数进行排序,最后按照从小到大的顺序输出这三个整数。注意,这个程序假定用户输入的三个整数之间用逗号分隔。
相关问题
用C语言输入3个整数,按由小到大的顺序输出。 函数原型:void swap(int *p1, int *p2) 输出格式: Please input three integers: 34,21,25↙ The ascending order is 21,25,34.
好的,我来为您解答。以下是代码实现:
```c
#include <stdio.h>
void swap(int *p1, int *p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main() {
int a, b, c;
printf("Please input three integers: ");
scanf("%d,%d,%d", &a, &b, &c);
if (a > b) {
swap(&a, &b);
}
if (a > c) {
swap(&a, &c);
}
if (b > c) {
swap(&b, &c);
}
printf("The ascending order is %d,%d,%d.", a, b, c);
return 0;
}
```
当您输入 34,21,25 时,程序将输出 "The ascending order is 21,25,34.",符合题目要求。
利用c语言编程实现:输入3个整数,按由小到大的顺序输出。函 数定义如下: void swap(int * pl.int *p2)
以下是使用C语言实现输入3个整数,按由小到大的顺序输出的代码:
```c
#include <stdio.h>
void swap(int* p1, int* p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main() {
int a, b, c;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
swap(&a, &b);
}
if (a > c) {
swap(&a, &c);
}
if (b > c) {
swap(&b, &c);
}
printf("从小到大的顺序是:%d %d %d\n", a, b, c);
return 0;
}
```
在上面的代码中,`swap` 函数用于交换两个整数的值,接收两个指向 `int` 类型变量的指针作为参数。在 `main` 函数中,我们先输入三个整数,然后使用三个 `if` 语句来比较它们的大小,并调用 `swap` 函数进行交换,最后输出结果。
阅读全文
相关推荐
















