第一章:分支导例:输入生日并显示#include stdio.h /*编译预处理命令*/int main( ) /*定义了一个名字为main的函数*/{int month, day;/*定义变量month和day*/ printf(Enter the month and the day of your birthday:); scanf(%d%d, month,day); printf(“Your birthday is :%d月%d日\n, month, day);return 0;}导例:汇率换算#include stdio.h main( ){ int rmb; /*定义整型变量,存放人民币值*/ float rate; /*定义浮点型放汇率值*/ float dollar; /*定义浮点型放美元值*/ rate=0.1642; /*对变量rate赋值*/ printf(rmb=); scanf(“%d”,rmb); /*接收用户输入*/ dollar=rmb*rate; /*汇率换算*/printf(¥%d can exchange $%.2f\n,rmb,dollar); /*调用printf()函数输出结果*/导例:字符加密#include stdio.hmain (){ char ch; /*定义一个字符变量ch*/? ch=getchar(); /*调用getchar()函数,从键盘上输入一个字符,赋值给ch*/ ch=ch+3; /*为字符ch加密 */ putchar(ch); /*调用putchar()函数,输出加密后的ch*/? putchar(‘\n’); /*输出回车换行符*/}导例:考核通过了吗#include stdio.h main (){int score1,score2,score3; /*定义成绩变量*/ printf(请输入三项考核成绩:);scanf(%d%d%d,score1,score2,score3);printf(“第一项考核果:%d\n,score1=60);printf(第二项考核果:%d\n,score2=60);printf(第三项考核果:%d\n,score3=60);printf(综合考核结果:%d\n,(score1=60) (score2=60) (score3=60));}算术运算符算术运算符用于各种数值运算,包括5个二元运算符:加(+)、减(-)、乘(*)、除(/)、求余(%),和2个一元运算符:自增(++)、自减(--)。判断数字的奇偶性#include stdio.h int main(void){ int number; printf(Enter a number: ); scanf(%d, number); if(number % 2 == 0){ printf(“偶数. \n); } else{ printf(“奇数. \n); } return 0;今年是闰年吗?#include stdio.h main( ){ int year;? printf(Enter year:); scanf(%d, year);? if(year%4==0year%100!=0||year%400==0) /*闰年的条件*/ printf(YES\n); else printf(NO\n);?}求解一元二次方程从键盘输入一元二次方程ax2+bx+c=0的3个系数a,b,c,编程计算该方程的解,并输出之#include stdio.h#include math.hmain(){int a, b, c, s; double x1,x2 ;printf(Enter 3 integers:);scanf(“%d%d%d”, a, b, c);s=b*b-4*a*c ;/*条件判断,输出结果*/}if (s0){ x1=(-b+sqrt(s))/(2*a) ; x2=(-b-sqrt(s))/(2*a) ; printf(x1=%f, x2=%f\n, x1,x2) ;} else if (s==0){ x1=x2=(-b)/(2.0*a) ; printf(x=%f\n, x1) ;} else printf(“No solution\n”);求解简单的四则运算表达式输入一个形式如“操作数运算符操作数”的四则运算表达式,根据输入的运算符,做相应运算,输出运算结果。使用if语句实现四
原创力文档

文档评论(0)