编写c++程序:用引用变量作为形参,实现3个整数由小到大输出
时间: 2024-05-16 12:11:52 浏览: 16
好的,我可以回答这个问题。下面是对应的 C 代码示例:
```c
#include <stdio.h>
void sort_integers(int* a, int* b, int* c) {
if (*a > *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
if (*a > *c) {
int tmp = *a;
*a = *c;
*c = tmp;
}
if (*b > *c) {
int tmp = *b;
*b = *c;
*c = tmp;
}
}
int main() {
int a = 3, b = 1, c = 2;
sort_integers(&a, &b, &c);
printf("%d %d %d\n", a, b, c);
return 0;
}
```
这里使用了引用类型的指针作为函数的形参,通过在函数内部对指针所指向的值进行操作,实现了对原始变量的修改。函数 `sort_integers` 的作用是将输入的三个整数按从小到大的顺序排序,实现方式是每次比较两个数进行交换。在 `main` 函数中,我们先定义了三个整数变量 `a`、`b`、`c`,然后将它们的地址作为参数传递给函数 `sort_integers`,最后输出排好序的结果。
相关问题
采用指针变量作为形参,编写一个实现两个整数交换的函数。在主函数中输入三个整数,调用数据交换函数将他们按从大到小的顺序排序后输出。
好的,以下是实现代码:
```c++
#include <iostream>
using namespace std;
// 定义交换函数
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
// 定义排序函数
void sort(int *a, int *b, int *c) {
if (*a < *b) swap(a, b);
if (*a < *c) swap(a, c);
if (*b < *c) swap(b, c);
}
int main() {
int a, b, c;
cout << "请输入三个整数:";
cin >> a >> b >> c;
sort(&a, &b, &c);
cout << "排序后的结果为:" << a << " " << b << " " << c << endl;
return 0;
}
```
解释一下代码的实现:
首先定义了一个 `swap` 函数,使用指针变量作为形参,实现了两个整数的交换。然后定义了一个 `sort` 函数,使用指针变量作为形参,实现了三个整数的排序。在 `main` 函数中,先输入三个整数,然后调用 `sort` 函数将其排序,最后输出排序后的结果。
注意:在调用函数时需要传入变量的地址,即使用 `&` 取地址符号。同时,在函数内部使用指针变量时需要使用 `*` 解引用符号,获取指针所指向的变量的值。
函数形参用指针变量输入3个整数,按由小到大的顺序输出。然后将程序改为:输入三个字符串,按由小到大顺序输出。
以下是输入3个整数,按由小到大的顺序输出的代码:
```c++
#include <iostream>
using namespace std;
void sort(int* a, int* b, int* c) {
if (*a > *b) swap(*a, *b);
if (*a > *c) swap(*a, *c);
if (*b > *c) swap(*b, *c);
}
int main() {
int a, b, c;
cin >> a >> b >> c;
sort(&a, &b, &c);
cout << a << " " << b << " " << c << endl;
return 0;
}
```
以下是输入三个字符串,按由小到大顺序输出的代码:
```c++
#include <iostream>
#include <string>
using namespace std;
void sort(string* a, string* b, string* c) {
if (*a > *b) swap(*a, *b);
if (*a > *c) swap(*a, *c);
if (*b > *c) swap(*b, *c);
}
int main() {
string a, b, c;
cin >> a >> b >> c;
sort(&a, &b, &c);
cout << a << " " << b << " " << c << endl;
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.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)