此代码一直工作到可见光谱的末尾,但是当您键入小数点时,它会显示错误的结果:
#include <stdio.h>
#define SENTINEL 0
int main ()
{
int wavelength=0, power=0;
printf("Enter wavelength using scientific notation (e.g. 3E-9) >> 3 x 10^-9 // Enter 0 to quit: ");
scanf("%dE %d", &wavelength, &power);
while (wavelength!=SENTINEL)
{printf("nYou inputted : %dE%d which is %d x 10^%d and belong to the region of ", wavelength, power, wavelength, power);
if(wavelength==3 && power>=-11 && power<-9)printf("Gamma Ray.");
else if(wavelength==3&&power==-9){printf("X-ray.");}
else if(wavelength==4&&power==-7){printf("Ultraviolet.");}
else if(wavelength==7&&power==-7){printf("Visible Spectrum.");}
else if(wavelength=1.4&&power==-5){printf("Infrared.");}
else if(wavelength==.1&&power==-1){printf("Microwaves.");}
else printf("Radiowaves");
printf("nnEnter wavelength using scientific notation (e.g. 3E-9) >> 3 x 10^-9 // Enter 0 to quit: ");
scanf("%dE %d", &wavelength, &power);}
}
输出:
You inputted : 1E0 which is 1 x 10^0 and belong to the region of Radiowaves
Enter wavelength using scientific notation (e.g. 3E-9) >> 3 x 10^-9 // Enter 0 to quit:
我知道它应该是双精度的,这样它就可以存储十进制值,但是当我尝试它时,它显示了更多错误的结果:有人可以帮助我吗?
#define SENTINEL 0
int main ()
{
double wavelength=0, power=0;
printf("Enter wavelength using scientific notation (e.g. 3E-9) >> 3 x 10^-9 // Enter 0 to quit: ");
scanf("%lfE %lf", &wavelength, &power);
while (wavelength!=SENTINEL)
{printf("nYou inputted : %.2lfE%.2lf which is %.2lf x 10^%.2lf and belong to the region of ", wavelength, power, wavelength, power);
if(wavelength==3 && power>=-11 && power<-9)printf("Gamma Ray.");
else if(wavelength==3&&power==-9){printf("X-ray.");}
else if(wavelength==4&&power==-7){printf("Ultraviolet.");}
else if(wavelength==7&&power==-7){printf("Visible Spectrum.");}
else if(wavelength=1.4&&power==-5){printf("Infrared.");}
else if(wavelength==.1&&power==-1){printf("Microwaves.");}
else printf("Radiowaves");
printf("nnEnter wavelength using scientific notation (e.g. 3E-9) >> 3 x 10^-9 // Enter 0 to quit: ");
scanf("%lfE %lf", &wavelength, &power);}
}
输出:
Enter wavelength using scientific notation (e.g. 3E-9) >> 3 x 10^-9 // Enter 0 to quit: 1.4E-5
You inputted : 0.00E0.00 which is 0.00 x 10^0.00 and belong to the region of Radiowaves
Enter wavelength using scientific notation (e.g. 3E-9) >> 3 x 10^-9 // Enter 0 to quit:
输出应该是红外线。你们如何帮助:)
> 它并不漂亮。你必须调整它。
#define SENTINEL 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
double wavelength = 0, power = 0;
char line[20];
void read_base_exponent_double(char *line)
{
char *ptr = line;
size_t n;
char base_buf[10];
char power_buf[10];
while (*ptr != 'E') ptr++;
n = ptr - line;
strncat(base_buf, line, n);
wavelength = strtod(base_buf, NULL);
ptr++;
strcat(power_buf, ptr);
power = strtod(power_buf, NULL);
}
int main(void)
{
printf("Enter wavelength using scientific notation (e.g. 3E-9) >> 3 x 10^-9"
"// Enter 0 to quit: ");
fgets(line, 20, stdin);
read_base_exponent_double(line);
while (wavelength != SENTINEL) {
printf("nYou inputted : %.2lfE%.2lf which is %.2lf x 10^%.2lf "
"and belong to the region of ",
wavelength, power, wavelength, power);
if (wavelength == 3 && power >= -11 && power < -9) {
printf("Gamma Ray.");
} else if (wavelength == 3 && power == -9) {
printf("X-ray.");
} else if (wavelength == 4 && power == -7) {
printf("Ultraviolet.");
} else if (wavelength == 7 && power == -7) {
printf("Visible Spectrum.");
} else if (wavelength = 1.4 && power == -5) {
printf("Infrared.");
} else if (wavelength == .1 && power == -1) {
printf("Microwaves.");
} else
printf("Radiowaves");
printf("nnEnter wavelength using scientific notation (e.g. 3E-9) >> 3 x "
"10^-9 // Enter 0 to quit: ");
fgets(line, 20, stdin);
read_base_exponent_double(line);
}
return 0;
}
试试这个。
#define SENTINEL 0
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
double wavelength = 0.0, power = 0.0;
char line[20];
void read_base_exponent_double(char *line)
{
const char delimeter[] = "Ee ";
char *token = strtok(line, delimeter);
int counter = 0;
while (token != NULL) {
if (isdigit(token[0]) && counter == 0) {
counter++;
wavelength = strtod(token, NULL);
} else if ((isdigit(token[0]) || token[0] == '-') && counter == 1) {
counter++;
power = strtod(token, NULL);
}
token = strtok(NULL, delimeter);
}
}
int main(void)
{
printf("Enter wavelength using scientific notation (e.g. 3E-9) >> 3 x 10^-9"
"// Enter 0 to quit: ");
fgets(line, 20, stdin);
read_base_exponent_double(line);
while (wavelength != SENTINEL) {
printf("nYou inputted : %.2lfE%.2lf which is %.2lf x 10^%.2lf "
"and belong to the region of ",
wavelength, power, wavelength, power);
if (wavelength == 3 && power >= -11 && power < -9) {
printf("Gamma Ray.");
} else if (wavelength == 3 && power == -9) {
printf("X-ray.");
} else if (wavelength == 4 && power == -7) {
printf("Ultraviolet.");
} else if (wavelength == 7 && power == -7) {
printf("Visible Spectrum.");
} else if (wavelength == 1.4 && power == -5) {
printf("Infrared.");
} else if (wavelength == 0.1 && power == -1) {
printf("Microwaves.");
} else {
printf("Radiowaves");
}
printf("nnEnter wavelength using scientific notation (e.g. 3E-9) >> 3 x "
"10^-9 // Enter 0 to quit: ");
fgets(line, 20, stdin);
read_base_exponent_double(line);
}
return 0;
}
你的扫描行是错误的。也许使用 fget 来读取该行,而不是使用 atof 来读取下一个双精度值。