c语言上机实验_第1页
c语言上机实验_第2页
c语言上机实验_第3页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验 3( 1) 程序#include <stdio.h>int main()int a=3,b=5,c=7,x=1,y,z;a=b=c;x+2=5;z=y+3;return 0;程序错误;提示语言Configuration: fd - Win32 DebugCompiling.fd.cppC:Documents and 桌面 fd.cpp(2) : error C2447: missing function header (old-style formal list?)执行 cl.exe 时出错 .fd.exe - 1 error(s), 0 warning(s) 修改为:#in

2、clude <stdio.h>int main()int a=3,b=5,c=7,x=1,y,z;a=b=c;x=5+3;z=y+3;return 0; 分析结果:在赋值和运算中是从右到左。分析:A 变量名可以是数字,英文字母(大小写均可) 。下划线B 大小写可以区分是成不同文件。C 赋值运算的特点是自右向左的。实验4(i) 整数除的危险性#i nclude <stdio.h>int main()int a=5,b=7,c=100,d,e,f;d=a/b*c;e=a*c/d;f=c/b*a;程序在运行结果为:0 0 0;分析原因:A5/7*100,结果是5/7等于0,再

3、0乘上100等于0。B 5*100/7,结果是 5*100 等于 500,再 500/7 等于 0。C 100/7*5 结果是100/7等于0,再0*5等于0。结论:原因在于,当整除不成立时,结果为 0,所以结论会影响下一次的运行#i nclude <stdio.h>mai n()int a=5,b=8;printf( a+=%d”,a+);printf( a=%d”,a);printf( +b=%d ”+b);printf( b=%d”,b);a*=5a=6 +b=?h=9Press nny Ley to continue得到结论为分析结果:i+ “先引用,后增值”:+i “先增

4、值,后引用”所以a+是 5,因为先引用,a是6,因为是增值的结果,+b是9,是因为先增 值,b是9,是因为后引用的结果。(3)对这些表达式进行测试分析。b+a+a b+(a+)+a b+a+(+a) b+a+a+编程:#i nclude <stdio.h>mai n()int a=1,b=1;printf( “b+a+a%d”,b+a+a);结果:b+a+=3Pi*ess anyto continue#i nclude <stdio.h>mai n()int a=1,b=1;printf( b+(a+)+a=%d”,b+(a+)+a);#i nclude <std

