您好,欢迎访问三七文档
第一章【例1.1】在屏幕上输出一行文本信息“**欢迎光临**”。【程序代码】#include“stdio.h”voidmain()/*主函数*/{printf(“**欢迎光临**\n”);/*在屏幕上输出“**欢迎光临**”*/}【例1.2】计算两个整数的平均值,并输出结果。【程序代码】#includestdio.hvoidmain(){intx,y;/*变量定义*/floatave;x=46;/*为变量x赋值*/y=78;/*为变量y赋值*/ave=(x+y)/2.0;/*计算平均值存放在ave变量中*/printf(“平均值是%0.1f”,ave);/*在屏幕上输出平均值*/}【例1.6】无参宏的应用【程序代码】#includestdio.h#defineM(y*y+3*y)voidmain(){ints,y;printf(inputanumber:);scanf(%d,&y);s=3*M+4*M+5*M;printf(s=%d\n,s);}【例1.7】带参的宏应用【程序代码】#includestdio.h#defineMAX(a,b)(ab)?a:bvoidmain(){intx,y;printf(inputtwonumbers:);scanf(%d%d,&x,&y);printf(max=%d\n,MAX(x,y));}第二章【例2.1】符号常量的使用。【程序代码】#includestdio.h#definePI3.14voidmain(){floatr,s;printf(pleaseinputr:\n);scanf(%f,&r);s=r*PI;printf(s=%f\n,s);}【例2.2】除法运算符应用【程序代码】#includestdio.hvoidmain(){printf(%d,%d\n,20/7,-20/7);printf(%f,%f\n,20.0/7,-20.0/7);}【例2.3】求余运算符应用【程序代码】#includestdio.hvoidmain(){printf(%d\n,100%3);}【例2.4】计算下列程序的输出结果【程序代码】#includestdio.voidmain(){inti=8;printf(%d\n,++i);printf(%d\n,--i);printf(%d\n,i++);printf(%d\n,i--);printf(%d\n,-i++);printf(%d\n,-i--);}【例2.5】逗号运算符及逗号表达式的应用。【程序代码】#includestdio.hvoidmain(){inta=2,b=4,c=6,x,y;y=(x=a+b),(b+c);printf(y=%d,x=%d\n,y,x);}第三章【例3.1】格式化输出。【程序代码】#includestdio.hvoidmain(){inta=15;floatb=123.1234567;doublec=12345678.1234567;chard='p';printf(a=%d,%5d,%o,%x\n,a,a,a,a);printf(b=%f,%lf,%5.4lf,%e\n,b,b,b,b);printf(c=%lf,%f,%8.4lf\n,c,c,c);printf(d=%c,%8c\n,d,d);}【例3.2】格式化输入【程序代码】#includestdio.hvoidmain(){inta,b,c;printf(inputa,b,c\n);scanf(%d%d%d,&a,&b,&c);printf(a=%d,b=%d,c=%d\n,a,b,c);}【例3.3】scanf函数应用。【程序代码】#includestdio.hvoidmain(){chara,b;printf(input\n);scanf(%c%c,&a,&b);printf(%c%c\n,a,b);}【例3.4】格式化输入。【程序代码】#includestdio.hvoidmain(){inta;printf(inputanumber\n);scanf(%d,&a);printf(%ld,a);}【例3.5】输入单个字符。【程序代码】#includestdio.hvoidmain(){charc;printf(inputacharacter\n);c=getchar();putchar(c);putchar('\n');}【例3.6】puts函数的用法【程序代码】#includestdio.hvoidmain(){charc[]=BASIC\ndBASE;puts(c);}【例3.7】gets函数的用法【程序代码】#includestdio.hvoidmain(){charst[15];printf(inputstring:\n);gets(st);puts(st);}【例3.7】输入三角形的3条边,计算三角形的面积。假设输入的3边长能构成三角形。三角形的面积公式为:area=))()((csbsass其中a、b、c为三角形的三边长,s为三角形的半周长,其值为(a+b+c)/2,area为三角形的面积。【程序代码】#includemath.h#includestdio.hvoidmain(){floata,b,c,s,area;printf(“pleaseinputthreenumbers:\n”);scanf(“%f,%f,%f”,&a,&b,&c);s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c));printf(“area=%.2f\n”,area);}【例3.8】已知:f(x,y,z)=lg(12x)/ln(65y)+cos(z+0.5x),求:w=(f(x1,y1,z1)-f(x2,y2,z2))/(f(x3,y3,z3)+f(x4,y4,z4))【程序代码】#includemath.h#includestdio.hvoidmain(){floatx,y,z,w,f1,f2,f3,f4;printf(“pleaseinputx1,y1,z1:\n”);scanf(“%f,%f,%f”,&x,&y,&z);f1=log10(12*x)/log(65*y)+cos(z+0.5*x);printf(“pleaseinputx2,y2,z2:\n”);scanf(“%f,%f,%f”,&x,&y,&z);f2=log10(12*x)/log(65*y)+cos(z+0.5*x);printf(“pleaseinputx3,y3,z3:\n”);scanf(“%f,%f,%f”,&x,&y,&z);f3=log10(12*x)/log(65*y)+cos(z+0.5*x);printf(“pleaseinputx4,y4,z4:\n”);scanf(“%f,%f,%f”,&x,&y,&z);f4=log10(12*x)/log(65*y)+cos(z+0.5*x);w=(f1-f2)/(f3+f4);printf(“w=%f\n”,w);}【例3.9】输入一个字符,求出其前导字符和后续字符,最后按由小到大的顺序输出它们的字符及其ASCII值。#includestdio.hvoidmain(){charb;inta,c;printf(“pleaseinputacharacter:\n”);b=getchar();a=b-1;c=b+1;printf(“%c%c%c\n”,a,b,c);printf(“%d%d%d\n”,a,b,c);}【任务】商场超市购物小票今天晓华去商场买了件毛衣,付账时收银员给她打印了一张如下所示小票。我们用计算机来完成。--------------------------------------------家乐园大洋百货(三层)交易号收银员号机号销售时间0111561152805452008/02/18商品名称(编码)数量单价金额千百惠女装47801268.00268.00--------------------------------------------实计件数:1合计金额:268.00实收金额270.00找零金额:、0.00--------------------------------------------质量问题7日内凭票退换!15:28:31【程序代码】#includestdio.hvoidmain(){printf(------------------------------\n);printf(家乐园大洋百货(三层)\n);printf(交易号收银员号机号销售时间\n);printf(0111561152805452008/02/18\n);printf(商品名称(编码)数量单价金额\n);printf(千百惠女装47801268.00268.00\n);printf(------------------------------\n);printf(实计件数:1合计金额:268.00\n);printf(实收金额270.00找零金额:、0.00\n);printf(------------------------------\n);printf(质量问题7日内凭票退换!15:28:31\n);}第四章【例4.1】输入任意两个实数,按由小到大的顺序输出。【程序代码】#includestdio.hmain(){floata,b,t;printf(“pleaseinputtwonumbers:\n”);scanf(%f,%f,&a,&b);if(ab){t=a;a=b;b=t;}printf(%5.2f,%5.2f\n,a,b);}【例4.2】输入任意三个实数,按由小到大的顺序输出。【程序代码】#includestdio.hmain(){floata,b,c,t;printf(“pleaseinputthreenumbers:\n”);scanf(%f,%f,%f,&a,&b,&c);if(ab){t=a;a=b;b=t;}if(ac){t=a;a=c;c=t;}if(bc){t=b;b=c;c=t;}printf(%5.2f,%5.2f,%5.2f\n,a,b,c);}【例4.3】输入一个字符,判别其类型,并输出相关信息。【程序代码】#includestdio.hvoidmain(){charc;printf(inputacharacter:);c=getchar();if(c32)printf(Thisisacontrolcharacter\n);elseif(c='0'&&c='9')printf(Thisisadigit\n);elseif(c='A'&&c='Z')printf(Thisisacapitalletter\n);elseif(c='a'&&c='z')printf(Thisisasmallletter\n);elseprintf(Thisisanothercharacter\n);}【例4.4】输入一个成绩等级,打印相应的分数段(85~100等级为‘A’;70~84等级为’B’,60~69等级为’C’,低于60分等级为’D’)。【程序代码】#includestdio.hvoidmain(){chargrade;printf(pleaseinputyourgrade:);scanf(%c,&grade);switch(grade){case‘A’:printf(“85~100.\n”);break;case‘B’:printf(“70~84.\n”);break;case‘C’:printf(“60~69.\n”);break;case‘D’:printf(“60.\n”);break;default:printf(“error!\n”);}}【例4.5】在计算机中输入某年某月某日,判断这一天是这一年的第几
本文标题:C源代码(w)
链接地址:https://www.777doc.com/doc-3872850 .html