C语言中如何判断多边形边数为整数及实现内切圆多边形面积计算
Let's break down how to address both of your requirements step by step, with practical code examples:
1. Validating num_pol is an Integer (Not a Decimal)
The key here is to avoid reading the side count directly as an integer—since decimal inputs (like 5.5) would get truncated to 5 and slip past your checks. Instead:
- Read the input as a
doublefirst - Verify it has no fractional part
- Ensure the integer value is at least 3 (since a polygon needs 3+ sides)
Update your validation condition to include this check:
double num_pol_double; int num_pol; // Read side count as a floating-point value printf("Enter number of sides: "); scanf("%lf", &num_pol_double); // Combined validation check (including decimal detection) if (side_a < 0 || radius < 0 || diameter < side_a || num_pol_double < 3 || num_pol_double != (int)num_pol_double) { printf("Invalid input! Number of sides must be an integer ≥3, and all values must be positive with diameter ≥ side length.\n"); return 1; // Exit with error code } // Convert to integer now that we've confirmed it's valid num_pol = (int)num_pol_double;
For strict floating-point precision (to handle tiny rounding errors), you can replace the equality check with:
fmod(num_pol_double, 1.0) > 1e-9
This checks if there's a remainder when divided by 1, which indicates a decimal value.
2. Calculating the Area of the Polygon with an Inscribed Circle
Your formula is correct, but remember: C's tan() function uses radians, not degrees. Convert 180/num_pol degrees to radians by substituting π / num_pol (since 180° = π radians).
Implementation Steps:
- Define
_USE_MATH_DEFINESbefore includingmath.hto access theM_PImacro - Use either
pow(radius, 2)or the more efficientradius * radiusfor the squared radius - Compute the area with the adjusted formula
Here's the code snippet:
#define _USE_MATH_DEFINES #include <math.h> // ... after validating num_pol is an integer ... double area = radius * radius * num_pol * tan(M_PI / num_pol); printf("Area of the polygon with inscribed circle: %.2lf\n", area);
Complete Working Example
Putting it all together into a full program:
#define _USE_MATH_DEFINES #include <stdio.h> #include <math.h> int main() { double side_a, radius, diameter, num_pol_double; int num_pol; // Collect user inputs printf("Enter side length: "); scanf("%lf", &side_a); printf("Enter inscribed circle radius: "); scanf("%lf", &radius); printf("Enter diameter: "); scanf("%lf", &diameter); printf("Enter number of sides: "); scanf("%lf", &num_pol_double); // Validate all inputs if (side_a < 0 || radius < 0 || diameter < side_a || num_pol_double < 3 || fmod(num_pol_double, 1.0) > 1e-9) { printf("Error: Invalid input!\n"); printf("- Side length and radius must be positive.\n"); printf("- Diameter must be greater than or equal to side length.\n"); printf("- Number of sides must be an integer ≥3.\n"); return 1; } num_pol = (int)num_pol_double; // Calculate and display area double area = radius * radius * num_pol * tan(M_PI / num_pol); printf("Calculated polygon area: %.2lf\n", area); return 0; }
Compilation Note:
When compiling with GCC, link the math library using the -lm flag:
gcc polygon_area.c -o polygon_area -lm
内容的提问来源于stack exchange,提问作者Amir J.

