问题描述
- C++中用指针比较三个数的大小
-
#include
using namespace std;
void sort(int *x, int *y, int *z);
int main()
{
int a, b, c, a1, b1, c1;
cout << "Please enter 3 integers:";
cin >> a >> b >> c;
a1 = a, b1 = b, c1 = c;
sort(&a1, &b1,&c1);
cout << a << " " << b << " " << c << " " << "in sorted order is ";
cout << a1 << " " << b1 << " " << c1 << endl;
return 0;
}
void sort(int *x, int *y, int *z)
{
void change(int *i, int *j);
if (x>y)change(x, y);
if (x>z)change(x, z);
if (y>z)change(y, z);
}
void change(int *i, int *j)
{
int *f;
f = i;
i = j;
j = f;
}
解决方案
#include<iostream>
using namespace std;
void sort(int *x, int *y, int *z);
int main()
{
int a, b, c, a1, b1, c1;
cout << "Please enter 3 integers:";
cin >> a >> b >> c;
a1 = a, b1 = b, c1 = c;
sort(&a1, &b1,&c1);
cout << a << " " << b << " " << c << " " << "in sorted order is ";
cout << a1 << " " << b1 << " " << c1 << endl;
return 0;
}
void sort(int *x, int *y, int *z)
{
void change(int *i, int *j);
if (*x>*y)change(x, y);
if (*y>*z)change(y, z);
if (*x>*y)change(x, y);
}
void change(int *i, int *j)
{
int f;
f = *i;
*i = *j;
*j = f;
}
解决方案二:
看看你的代码,函数都没调用,全在主函数中直接输出了,你仅仅做了一次输出,没做任何操作。
解决方案三:
没有调用chang函数,肯定没法比较啊
解决方案四:
你比较的不是地址么?
解决方案五:
指针问题,你只是比较了地址,而非地址中的数
时间: 2024-09-17 04:45:25