如何用指针法编程 输入十个数 求最大数跟最小数

来源:百度知道 编辑:UC知道 时间:2024/06/25 18:26:54
用指针法求 谢谢

#include "iostream"
#define LEN 10
using namespace std;
void Find_MAXMIN(double *a,double *b);
double *ptrMax=NULL;
double *ptrMin=NULL;
void main(void)
{
double data[LEN];
cout<<"输入十个数字"<<endl;
for(int a=0;a<LEN;a++)
cin>>data[a];
for(int a=0;a<LEN-1;a++)
Find_MAXMIN(&data[a],&data[a+1]);
cout<<"Max is "<<*ptrMax<<" Min is "<<*ptrMin<<endl;
}
void Find_MAXMIN(double *a,double *b)
{
if(ptrMax==NULL){
if(*a>*b)
{ptrMin=b;ptrMax=a;}
else
{ptrMin=a;ptrMax=b;}
}
else{
if ((*ptrMax<*a)||(*ptrMax<*b))
{
if(*a>*b)
{ptrMax=a;}
else
{ptrMax=b;}
}
if ((*ptrMin>*a)||(*ptrMin>*b))
{
if(*a>*b)
{ptrMin=b;}
else
{ptrMin=a;}
}
}
}