1.題目要求如下:
C++用帶有默認參數的函數實現,求2個或3個正整數中的最大數
2.來吧,展示:
#include <iostream> using namespace std; void sort(int a,int b) { int temp; if (a>b){temp = a;a = b;b = temp;} cout <<a<< ',' <<b<<endl; } void sort(float x,float y,float z) { float temp; if(x>y){temp=x;x=y;y=temp;} if(z<x) cout <<z<< ',' <<x<< ',' <<y<< endl; else if(z<y) cout <<x<< ',' <<z<< ',' <<y<< endl; else cout <<x<< ',' <<y<< ',' <<z<< endl; } int main() { void sort(int a,int b); void sort(float x,float y,float z); float x,y,z; int a,b; cout <<"請輸入兩個整數:"<<endl; cin >>a>>b; cout << "排序從小到大為:" << endl; sort(a,b); cout <<"請輸入三個浮點數:"<<endl; cin >>x>>y>>z; cout << "排序從小到大為:" << endl; sort(x,y,z); return 0; }
3.運行結果如下:
希望能幫到大家,問你們要一個贊,你們會給嗎,謝謝大家
版權聲明:本文版權歸作者(@攻城獅小關)和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
大家寫文都不容易,請尊重勞動成果~
交流加Q:1909561302
CSDN地址https://blog.csdn.net/Mumaren6/