引言C语言作为一门历史悠久且功能强大的编程语言,其分支结构(如ifelse和switchcase)是编程逻辑的重要组成部分。掌握分支结构对于解决实际问题至关重要。本文将介绍20个经典C语言分支例子,帮...
C语言作为一门历史悠久且功能强大的编程语言,其分支结构(如if-else和switch-case)是编程逻辑的重要组成部分。掌握分支结构对于解决实际问题至关重要。本文将介绍20个经典C语言分支例子,帮助读者入门编程逻辑。
这个例子涉及字符串处理、字符数组和循环控制。通过编程实现将给定的算术竖式转换成数字并进行计算。
#include
#include
int main() { char equation[100]; printf("Enter an arithmetic equation: "); scanf("%s", equation); // 省略具体实现代码,涉及字符串处理和字符数组操作 return 0;
} 这是一个基于条件的逻辑推理问题,可以使用if-else语句和条件表达式来解决。
#include
int main() { int condition1, condition2; printf("Enter two conditions: "); scanf("%d %d", &condition1, &condition2); if (condition1 && condition2) { printf("Both conditions are true.\n"); } else if (condition1) { printf("Only the first condition is true.\n"); } else if (condition2) { printf("Only the second condition is true.\n"); } else { printf("Both conditions are false.\n"); } return 0;
} 这个问题需要使用嵌套循环和字符串格式化来打印特定形状的字符方阵。
#include
int main() { int n; printf("Enter the size of the matrix: "); scanf("%d", &n); // 省略具体实现代码,涉及嵌套循环和字符串格式化 return 0;
} 拉丁方阵是一个数论概念,每个数字在每一行和每一列只出现一次。这个问题可以通过回溯算法或动态规划来解决。
#include
int main() { int n; printf("Enter the size of the Latin square: "); scanf("%d", &n); // 省略具体实现代码,涉及回溯算法或动态规划 return 0;
} 这个问题涉及到数制转换,可以使用除法和取余操作。
#include
int main() { int decimal, base, remainder; printf("Enter a decimal number: "); scanf("%d", &decimal); printf("Enter the base: "); scanf("%d", &base); // 省略具体实现代码,涉及除法和取余操作 return 0;
} 这个例子展示了如何使用二维数组和循环结构来填充矩阵。
#include
int main() { int rows, cols; printf("Enter the number of rows and columns: "); scanf("%d %d", &rows, &cols); // 省略具体实现代码,涉及二维数组和循环结构 return 0;
} 这个例子展示了如何使用分支结构实现一个简单的计算器程序。
#include
int main() { char operator; double num1, num2; printf("Enter an operator (+, -, *, /): "); scanf(" %c", &operator); printf("Enter two numbers: "); scanf("%lf %lf", &num1, &num2); switch (operator) { case '+': printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2); break; case '-': printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2); break; case '*': printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2); break; case '/': printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2); break; default: printf("Invalid operator!\n"); } return 0;
} 这个例子展示了如何使用if-else语句和switch-case结构来评定学生成绩。
#include
int main() { int score; printf("Enter the student's score: "); scanf("%d", &score); if (score >= 90) { printf("Excellent!\n"); } else if (score >= 80) { printf("Good!\n"); } else if (score >= 70) { printf("Average!\n"); } else if (score >= 60) { printf("Pass!\n"); } else { printf("Fail!\n"); } return 0;
} 这个例子展示了如何使用嵌套循环和条件判断来计算给定日期在当年的位置。
#include
int main() { int day, month, year; printf("Enter the day, month, and year: "); scanf("%d %d %d", &day, &month, &year); // 省略具体实现代码,涉及嵌套循环和条件判断 return 0;
} 这个例子展示了如何使用if-else语句比较两个字符串。
#include
#include
int main() { char str1[100], str2[100]; printf("Enter two strings: "); scanf("%s %s", str1, str2); if (strcmp(str1, str2) == 0) { printf("The strings are equal.\n"); } else if (strcmp(str1, str2) < 0) { printf("%s is less than %s.\n", str1, str2); } else { printf("%s is greater than %s.\n", str1, str2); } return 0;
} 这个例子展示了如何使用指针来交换两个变量的值。
#include
int main() { int a = 10, b = 20; printf("Before swapping: a = %d, b = %d\n", a, b); int *ptr1 = &a, *ptr2 = &b; int temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; printf("After swapping: a = %d, b = %d\n", a, b); return 0;
} 这个例子展示了如何使用指针和数组操作来计算数组的平均值。
#include
int main() { int n; printf("Enter the number of elements: "); scanf("%d", &n); int arr[n]; printf("Enter %d elements: ", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; } double average = (double)sum / n; printf("Average = %.2lf\n", average); return 0;
} 这个例子展示了如何定义和调用函数。
#include
// 函数声明
int add(int a, int b);
int main() { int num1 = 10, num2 = 20; int sum = add(num1, num2); printf("The sum of %d and %d is %d\n", num1, num2, sum); return 0;
}
// 函数定义
int add(int a, int b) { return a + b;
} 这个例子展示了如何使用字符串函数来处理字符串。
#include
#include
int main() { char str1[100], str2[100]; printf("Enter two strings: "); scanf("%s %s", str1, str2); // 省略具体实现代码,涉及字符串函数操作 return 0;
} 这个例子展示了如何使用malloc和free函数进行动态内存分配。
#include
#include
int main() { int *arr; int n; printf("Enter the number of elements: "); scanf("%d", &n); arr = (int *)malloc(n * sizeof(int)); if (arr == NULL) { printf("Memory allocation failed!\n"); return 1; } // 省略具体实现代码,涉及动态内存分配和释放 free(arr); return 0;
} 这个例子展示了如何使用链表来存储和操作数据。
#include
#include
// 链表节点定义
struct Node { int data; struct Node *next;
};
// 函数声明
struct Node *createNode(int data);
void insertNode(struct Node **head, int data);
void printList(struct Node *head);
void freeList(struct Node *head);
int main() { struct Node *head = NULL; // 省略具体实现代码,涉及链表操作 freeList(head); return 0;
}
// 函数定义
struct Node *createNode(int data) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory allocation failed!\n"); return NULL; } newNode->data = data; newNode->next = NULL; return newNode;
}
void insertNode(struct Node **head, int data) { struct Node *newNode = createNode(data); if (newNode == NULL) { return; } newNode->next = *head; *head = newNode;
}
void printList(struct Node *head) { struct Node *current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n");
}
void freeList(struct Node *head) { struct Node *current = head; while (current != NULL) { struct Node *next = current->next; free(current); current = next; }
} 这个例子展示了如何使用冒泡排序算法对数组进行排序。
#include
void bubbleSort(int arr[], int n) { int i, j, temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } }
}
int main() { int n; printf("Enter the number of elements: "); scanf("%d", &n); int arr[n]; printf("Enter %d elements: ", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } bubbleSort(arr, n); printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0;
} 这个例子展示了如何使用二分查找算法在有序数组中查找元素。
#include
int binarySearch(int arr[], int l, int r, int x) { while (l <= r) { int m = l + (r - l) / 2; // 检查x是否在中间 if (arr[m] == x) { return m; } // 如果x大于中间元素,则只考虑右半部分 if (arr[m] < x) { l = m + 1; } // 否则只考虑左半部分 else { r = m - 1; } } // 如果元素不存在,则返回-1 return -1;
}
int main() { int arr[] = {2, 3, 4, 10, 40}; int n = sizeof(arr) / sizeof(arr[0]); int x = 10; int result = binarySearch(arr, 0, n - 1, x); if (result == -1) { printf("Element is not present in array\n"); } else { printf("Element is present at index %d\n", result); } return 0;
} 这个例子展示了如何使用链表来存储和查找数据。
#include
#include
// 链表节点定义
struct Node { int data; struct Node *next;
};
// 函数声明
struct Node *createNode(int data);
void insertNode(struct Node **head, int data);
void printList(struct Node *head);
void freeList(struct Node *head);
struct Node *searchList(struct Node *head, int x);
int main() { struct Node *head = NULL; // 省略具体实现代码,涉及链表操作 struct Node *result = searchList(head, x); if (result != NULL) { printf("Element is present in the list\n"); } else { printf("Element is not present in the list\n"); } freeList(head); return 0;
}
// 函数定义
struct Node *createNode(int data) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory allocation failed!\n"); return NULL; } newNode->data = data; newNode->next = NULL; return newNode;
}
void insertNode(struct Node **head, int data) { struct Node *newNode = createNode(data); if (newNode == NULL) { return; } newNode->next = *head; *head = newNode;
}
void printList(struct Node *head) { struct Node *current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n");
}
void freeList(struct Node *head) { struct Node *current = head; while (current != NULL) { struct Node *next = current->next; free(current); current = next; }
}
struct Node *searchList(struct Node *head, int x) { struct Node *current = head; while (current != NULL) { if (current->data == x) { return current; } current = current->next; } return NULL;
} 这个例子展示了如何使用递归函数计算阶乘。
#include
int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); }
}
int main() { int n; printf("Enter a number: "); scanf("%d", &n); printf("Factorial of %d is %d\n", n, factorial(n)); return 0;
} 通过以上20个经典C语言分支例子,读者可以更好地理解C语言分支结构的应用,并掌握编程逻辑。在实际编程中,灵活运用分支结构可以有效地解决各种实际问题。希望这些例子对您的编程学习有所帮助!