代码如下:
#includeusing namespace std; //使用重载实现数组的排序 //重载:函数名相同,参数列表(特征标不同) //实现排序 void Sort(int[], int len); void Sort(float[], int len); void Sort(double[], int len); //实现输出 void Show(int[], int len); void Show(float[], int len); void Show(double[], int len); void Sort(int nums[], int len){ int temp; for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - i - 1; j++) { if (nums[j] < nums[j + 1]) { temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } } void Sort(float nums[], int len) { float temp; for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - i - 1; j++) { if (nums[j] < nums[j + 1]) { temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } } void Sort(double nums[], int len) { double temp; for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - i - 1; j++) { if (nums[j] < nums[j + 1]) { temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } } void Show(int nums[], int len) { for (int i = 0; i < len - 1; i++) { cout << nums[i] << ", "; } cout << endl; } void Show(float nums[], int len) { for (int i = 0; i < len - 1; i++) { cout << nums[i] << ", "; } cout << endl; } void Show(double nums[], int len) { for (int i = 0; i < len - 1; i++) { cout << nums[i] << ", "; } cout << endl; } int main(){ int iNums[] = { 56 ,47, 13, 89, 32 }; float fNums[] = { 10.8f, 28.6f, 35.5f, 50.2f, 99.9f }; double dNums[] = { 70.8, 65.9, 22.2, 84.3, 53.4 }; cout << "排序前:" ; Show(iNums, sizeof(iNums) / sizeof(iNums[0])); Sort(iNums, sizeof(iNums) / sizeof(iNums[0])); //Sort(iNums, sizeof(iNums) / sizeof(int)); cout << "排序后:" ; Show(iNums, sizeof(iNums) / sizeof(iNums[0])); cout << endl; cout << "排序前:"; Show(fNums, sizeof(fNums) / sizeof(fNums[0])); Sort(fNums, sizeof(fNums) / sizeof(fNums[0])); //Sort(fNums, sizeof(fNums) / sizeof(int)); cout << "排序后:"; Show(fNums, sizeof(fNums) / sizeof(fNums[0])); cout << endl; cout << "排序前:"; Show(dNums, sizeof(dNums) / sizeof(dNums[0])); Sort(iNums, sizeof(dNums) / sizeof(dNums[0])); //Sort(iNums, sizeof(dNums) / sizeof(int)); cout << "排序后:"; Show(dNums, sizeof(dNums) / sizeof(dNums[0])); }
调试结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)