5、io.h>mai n()int a=1,b=1;printf( b+a+(+a)=%d”,b+a+(+a);结果:#i nclude <stdio.h>mai n()int a=1,b=1;printf( b+a+a+=%d”, b+a+a+);+*=3Press any It巳里 to cont inue结果:#i nclude <stdio.h>mai n()int a=1,b=;printf( “b+a+a%d”,b+a+a);(4)对这些表达式进行测试分析。i,ji+1,j+1i+,j+i,+ji+j编程:#i nclude <stdio.h>

6、mai n()int i=1,j=1;printf( i=%d ”,i);printf( j=%d ”,j);gc t :Emith alii S上七七匚皿琶亍、扎dhiinii=lj=lPress anv key to continue结果:#i nclude <stdio.h> mai n()int i=1,j=1;printf( “+1=%d”,i+1); printf( j+1= %d”,j+1); 环 VC: VDlocmenlzs ain d. SetLi iLgsAdBi iki s trat i+l =2j+l=2Pi*ess ani/ kev to continu

7、e结果:#i nclude <stdio.h> mai n()int i=1,j=1;printf( i+=%d ”,i+); printf( j+=%d ”,j+); 结果:#i nclude <stdio.h> mai n() int i=1,j=1;printf( +i=%d ”+i); printf( +j=%d ”+j);结果:#i nclude <stdio.h>mai n()int i=1,j=1;printf( i+j=%d ”,i+j);结果: Configuration: Cpp1 - Win32 DebugCompili ng.Cpp1

8、.cpp桌面Cpp1.cpp(5) : error C2105: '+' needsl-value桌面Cpp1.cpp(6) : warning C4508: 'main':fun cti on should retur n a value; 'void' retur n type assumed执行cl.exe时出错.Cpp1.exe - 1 error(s), 0 warni ng(s) 结论:程序是错误的。1 分析结果:整除有危险性。A 因为小数不能除大的数字,会显示 0。还有结果一定是整数 B.算数运算的方向是自左向右。2 分析结果:A.

9、 i+ “先引用,后增值”:+i “先增值,后引用” 所以a+是 5,因为先引用,a是6,因为是增值的结果,B. +b是9,是因为先增值,b是9,是因为后引用的结果。3.分析结果:A .可靠性低B.不容易读懂实验5printf ( lOng”,sizeof(long);编程:#include <stdio.h>int mai n()int i=0;printf ("l ong: %d bytes' n",sizeof(l on g); return 0;(2) 编写程序来测试基本数据类型的取值范围下。编程:#include <stdio.h>

10、int mai n()char c0,c1,c2,c4,c5,c6;c0=-129,c1=-128,c2=-127,c4=128,c5=127,c6=126;prin tf("%dn%dn%dn%dn%dn%dn",sizeof(c0),sizeof(c1),sizeof(c2),sizeof(c4 ),sizeof(c5),sizeof(c6);结果为;(1) 编写程序来测试基本数据类型的有效长度和精度 编程:#include <stdio.h>int mai n()prin tf("%lf,%lfn",d1,d2); 结果:0-1234B

11、7,12345679355QG094090.900909 Presstotinuc(2) 编写程序来测试不同类型之间的转换所在出现的截取高位,丢失精度和变得不可知等问题。编程:#include <stdio.h>int mai n()i1=f1;i2=f2;prin tf("%d,%dn",i1,i2);结果:IX: Doeua ents and 5et-tingsVA.dBiiii2,1073741824HPfses an>t o c n t in u.&X: XDoeua ents and Sett iBgsild: e710?3?41824P

12、ress einy Jcej/ 七o continue(3) 编写程序来测试字符型数据的算术特征 编程:#include <stdio.h>int mai n()char c1=35,c2='A',c3;c3=c1+c2;prin tf("%d,%cn",c3,c3);(4) 编写程序来测试转移字符的用法 如 printf ("7,7,7” ;编程:#in clude <stdio.h>int mai n()prin tf("7,7,7");在程序中只是会输出两个 ,。因为7显示的是所在高度,所以只会显示

13、 ,。实验6(1)设计一个程序来测试printf ()函数中数据参数被引用的顺序Int a=1 ;Printf( %d,%d%d,a+,a+,a+”;编程:#i nclude <stdio.h>int mai n()int a=1;prin tf("%d,%d,%d",+a,+a,+a);结果:4.3,2Pr&s:s:段n# key to continue分析结果:因为程序输出是从右向左进行的。(2)设计一个程序来测试printf ()函数中格式的意义。举例:Double d=123456789.23456789; Printf ( %e”,d);编程:

14、#include <stdio.h> int mai n()double d=123456789.23456789; printf ("%e",d);“ -CAD11 _234560e+908Pres£ any t o c ont inue结果:(3) 设计一个程序来测试printf ()函数定义域与精度的方法。 编程:#include <stdio.h>int mai n()prin tf("%12.5fn",123.1234567);prin tf("%12fn",123.1234567);pri

15、n tf("%12.5gn",123.1234567);printf("%5.10s%sn",”abcdefghijkm”,”a” );prin tf("%12.8dn",12345);return 0;Vlnclude <stdio.h>Int nadn()printf('V2 .5fXr)" ,129.123U567); prin,123-123 45 A?);.5gXn,', 123.1234567);printf ("%5.1ghi jkn" >"a,B

16、):priDtffia.SdXn", 12345);return B;c < 'C: DociwaMts and 5ettincV AteiniEtr«t面123-12346123.123457123*12abcdef $|i ij a06012345Press anj to uontinue结果:(4) 设计一个程序来测试sca nf()函数中格式的意义 举例:double x,y;sca nf("%f,%lf", &x,&y); prin tf("%f,%lf", &x, &x); p

17、rin tf("%f,%lf", &y, &y);编程:#include <stdio.h>int mai n() double x,y;sca nf("%f,%lf", &x,&y); prin tf("%f,%lf", &x, &x); prin tf("%f,%lf", &y,&y); 结果:.-a "C: ACnaents and £«ttii£sAd*i ni面"命血以旺尸一11,

18、2,3,49.0000000-8000080-300000,0-0Q0S0QPpess an51 人乍$ to cantinue(1)设计一个程序来测试用scanf ()输入含有字符型数据的多项时, 数据项之间的分割问题。举例如下:Char c1,c2,c3;Int a1,a2,a3;Scanf( %d%d%d,&a1,&a2,&a3 ”);printf( %d%d%d,a1,a2,a3);Scanf( %c%c%c,&c1,&c2,&c3 ”);printf( %c%c%c,c1,c2,c3');Scanf( %c%c%c,&c1,&c2,&c3 ”);printf( %c%c%c,c1,c2,c3'); 编程:#include <stdio.h>int mai n()char c1,c2,c3;int a1,a2,a3;scan f("%d%d%d,&a1,&a2,&a3");pri ntf("%d%d%d,a1,a2,a3"); scan f("%c%c%c,&c1,&c2,&c3");pri ntf("

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论