C++的题目求教

来源:百度知道 编辑:UC知道 时间:2024/09/23 16:27:11
用直接和间接的访问方式,编程实现整型变量A,B,C三个数据的大小比较,并按从小到大的顺序依次存到A,B,C中

//直接访问,通过对变量值的访问,完成由小到大的排序任务
#include <iostream>
using namespace std;

void main()
{int a,b,c;
cout<<"Please input three numbers:"<<endl;
cin>>a>>b>>c;

if(a>b)
{int temp=a+b; //按照值访问
b=temp-b;
a=temp-a;
}
if(b>c)
{int temp=b+c;
b=temp-b;
c=temp-c;
}
//the following for test
cout<<a<<b<<c;
}
//间接访问,就是按照地址访问
#include <iostream>
using namespace std;
void main()
{int a,b,c;
cout<<"Please input three numbers:"<<endl;
cin>>a>>b>>c;
if(a>b)
{int *pint=b;//按照地址访问
a=*pint;
pint=&a;
b=*pint;
}
if(b>c)
{int* pint=b;
c=*pint;
pint=&c;
b=*pint;
}
//for test
cout<<a<<b<<c;
}

#include <iostream>

using namespace std;