下列代码在输入能耗之后回车会直接输出最终结果无法输入CCM值,请改良下列代码。#include <stdio.h> // 定义各项标准的等级划分 #define CADR_LEVEL_1 350 #define CADR_LEVEL_2 450 #define CADR_LEVEL_3 600 #define NOISE_LEVEL_1 30 #define NOISE_LEVEL_2 40 #define NOISE_LEVEL_3 50 #define POWER_CONSUMPTION_LEVEL_1 0.025 #define POWER_CONSUMPTION_LEVEL_2 0.04 #define POWER_CONSUMPTION_LEVEL_3 0.07 #define CCM_LEVEL_1 10 #define CCM_LEVEL_2 13 #define CCM_LEVEL_3 16 // 定义评价函数 int evaluate(int cadr, int noise, int power_consumption, int ccm) { int cadr_level = 0, noise_level = 0, power_consumption_level = 0, ccm_level = 0; // 判断 CADR 级别 if (cadr >= CADR_LEVEL_3) { cadr_level = 3; } else if (cadr >= CADR_LEVEL_2) { cadr_level = 2; } else if (cadr >= CADR_LEVEL_1) { cadr_level = 1; } // 判断噪音级别 if (noise <= NOISE_LEVEL_1) { noise_level = 3; } else if (noise <= NOISE_LEVEL_2) { noise_level = 2; } else if (noise <= NOISE_LEVEL_3) { noise_level = 1; } // 判断能耗级别 if (power_consumption <= POWER_CONSUMPTION_LEVEL_1) { power_consumption_level = 3; } else if (power_consumption <= POWER_CONSUMPTION_LEVEL_2) { power_consumption_level = 2; } else if (power_consumption <= POWER_CONSUMPTION_LEVEL_3) { power_consumption_level = 1; } // 判断 CCM 级别 if (ccm >= CCM_LEVEL_3) { ccm_level = 3; } else if (ccm >= CCM_LEVEL_2) { ccm_level = 2; } else if (ccm >= CCM_LEVEL_1) { ccm_level = 1; } // 计算综合评价 int total_evaluation = cadr_level+noise_level + power_consumption_level + ccm_level; return total_evaluation; } int main() { int cadr,noise, power_consumption, ccm; // 读取各项参数值 printf("请输入 CADR 值:"); scanf("%d", &cadr); printf("请输入噪音值:"); scanf("%d", &noise); printf("请输入能耗值:"); scanf("%d", &power_consumption); printf("请输入 CCM 值:"); scanf("%d", &ccm); // 计算评价结果 int evaluation = evaluate(cadr,noise, power_consumption, ccm); printf("总评价等级为:%d\n", evaluation); return 0; }
时间: 2023-06-14 20:07:11 浏览: 135
cpp代码-从键盘输入一串字符,以回车键结束,分别统计输入的字母、数字和其它字符的个数
可以使用fgets函数读取输入的CCM值,然后使用sscanf函数将读取的字符串转化为整数类型。修改后的代码如下:
```c
#include <stdio.h>
#define CADR_LEVEL_1 350
#define CADR_LEVEL_2 450
#define CADR_LEVEL_3 600
#define NOISE_LEVEL_1 30
#define NOISE_LEVEL_2 40
#define NOISE_LEVEL_3 50
#define POWER_CONSUMPTION_LEVEL_1 0.025
#define POWER_CONSUMPTION_LEVEL_2 0.04
#define POWER_CONSUMPTION_LEVEL_3 0.07
#define CCM_LEVEL_1 10
#define CCM_LEVEL_2 13
#define CCM_LEVEL_3 16
int evaluate(int cadr, int noise, int power_consumption, int ccm) {
int cadr_level = 0, noise_level = 0, power_consumption_level = 0, ccm_level = 0;
if (cadr >= CADR_LEVEL_3) {
cadr_level = 3;
} else if (cadr >= CADR_LEVEL_2) {
cadr_level = 2;
} else if (cadr >= CADR_LEVEL_1) {
cadr_level = 1;
}
if (noise <= NOISE_LEVEL_1) {
noise_level = 3;
} else if (noise <= NOISE_LEVEL_2) {
noise_level = 2;
} else if (noise <= NOISE_LEVEL_3) {
noise_level = 1;
}
if (power_consumption <= POWER_CONSUMPTION_LEVEL_1) {
power_consumption_level = 3;
} else if (power_consumption <= POWER_CONSUMPTION_LEVEL_2) {
power_consumption_level = 2;
} else if (power_consumption <= POWER_CONSUMPTION_LEVEL_3) {
power_consumption_level = 1;
}
if (ccm >= CCM_LEVEL_3) {
ccm_level = 3;
} else if (ccm >= CCM_LEVEL_2) {
ccm_level = 2;
} else if (ccm >= CCM_LEVEL_1) {
ccm_level = 1;
}
int total_evaluation = cadr_level + noise_level + power_consumption_level + ccm_level;
return total_evaluation;
}
int main() {
int cadr, noise, power_consumption, ccm;
char ccm_str[100];
printf("请输入 CADR 值:");
scanf("%d", &cadr);
printf("请输入噪音值:");
scanf("%d", &noise);
printf("请输入能耗值:");
scanf("%d", &power_consumption);
printf("请输入 CCM 值:");
fgets(ccm_str, 100, stdin);
sscanf(ccm_str, "%d", &ccm);
int evaluation = evaluate(cadr, noise, power_consumption, ccm);
printf("总评价等级为:%d\n", evaluation);
return 0;
}
```
阅读全文