题目:随机生成3个数,创建3个指针指向这3个数,不改变这3个数,从大到小显示,具体实现如下:

#include<stdlib.h>
#include<stdio.h>
#include<time.h>
void main()
{
int a = 0;
int b = 0;
int c = 0;
srand(time(NULL));
a = 3; //rand() % 10;
b = 2; //rand() % 10;
c = 1;// rand() % 10;
int * pa = &a;
int * pb = &b;
int * pc = &c;
//从小到大
int * pMin = pa;
if (*pb < *pMin)
{
pMin = pb;
}
if (*pc < *pMin)
{
pMin = pc;
}
printf("数据从小到大:\n");
if (pMin == pa)
{
printf("%d ", *pMin);
if(*pc < * pb)
{
printf("%d ", *pc);
printf("%d", *pb);
}
else
{
printf("%d ", *pb);
printf("%d", *pc);
}
}
else if (pMin == pb)
{
printf("%d ", *pMin);
if (*pc < *pa)
{
printf("%d ", *pc);
printf("%d", *pa);
}
else
{
printf("%d ", *pa);
printf("%d", *pc);
}
}
else if (pMin == pc)
{
printf("%d ", *pMin);
if (*pa < *pb)
{
printf("%d ", *pa);
printf("%d", *pb);
}
else
{
printf("%d ", *pb);
printf("%d", *pa);
}
}
else
{
printf("指针无效.\n");
return;
}
printf("\n");
//数据从大到小
int * pMax = pa;
if (*pb > *pMax)
{
pMax = pb;
}
if (*pc > *pMax)
{
pMax = pc;
}
printf("数据从大到小:\n");
if (pMax == pa)
{
printf("%d ", *pMax);
if (*pc < *pb)
{
printf("%d ", *pb);
printf("%d", *pc);
}
else
{
printf("%d ", *pc);
printf("%d", *pb);
}
}
else if (pMax == pb)
{
printf("%d ", *pMax);
if (*pc < *pa)
{
printf("%d ", *pa);
printf("%d", *pc);
}
else
{
printf("%d ", *pc);
printf("%d", *pa);
}
}
else if (pMax == pc)
{
printf("%d ", *pMax);
if (*pa < *pb)
{
printf("%d ", *pb);
printf("%d", *pa);
}
else
{
printf("%d ", *pa);
printf("%d", *pb);
}
}
else
{
printf("指针无效.\n");
return;
}
printf("\n");
system("pause");
return;
}

运行效果如图1所示:

随机生成3个数,创建3个指针指向这3个数,不改变这3个数,从大到小显示。_#include

图1 运行效果