C Primer Plus_第8章_字符输入输出和输入确认_编程练习
生活随笔
收集整理的這篇文章主要介紹了
C Primer Plus_第8章_字符输入输出和输入确认_编程练习
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.題略
#include <stdio.h>int main(void) {int ch,i=0;printf("Please enter text here(end with Ctrl + Z):\n");while (ch=getchar() != EOF)i++;printf("There are %d characters in text.\n", i);return 0; }運行結果
輸入第一個Ctrl+Z時,并沒有結束,下一行再輸入Ctrl+Z才檢測到EOF。說明我的控制臺環(huán)境下,文件結尾形式是一行的開始位置Ctrl+Z,而不是任意位置的Ctrl+Z。
?
2.題略
/* */ #include <stdio.h> int main(void) {int i=0, ch;while ((ch = getchar()) != EOF){if (ch == '\n')printf("\\n%3d \n",ch); //換行符就是一個字符,同時它提示讀入行緩沖區(qū)中的數(shù)據(jù)else if (ch == '\t')printf("\\t%3d ",ch);else if ((ch < ' ') && (ch != '\n') && (ch != '\t'))printf("^%c%3d ",ch+64,ch);elseprintf("%-2c%3d ",ch,ch);i++;if(i%10 == 0)putchar('\n');} }?
3.題略
/*統(tǒng)計大小字母個數(shù)*/ #include <stdio.h> #include <ctype.h> //關聯(lián)函數(shù)isupper(),islower()int main(void) {int count_up = 0, count_low = 0;int count_other = 0;int ch;printf("Please enter some text: \n");while ((ch = getchar()) != EOF){if (isupper(ch)) //isupper(ch)函數(shù):ch是大寫字母的話,函數(shù)返回真值1count_up++;else if (islower(ch)) //islower(ch)函數(shù):ch是小寫字母的話,函數(shù)返回真值1count_low++;elsecount_other++;}printf("There are %d upper letters\n", count_up);printf("and %d lower letters\n", count_low);printf("and %d other letters\n", count_other);return 0; }運行結果
?
4.題略
/*報告單詞中的字母數(shù)*/ #include <stdio.h> #include <ctype.h>int main(void) {int CountLet = 0, CountWrd =0;int ch, ch_pre=' ';printf("Please enter some text (ctrl+z to quit): \n");while ((ch = getchar()) != EOF){if (isalpha(ch))CountLet++;if ((isspace(ch) || ispunct(ch)) && isalnum(ch_pre))CountWrd++;ch_pre = ch;}printf("There are %d letters and %d words.\n", CountLet, CountWrd);printf("There are %d letters in a word on average.\n", CountLet / CountWrd);return 0; }運行結果
?
5.題略
/*問大小后再猜數(shù)*/ #include <stdio.h> int main(void) {int min,max,mid,ch;printf("請輸入被猜整數(shù)的范圍:min(較小的數(shù))和max(較大的數(shù))\n");scanf("%d%d",&min,&max);printf("min = %d, max = %d\n",min, max);mid = min + (max-min)/2;printf("is it %d? (please enter y(yes), b(big), s(small))", mid);while((ch = getchar ()) != 'y'){if (ch == 'b'){max = mid;mid = min + (max-min)/2;printf("is it %d? (please enter y(yes), b(big), s(small))", mid);}else if (ch == 's'){min = mid;mid = min + (max-min)/2;printf("is it %d? (please enter y(yes), b(big), s(small))", mid);}else{if (ch == '\n')continue;elseprintf("Please just enter y, b, s.\n");}}printf("the number is %d!\n",mid);return 0; }自己寫的有點復雜了可能,不過運行起來還是可行的
?
6.題略
#include <stdio.h> #include <ctype.h>char get_first();int main(void) {printf("get_first() is %c", get_first()); }char get_first() {int ch;printf("Please enter some words:\n");while ((ch = getchar()) && (isspace(ch) == 0))continue;ch = getchar();return ch; }8.題略
/**/ #include<stdio.h> #include<ctype.h> float get_float(void); char get_first(void);int main(void) {char select;float num1,num2;while(1){printf("Enter the operation of your choice:\n");printf("a.add s.subtract:\n");printf("m.multiply d.divide\n");printf("q.quit\n");select = get_first();if( select != 'a' && select != 's' && select != 'm' && select != 'd'){printf("Bye.\n");break;}printf("Enter first number:");num1 = get_float();printf("Enter second number:");num2 = get_float();while( select == 'd' && num2 == 0) {printf("Enter a number other than 0:");num2 = get_float();}switch(select){case 'a': printf("%.2f + %.2f = %.2f\n",num1, num2, num1 + num2); break;case 's': printf("%.2f - %.2f = %.2f\n",num1, num2, num1 - num2); break;case 'm': printf("%.2f * %.2f = %.2f\n",num1, num2, num1 * num2); break;case 'd': printf("%.2f / %.2f = %.2f\n",num1, num2, num1 / num2); break;default : break;}}return(0); }float get_float(void) //得到一個合適的浮點數(shù),濾除非法數(shù) {float num;char str[40];while(scanf("%f",&num)!=1){gets(str);printf("%s is not a number.\n",str);printf("Please enter a numbe, such as 2.5, -1.78E8, or 3:");}while ( getchar() != '\n');return num; }char get_first(void) //得到字符串中的第一個字符,濾除其他字符 {int ch;while( isspace( ch = getchar() ) );while ( getchar() != '\n');return ch; }?
轉載于:https://www.cnblogs.com/TomLily/p/5819384.html
總結
以上是生活随笔為你收集整理的C Primer Plus_第8章_字符输入输出和输入确认_编程练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Conclusion
- 下一篇: PHP函数之日期时间函数date()使用