我不明白程序是如何通過運行的。。。各種錯誤。。。
#include?<iostream> //#include?<stdlib.h>??????????????????這就沒有用233??? using?namespace?std; ?????????????????????????????????????/*???基礎求最大值 int?main() { int?max(int?x,int?y); int?a,b,c; cin>>a>>b; c=max(a,b); cout<<"max="<<c<<endl; ????system("pause"); return?0; } int?max(int?x,int?y) { int?z; if(x>y)z=x; else?z=y; return(z); } ??????????????????????????????????????????*/ ?????????????????????????????????????//??C++起航篇??單元鞏固??第一次編譯 //#include?<iostream> //using?namespace?std; ///** //??*函數功能:返回a和b的最大值 //??*a和b是兩個整數 //??*/ //int?getMax(int?a,?int?b) //{ //????return?if(a?>?b)a?=b; //} // ///** //??*?函數功能:返回數組中的最大值 //??*?arr:整型數組 //??*?count:數組長度 //??*?該函數是對上面函數的重載 //??*/ //int?getMax(numArr[i],count) //{ //????//定義一個變量并獲取數組的第一個元素 //????maxNum=numArr[0]; // for(int?i?=?1;?i?<?count;?i++) // { //????????//比較變量與下一個元素的大小 // if(maxNum<numArr[i]) // { //????????????//如果數組中的元素比maxNum大,則獲取數組中的值 // maxNum=numArr[i]; // } // } // return?maxNum; //} // //int?main(void) //{ //????//定義int數組并初始化 // int?numArr[3]?=?{3,?8,?6}; //???? //????//自動調用int?getMax(int?a,?int?b) // cout?<<?getMax()?<<?endl; //???? //????//自動調用返回數組中最大值的函數返回數組中的最大值 // cout?<<?getMax(numArr[i],count)?<<?endl; // return?0; //} ??????????????????????????????????????????????//end; ???????????????????????????????????????????//第一次編譯單元鞏固 //#include?<iostream> //using?namespace?std; ///** //??*函數功能:返回a和b的最大值 //??*a和b是兩個整數 //??*/ //int?getMax(int?a,?int?b) //{ // //int?x=a; // //int?y=b; // //if(x?>?y)?x?=y; // if(a>b)a=b; //????return?a; //} // ///** //??*?函數功能:返回數組中的最大值 //??*?arr:整型數組 //??*?count:數組長度 //??*?該函數是對上面函數的重載 //??*/ //int?getMax(int?numArr[],int?count) //{ //????//定義一個變量并獲取數組的第一個元素 //????int?maxNum=numArr[0]; // for(int?i?=?1;?i?<?count;?i++) // { //????????//比較變量與下一個元素的大小 // if(maxNum<numArr[i]) // { //????????????//如果數組中的元素比maxNum大,則獲取數組中的值 // maxNum=numArr[i]; // } // } // return?maxNum; //} // //int?main(void) //{ //????//定義int數組并初始化 // int?numArr[3]?=?{3,?8,?6}; //???? //????//自動調用int?getMax(int?a,?int?b) // //cout?<<?getMax()?<<?endl; //???? //????//自動調用返回數組中最大值的函數返回數組中的最大值 // cout?<<?getMax(numArr[3],3)?<<?endl; // system("pause"); // return?0; //} #include?<iostream> using?namespace?std; /** ??*函數功能:返回a和b的最大值 ??*a和b是兩個整數 ??*/ int?getMax(int?a,?int?b) { if(a?>?b)a=b; ????return?a; } /** ??*?函數功能:返回數組中的最大值 ??*?arr:整型數組 ??*?count:數組長度 ??*?該函數是對上面函數的重載 ??*/ int?getMax(int?*arr,int?count) { ????//定義一個變量并獲取數組的第一個元素 int?maxNum=arr[0]; for(int?i?=?1;?i?<?count;?i++) { ????????//比較變量與下一個元素的大小 if(maxNum<arr[i]) { ????????????//如果數組中的元素比maxNum大,則獲取數組中的值 maxNum=arr[i]; } } return?maxNum; } int?main(void) { ????//定義int數組并初始化 int?numArr[3]?=?{3,?8,?6}; ???? ????//自動調用int?getMax(int?a,?int?b) cout?<<?getMax(6,6)?<<?endl; ???? ????//自動調用返回數組中最大值的函數返回數組中的最大值 cout?<<?getMax(numArr,3)<<?endl; system("pause"); return?0; }
我不明白
int getMax(int a,int b)
{
return (a>b)? a:b;
}
這里改如何理解
并且
用
int getMax( int *arr,int count)
{
}
是如何對int getMax(int a, int b)
進行重載的。
求解答
(這其實是三個問題,因為每天提問有限額,所以。。。)
2018-04-02
#include <iostream>
using namespace std;
/**
? *函數功能:返回a和b的最大值
? *a和b是兩個整數
? */
int getMax(int a, int b)
{
? ? return a > b ? a : b; /*這是一個三目表達式,你可以百度一下 (表達式1?表達式2:表達式3)若1成立則2有效,反之3有效*/
}
/**
? * 函數功能:返回數組中的最大值
? * arr:整型數組
? * count:數組長度
? * 該函數是對上面函數的重載
? */
int getMax(int arr[],int count)
{
? ? //定義一個變量并獲取數組的第一個元素
? ? int max = arr[0];
for(int i = 1; i < count; i++)
{
? ? ? ? //比較變量與下一個元素的大小
if(arr[i]>max)
{
? ? ? ? ? ? //如果數組中的元素比maxNum大,則獲取數組中的值
max = arr[i];
}
}
return max;
}
int main(void)
{
? ? //定義int數組并初始化
int numArr[3] = {3, 8, 6};
? ??
? ? //自動調用int getMax(int a, int b)
cout << getMax(numArr[0], numArr[2]) << endl;
? ??
? ? //自動調用返回數組中最大值的函數返回數組中的最大值
cout << getMax(numArr,3) << endl;
return 0;
}
這是我的代碼,運行通過,你看一下。第一個問題我在注釋里面寫了。重載問題是?因為 nt getMax( int *arr,int count)里面的形參與int getMax(int a, int b)中的形參參數類型不一樣所以實現了重載啊,建議重新看一下課程。
2018-04-02
好的,謝謝。
回答的很詳細,我明白了!