我有以下代码,而我的问题是我不知道如何在输入中设置EOF。我的意思是,当用户只是不想添加另一个输入时,如何结束输入序列。
#include <stdio.h>
#include <stdlib.h>
/* The main program */
int main(int argc, char *argv[])
while(ret == 0){
if(scanf("%i", &v) && (v >= -10000 && v <= 10000) == 1) {
numbers += 1;
value += v;
if (numbers == 1){
printf("%d",v);
}
else {
printf(", %d",v);
}
if (v <= Min){
Min = v;
}
if (v >= Max){
Max = v;
}
if (v > 0) {
kladne += 1;
}
if(v < 0){
zaporne += 1;
}
if (v % 2 == 0){
parne += 1;
}
else {
neparne += 1;
}
}
else {
printf("\nError: Vstup je mimo interval!\n");
ret = 100;
return ret;
}
}
prumer = (value / numbers);
Pkladne = (((float)kladne / numbers) * 100.00);
Pzaporne = (((float)zaporne / numbers) * 100.00);
Pparne = (((float)parne / numbers) * 100.00);
Pneparne = (((float)neparne / numbers) * 100.00);
printf("Pocet cisel: %d\n",numbers);
printf("Pocet kladnych: %d\n", kladne);
printf("Pocet zapornych: %d\n", zaporne);
printf("Procento kladnych: %0.2f\n", Pkladne);
printf("Procento zapornych: %0.2f\n", Pzaporne);
printf("Pocet sudych: %d\n", parne);
printf("Pocet lichych: %d\n", neparne);
printf("Procento sudych: %0.2f\n", Pparne);
printf("Procento lichych: %0.2f\n", Pneparne);
printf("Prumer: %0.2f\n", prumer);
printf("Maximum: %d\n", Max);
printf("Minimum: %d\n", Min);
return ret;
}
我已经解决了输入不是整数的情况。