1.用c写一个比较函数,有三个变量,分别为scanf_count,usart_count,scanf _count_old. 2.若usart_count,为0时。scanf_count和scanf_count_old比较。若scanf_count大于scanf_count_old,则scanf_count_old==scanf_count; 若usart_count,不为0时。scanf_count和usart_count比较。若scanf_count大于usart_count,则输出设备没有取完;
时间: 2023-05-29 07:06:45 浏览: 117
若scanf_count小于等于usart_count,则scanf_count_old==usart_count。
以下是比较函数的代码:
```c
int compare(int scanf_count, int usart_count, int scanf_count_old) {
if (usart_count == 0) {
if (scanf_count > scanf_count_old) {
scanf_count_old = scanf_count;
}
} else {
if (scanf_count > usart_count) {
printf("设备没有取完\n");
} else {
scanf_count_old = usart_count;
}
}
return scanf_count_old;
}
```
使用方法:
1. 将以上代码放入你的程序中。
2. 调用`compare()`函数,传入三个变量(`scanf_count`、`usart_count`、`scanf_count_old`),并接收返回值。
```c
scanf_count_old = compare(scanf_count, usart_count, scanf_count_old);
```
注意,`scanf_count_old`需要在调用函数之前定义并初始化。
完整示例:
```c
#include <stdio.h>
int compare(int scanf_count, int usart_count, int scanf_count_old);
int main() {
int scanf_count = 5;
int usart_count = 0;
int scanf_count_old = 0;
scanf_count_old = compare(scanf_count, usart_count, scanf_count_old);
printf("scanf_count_old = %d\n", scanf_count_old);
usart_count = 6;
scanf_count_old = compare(scanf_count, usart_count, scanf_count_old);
printf("scanf_count_old = %d\n", scanf_count_old);
scanf_count = 7;
scanf_count_old = compare(scanf_count, usart_count, scanf_count_old);
printf("scanf_count_old = %d\n", scanf_count_old);
return 0;
}
int compare(int scanf_count, int usart_count, int scanf_count_old) {
if (usart_count == 0) {
if (scanf_count > scanf_count_old) {
scanf_count_old = scanf_count;
}
} else {
if (scanf_count > usart_count) {
printf("设备没有取完\n");
} else {
scanf_count_old = usart_count;
}
}
return scanf_count_old;
}
```
输出结果:
```
scanf_count_old = 5
scanf_count_old = 6
scanf_count_old = 7
```
阅读全文