前言:这章是昨日刚打完的,其实第五题不是很满意,我也有点搞的一头雾水,当然其他章肯定有点小细节问题我还没发现,突然想起作者写到的有句话“不管你的程序提示打完多好,总会有人吐槽这个程序有多烂”,前几个我输入验证做的不是很完美(其实是当时有点烦就懒得做了),如被小伙伴学习参考,My pleasure.
仅供参考,新手勿喷。
#include <stdio.h> #include <string.h> int main(void) { char ch; int count = 0; while((ch = getchar()) != EOF) //这里会把换行符也读取 count++; printf("%d",count); }
#include <stdio.h> int main() { char ch; int count = 0; while((ch = getchar()) != EOF) //有个问题就是一换行就触发了 { count++; if(ch < ' ') { if(ch == '\t') printf("\\t-%d ",ch); else if(ch == '\n') printf("\\n-%d ",ch); else printf("^%c-%d ",ch+64,ch); } else printf("%c-%d ",ch,ch); count%10 == 0 ? count=0,printf("\n") : count ; } }
#include <stdio.h> #include <ctype.h> int main(void) { char ch; int lower_count=0,upper_count=0; while((ch = getchar()) != EOF) { if(islower(ch)) lower_count++; else if(isupper(ch)) upper_count++; } printf("upper:%d lower:%d",lower_count,upper_count); }
#include <stdio.h> #include <ctype.h> int main(void) { int later_count,word_count,flag; char ch; later_count=word_count=flag=0; while((ch = getchar()) != EOF) { if((isupper(ch) || islower(ch)) && !ispunct(ch)) { later_count++; flag = 1; } else if(isspace(ch) && flag == 1) { word_count++; flag = 0; } } printf("later:%d word:%d avg:%f",later_count,word_count,(float)later_count/(float)word_count); }
#include <stdio.h> int main(void) { int low=0,high=100; int guess = (high+low)/2; char ch; printf("Pick an integer from 1 to 100. I will try to guess "); printf("it.\nRespond with a y if my guess is right and with"); printf("\nan n if it is wrong.\n"); printf("Uh...is your number %d?\n", guess); while ((ch = getchar()) != 'y') /* get response, compare to y */ { if(ch == 'h') { low = 0; high = guess; guess = (low+high)/2; printf("Well, then, is it %d?\n", guess); } else if(ch == 'l') { low = guess; high = 100; guess = (low+high)/2; printf("Well, then, is it %d?\n", guess); } while(getchar() != '\n') continue; } printf("I knew I could do it!\n"); return 0; }
#include <stdio.h> #include <ctype.h> int main(void) { char ch; while( isspace(ch = getchar()) ) //获取第一个非空字符 continue; putchar(ch); //输出第一个字符 while(getchar() != '\n') //将多余的缓冲区输入排除 continue; }
这个是修改之前的程序,所以有些宏定义还留着。
#include <stdio.h> #include <ctype.h> #define SALARY 10 #define OVERTIME 40 #define Q300 0.15 #define X150 0.2 #define OM 0.25 #define r1 8.75 #define r2 9.33 #define r3 10.00 #define r4 11.20 void Print(void); char get_choice(void); char get_first(void); int main(void) { float money; int ho; char choice; float sa; while((choice = get_choice()) != 'q') { switch (choice) { case 'a': sa = r1; break; case 'b': sa = r2; break; case 'c': sa = r3; break; case 'd': sa = r4; break; } printf("请输入你工作了多少小时:"); scanf("%d",&ho); if(ho > 40) money = (ho-40)*1.5*sa + 40*sa; else money = ho*sa; if(money <= 300) money = money - Q300 * money; else if(money <= 450) money = money - ((money-300)*X150 + 300 * Q300); else money = money - ((money-450)*OM + 150*X150 + 300*Q300); printf("你的工资为%f\n",money); } printf("bye!"); } void Print(void) { printf("*****************************************************************\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/h d) $11.20/hr\n"); printf("q) quit\n"); printf("*****************************************************************\n"); printf("请选择:"); } char get_first(void) { char ch; while( isspace(ch = getchar()) ) //获取第一个非空字符 continue; //putchar(ch); //输出第一个字符 while(getchar() != '\n') //将多余的缓冲区输入排除 continue; return ch; } char get_choice(void) { char ch; Print(); ch = get_first(); while( (ch < 'a' || ch > 'd') && ch != 'q' ) { printf("please respond with a, b, c, d, or q!"); ch = get_first(); } return ch; }
#include <stdio.h> #include <ctype.h> char get_choice(void); char get_first(void); float get_float(char choice); int main(void) { float a,b; char choice; while( (choice = get_choice()) != 'q') { printf("Enter first number:"); a = get_float(choice); printf("Enter second number:"); b = get_float(choice); switch(choice) { case 'a': printf("%f + %f = %f\n",a,b,a+b); break; case 's': printf("%f - %f = %f\n",a,b,a-b); break; case 'm': printf("%f * %f = %f\n",a,b,a*b); break; case 'd': printf("%f / %f = %f\n",a,b,a/b); break; default: printf("program error!\n"); } } printf("bye!"); return 0; } char get_choice(void) { char ch; printf("**********************************\n"); printf("Enter the operation of your choice\n"); printf("a. add s. subtract\n"); printf("m. multiply d. divide\n"); printf("q. quit\n"); printf("**********************************\n"); ch = get_first(); while( ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q') { printf("please respond a, s, m, d or q\n"); ch = get_first(); } return ch; } char get_first(void) { char ch; while( isspace(ch = getchar()) ) //选到第一个非空字符 continue; while(getchar() != '\n') //去掉多余字符 continue; return ch; } float get_float(char choice) { float input; char ch; while(scanf("%f",&input) != 1) { while((ch = getchar()) != '\n') putchar(ch); printf(" is not an num.\nplease enter an num value,such as 2, -178 or 3.5:"); } if(choice == 'd' && input == 0 ) { printf("Enter a number other than 0:"); input = get_float(choice); //重新调用这个函数,当里面返回了正确的值要记得接受再向上返回 } return input; }