【例3-1】简单的猜数游戏。输入你所猜的整数(假定1~100内),与计算机产生的被猜数比较,若相等,显示猜中;若不等,显示与被猜数的大小关系。
/*简单的猜数游戏*/
#include
int main(void)
{int mynumber=38;int yournumber;printf("Input your number:");scanf("%d",&yournumber);if(yournumber=mynumber)printf("Ok!you are right!\n");elseif(yournumber>mynumber)printf("Sorry!your number is bigger than my number!\n");elseprintf("Sorry!your number is smaller than my number!\n");return 0;
}
【例3-2】输入一个整数,判断该数是奇数还是偶数。
/*判断是奇数还是偶数*/
#include
int main(void)
{int number;printf("Enter a number:");scanf("%d",&number);if(number%2==0){printf("The number is even.\n");}else{printf("The number is odd.\n");}return 0;
}
【例3-3】输入一个正整数n,再输入n个学生的成绩,计算平均分,并统计不及格成绩的学生人数。
/* 输入一批学生的成绩,计算平均分,并统计不及格成绩的学生人数*/
#include
int main(void)
{int count,i,n;double grade,total;printf("Enter n:");scanf("%d",&n);total&#61;0;count&#61;0;for(i&#61;1;i<&#61;n;i&#43;&#43;){printf("Enter grade#%d:",i);scanf("%lf",&grade);total&#61;total&#43;grade;if(grade<60){count&#43;&#43;;}}printf("Grade average&#61;%.2f\n",total/n);printf("Number of failures&#61;%d\n",count);return 0;
}
【例3-4】继续讨论例2-4中提出的分段计算水费问题。
/*计算多分段函数*/
#include
int main(void)
{double x,y;printf("Enter x:");scanf("%lf",&x);if(x<0){y&#61;0;}else if(x<&#61;15){y&#61;4*x/3;}else{y&#61;2.5*x-10.5;}printf("f(%.2f)&#61;%.2f\n",x,y);return 0;
}
【例3-5】求解简单的四则运算。
/*求解简单的四则运算*/
#include
int main(void)
{double value1,value2;char op;printf("Type in an expression:");scanf("%lf%c%lf",&value1,&op,&value2);if(op&#61;&#61;&#39;&#43;&#39;)printf("&#61;%.2f\n",value1&#43;value2);else if(op&#61;&#61;&#39;-&#39;)printf("&#61;%.2f\n",value1-value2);else if(op&#61;&#61;&#39;/&#39;)printf("%.2f\n",value1/value2);elseprintf("Unknown operator\n");return 0;
}
【3-6】略
【3-7】输入10个字符&#xff0c;统计其中英文字母&#xff0c;数字字符和其他字符个数。
/* 统计字符&#xff0c;包括英文字母、数字字母和其他字母的个数 */
#include
int main(void)
{int digit,letter,other;char ch;int i;digit&#61;letter&#61;other&#61;0;printf("Enter 10 characters:");for(i&#61;1;i<&#61;10;i&#43;&#43;){ch&#61;getchar();if(ch>&#61;&#39;a&#39;&& ch<&#61;&#39;z&#39;)//(ch>&#61;&#39;A&#39;&& ch<&#61;&#39;Z&#39;))letter&#43;&#43;;else if(ch>&#61;&#39;0&#39;&& ch<&#61;&#39;9&#39;)digit&#43;&#43;;elseother&#43;&#43;;}printf("letter&#61;%d,digit&#61;%d,other&#61;%d\n",letter,digit,other);return 0;
}
【例3-8】查询自动售货机中商品的价格。
/* 查询自动售货机中商品的价格 */
#include
int main(void)
{int choice,i;double price;for(i&#61;1;i<&#61;5;i&#43;&#43;){printf("[1]Select crisps\n");printf("[2]Select popcorn\n");printf("[3]Select chocolate\n");printf("[4]Select cola\n");printf("[0]exit\n");printf("Enter choice:");scanf("%d",&choice);if(choice&#61;&#61;0)break;switch(choice){case1:price&#61;3.0;break;case2:price&#61;2.5;break;case3:price&#61;4.0;break;case4:price&#61;3.5;break;default:price&#61;0.0;break;}}printf("Thanks\n");return 0;
}
【3-9】求解简单表达式。
/*求解简单的四则运算表达式*/
#include
int main(void)
{double value1,value2;char op;printf("Type in an expression:");scanf("%lf%c%lf",&value1,&op,&value2);switch(op){case&#39;&#43;&#39;:printf("&#61;%.2f\n",value1&#43;value2);break;case&#39;-&#39;:printf("&#61;%.2f\n",value1-value2);break;case&#39;*&#39;:printf("&#61;%.2f\n",value1*value2);case&#39;/&#39;:printf("&#61;%.2f\n",value1/value2);break;default:printf("Unknown operator\n");break;}return 0;
}
【3-10】输入10个字符&#xff0c;分别统计出其中空格或回车&#xff0c;数字字符和其他字符的个数。
/* 统计字符&#xff0c;包括空格或回车、数字字符和其他字符的个数 */
#include
int main(void)
{int blank,digit,other;char ch;int i;blank&#61;digit&#61;other&#61;0;printf("Enter 10 characters:");for(i&#61;1;i<&#61;10;i&#43;&#43;){ch&#61;getchar();switch(ch){case&#39; &#39;:case&#39;\n&#39;:blank&#43;&#43;;break;case&#39;0&#39;: case&#39;1&#39;: case&#39;2&#39;: case&#39;3&#39;: case&#39;4&#39;:case&#39;5&#39;: case&#39;6&#39;: case&#39;7&#39;: case&#39;8&#39;: case&#39;9&#39;:digit&#43;&#43;;break;default:other&#43;&#43;;break;}}printf("blank&#61;%d,digit&#61;%d,other&#61;%d\n",blank,blank,digit,other);return 0;
}
【例3-11】求简单表达式。
/*求解简单的四则运算表达式*/
#include
int main(void)
{double value1,value2;char op;printf("Type in an expression:");scanf("%lf%c%lf",&value1,&op,&value2);if(op&#61;&#61;&#39;&#43;&#39;)printf("&#61;%.2f\n",value1&#43;value2);else if(op&#61;&#61;&#39;-&#39;)printf("%.2f\n",value1-value2);else if(op&#61;&#61;&#39;*&#39;)printf("&#61;%.2f\n",value1*value2);else if(op&#61;&#61;&#39;/&#39;)if(value2!&#61;0)printf("&#61;%.2f\n",value1/value2);elseprintf("Divisor can not be 0!\n");elseprintf("Unknown operator!\n");return 0;
}
心得&#xff1a;敲代码时&#xff0c;一定要敲完整。不要漏了哪一条&#xff0c;有时候敲漏了&#xff0c;但组建时并不能找出其中的错误&#xff0c;依然能够运行&#xff0c;但结果会有一点不同。
问题&#xff1a;在组建有错误后&#xff0c;怎样能快速地找出错误所在&#xff1f;&#xff1f;&#xff1f;&#xff1f;&#xff1f;&#xff1f;&#xff1f;