c语言中的void action
时间: 2023-07-02 14:21:17 浏览: 90
`void` 是 C 语言中的一种特殊类型,表示没有返回值的函数或指针。`void` 关键字可以在函数定义时作为函数的返回类型,表示该函数不返回任何值。例如:
```c
void print_hello() {
printf("Hello, world!\n");
}
```
此函数不返回任何值,因为它的返回类型是 `void`。`void` 还可以用作指针类型,表示指针不指向任何具体类型的值。例如:
```c
void* ptr = NULL;
```
这里的 `ptr` 是一个 `void*` 类型的指针,指向 `NULL`。由于 `void*` 指针不知道指向什么类型的值,因此需要在使用之前将其转换为特定类型的指针。
相关问题
c语言 fsm有限状态机
有限状态机(Finite State Machine,FSM)是一种描述有限个状态以及在这些状态之间转移和动作等行为的数学模型。在C语言中,可以使用结构体和函数指针实现一个简单的有限状态机,实现状态的转移和响应动作。
以下是一个简单的有限状态机的代码示例:
```c
typedef enum {
STATE_A,
STATE_B,
STATE_C,
STATE_D
} state_t;
typedef void (*action_t)(void);
typedef struct {
state_t current_state;
action_t action;
state_t next_state;
} transition_t;
void action_a(void) {
printf("Performing action A.\n");
}
void action_b(void) {
printf("Performing action B.\n");
}
void action_c(void) {
printf("Performing action C.\n");
}
void action_d(void) {
printf("Performing action D.\n");
}
transition_t transitions[] = {
{STATE_A, action_a, STATE_B},
{STATE_B, action_b, STATE_C},
{STATE_C, action_c, STATE_D},
{STATE_D, action_d, STATE_A}
};
int main(void) {
state_t current_state = STATE_A;
while (1) {
action_t action = NULL;
state_t next_state = STATE_A;
for (int i = 0; i < sizeof(transitions)/sizeof(transitions[0]); i++) {
if (transitions[i].current_state == current_state) {
action = transitions[i].action;
next_state = transitions[i].next_state;
break;
}
}
if (action != NULL) {
action();
}
current_state = next_state;
}
return 0;
}
```
以上代码定义了一个包含四个状态和四个动作的有限状态机。在 `main` 函数中,通过循环不断地执行当前状态对应的动作,并根据状态转移表更新当前状态。在此过程中,可通过添加新的状态和动作,扩展有限状态机的功能。
C语言租户模块开发代码
租户模块是指企业或个人在系统中的账户信息及其相关信息管理模块。在C语言中,租户模块的开发代码可以包括以下几个部分:
1. 定义租户结构体:定义租户结构体,包括租户ID、租户名称、联系方式等基本信息。
2. 租户信息管理函数:开发一些租户信息管理函数,包括新增租户、修改租户信息、查询租户信息等。
3. 租户权限管理函数:开发一些租户权限管理函数,包括授权、取消授权、修改权限等。
4. 租户日志记录函数:开发一些租户日志记录函数,记录租户的操作行为以及相关信息。
以下是一个简单的C语言租户模块开发代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义租户结构体
typedef struct {
int tenant_id; // 租户ID
char tenant_name[50]; // 租户名称
char contact[50]; // 联系方式
} Tenant;
// 新增租户
void add_tenant(Tenant *tenants, int *count) {
Tenant tenant;
printf("请输入新租户ID:");
scanf("%d", &tenant.tenant_id);
printf("请输入新租户名称:");
scanf("%s", tenant.tenant_name);
printf("请输入新租户联系方式:");
scanf("%s", tenant.contact);
tenants[*count] = tenant;
(*count)++;
printf("新增成功!\n");
}
// 修改租户信息
void modify_tenant(Tenant *tenants, int count) {
int tenant_id, index = -1;
printf("请输入要修改的租户ID:");
scanf("%d", &tenant_id);
for (int i = 0; i < count; i++) {
if (tenants[i].tenant_id == tenant_id) {
index = i;
break;
}
}
if (index == -1) {
printf("找不到该租户!\n");
return;
}
Tenant tenant;
printf("请输入新租户名称(原名称:%s):", tenants[index].tenant_name);
scanf("%s", tenant.tenant_name);
printf("请输入新租户联系方式(原联系方式:%s):", tenants[index].contact);
scanf("%s", tenant.contact);
tenants[index] = tenant;
printf("修改成功!\n");
}
// 查询租户信息
void query_tenant(Tenant *tenants, int count) {
int tenant_id, index = -1;
printf("请输入要查询的租户ID:");
scanf("%d", &tenant_id);
for (int i = 0; i < count; i++) {
if (tenants[i].tenant_id == tenant_id) {
index = i;
break;
}
}
if (index == -1) {
printf("找不到该租户!\n");
return;
}
printf("租户ID:%d,租户名称:%s,联系方式:%s\n",
tenants[index].tenant_id, tenants[index].tenant_name, tenants[index].contact);
}
// 授权
void authorize(Tenant *tenants, int count) {
int tenant_id, index = -1;
printf("请输入要授权的租户ID:");
scanf("%d", &tenant_id);
for (int i = 0; i < count; i++) {
if (tenants[i].tenant_id == tenant_id) {
index = i;
break;
}
}
if (index == -1) {
printf("找不到该租户!\n");
return;
}
printf("授权成功!\n");
}
// 取消授权
void revoke(Tenant *tenants, int count) {
int tenant_id, index = -1;
printf("请输入要取消授权的租户ID:");
scanf("%d", &tenant_id);
for (int i = 0; i < count; i++) {
if (tenants[i].tenant_id == tenant_id) {
index = i;
break;
}
}
if (index == -1) {
printf("找不到该租户!\n");
return;
}
printf("取消授权成功!\n");
}
// 记录日志
void log(Tenant *tenants, int count, char *action) {
int tenant_id, index = -1;
printf("请输入操作的租户ID:");
scanf("%d", &tenant_id);
for (int i = 0; i < count; i++) {
if (tenants[i].tenant_id == tenant_id) {
index = i;
break;
}
}
if (index == -1) {
printf("找不到该租户!\n");
return;
}
printf("操作记录:租户ID:%d,操作:%s\n", tenants[index].tenant_id, action);
}
int main() {
Tenant tenants[100];
int count = 0;
while (1) {
int option;
printf("\n请选择操作:\n");
printf("1. 新增租户\n");
printf("2. 修改租户信息\n");
printf("3. 查询租户信息\n");
printf("4. 授权\n");
printf("5. 取消授权\n");
printf("6. 记录日志\n");
printf("7. 退出\n");
scanf("%d", &option);
switch (option) {
case 1:
add_tenant(tenants, &count);
break;
case 2:
modify_tenant(tenants, count);
break;
case 3:
query_tenant(tenants, count);
break;
case 4:
authorize(tenants, count);
break;
case 5:
revoke(tenants, count);
break;
case 6:
log(tenants, count, "操作");
break;
case 7:
exit(0);
default:
printf("无效操作!\n");
break;
}
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)