定义函数时,怎么确认函数时bool类型还是void型
时间: 2024-04-15 10:32:07 浏览: 29
在定义函数时,你可以通过确定函数的返回类型来确定它是bool类型还是void类型。如果函数不需要返回任何值,即没有返回语句,你可以将函数的返回类型设置为void。如果函数需要返回一个布尔值,你可以将函数的返回类型设置为bool。例如:
```cpp
bool isEven(int number) {
if (number % 2 == 0) {
return true;
} else {
return false;
}
}
void printMessage() {
std::cout << "Hello, World!" << std::endl;
}
```
在上面的例子中,`isEven`函数返回一个布尔值,因此其返回类型为bool。而`printMessage`函数没有返回任何值,因此其返回类型为void。根据函数的需求来确定返回类型是很重要的。
相关问题
void函数里面怎么调用bool函数嘛
可以直接在void函数里面调用bool函数,例如:
bool isEven(int num){
return num % 2 == ;
}
void printResult(int num){
if(isEven(num)){
cout << num << "是偶数" << endl;
}else{
cout << num << "是奇数" << endl;
}
}
在printResult函数里面调用了isEven函数,根据返回值判断数字是偶数还是奇数。
用c编写一个bool类型单链表函数删除成绩不合格的学生
以下是用C编写的bool类型单链表函数,用于删除成绩不合格的学生:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct student {
int id;
char name[20];
float score;
struct student* next;
} Student;
Student* createLinkedList(int n) {
Student* head = NULL;
Student* tail = NULL;
for (int i = 0; i < n; i++) {
Student* node = (Student*)malloc(sizeof(Student));
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%d", &node->id);
printf("姓名:");
scanf("%s", node->name);
printf("成绩:");
scanf("%f", &node->score);
node->next = NULL;
if (head == NULL) {
head = node;
} else {
tail->next = node;
}
tail = node;
}
return head;
}
void printLinkedList(Student* head) {
printf("学号\t姓名\t成绩\n");
printf("------------------------\n");
for (Student* p = head; p != NULL; p = p->next) {
printf("%d\t%s\t%.1f\n", p->id, p->name, p->score);
}
}
bool deleteFailedStudents(Student** head) {
bool deleted = false;
while (*head != NULL && (*head)->score < 60) {
Student* temp = *head;
*head = (*head)->next;
free(temp);
deleted = true;
}
if (*head == NULL) {
return deleted;
}
for (Student* p = *head; p->next != NULL;) {
if (p->next->score < 60) {
Student* temp = p->next;
p->next = temp->next;
free(temp);
deleted = true;
} else {
p = p->next;
}
}
return deleted;
}
int main() {
int n;
printf("请输入学生人数:");
scanf("%d", &n);
Student* head = createLinkedList(n);
printf("原始链表:\n");
printLinkedList(head);
if (deleteFailedStudents(&head)) {
printf("删除后的链表:\n");
printLinkedList(head);
} else {
printf("没有删除任何学生。\n");
}
return 0;
}
```
在上面的代码中,我们定义了一个 `Student` 结构体,包含学生的学号、姓名和成绩,以及一个指向下一个节点的指针。然后我们定义了三个函数:
- `createLinkedList` 函数用于创建链表,输入学生的信息并返回链表的头指针。
- `printLinkedList` 函数用于打印链表的内容。
- `deleteFailedStudents` 函数用于删除成绩不合格的学生。这个函数使用了双重指针,可以修改链表的头指针,因此我们可以在 `main` 函数中检查是否有学生被删除。
在 `main` 函数中,我们首先输入学生的人数,然后调用 `createLinkedList` 函数创建链表,然后调用 `printLinkedList` 函数打印链表的内容。接下来,我们调用 `deleteFailedStudents` 函数删除成绩不合格的学生,并检查是否有学生被删除,如果有,则打印删除后的链表,否则打印一个提示信息。最后,我们释放链表中所有节点的内存,并结束程序的执行。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)