您好,欢迎访问三七文档
手工打造天然无添加O(∩_∩)O姓名:陈程洁学号:专业班级:医学信息工程16级2班实验一熟悉C语言编程环境调试示例:#includestdio.hintmain(){printf(Welcometoyou!\n);return0;}基础编程题(1)输出短句:在屏幕上显示一个短句“ProgramminginCisfun!”。#includestdio.hintmain(){printf(ProgramminginCisfun!\n);return0;}(2)输出三角形:在屏幕上显示下列三角形图案。#includestdio.hintmain(){inti,j;for(i=0;i4;i++){for(j=4;ji;j--)printf(*);printf(\n);}return0;}(3)输出菱形图案:在屏幕上显示下列菱形图案。#includestdio.hintmain(){printf(A\n);printf(AA\n);printf(A\n);return0;}改错题:输出带框文字:在屏幕上输出以下3行信息。*************Welcome*************#includestdio.hintmain(){printf(*************\n);printf(Welcome\n);printf(*************\n);return0;}拓展编程题:(1)输出短句(Whatisacomputer?):在屏幕上显示一个短句“Whatisacomputer?”#includestdio.hintmain(){printf(Whatisacomputer\n);return0;}(2)输出倒三角形图案:在屏幕上显示下列倒三角图案。#includestdio.hintmain(){inti,j,k;for(j=4;j0;j--){for(i=0;i4-j;i++)printf();for(i=0;ij;i++)printf(*);printf(\n);}return0;}实验二用C语言编写简单程序2.1基本数据处理调试示例#includestdio.hintmain(){intcelsius,fahr;fahr=100;celsius=5*(fahr-32)/9;printf(fahr=%d,celsius=%d\n,fahr,celsius);return0;}基础编程题(1)温度转换:求华氏温度150°F对应的摄氏温度。#includestdio.hintmain(){intcelsius,fahr;fahr=150;celsius=5*fahr/9-5*32/9;printf(fahr=%d,celsius=%d\n,fahr,celsius);return0;}(2)计算物体自由下落的距离:一个物体从100m的高空自由落下,编写程序,求它在前面3s内下落的垂直距离。设重力加速度为10m/。#includestdio.hintmain(){inth,g,t;g=10;t=3;h=g*t*t/2;printf(height=%d\n,h);return0;}(3)计算平均分:已知某为学生的数学、英语和计算机课程的成绩分别是87分、72分和93分,求该生3门课程的平均分。#includestdio.hintmain(){intMt,Eg,Cp;Mt=87;Eg=72;Cp=93;printf(math=%d,end=%d,comp=%d,average=%d,Mt,Eg,Cp,(Mt+Eg+Cp)/3);return0;}改错题:计算某个数的平方赋值给y;分别以“y=x*x”和“x*x=y”的形式输出x和y的值。请不要删除源程序中的任何注释。示例:9=3*33*3=9#includestdio.hintmain(void){intx,y;scanf(%d,&x);y=x*x;printf(%d=%d*%d\n,y,x,x);/*ê?3?*/printf(%d*%d=%d\n,x,x,y);return0;}拓展编程题(1)计算华氏温度:求摄氏温度26°C对应的华氏温度。计算公式如下:f=(9/5)xc+32其中,c表示摄氏温度,f表示华氏温度。#includestdio.hintmain(void){intc,f;c=26;f=9*c/5+32;printf(celsius=%d,fahr=%d,c,f);return0;}(2)求一个三位数的各位数字:当n为152时,分别求出n的个位数字(digit1)、十位数(digit2)和百位数字(digit3)的值。#includestdio.hintmain(void){intn,digit1,digit2,digit3;n=152;digit1=n%10;digit2=(n/10)%10;digit3=n/100;printf(整数%d的个位数字是%d,十位数字是%d,百位数字%d\n,n,digit1,digit2,digit3);return0;}2.2计算分段函数调试示例#includestdio.hintmain(void){doublex,y;printf(Enterx:);scanf(%lf,&x);if(x!=0){y=1/x;}elsey=0;printf(f(%.2f)=%.1f\n,x,y);return0;}基础编程题(1)计算摄氏温度:输入华氏温度,输出对应的摄氏温度。#includestdio.hintmain(){intc,f;scanf(%d,&f);c=5*(f-32)/9;printf(fahr=%d\n,c);return0;}(2)计算存款利息:输入存款金额money,存期year和年利率rate,根据下列公式计算存款到期时的利息interest(税前),输出时保留2位小数。#includestdio.h#includemath.hintmain(){intm,y;doubler,i;scanf(%d%d%lf,&m,&y,&r)printf(money=%d,year=%d,rate=%f\n,m,y,r);i=m*pow(1+r,y)-m;printf(interest=%.2f,i);return0;}(2)计算分段函数:输入x,计算并输出下列分段函数f(x)的值(保留2位小数),请调用sqrt函数求平方根,调用pow函数求幂。#includestdio.h#includemath.hintmain(void){doublex,y;scanf(%lf,&x);if(x0)y=pow(x+1,2)+2*x+1/x;elsey=sqrt(x);printf(f(%.2f)=%.2f,x,y);return0;}改错题:计算f(x)的值:输入实数x,计算并输出下列分段函数f(x)的值,输出是保留1位小数。#includestdio.hintmain(void){doublex,y;printf(Enterx:);scanf(%lf,&x);if(x==10)y=1/x;elsey=x;printf(f(%.1f)=%.1f,x,y);return0;}拓展编程题(1)整数四则运算:输入两个整数num1和num2,计算并输出他们的和、差、积、商与余数。#includestdio.hintmain(void){intnum1,num2;scanf(%d%d,&num1,&num2);printf(%d+%d=%d\n,num1,num2,num1+num2);printf(%d-%d=%d\n,num1,num2,num1-num2);printf(%d*%d=%d\n,num1,num2,num1*num2);printf(%d/%d=%d\n,num1,num2,num1/num2);printf(%d%%%d=%d,num1,num2,num1%num2);return0;}(2)阶梯电价。#includestdio.hintmain(void){intx;doubley;scanf(%d,&x);if(x=50)y=0.53*x;else{y=0.53*50+(x-50)*0.58;}printf(cost=%.2f,y);return0;}(3)计算火车运行时间:#includestdio.h#includemath.hintmain(){inta,b,m,n;scanf(%d%d,&a,&b);if((b%100-a%100)0){m=((b-a)/100)%10;n=b%100+60-a%100;}if((b%100-a%100)=0){m=((b-a)/100)%10;n=b%100-a%100;}printf(Thetrainjourneytimeis%dhrs%dmins.,m,n);return0;}2.3指定次数循环调试示例#includestdio.hintmain(){inti,sum;sum=0;for(i=1;i=100;i++){sum=sum+i;}printf(sum=%d\n,sum);return0;}基础编程题(1)求给定序列(1+1/2+1/3+…)前n项和:输入一个正整数n,计算序列1+1/2+1/3+…的前n项和。#includestdio.hintmain(){intn;doublesum,i;scanf(%d,&n);sum=0;for(i=1;i=n;i++){sum=sum+1/i;}printf(sum=%.6f,sum);return0;}(2)求给定序列(1+1/3+1/5+…)的前n项和:入一个正整数n,计算序列1+1/3+1/5+…的前n项和。#includestdio.hintmain(){intn;doublesum,i;scanf(%d,&n);sum=0;for(i=1;i=n;i++){sum=sum+1/(i*2-1);}printf(sum=%.6f,sum);return0;}(3)求给定数列(1-1/4+1/7-1/10+…)前n项和:入一个正整数n,计算序列1-1/4+1/7-1/10+…的前n项和。#includestdio.hintmain(){intn,item=1;doublesum,i;scanf(%d,&n);sum=0;for(i=1;i=n;i++){sum=sum+(1/(3*i-2))*item;item=-item;}printf(sum=%.3f,sum);return0;}改错题:输出华氏-摄氏温度转换表:#includestdio.hintmain(){intfahr,lower,upper;doublecelsius;printf(Enterlower:);scanf(%d,&lower);printf(Enterupper:);scanf(%d,&upper);printf(fahrcelsius\n);for(fahr=lower;fahr=upper;fahr=fahr+2){celsius=(5.0/9.0)*(fahr-32.0);printf(%d%.1f\n,fahr,celsius);}return0;}拓展编程题(1)求交错序列前n项和:输入一个正整数n,计算交错序列1-2/3+3/5-4/7+5/9-6/11…的前n项之和,输出时保留3位小数。#includestdio.hintmain(){intn,item=1;doublesum=0,i;scanf(%d,&n);for(i=1;i=n;i++){sum=sum+(i/(2*i-1))*item;item=-item;}printf(sum=%.3f\n,sum);return0;}(2)序列求和(mxm+1/m+(m+1)x(m+1)+…):输入两个正整数m
本文标题:C语言实验报告
链接地址:https://www.777doc.com/doc-7216283 .html