C++程序设计 (第三版) 谭浩强 习题3.9
习题 3.9 有3个整数a,b,c,由键盘输入,输出其中最大的数。
IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。
代码块
方法1:
#include <iostream>
using namespace std;
int main(){
int a, b, c, max;
cout<<"Please enter a, b, c: ";
cin>>a>>b>>c;
max = a > b ? a : b;
max = max > c ? max : c;
cout<<"Max= "<<max<<endl;
system("pause");
return 0;
}
方法2:使用函数的模块化设计
#include <iostream>
using namespace std;
int max(int a, int b, int c){
int m;
m = a > b ? a : b;
return c > m ? c : m;
}
int main(){
int a, b, c;
cout<<"Enter a b c: ";
cin>>a>>b>>c;
cout<<"Max = "<<max(a, b, c)<<endl;
system("pause");
return 0;
}