上述代码怎么改输出结果是19?
时间: 2024-03-19 20:43:40 浏览: 28
要将原代码的输出结果改为 `19`,可以将宏定义 `S(x)` 的展开改为 `(x) + (x)`,即相加而不是相乘。这样在 `main()` 函数中使用 `S(k + 4)` 时,会先将 `k + 4` 和 `x` 相加,再将结果和 `k` 相加,得到 `3 + 4 + 3 = 10`,即输出结果为 `10`。
修复后的代码如下所示:
```c
#include <stdio.h>
#define S(x) (x) + (x)
int main() {
int a, k = 3;
a = S(k + 4);
printf("%d\n", a);
return 0;
}
```
输出结果为 `10`。如果要输出结果为 `19`,可以修改 `S(x)` 的展开为 `(x) * 2 - 1`,这样在 `main()` 函数中使用 `S(k + 4)` 时,会先将 `k + 4` 和 `x` 相加,再将结果乘以 2,最后减去 1,得到 `2 * (3 + 4) - 1 = 14 - 1 = 13`,即输出结果为 `13`。
修复后的代码如下所示:
```c
#include <stdio.h>
#define S(x) (x) * 2 - 1
int main() {
int a, k = 3;
a = S(k + 4);
printf("%d\n", a);
return 0;
}
```
输出结果为 `13`。如果还有其他的要求,请提供更多信息。
相关问题
输出含有注释的上述代码
注释已添加在代码中,如下所示:
```c
#include <reg52.h>
#include <onewire.h>
unsigned char duanma[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; // 数码管常规数字显示数组
unsigned char duanma_x[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; // 数码管小数位数字显示数组
unsigned int temp=0; // 当前温度值
unsigned int temp_h=0; // 最高温度值
unsigned int temp_l=30; // 最低温度值
void Delay_SMG(unsigned int t) {
while(t--);
}
void SelectHC573(unsigned char n) {
switch(n) {
case 0 : P2 = (P2 & 0x1f ) | 0x00; break;
case 4 : P2 = (P2 & 0x1f ) | 0x80; break;
case 5 : P2 = (P2 & 0x1f ) | 0xa0; break;
case 6 : P2 = (P2 & 0x1f ) | 0xc0; break;
case 7 : P2 = (P2 & 0x1f ) | 0xe0; break;
}
}
void DisplaySMG_Bit(unsigned char pos,unsigned char dat) {
P2=0xE0;P0=0xff;//先全部关掉数码管,避免显示不正常
P2=0xC0;P0=0x01<<pos;
P2=0xE0;P0=dat;
}
void DisplaySMG_temp() {
DisplaySMG_Bit(1,duanma[temp%10]); // 显示个位数字
Delay_SMG(100);
DisplaySMG_Bit(0,duanma[temp/10]); // 显示十位数字
Delay_SMG(100);
DisplaySMG_Bit(5,0xbf); // 显示小数点
Delay_SMG(100);
DisplaySMG_Bit(7,duanma[temp_l%10]); // 显示小数位个位数字
Delay_SMG(100);
DisplaySMG_Bit(6,duanma[temp_l/10]); // 显示小数位十位数字
Delay_SMG(100);
DisplaySMG_Bit(2,0xbf); // 显示小数点
Delay_SMG(100);
DisplaySMG_Bit(4,duanma[temp_h%10]); // 显示最高温度个位数字
Delay_SMG(100);
DisplaySMG_Bit(3,duanma[temp_h/10]); // 显示最高温度十位数字
Delay_SMG(100);
P2=0xC0;P0=0xff; // 关闭所有数码管的显示
P2=0xE0;P0=0xff;
}
void Delay(unsigned int t) {
while(t--) {
DisplaySMG_temp();
}
}
void Read_DS18B20_temp() {
unsigned char LSB,MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay(1000);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB=Read_DS18B20();
MSB=Read_DS18B20();
temp=MSB;
temp=(temp<<8)|LSB;
temp>>=4;
}
void open_buzz() {
P0|=0x40;
SelectHC573(5);
SelectHC573(0);
}
void close_buzz() {
P0&=0xbf;
SelectHC573(5);
SelectHC573(0);
}
void main() {
P2=0x80;P0=0xff;
while(1) {
Read_DS18B20_temp(); // 读取温度值
if(temp>temp_h) { // 更新最高温度值
temp_h=temp;
}
if(temp_l>temp) { // 更新最低温度值
temp_l=temp;
}
if(temp>=30) { // 当温度大于等于30度时,打开蜂鸣器
open_buzz();
}else if(temp<30) { // 当温度小于30度时,关闭蜂鸣器
close_buzz();
}
DisplaySMG_temp(); // 显示温度值和最高/低温度值
}
}
```
给出上述代码的一个输入输出样例
输入样例:
```
5 8
A B C D E
A B 1
A C 2
A D 3
B C 4
B D 5
B E 6
C D 7
D E 8
```
输出样例:
```
(A,B)(B,C)(A,D)(D,E)
19
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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_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)