int guess = (high + low) / 2; #注:中位数等于大数加小数除以2
printf("Please put a integer from 0 to 100\n");
printf("Is this number %d?\n",guess);
printf("s means too small, b means too big, y means the answer is correct.");
while ((answer=getchar()) != 'y')
switch (answer) #注:除了switch语句还可以用if语句
case '\n': #注:getchar每次按下回车都会输入一个\n所以有必要添加这个case 不然会多出default中的语句。
printf("Well, is it %d?", guess);
printf("Well, is it %d?", guess);
printf("Please answer with 's', 'b' or 'y'");
根据解释如果在 ch=getchar();中键入了 abc,return ch时 ch = a,其他输入会被清除。
当输入abc时,第一个字符会被直接赋值给ch 也就是 ch = a,其余的 bc和外加回车的 \n会进入缓存区。
当程序运行到 while (getchar() != '\n') 时 getchar()会从缓冲区读取字符 也就是剩下的 bc\n 用于判读并清除。
于是之后首字母a得以保留,这也是 为什么不用 while((ch=getchar()) != '\n')的原因,如果用了,a也应该会被清除。
printf("Please enter some characters ('#' to quit):\n");
while ((ch = get_first()) != '#')
printf("You can enter again ('#' to quit):\n");
}
} while (isspace(ch)); #注:do while循环一定会执行一次,如果输入的是空格,制表符,换行符,就会一直循环直到输入字符为止。
while (getchar() != '\n')#注:用来去除除首字符外所有缓存中的数据。
int get_first(void); #注:如果需要用字符来表示选项,这步很重要,这个函数需要牢记
float income = 0.0, tax = 0.0;
while ((ch=get_first()) !='q')
printf("Please enter how many hours you worked:");
printf("Please enter how many hours you worked:");
printf("Please enter how many hours you worked:");
printf("Please enter how many hours you worked:");
printf("Please enter letter between a~q");
float income = 0.0, tax = 0.0;
tax = 300 * rate_300 + 150 * rate_150 + (income - 450) * rate_l;
income = a * (40 + (b - 40) * 1.5);
tax = 300 * rate_300 + 150 * rate_150 + (income - 450) * rate_l;
printf("This week your income is $%g, tax is $%g, net income $%g\n",
printf("*****************************************************************\n\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("a) $8.75/hr b) $9.33/hr\n");
printf("c) $10.00/hr d) $11.20/hr\n");
printf("*****************************************************************\n\n");
while ((ch = get_first()) != 'q')
if (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd')
printf("Please enter the letter show on the menus\n");
printf("Enter first number:");
printf("Enter second number:");
printf("%g + %g = %g\n", a, b, reslut);
printf("%g - %g = %g\n", a, b, reslut);
printf("%g * %g = %g\n", a, b, reslut);
printf("%g / %g = %g\n", a, b, reslut);
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
while (scanf("%f", &input) != 1)
while ((ch = getchar()) != '\n') #注:如果上方是scanf,输入后getchar可以继承scanf存储在缓存中的值,从而无需输入直接判定,由于scanf输入后每次都会有回车,所以当getchar读取到末尾后,循环结束。
printf(" is not a number.\nPlease enter a number, such as 2.5, -1.78E8, or 3:");
加油!