Topic 1041: [Introduction to Programming] Macro Definition: Finding the Maximum Number

Use functions and macros with parameters to find the largest number from three numbers.

Input format

3 real numbers

Output format

The largest number is output twice, using the function first and then the macro. Keep 3 decimal places.

Sample input

copy

1 2 3

Sample output

copy

3.000
3.000

The core solution, mine is to use the ternary operator --- int Max=a>b?a:b; (judgment sentence)? (Answer 1): (Answer 2); If true, it means 1. If not true, it means 2. Remember the punctuation mark 3? 1:2 will do.

%0.3f in printf(" %0.3f ",Max); is very common,

- `%`: Format control string starts with %.
- `6`: Indicates that the total width of the output is 6 characters. If the number of output digits is less than 6 characters, it is padded with spaces on the left.
- `.2`: Indicates that when outputting floating point numbers, retain two digits after the decimal point.
- `f`: indicates outputting a floating point number

//宏定义找最大数
#include<iostream>
#include<iomanip>  //输出3位小数
using namespace std;
//宏定义版
#define FindMax(a,b,c)\
{\
   float Max=a>b?a:b;\
   Max=c>Max?c:Max;\
   cout<<fixed<<setprecision(3)<<Max<<endl;\
}

//函数版
int Find(float a,float b,float c) {
	float Max = a > b ? a : b;
	Max = c > Max ? c : b;
	/*cout << fixed << setprecision(3) << Max<<endl;*/
	printf("%0.3f ",Max);
	return 0;
}

int main() {
	float a, b, c; 
	cin >> a >> b >> c;
	FindMax(a, b, c);
	Find(a, b, c);

	return 0;
}

Guess you like

Origin blog.csdn.net/qq_63999224/article/details/132366066