printf("1: %s\n", result ? "TRUE":"FALSE");
时间: 2023-11-21 21:58:43 浏览: 31
这段代码使用了三目运算符,如果result为真,则输出TRUE,否则输出FALSE。其中%s是格式化字符串,用于输出字符串类型的变量。三目运算符的语法为:condition ? value_if_true : value_if_false。如果condition为真,则返回value_if_true,否则返回value_if_false。下面是一个例子:
```c
int result = 1;
printf("1: %s\n", result ? "TRUE":"FALSE"); // 输出:1: TRUE
result = 0;
printf("2: %s\n", result ? "TRUE":"FALSE"); // 输出:2: FALSE
```
相关问题
用c语言写一个输出关键路径 功能描述:根据创建的有向图及权重,调用TopologicalSort函数应用栈结构输出关键路径。 输出格式printf("Criticalpath: <%d,%d> length: %d\n", 开始节点数组index下标, 下一节点数组index下标, 权重);
好的,以下是用C语言编写输出关键路径的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_VERTEX_NUM 20
#define INFINITY 65535
typedef struct {
int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵
int indegree[MAX_VERTEX_NUM]; // 入度
int ve[MAX_VERTEX_NUM]; // 事件最早发生时间
int vl[MAX_VERTEX_NUM]; // 事件最迟发生时间
int path[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 路径
int vertex_num; // 顶点数
int arc_num; // 弧数
} Graph;
typedef struct {
int data[MAX_VERTEX_NUM];
int top;
} Stack;
// 初始化栈
void InitStack(Stack *stack) {
stack->top = -1;
}
// 判断栈是否为空
bool StackIsEmpty(Stack *stack) {
return stack->top == -1;
}
// 判断栈是否已满
bool StackIsFull(Stack *stack) {
return stack->top == MAX_VERTEX_NUM - 1;
}
// 入栈
void Push(Stack *stack, int data) {
if (StackIsFull(stack)) {
printf("Stack is full.\n");
exit(1);
}
stack->data[++stack->top] = data;
}
// 出栈
int Pop(Stack *stack) {
if (StackIsEmpty(stack)) {
printf("Stack is empty.\n");
exit(1);
}
return stack->data[stack->top--];
}
// 拓扑排序
bool TopologicalSort(Graph *graph, Stack *stack) {
int i, j, k, count = 0;
InitStack(stack);
for (i = 0; i < graph->vertex_num; i++) {
if (graph->indegree[i] == 0) {
Push(stack, i);
}
}
while (!StackIsEmpty(stack)) {
j = Pop(stack);
printf("%d ", j);
count++;
for (k = 0; k < graph->vertex_num; k++) {
if (graph->arc[j][k] != INFINITY) {
if (--graph->indegree[k] == 0) {
Push(stack, k);
}
if (graph->ve[j] + graph->arc[j][k] > graph->ve[k]) {
graph->ve[k] = graph->ve[j] + graph->arc[j][k];
}
}
}
}
if (count < graph->vertex_num) {
return false;
} else {
return true;
}
}
// 关键路径
void CriticalPath(Graph *graph) {
int i, j, k, e, l;
Stack stack;
bool result;
result = TopologicalSort(graph, &stack);
if (!result) {
printf("The graph has a circle.\n");
exit(1);
}
for (i = 0; i < graph->vertex_num; i++) {
graph->vl[i] = graph->ve[graph->vertex_num - 1];
}
while (!StackIsEmpty(&stack)) {
j = Pop(&stack);
for (k = 0; k < graph->vertex_num; k++) {
if (graph->arc[j][k] != INFINITY) {
if (graph->vl[k] - graph->arc[j][k] < graph->vl[j]) {
graph->vl[j] = graph->vl[k] - graph->arc[j][k];
}
}
}
}
for (i = 0; i < graph->vertex_num; i++) {
for (j = 0; j < graph->vertex_num; j++) {
graph->path[i][j] = -1;
}
}
for (i = 0; i < graph->vertex_num; i++) {
for (j = 0; j < graph->vertex_num; j++) {
if (graph->arc[i][j] != INFINITY) {
e = graph->ve[i];
l = graph->vl[j] - graph->arc[i][j];
if (e == l) {
printf("Criticalpath: <%d,%d> length: %d\n", i, j, graph->arc[i][j]);
}
}
}
}
}
int main() {
Graph graph;
int i, j, k, m, n, w;
printf("Please input the vertex number and the arc number: ");
scanf("%d %d", &graph.vertex_num, &graph.arc_num);
for (i = 0; i < graph.vertex_num; i++) {
for (j = 0; j < graph.vertex_num; j++) {
graph.arc[i][j] = INFINITY;
}
}
for (k = 0; k < graph.arc_num; k++) {
printf("Please input the start vertex, the next vertex and the weight: ");
scanf("%d %d %d", &m, &n, &w);
graph.arc[m][n] = w;
graph.indegree[n]++;
}
CriticalPath(&graph);
return 0;
}
```
这个程序通过邻接矩阵来存储有向图,使用拓扑排序来求出每个事件的最早发生时间和最晚发生时间,然后求出关键路径。其中,INFINITY 表示两个顶点之间没有连边。
enum isBOOL{ isFalse=0, isTrue }; enum Choose { TcpHeartbeat=200, TcpExeCmd }; // 定义结构体 struct DataPacket { int sockfd; enum Choose choose; char cmdBuf[BUFFER_SIZE]; char returnValue[BUFFER_SIZE]; }; struct DataPacket datapacket; struct DataPacket receivePackets; int SetUpTCPtoSendInformation(char option[], char **command) { static int isFirstCall = 1; pthread_mutex_lock(&mutex); // 加锁 if (isFirstCall && strstr(option, "set")) { if (TCPCommand != NULL) { free(TCPCommand); } TCPCommand = malloc(strlen(*command) + 1); if (TCPCommand == NULL) { printf("Failed to set and obtain TCP command variable memory allocation\n"); goto fail; } strcpy(TCPCommand, *command); printf("set:%s\n", TCPCommand); isFirstCall = 0; goto succeed; } else if (!isFirstCall && strstr(option, "get") && TCPCommand != NULL && strlen(TCPCommand)) { free(*command); *command = malloc(strlen(TCPCommand) + 1); strcpy(*command, TCPCommand); printf("get:%s\n", *command); memset(TCPCommand, '\0', strlen(TCPCommand)); free(TCPCommand); TCPCommand = NULL; isFirstCall = 1; goto succeed; } memset(*command, 0, sizeof(*command)); strcpy(*command, ""); fail: pthread_mutex_unlock(&mutex); // 解锁 return 0; succeed: pthread_mutex_unlock(&mutex); // 解锁 return 1; } memset(&receivePackets, 0, sizeof(receivePackets)); int dataLen = recv(fd, &receivePackets, sizeof(receivePackets), 0); char *SendString = NULL; int result = SetUpTCPtoSendInformation("get", &SendString); if (result && SendString != NULL && strlen(SendString) > 0)详细代码
根据你提供的代码,我注意到你使用了一个名为 `datapacket` 的结构体来存储发送数据包的信息,并使用 `recv` 函数接收数据包。
在之后的代码中,你调用了 `SetUpTCPtoSendInformation("get", &SendString)` 来获取要发送的字符串。然后,你检查了 `SendString` 是否为 `NULL` 并且长度是否大于零。
这是一个合理的逻辑检查。如果条件满足,你可以使用 `SendString` 的值来执行发送操作。
请注意,你还需要在调用 `recv` 函数之前,使用 `memset` 函数将接收数据包的结构体 `receivePackets` 初始化为零,以确保接收到的数据正确填充到结构体中。
以下是修改后的代码示例:
```c
enum isBOOL {
isFalse = 0,
isTrue
};
enum Choose {
TcpHeartbeat = 200,
TcpExeCmd
};
struct DataPacket {
int sockfd;
enum Choose choose;
char cmdBuf[BUFFER_SIZE];
char returnValue[BUFFER_SIZE];
};
struct DataPacket datapacket;
struct DataPacket receivePackets;
int SetUpTCPtoSendInformation(char option[], char **command) {
static int isFirstCall = 1;
pthread_mutex_lock(&mutex); // 加锁
if (isFirstCall && strstr(option, "set")) {
if (TCPCommand != NULL) {
free(TCPCommand);
}
TCPCommand = malloc(strlen(*command) + 1);
if (TCPCommand == NULL) {
printf("Failed to set and obtain TCP command variable memory allocation\n");
goto fail;
}
strcpy(TCPCommand, *command);
printf("set:%s\n", TCPCommand);
isFirstCall = 0;
goto succeed;
} else if (!isFirstCall && strstr(option, "get") && TCPCommand != NULL && strlen(TCPCommand)) {
free(*command);
*command = malloc(strlen(TCPCommand) + 1);
strcpy(*command, TCPCommand);
printf("get:%s\n", *command);
memset(TCPCommand, '\0', strlen(TCPCommand));
free(TCPCommand);
TCPCommand = NULL;
isFirstCall = 1;
goto succeed;
}
memset(*command, 0, sizeof(*command));
strcpy(*command, "");
fail:
pthread_mutex_unlock(&mutex); // 解锁
return 0;
succeed:
pthread_mutex_unlock(&mutex); // 解锁
return 1;
}
memset(&receivePackets, 0, sizeof(receivePackets)); // 初始化接收数据包的结构体
int dataLen = recv(fd, &receivePackets, sizeof(receivePackets), 0);
char *SendString = NULL;
int result = SetUpTCPtoSendInformation("get", &SendString);
if (result && SendString != NULL && strlen(SendString) > 0) {
// 在这里执行发送 SendString 的操作
printf("SendString: %s\n", SendString);
}
```
请确保你在使用 `SendString` 之前检查其是否为 `NULL`,以避免访问空指针错误。同时,根据你的实际需求,对发送操作进行相应的处理。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)