我不明白程序是如何通過(guò)運(yùn)行的。。。各種錯(cuò)誤。。。
#include?<iostream> //#include?<stdlib.h>??????????????????這就沒(méi)有用233??? using?namespace?std; ?????????????????????????????????????/*???基礎(chǔ)求最大值 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; ///** //??*函數(shù)功能:返回a和b的最大值 //??*a和b是兩個(gè)整數(shù) //??*/ //int?getMax(int?a,?int?b) //{ //????return?if(a?>?b)a?=b; //} // ///** //??*?函數(shù)功能:返回?cái)?shù)組中的最大值 //??*?arr:整型數(shù)組 //??*?count:數(shù)組長(zhǎng)度 //??*?該函數(shù)是對(duì)上面函數(shù)的重載 //??*/ //int?getMax(numArr[i],count) //{ //????//定義一個(gè)變量并獲取數(shù)組的第一個(gè)元素 //????maxNum=numArr[0]; // for(int?i?=?1;?i?<?count;?i++) // { //????????//比較變量與下一個(gè)元素的大小 // if(maxNum<numArr[i]) // { //????????????//如果數(shù)組中的元素比maxNum大,則獲取數(shù)組中的值 // maxNum=numArr[i]; // } // } // return?maxNum; //} // //int?main(void) //{ //????//定義int數(shù)組并初始化 // int?numArr[3]?=?{3,?8,?6}; //???? //????//自動(dòng)調(diào)用int?getMax(int?a,?int?b) // cout?<<?getMax()?<<?endl; //???? //????//自動(dòng)調(diào)用返回?cái)?shù)組中最大值的函數(shù)返回?cái)?shù)組中的最大值 // cout?<<?getMax(numArr[i],count)?<<?endl; // return?0; //} ??????????????????????????????????????????????//end; ???????????????????????????????????????????//第一次編譯單元鞏固 //#include?<iostream> //using?namespace?std; ///** //??*函數(shù)功能:返回a和b的最大值 //??*a和b是兩個(gè)整數(shù) //??*/ //int?getMax(int?a,?int?b) //{ // //int?x=a; // //int?y=b; // //if(x?>?y)?x?=y; // if(a>b)a=b; //????return?a; //} // ///** //??*?函數(shù)功能:返回?cái)?shù)組中的最大值 //??*?arr:整型數(shù)組 //??*?count:數(shù)組長(zhǎng)度 //??*?該函數(shù)是對(duì)上面函數(shù)的重載 //??*/ //int?getMax(int?numArr[],int?count) //{ //????//定義一個(gè)變量并獲取數(shù)組的第一個(gè)元素 //????int?maxNum=numArr[0]; // for(int?i?=?1;?i?<?count;?i++) // { //????????//比較變量與下一個(gè)元素的大小 // if(maxNum<numArr[i]) // { //????????????//如果數(shù)組中的元素比maxNum大,則獲取數(shù)組中的值 // maxNum=numArr[i]; // } // } // return?maxNum; //} // //int?main(void) //{ //????//定義int數(shù)組并初始化 // int?numArr[3]?=?{3,?8,?6}; //???? //????//自動(dòng)調(diào)用int?getMax(int?a,?int?b) // //cout?<<?getMax()?<<?endl; //???? //????//自動(dòng)調(diào)用返回?cái)?shù)組中最大值的函數(shù)返回?cái)?shù)組中的最大值 // cout?<<?getMax(numArr[3],3)?<<?endl; // system("pause"); // return?0; //} #include?<iostream> using?namespace?std; /** ??*函數(shù)功能:返回a和b的最大值 ??*a和b是兩個(gè)整數(shù) ??*/ int?getMax(int?a,?int?b) { if(a?>?b)a=b; ????return?a; } /** ??*?函數(shù)功能:返回?cái)?shù)組中的最大值 ??*?arr:整型數(shù)組 ??*?count:數(shù)組長(zhǎng)度 ??*?該函數(shù)是對(duì)上面函數(shù)的重載 ??*/ int?getMax(int?*arr,int?count) { ????//定義一個(gè)變量并獲取數(shù)組的第一個(gè)元素 int?maxNum=arr[0]; for(int?i?=?1;?i?<?count;?i++) { ????????//比較變量與下一個(gè)元素的大小 if(maxNum<arr[i]) { ????????????//如果數(shù)組中的元素比maxNum大,則獲取數(shù)組中的值 maxNum=arr[i]; } } return?maxNum; } int?main(void) { ????//定義int數(shù)組并初始化 int?numArr[3]?=?{3,?8,?6}; ???? ????//自動(dòng)調(diào)用int?getMax(int?a,?int?b) cout?<<?getMax(6,6)?<<?endl; ???? ????//自動(dòng)調(diào)用返回?cái)?shù)組中最大值的函數(shù)返回?cái)?shù)組中的最大值 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)
{
}
是如何對(duì)int getMax(int a, int b)
進(jìn)行重載的。
求解答
(這其實(shí)是三個(gè)問(wèn)題,因?yàn)槊刻焯釂?wèn)有限額,所以。。。)
2018-04-02
#include <iostream>
using namespace std;
/**
? *函數(shù)功能:返回a和b的最大值
? *a和b是兩個(gè)整數(shù)
? */
int getMax(int a, int b)
{
? ? return a > b ? a : b; /*這是一個(gè)三目表達(dá)式,你可以百度一下 (表達(dá)式1?表達(dá)式2:表達(dá)式3)若1成立則2有效,反之3有效*/
}
/**
? * 函數(shù)功能:返回?cái)?shù)組中的最大值
? * arr:整型數(shù)組
? * count:數(shù)組長(zhǎng)度
? * 該函數(shù)是對(duì)上面函數(shù)的重載
? */
int getMax(int arr[],int count)
{
? ? //定義一個(gè)變量并獲取數(shù)組的第一個(gè)元素
? ? int max = arr[0];
for(int i = 1; i < count; i++)
{
? ? ? ? //比較變量與下一個(gè)元素的大小
if(arr[i]>max)
{
? ? ? ? ? ? //如果數(shù)組中的元素比maxNum大,則獲取數(shù)組中的值
max = arr[i];
}
}
return max;
}
int main(void)
{
? ? //定義int數(shù)組并初始化
int numArr[3] = {3, 8, 6};
? ??
? ? //自動(dòng)調(diào)用int getMax(int a, int b)
cout << getMax(numArr[0], numArr[2]) << endl;
? ??
? ? //自動(dòng)調(diào)用返回?cái)?shù)組中最大值的函數(shù)返回?cái)?shù)組中的最大值
cout << getMax(numArr,3) << endl;
return 0;
}
這是我的代碼,運(yùn)行通過(guò),你看一下。第一個(gè)問(wèn)題我在注釋里面寫(xiě)了。重載問(wèn)題是?因?yàn)?nt getMax( int *arr,int count)里面的形參與int getMax(int a, int b)中的形參參數(shù)類型不一樣所以實(shí)現(xiàn)了重載啊,建議重新看一下課程。
2018-04-02
好的,謝謝。
回答的很詳細(xì),我明白了!