c语言替换 switch的方法
时间: 2024-03-28 13:17:16 浏览: 23
C语言中可以使用if-else语句或者数组来替换switch语句。具体来说,使用if-else语句需要对每个case做单独处理,而使用数组需要将case的值作为数组下标,将处理语句作为数组元素。以下是这两种方法的示例代码:
使用if-else语句:
```c
int a = 2;
if (a == 1) {
// do something for case 1
}
else if (a == 2) {
// do something for case 2
}
else if (a == 3) {
// do something for case 3
}
// ...
```
使用数组:
```c
typedef void (*case_func_ptr)(void);
case_func_ptr case_funcs[] = {
/* case 0 */ func_for_case_0,
/* case 1 */ func_for_case_1,
/* case 2 */ func_for_case_2,
/* case 3 */ func_for_case_3,
/* ... */
};
int a = 2;
case_funcs[a]();
```
注意,这里的case_funcs数组中每个元素都是一个函数指针,指向对应case的处理函数。最后根据a的值来调用相应的函数。
相关问题
C语言函数print+day
根据你提供的引用内容,C语言中没有名为print的函数。C语言中用于打印输出的函数是printf。printf函数用于将格式化的数据输出到标准输出设备(通常是终端)。它可以接受多个参数,其中第一个参数是格式化字符串,后面的参数是要输出的数据。
下面是一个示例,演示了如何使用printf函数打印出变量day的值:
```c
int day = 6;
printf("day = %d\n", day); // 输出:day = 6
```
在上面的示例中,%d是格式化字符串中的占位符,用于表示要输出的数据是一个整数。%d会被day的值替换。
如果你想要根据不同的取值执行不同的操作,可以使用switch语句。switch语句根据表达式的值选择执行不同的代码块。如果没有匹配的case,可以使用default来执行默认的代码块。
下面是一个示例,演示了如何使用switch语句根据不同的day值执行不同的操作:
```c
int day = 6;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
break;
}
```
在上面的示例中,根据day的值,switch语句会选择执行相应的case代码块。如果day的值是6,那么输出将是"Saturday"。
C语言将json内容替换成另外一种数据格式代码
以下是一个示例代码,将输入的 JSON 字符串转换成 YAML 格式,并将结果输出到控制台:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <yaml.h>
#include <cJSON.h>
void print_yaml(yaml_document_t *doc, yaml_node_t *node, int level);
void print_scalar(yaml_document_t *doc, yaml_node_t *node) {
printf("%s", yaml_scalar_node_get_value(node));
}
void print_sequence(yaml_document_t *doc, yaml_node_t *node, int level) {
yaml_node_item_t *item;
for (item = node->data.sequence.items.start; item < node->data.sequence.items.top; item++) {
print_yaml(doc, yaml_document_get_node(doc, *item), level + 1);
}
}
void print_mapping(yaml_document_t *doc, yaml_node_t *node, int level) {
yaml_node_pair_t *pair;
for (pair = node->data.mapping.pairs.start; pair < node->data.mapping.pairs.top; pair++) {
yaml_node_t *key = yaml_document_get_node(doc, pair->key);
yaml_node_t *value = yaml_document_get_node(doc, pair->value);
for (int i = 0; i < level; i++) {
printf(" ");
}
printf("%s: ", yaml_scalar_node_get_value(key));
print_yaml(doc, value, level + 1);
}
}
void print_yaml(yaml_document_t *doc, yaml_node_t *node, int level) {
switch (node->type) {
case YAML_SCALAR_NODE:
print_scalar(doc, node);
printf("\n");
break;
case YAML_SEQUENCE_NODE:
print_sequence(doc, node, level);
break;
case YAML_MAPPING_NODE:
print_mapping(doc, node, level);
break;
default:
break;
}
}
int main(int argc, char *argv[]) {
char *json_str = "{\"name\": \"Alice\", \"age\": 20, \"friends\": [\"Bob\", \"Charlie\"]}";
cJSON *root = cJSON_Parse(json_str);
yaml_emitter_t emitter;
yaml_document_t document;
yaml_node_t *node = NULL;
if (!yaml_emitter_initialize(&emitter)) {
printf("Failed to initialize emitter.\n");
return 1;
}
if (!yaml_document_initialize(&document, NULL, NULL, NULL, 1, 1)) {
printf("Failed to initialize document.\n");
return 1;
}
node = yaml_document_add_mapping(&document, NULL, YAML_BLOCK_MAPPING_STYLE);
cJSON_ArrayForEach(root, item) {
char *key_str = item->string;
cJSON *value = item->child;
yaml_node_t *key_node = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)key_str, strlen(key_str), YAML_PLAIN_SCALAR_STYLE);
yaml_node_t *value_node = NULL;
switch (value->type) {
case cJSON_String:
value_node = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)value->valuestring, strlen(value->valuestring), YAML_DOUBLE_QUOTED_SCALAR_STYLE);
break;
case cJSON_Number:
value_node = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)cJSON_Print(value), strlen(cJSON_Print(value)), YAML_PLAIN_SCALAR_STYLE);
break;
case cJSON_Array:
value_node = yaml_document_add_sequence(&document, NULL, YAML_BLOCK_SEQUENCE_STYLE);
cJSON_ArrayForEach(value, item) {
yaml_node_t *item_node = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)value->valuestring, strlen(value->valuestring), YAML_DOUBLE_QUOTED_SCALAR_STYLE);
yaml_sequence_append(value_node, item_node);
}
break;
default:
break;
}
yaml_mapping_append(node, key_node, value_node);
}
yaml_emitter_set_output_file(&emitter, stdout);
yaml_emitter_dump(&emitter, &document);
yaml_emitter_delete(&emitter);
yaml_document_delete(&document);
cJSON_Delete(root);
return 0;
}
```
该示例代码使用了 `cJSON` 库来解析 JSON 字符串,并使用 `libyaml` 库来生成 YAML 格式的文档。在 `print_yaml` 函数中,根据节点类型分别调用 `print_scalar`、`print_sequence` 和 `print_mapping` 函数来递归打印节点内容。在 `main` 函数中,先将 JSON 字符串解析成 cJSON 对象,然后根据 cJSON 对象生成相应的 YAML 节点,并最终使用 `yaml_emitter_dump` 函数将 YAML 文档输出到控制台。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)