【发布时间】:2014-02-23 21:45:06
【问题描述】:
有没有更有效的方法来完成我在这里所做的事情?也许不必使用这么多 if 语句。
//This function takes the references of the values inputed and sorts them
//in ascending order without returning anything.
void sortFunction(int *fptrOne, int *fptrTwo, int *fptrThree){
//Variables to hold max min and mid values
int max, min, mid;
//Series of if statements to determine max min and mid values.
if(*fptrOne < *fptrTwo && *fptrOne < *fptrThree)
min = *fptrOne;
else if(*fptrTwo < *fptrOne && *fptrTwo < *fptrThree)
min = *fptrTwo;
else if (*fptrThree < *fptrOne && *fptrThree < *fptrTwo)
min= *fptrThree;
if(*fptrOne > *fptrTwo && *fptrOne > *fptrThree)
max = *fptrOne;
else if(*fptrTwo > *fptrOne && *fptrTwo > *fptrThree)
max = *fptrTwo;
else if (*fptrThree > *fptrOne && *fptrThree > *fptrTwo)
max = *fptrThree;
if(*fptrOne != max && *fptrOne != min)
mid = *fptrOne;
else if(*fptrTwo != max && *fptrTwo != min)
mid = *fptrTwo;
else if(*fptrThree != max && *fptrThree != min)
mid = *fptrThree;
//Assign min mid and max to pointers in ascending order
*fptrOne = min;
*fptrTwo = mid;
*fptrThree = max;
}
【问题讨论】:
-
你看过例如en.wikipedia.org/wiki/…?
-
其实有很多更有效的方法。
标签: c function sorting pointers