编写程序,在main函数中定义3个整数a、b、c及整数指针,从键盘输入整数的值;自定义sort函数,在sort函数中通过指针改变a、b、c的值使其按升序排列;在main中依次输出3个整数。
时间: 2023-04-23 08:02:38 浏览: 84
好的,以下是程序的中文回复:
```
#include <stdio.h>
void sort(int *a, int *b, int *c) {
int temp;
if (*a > *b) {
temp = *a;
*a = *b;
*b = temp;
}
if (*a > *c) {
temp = *a;
*a = *c;
*c = temp;
}
if (*b > *c) {
temp = *b;
*b = *c;
*c = temp;
}
}
int main() {
int a, b, c;
int *pa = &a, *pb = &b, *pc = &c;
printf("请输入三个整数:");
scanf("%d%d%d", &a, &b, &c);
sort(pa, pb, pc);
printf("升序排列后的三个整数为:%d %d %d\n", a, b, c);
return ;
}
```
在这个程序中,我们首先定义了三个整数变量a、b、c和三个整数指针pa、pb、pc。然后从键盘输入三个整数的值,将它们分别赋值给a、b、c。接着调用自定义的sort函数,将a、b、c按升序排列。最后在main函数中输出排列后的三个整数。
相关问题
用c函数和指针编写程序,输入3个整数,从小到大排序后输出。
好的,这是一个编程问题,我可以回答。以下是使用 C 语言和指针编写的程序,可以将输入的三个整数从小到大排序后输出:
```c
#include <stdio.h>
void sort(int *a, int *b, int *c) {
if (*a > *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
if (*b > *c) {
int tmp = *b;
*b = *c;
*c = tmp;
}
if (*a > *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
}
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d%d%d", &a, &b, &c);
sort(&a, &b, &c);
printf("从小到大排序后的结果是:%d %d %d\n", a, b, c);
return 0;
}
```
运行程序后,输入三个整数,程序会将它们从小到大排序后输出。注意,sort 函数中使用的指针可以改变原始变量的值,因此在调用 sort 函数时要使用变量的地址(&a, &b, &c)。
C语言:1、编写程序:从键盘输入三个整数,通过调用函数按从大到小输出,要求用指针实现;
可以参考以下代码实现:
```c
#include <stdio.h>
void sort(int *a, int *b, int *c);
int main()
{
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
sort(&a, &b, &c);
printf("从大到小排序后的结果为:%d %d %d\n", a, b, c);
return 0;
}
void sort(int *a, int *b, int *c)
{
int temp;
if (*a < *b) {
temp = *a;
*a = *b;
*b = temp;
}
if (*a < *c) {
temp = *a;
*a = *c;
*c = temp;
}
if (*b < *c) {
temp = *b;
*b = *c;
*c = temp;
}
}
```
这里我们定义了一个 sort 函数,通过指针实现了按从大到小排序的功能。在主函数中,我们先从键盘输入三个整数,然后调用 sort 函数进行排序,最后输出排序后的结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)