當前位置:編程學習大全網 - 編程語言 - 求壹個c++編程編寫重載函數min可分別求取2個整數、3個整數、2個雙精度數和3個雙精度數的最小值。不要max的

求壹個c++編程編寫重載函數min可分別求取2個整數、3個整數、2個雙精度數和3個雙精度數的最小值。不要max的

這個是我以前做實驗的時候寫的,是求較大值的,妳改壹下判斷條件就能求較小值了。

#include<iostream>

using namespace std;

int Max1(int m,int n);

int Max1(int m,int n,int o);

double Max1(double m,double n);

double Max1(double m,double n,double o);

void main()

{

int a[2],b,c,d; char ch;

double h,i,k;

A: cout<<"請輸入要比較的數的個數:(兩個或三個)"<<endl;

cin>>a[0];

if(a[0]!=2&&a[0]!=3) {

cout<<"對不起,妳輸入錯誤,請重新選擇。"<<endl;

goto A;

}

B: cout<<"請輸入要比較的數的類型:(1-int,2-double)"<<endl;

cin>>a[1];

switch(a[0]){

case 2: {

if(a[1]==1) {

cout<<"請輸入要比較的兩個int型數:" <<endl;

cin>>b>>c;

cout<<"較大的數為:"<<Max1(b,c)<<endl;

break;

}

else if(a[1]==2) {

cout<<"請輸入要比較的兩個double型數:" <<endl;

cin>>h>>i;

cout<<"較大的數為:"<<Max1(h,i)<<endl;

break;

}

else {

cout<<"對不起,妳輸入錯誤,請重新選擇。"<<endl;

goto B;

}

}

case 3: {

if(a[1]==1) {

cout<<"請輸入要比較的三個int型數:"<<endl;

cin>>b>>c>>d;

cout<<"較大的數為:"<<Max1(b,c,d)<<endl;

break;

}

else if(a[1]==2) {

cout<<"請輸入要比較的三個double型數:"<<endl;

cin>>h>>i>>k;

cout<<"較大的數為:"<<Max1(h,i,k)<<endl;

break;

}

else{

cout<<"對不起,妳輸入錯誤,請重新選擇。"<<endl;

goto B;

}

}

}

cout<<"重新運行?Y or N?"<<endl<<"輸入Y重新運行,輸入其它字符退出。"<<endl;

cin>>ch;

if(ch=='y'||ch=='Y') main();

else exit;

system("pause");

}

int Max1(int m,int n)

{

if(m>n) return m;

else return n;

}

int Max1(int m,int n,int o)

{

int x;

x=Max1(m,n);

x=Max1(x,o);

return (x);

}

double Max1(double m,double n)

{

if(m>n) return m;

else return n;

}

double Max1(double m,double n,double o)

{

double x;

x=Max1(m,n);

x=Max1(x,o);

return (x);

}

  • 上一篇:美術專業可以找什麽工作
  • 下一篇:工業兄弟縫紉機8700款,面線和底線不和,面線打幾針就斷,怎麽回事?
  • copyright 2024編程學習大全網