MindMap Gallery C Language Question 16-20
This is a mind map about questions 16-20 of C language, and the main contents include: 20, C language examples - judge letters, 20, C language examples - judge letters, 19, C language examples - judge positive and negative numbers Zero, 18. C language examples - judge leap years, 17. C language examples - unary quadratic equations, 16. C language examples - judge the largest number among the three numbers.
Edited at 2025-02-13 20:37:16Rumi: 10 dimensions of spiritual awakening. When you stop looking for yourself, you will find the entire universe because what you are looking for is also looking for you. Anything you do persevere every day can open a door to the depths of your spirit. In silence, I slipped into the secret realm, and I enjoyed everything to observe the magic around me, and didn't make any noise. Why do you like to crawl when you are born with wings? The soul has its own ears and can hear things that the mind cannot understand. Seek inward for the answer to everything, everything in the universe is in you. Lovers do not end up meeting somewhere, and there is no parting in this world. A wound is where light enters your heart.
Chronic heart failure is not just a problem of the speed of heart rate! It is caused by the decrease in myocardial contraction and diastolic function, which leads to insufficient cardiac output, which in turn causes congestion in the pulmonary circulation and congestion in the systemic circulation. From causes, inducement to compensation mechanisms, the pathophysiological processes of heart failure are complex and diverse. By controlling edema, reducing the heart's front and afterload, improving cardiac comfort function, and preventing and treating basic causes, we can effectively respond to this challenge. Only by understanding the mechanisms and clinical manifestations of heart failure and mastering prevention and treatment strategies can we better protect heart health.
Ischemia-reperfusion injury is a phenomenon that cellular function and metabolic disorders and structural damage will worsen after organs or tissues restore blood supply. Its main mechanisms include increased free radical generation, calcium overload, and the role of microvascular and leukocytes. The heart and brain are common damaged organs, manifested as changes in myocardial metabolism and ultrastructural changes, decreased cardiac function, etc. Prevention and control measures include removing free radicals, reducing calcium overload, improving metabolism and controlling reperfusion conditions, such as low sodium, low temperature, low pressure, etc. Understanding these mechanisms can help develop effective treatment options and alleviate ischemic injury.
Rumi: 10 dimensions of spiritual awakening. When you stop looking for yourself, you will find the entire universe because what you are looking for is also looking for you. Anything you do persevere every day can open a door to the depths of your spirit. In silence, I slipped into the secret realm, and I enjoyed everything to observe the magic around me, and didn't make any noise. Why do you like to crawl when you are born with wings? The soul has its own ears and can hear things that the mind cannot understand. Seek inward for the answer to everything, everything in the universe is in you. Lovers do not end up meeting somewhere, and there is no parting in this world. A wound is where light enters your heart.
Chronic heart failure is not just a problem of the speed of heart rate! It is caused by the decrease in myocardial contraction and diastolic function, which leads to insufficient cardiac output, which in turn causes congestion in the pulmonary circulation and congestion in the systemic circulation. From causes, inducement to compensation mechanisms, the pathophysiological processes of heart failure are complex and diverse. By controlling edema, reducing the heart's front and afterload, improving cardiac comfort function, and preventing and treating basic causes, we can effectively respond to this challenge. Only by understanding the mechanisms and clinical manifestations of heart failure and mastering prevention and treatment strategies can we better protect heart health.
Ischemia-reperfusion injury is a phenomenon that cellular function and metabolic disorders and structural damage will worsen after organs or tissues restore blood supply. Its main mechanisms include increased free radical generation, calcium overload, and the role of microvascular and leukocytes. The heart and brain are common damaged organs, manifested as changes in myocardial metabolism and ultrastructural changes, decreased cardiac function, etc. Prevention and control measures include removing free radicals, reducing calcium overload, improving metabolism and controlling reperfusion conditions, such as low sodium, low temperature, low pressure, etc. Understanding these mechanisms can help develop effective treatment options and alleviate ischemic injury.
I—16—20
16. C Language Example - Determine the maximum number among the three numbers
Method 1: Use the if - else statement This is the most common and direct method, using conditional statements to compare three numbers.
#include <stdio.h>
int main() {
int num1, num2, num3, max;
printf("Please enter three integers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3) {
max = num1;
}
else if (num2 >= num1 && num2 >= num3) {
max = num2;
}
else {
max = num3;
}
printf("Maximum number is: %d ", max);
return 0;
}
Method 2: Use nested if statements This method gradually compares two numbers through nested if statements.
#include <stdio.h>
int main() {
int num1, num2, num3, max;
printf("Please enter three integers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2) {
if (num1 >= num3) {
max = num1;
}
else {
max = num3;
}
}
else {
if (num2 >= num3) {
max = num2;
}
else {
max = num3;
}
}
printf("Maximum number is: %d ", max);
return 0;
}
Method 3: Use functions to encapsulate the logic that determines the maximum number into a function to make the code clearer and more reusable.
#include <stdio.h>
int findMax(int a, int b, int c) {
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
int main() {
int num1, num2, num3;
printf("Please enter three integers: ");
scanf("%d %d %d", &num1, &num2, &num3);
int max = findMax(num1, num2, num3);
printf("Maximum number is: %d ", max);
return 0;
}
Method 4: Use pointers and arrays This method simplifies the code through arrays and pointers.
#include <stdio.h>
int main() {
int num[3], max;
printf("Please enter three integers: ");
for (int i = 0; i < 3; i ) {
scanf("%d", &num[i]);
}
max = num[0];
for (int i = 1; i < 3; i ) {
if (num[i] > max) {
max = num[i];
}
}
printf("Maximum number is: %d ", max);
return 0;
}
Method 5: Use the three-point operator The three-point operator is a concise way to handle simple conditional judgments.
#include <stdio.h>
int main() {
int num1, num2, num3, max;
printf("Please enter three integers: ");
scanf("%d %d %d", &num1, &num2, &num3);
max = (num1 >= num2) ? ((num1 >= num3) ? num1 : num3) : ((num2 >= num3) ? num2 : num3);
printf("Maximum number is: %d ", max);
return 0;
}
Method 6: Use the standard library function fmax. This method uses the fmax function in the C standard library to simplify the code, but it should be noted that the fmax function is suitable for floating point numbers.
#include <stdio.h>
#include <math.h>
int main() {
double num1, num2, num3, max;
printf("Please enter three numbers: ");
scanf("%lf %lf %lf", &num1, &num2, &num3);
max = fmax(num1, fmax(num2, num3));
printf("Maximum number is: %.2f ", max);
return 0;
}
cycle
Method 1: Use a for loop This is the most common and direct way to read the input and compare three numbers through a for loop. */
#include <stdio.h>
int main() {
int num[3], max;
int i;
printf("Please enter three integers: ");
for (i = 0; i < 3; i ) {
scanf("%d", &num[i]);
}
// Assume that the first number is the largest number
max = num[0];
// Use loop to compare other numbers
for (i = 1; i < 3; i ) {
if (num[i] > max) {
max = num[i];
}
}
printf("Maximum number is: %d ", max);
return 0;
}
Method 2: Use a while loop to read the input and compare three numbers through the while loop.
#include <stdio.h>
int main() {
int num[3], max;
int i = 0;
printf("Please enter three integers: ");
while (i < 3) {
scanf("%d", &num[i]);
i ;
}
// Assume that the first number is the largest number
max = num[0];
// Use loop to compare other numbers
i = 1;
while (i < 3) {
if (num[i] > max) {
max = num[i];
}
i ;
}
printf("Maximum number is: %d ", max);
return 0;
}
Method 3: Use the do - while loop to read the input and compare three numbers through the do - while loop.
#include <stdio.h>
int main() {
int num[3], max;
int i = 0;
printf("Please enter three integers: ");
do {
scanf("%d", &num[i]);
i ;
} while (i < 3);
// Assume that the first number is the largest number
max = num[0];
// Use loop to compare other numbers
i = 1;
do {
if (num[i] > max) {
max = num[i];
}
i ;
} while (i < 3);
printf("Maximum number is: %d ", max);
return 0;
}
Method 4: Use nested loops (for more complex scenarios) Although nested loops are not required in this simple example, for the sake of completeness, here is an example of nested loops. Nested loops are often used to handle more complex scenarios, such as handling multidimensional arrays.
#include <stdio.h>
int main() {
int num[3], max;
int i, j;
printf("Please enter three integers: ");
for (i = 0; i < 3; i ) {
scanf("%d", &num[i]);
}
// Assume that the first number is the largest number
max = num[0];
// Comparison of other numbers using nested loops (Nested loops don't make sense in this simple example)
for (i = 1; i < 3; i ) {
for (j = i; j < 3; j ) {
if (num[j] > max) {
max = num[j];
}
}
}
printf("Maximum number is: %d ", max);
return 0;
}
17. C Language Example - Monovariate Quadratic Equation
Several common methods to solve a quadratic equation in C language Method 1: Basic method (including real and complex roots)
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imaginePart;
printf("Please enter the coefficients a, b, c: " of the unary quadratic equation);
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The equation has two unequal real roots: %.2lf and %.2lf ", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("The equation has a real root: %.2lf ", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("The equation has two conjugated complex roots: %.2lf %.2lfi and %.2lf - %.2lfi ", realPart, imaginePart, realPart, imagePart);
}
return 0;
}
Method 2: Use Function Encapsulation Encapsulate the logic of the solution root in a function to make the code more modular and easy to maintain.
#include <stdio.h>
#include <math.h>
void solveQuadratic(double a, double b, double c) {
double discriminant, root1, root2, realPart, imagePart;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The equation has two unequal real roots: %.2lf and %.2lf ", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("The equation has a real root: %.2lf ", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("The equation has two conjugated complex roots: %.2lf %.2lfi and %.2lf - %.2lfi ", realPart, imaginePart, realPart, imagePart);
}
}
int main() {
double a, b, c;
printf("Please enter the coefficients a, b, c: " of the unary quadratic equation);
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
printf("This is not a quadratic equation. ");
}
else {
solveQuadratic(a, b, c);
}
return 0;
}
Method 3: Use structure to store results. Use structure to store root results, making the processing of results more convenient.
#include <stdio.h>
#include <math.h>
typedef struct {
double realPart;
double imagePart;
} ComplexRoot;
ComplexRoot solveQuadratic(double a, double b, double c) {
ComplexRoot roots;
double discriminant;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
roots.realPart = (-b sqrt(discriminant)) / (2 * a);
roots.imagPart = 0;
}
else if (discriminant == 0) {
roots.realPart = -b / (2 * a);
roots.imagPart = 0;
}
else {
roots.realPart = -b / (2 * a);
roots.imagPart = sqrt(-discriminant) / (2 * a);
}
return roots;
}
int main() {
double a, b, c;
ComplexRoot roots;
printf("Please enter the coefficients a, b, c: " of the unary quadratic equation);
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
printf("This is not a quadratic equation. ");
}
else {
roots = solveQuadratic(a, b, c);
if (roots.imagPart == 0) {
if (roots.realPart == roots.imagPart) {
printf("The equation has a real root: %.2lf ", roots.realPart);
}
else {
printf("The equation has two unequal real roots: %.2lf and %.2lf ", roots.realPart, roots.realPart);
}
}
else {
printf("The equation has two conjugated complex roots: %.2lf %.2lfi and %.2lf - %.2lfi ", roots.realPart, roots.imagPart, roots.realPart, roots.realPart, roots.imagPart);
}
}
return 0;
}
However, the above code has some problems when dealing with structures, especially for the processing part of real roots. Here is the revised version:
#include <stdio.h>
#include <math.h>
typedef struct {
double realPart;
double imagePart;
} ComplexRoot;
ComplexRoot solveQuadratic(double a, double b, double c) {
ComplexRoot roots;
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
roots.realPart = (-b sqrt(discriminant)) / (2 * a);
roots.imagPart = 0;
ComplexRoot root2;
root2.realPart = (-b - sqrt(discriminant)) / (2 * a);
root2.imagPart = 0;
printf("The equation has two unequal real roots: %.2lf and %.2lf ", roots.realPart, root2.realPart);
}
else if (discriminant == 0) {
roots.realPart = -b / (2 * a);
roots.imagPart = 0;
printf("The equation has a real root: %.2lf ", roots.realPart);
}
else {
roots.realPart = -b / (2 * a);
roots.imagPart = sqrt(-discriminant) / (2 * a);
printf("The equation has two conjugated complex roots: %.2lf %.2lfi and %.2lf - %.2lfi ", roots.realPart, roots.imagPart, roots.realPart, roots.realPart, roots.imagPart);
}
return roots;
}
int main() {
double a, b, c;
printf("Please enter the coefficients a, b, c: " of the unary quadratic equation);
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
printf("This is not a quadratic equation. ");
}
else {
solveQuadratic(a, b, c);
}
return 0;
}
Method 4: Pass the result using a pointer Use a pointer to pass the root result to the calling function.
#include <stdio.h>
#include <math.h>
void solveQuadratic(double a, double b, double c, double* real1, double* imag1, double* real2, double* imag2) {
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
*real1 = (-b sqrt(discriminant)) / (2 * a);
*imag1 = 0;
*real2 = (-b - sqrt(discriminant)) / (2 * a);
*imag2 = 0;
}
else if (discriminant == 0) {
*real1 = *real2 = -b / (2 * a);
*imag1 = *imag2 = 0;
}
else {
*real1 = *real2 = -b / (2 * a);
*imag1 = sqrt(-discriminant) / (2 * a);
*imag2 = -sqrt(-discriminant) / (2 * a);
}
}
int main() {
double a, b, c, real1, imagine1, real2, imagine2;
printf("Please enter the coefficients a, b, c: " of the unary quadratic equation);
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
printf("This is not a quadratic equation. ");
}
else {
solveQuadratic(a, b, c, &real1, &imag1, &real2, &imag2);
if (imag1 == 0 && imag2 == 0) {
if (real1 == real2) {
printf("The equation has a real root: %.2lf ", real1);
}
else {
printf("The equation has two unequal real roots: %.2lf and %.2lf ", real1, real2);
}
}
else {
printf("The equation has two conjugated complex roots: %.2lf %.2lfi and %.2lf - %.2lfi ", real1, imag1, real2, imag2);
}
}
return 0;
}
cycle
Method 1: Use do - while loop do - while loop executes the loop body at least once, and then decides whether to continue the loop based on the conditions. This approach is suitable for scenarios where it is necessary to ensure that the user enters at least one equation coefficients.
#include <stdio.h>
#include <math.h>
void solveQuadratic(double a, double b, double c) {
double discriminant, root1, root2, realPart, imagePart;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The equation has two unequal real roots: %.2lf and %.2lf ", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("The equation has a real root: %.2lf ", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("The equation has two conjugated complex roots: %.2lf %.2lfi and %.2lf - %.2lfi ", realPart, imaginePart, realPart, imagePart);
}
}
int main() {
double a, b, c;
char choice;
do {
printf("Please enter the coefficients a, b, c: " of the unary quadratic equation);
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
printf("This is not a quadratic equation. ");
}
else {
solveQuadratic(a, b, c);
}
printf("Do you continue to solve the equation? (y/n): ");
scanf(" %c", &choice); // Note the spaces here, used to skip line breaks
} while (choice == 'y' || choice == 'Y');
return 0;
}
Method 2: Use the while loop to check whether the condition is satisfied before each execution. This method is suitable for scenarios where it is necessary to dynamically decide whether to continue the loop based on conditions.
#include <stdio.h>
#include <math.h>
void solveQuadratic(double a, double b, double c) {
double discriminant, root1, root2, realPart, imagePart;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The equation has two unequal real roots: %.2lf and %.2lf ", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("The equation has a real root: %.2lf ", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("The equation has two conjugated complex roots: %.2lf %.2lfi and %.2lf - %.2lfi ", realPart, imaginePart, realPart, imagePart);
}
}
int main() {
double a, b, c;
char choice;
printf("Does it start to solve the equation? (y/n): ");
scanf(" %c", &choice); // Note the spaces here, used to skip line breaks
while (choice == 'y' || choice == 'Y') {
printf("Please enter the coefficients a, b, c: " of the unary quadratic equation);
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
printf("This is not a quadratic equation. ");
}
else {
solveQuadratic(a, b, c);
}
printf("Do you continue to solve the equation? (y/n): ");
scanf(" %c", &choice); // Note the spaces here, used to skip line breaks
}
return 0;
}
Method 3: Use for loops for loops for scenarios where the number of loops is already known. This method is suitable for situations where the user knows in advance how many equations they need to solve.
#include <stdio.h>
#include <math.h>
void solveQuadratic(double a, double b, double c) {
double discriminant, root1, root2, realPart, imagePart;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The equation has two unequal real roots: %.2lf and %.2lf ", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("The equation has a real root: %.2lf ", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("The equation has two conjugated complex roots: %.2lf %.2lfi and %.2lf - %.2lfi ", realPart, imaginePart, realPart, imagePart);
}
}
int main() {
int n, i;
double a, b, c;
printf("Please enter the number of unary quadratic equations to be solved: ");
scanf("%d", &n);
for (i = 0; i < n; i ) {
printf("Please enter the coefficients a, b, c: ", i 1 of the %dth unary quadratic equation);
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
printf("This is not a quadratic equation. ");
}
else {
solveQuadratic(a, b, c);
}
}
return 0;
}
Method 4: Use do - while loop to read coefficients in the file. Read the coefficients of multiple equations from the file and solve them. You can use do - while loop to process the contents in the file line by line.
#include <stdio.h>
#include <math.h>
void solveQuadratic(double a, double b, double c) {
double discriminant, root1, root2, realPart, imagePart;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The equation has two unequal real roots: %.2lf and %.2lf ", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("The equation has a real root: %.2lf ", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("The equation has two conjugated complex roots: %.2lf %.2lfi and %.2lf - %.2lfi ", realPart, imaginePart, realPart, imagePart);
}
}
int main() {
FILE* file;
double a, b, c;
file = fopen("equations.txt", "r");
if (file == NULL) {
printf("File cannot be opened. ");
return 1;
}
do {
if (fscanf(file, "%lf %lf %lf %lf", &a, &b, &c) != 3) {
break; // If no three coefficients are read, the loop will be exited
}
if (a == 0) {
printf("This is not a quadratic equation. ");
}
else {
solveQuadratic(a, b, c);
}
} while (!feof(file)); // Continue to loop until the end of the file
fclose(file);
return 0;
}
Method 5: Read coefficients in a file using a while loop Similarly, you can use a while loop to read coefficients in a file line by line.
#include <stdio.h>
#include <math.h>
void solveQuadratic(double a, double b, double c) {
double discriminant, root1, root2, realPart, imagePart;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The equation has two unequal real roots: %.2lf and %.2lf ", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("The equation has a real root: %.2lf ", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("The equation has two conjugated complex roots: %.2lf %.2lfi and %.2lf - %.2lfi ", realPart, imaginePart, realPart, imagePart);
}
}
int main() {
FILE* file;
double a, b, c;
file = fopen("equations.txt", "r");
if (file == NULL) {
printf("File cannot be opened. ");
return 1;
}
while (fscanf(file, "%lf %lf %lf %lf", &a, &b, &c) == 3) {
if (a == 0) {
printf("This is not a quadratic equation. ");
}
else {
solveQuadratic(a, b, c);
}
}
fclose(file);
return 0;
}
18. C Language Examples - Judge Leap Years
Method 1: Standard Method This is the most common and standard method, following the rules of leap years: years that can be divisible by 4 but cannot be divisible by 100, or years that can be divisible by 400 are leap years.
#include <stdio.h>
int main() {
int year;
// Prompt the user to enter the year
printf("Please enter a year: ");
scanf("%d", &year);
// Determine whether it is a leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year. ", year);
}
else {
printf("%d is not a leap year. ", year);
}
return 0;
}
Method 2: Use nested if statements. This method uses nested if statements to gradually determine whether the year is a leap year.
#include <stdio.h>
int main() {
int year;
// Prompt the user to enter the year
printf("Please enter a year: ");
scanf("%d", &year);
// Determine whether it is a leap year
if (year % 4 == 0) {
if (year % 100 != 0) {
printf("%d is a leap year. ", year);
}
else {
if (year % 400 == 0) {
printf("%d is a leap year. ", year);
}
else {
printf("%d is not a leap year. ", year);
}
}
}
else {
printf("%d is not a leap year. ", year);
}
return 0;
}
Method 3: Use function encapsulation. This method encapsulates the judgment logic of leap years in a function, making the code more modular and easy to maintain.
#include <stdio.h>
// Function declaration
int isLeapYear(int year);
int main() {
int year;
// Prompt the user to enter the year
printf("Please enter a year: ");
scanf("%d", &year);
// Call the function and output the result
if (isLeapYear(year)) {
printf("%d is a leap year. ", year);
}
else {
printf("%d is not a leap year. ", year);
}
return 0;
}
// Function definition
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1; // is a leap year
}
else {
return 0; // Not a leap year
}
}
Method 4: Use the three-item operator. This method uses the three-item operator to simplify the code.
#include <stdio.h>
int main() {
int year;
// Prompt the user to enter the year
printf("Please enter a year: ");
scanf("%d", &year);
// Use the trigonometric operator to determine whether it is a leap year
((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ?
printf("%d is a leap year. ", year) :
printf("%d is not a leap year. ", year);
return 0;
}
Summarize
The above are several common ways to judge leap years in C language. Each method has its own characteristics:
Standard method: Use conditional statements to judge directly in the main function.
Nested if statements: Gradually judged through nested if statements.
Use function encapsulation: encapsulate judgment logic in functions to make the code more modular.
Use the trinocular operator: Use the trinocular operator to simplify conditional judgment.
cycle
Method 1: Use a for loop
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int year, i, num;
// Prompt the user to enter the number of years to judge
printf("Please enter the number of years to be judged: ");
scanf("%d", &num);
// Use a for loop to judge leap years multiple times
for (i = 0; i < num; i ) {
printf("Please enter the %dth year: ", i 1);
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year. ", year);
}
else {
printf("%d is not a leap year. ", year);
}
}
return 0;
}
Method 2: Use while loop
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int year, num, count = 0;
// Prompt the user to enter the number of years to judge
printf("Please enter the number of years to be judged: ");
scanf("%d", &num);
// Use while loop to judge leap year multiple times
while (count < num) {
printf("Please enter the %dth year: ", count 1);
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year. ", year);
}
else {
printf("%d is not a leap year. ", year);
}
count ;
}
return 0;
}
Method 3: Use do- while loop
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int year, num, count = 0;
// Prompt the user to enter the number of years to judge
printf("Please enter the number of years to be judged: ");
scanf("%d", &num);
// Use do-while loop to judge leap years multiple times
do {
printf("Please enter the %dth year: ", count 1);
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year. ", year);
}
else {
printf("%d is not a leap year. ", year);
}
count ;
} while (count < num);
return 0;
}
19. C Language Example - Judge Positive Number Negative Number Zero
Method 1: Use if - else statement
#include <stdio.h>
int main() {
int number;
// Prompt the user to enter an integer
printf("Please enter an integer: ");
scanf("%d", &number);
// Determine whether the input integer is positive, negative or zero
if (number > 0) {
printf("This number is a positive number. ");
}
else if (number < 0) {
printf("This number is a negative number. ");
}
else {
printf("This number is zero. ");
}
return 0;
}
meeting
Method 2: Use nested if statements
#include <stdio.h>
int main() {
int number;
// Prompt the user to enter an integer
printf("Please enter an integer: ");
scanf("%d", &number);
// Determine whether the input integer is positive, negative or zero
if (number != 0) {
if (number > 0) {
printf("This number is a positive number. ");
}
else {
printf("This number is a negative number. ");
}
}
else {
printf("This number is zero. ");
}
return 0;
}
meeting
Method 3: Use the ternary operator
#include <stdio.h>
int main() {
int number;
// Prompt the user to enter an integer
printf("Please enter an integer: ");
scanf("%d", &number);
// Use the ternary operator to determine whether the input integer is a positive, negative or zero
(number > 0) ? printf("This number is a positive number. ") :
(number < 0) ? printf("This number is a negative number. ") :
printf("This number is zero. ");
return 0;
}
meeting
Method 4: Use switch statements (applicable to finite sets) This method is not very suitable for directly judging positive numbers, negative numbers and zeros, but can be implemented through some workarounds. Typically, switch statements are more suitable for handling discrete, finite sets of values. However, in order to meet the requirements, the following method can be adopted:
#include <stdio.h>
int main() {
int number;
// Prompt the user to enter an integer
printf("Please enter an integer: ");
scanf("%d", &number);
// Use the switch statement to determine whether the input integer is a positive, negative or zero
switch (number > 0) {
case 1:
printf("This number is a positive number. ");
break;
case 0:
switch (number < 0) {
case 1:
printf("This number is a negative number. ");
break;
case 0:
printf("This number is zero. ");
break;
}
break;
}
return 0;
}
meeting
Method 5: Use functions
#include <stdio.h>
void checkNumber(int number) {
if (number > 0) {
printf("This number is a positive number. ");
}
else if (number < 0) {
printf("This number is a negative number. ");
}
else {
printf("This number is zero. ");
}
}
int main() {
int number;
// Prompt the user to enter an integer
printf("Please enter an integer: ");
scanf("%d", &number);
// Call the function to determine whether the input integer is a positive, negative or zero
checkNumber(number);
return 0;
}
cycle
While loop:
#include <stdio.h>
int main() {
int number;
char choice = 'y';
while (choice == 'y' || choice == 'Y') {
printf("Please enter an integer: ");
scanf("%d", &number);
if (number > 0) {
printf("This number is a positive number. ");
}
else if (number < 0) {
printf("This number is a negative number. ");
}
else {
printf("This number is zero. ");
}
printf("Does the input continue? (y/n): ");
scanf(" %c", &choice);
}
return 0;
}
I
#include<stdio.h>
int main() {
int a;
char b = 'y';
for (; (b == 'y' || b == 'Y');) {
scanf("%d", &a);
if (a > 0) {
printf("This number is a positive number. ");
}
else if (a < 0) {
printf("This number is a negative number. ");
}
else {
printf("This number is zero. ");
}
printf("Does the input continue? (y/n): ");
scanf(" %c", &b);
}
return 0;
}
do - while loop:
#include <stdio.h>
int main() {
int number;
char choice;
do {
printf("Please enter an integer: ");
scanf("%d", &number);
if (number > 0) {
printf("This number is a positive number. ");
}
else if (number < 0) {
printf("This number is a negative number. ");
}
else {
printf("This number is zero. ");
}
printf("Does the input continue? (y/n): ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
return 0;
}
for loop:
#include <stdio.h>
int main() {
int count, number;
// Prompt the user to enter the integer number to be judged
printf("Please enter the number of integers to be judged: ");
scanf("%d", &count);
// Use a for loop to judge each integer
for (int i = 0; i < count; i ) {
printf("Please enter the %d-th integer: ", i 1);
scanf("%d", &number);
if (number > 0) {
printf("%d is a positive number. ", number);
}
else if (number < 0) {
printf("%d is a negative number. ", number);
}
else {
printf("%d is zero. ", number);
}
}
return 0;
}
20. C Language Examples - Judge Letters
Method 1: Use the isalpha function isalpha is a function in the C standard library, located in the <ctype.h> header file, used to determine whether a character is a letter (A - Z or a - z).
#include <stdio.h>
#include <ctype.h>
#define _CRT_SECURE_NO_WARNINGS
int main() {
char ch;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Use the isalpha function to determine whether the character is a letter
if (isalpha(ch)) {
printf("You entered the letter. ");
}
else {
printf("You are not entering a letter. ");
}
return 0;
}
Method 2: Use ASCII code range You can check whether a character is a letter by checking the ASCII code value.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
char ch;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Use the ASCII code range to determine whether the character is a letter
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
printf("You entered the letter. ");
}
else {
printf("You are not entering a letter. ");
}
return 0;
}
Method 3: Use conditional expressions and toupper functions to convert characters to uppercase form and then check whether they are within the ASCII code range of capital letters.
#include <stdio.h>
#include <ctype.h>
#define _CRT_SECURE_NO_WARNINGS
int main() {
char ch;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Use the toupper function to convert the characters to uppercase form and then determine whether they are letters
if (toupper(ch) >= 'A' && toupper(ch) <= 'Z') {
printf("You entered the letter. ");
}
else {
printf("You are not entering a letter. ");
}
return 0;
}
Method 4: Use bit operation This method uses bit operation to determine whether a character is a letter. It is relatively complex, but can be used as an interesting way to implement it.
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
int is_alpha(char ch) {
// A calculation to determine whether a character is a letter
return ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'));
}
int main() {
char ch;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Use the is_alpha function to determine whether the character is a letter
if (is_alpha(ch)) {
printf("You entered the letter. ");
}
else {
printf("You are not entering a letter. ");
}
return 0;
}
20. C Language Examples - Judge Letters
Method 1: Use the isalpha function isalpha is a function in the C standard library, located in the <ctype.h> header file, used to determine whether a character is a letter (A - Z or a - z).
#include <stdio.h>
#include <ctype.h>
#define _CRT_SECURE_NO_WARNINGS
int main() {
char ch;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Use the isalpha function to determine whether the character is a letter
if (isalpha(ch)) {
printf("You entered the letter. ");
}
else {
printf("You are not entering a letter. ");
}
return 0;
}
Method 2: Use ASCII code range You can check whether a character is a letter by checking the ASCII code value.
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
int main() {
char ch;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Use the ASCII code range to determine whether the character is a letter
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
printf("You entered the letter. ");
}
else {
printf("You are not entering a letter. ");
}
return 0;
}
Method 3: Use conditional expressions and toupper functions to convert characters to uppercase form and then check whether they are within the ASCII code range of capital letters.
#include <stdio.h>
#include <ctype.h>
#define _CRT_SECURE_NO_WARNINGS
int main() {
char ch;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Use the toupper function to convert the characters to uppercase form and then determine whether they are letters
if (toupper(ch) >= 'A' && toupper(ch) <= 'Z') {
printf("You entered the letter. ");
}
else {
printf("You are not entering a letter. ");
}
return 0;
}
Method 4: Use bit operation This method uses bit operation to determine whether a character is a letter. It is relatively complex, but can be used as an interesting way to implement it.
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
int is_alpha(char ch) {
// A calculation to determine whether a character is a letter
return ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'));
}
int main() {
char ch;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Use the is_alpha function to determine whether the character is a letter
if (is_alpha(ch)) {
printf("You entered the letter. ");
}
else {
printf("You are not entering a letter. ");
}
return 0;
}
Summarize
The above four methods can be used to determine whether a character is a letter. Each method has its own characteristics:
Use isalpha function: simple and straightforward, easy to understand, and suitable for all C compilers.
Use ASCII code range: No additional library functions are required by directly comparing ASCII values of characters.
Use the toupper function: simplifies conditional judgment by converting characters into unified capital forms for comparison.
Using bit operations: Although this method is more complex, it provides a different way of thinking.
These methods can be selected and used according to specific needs. Normally, using the isalpha function is the most recommended way because it is simple and easy to maintain.
cycle
1. Use a for loop. Assuming that you want to determine whether each character is a letter in a string, you can use a for loop.
#include <stdio.h>
#include <ctype.h>
#define _CRT_SECURE_NO_WARNINGS
int main() {
char str[100];
printf("Please enter a string:");
scanf("%s", str);
for (int i = 0; str[i] != '\0'; i ) {
if (isalpha(str[i])) {
printf("character '%c' is a letter. ", str[i]);
}
else {
printf("character '%c' is not a letter. ", str[i]);
}
}
return 0;
}
2. Use a while loop Similarly, if you want to determine whether each character is a letter in a string, you can use a while loop.
#include <stdio.h>
#include <ctype.h>
#define _CRT_SECURE_NO_WARNINGS
int main() {
char str[100];
printf("Please enter a string:");
scanf("%s", str);
int i = 0;
while (str[i] != '\0') {
if (isalpha(str[i])) {
printf("character '%c' is a letter. ", str[i]);
}
else {
printf("character '%c' is not a letter. ", str[i]);
}
i ;
}
return 0;
}
3. Use do - while loop Similarly, assuming you want to determine whether each character is a letter in a string, you can use do - while loop.
#include <stdio.h>
#include <ctype.h>
#define _CRT_SECURE_NO_WARNINGS
int main() {
char str[100];
printf("Please enter a string:");
scanf("%s", str);
int i = 0;
do {
if (isalpha(str[i])) {
printf("character '%c' is a letter. ", str[i]);
}
else {
printf("character '%c' is not a letter. ", str[i]);
}
i ;
} while (str[i] != '\0');
return 0;
}