课程实验报告课程名称:C语言程序设计专业班级:信息安全1302学号:姓名:指导教师:报告日期:2014年6月16日计算机科学与技术学院目录1 实验概述 (1)2 第一次实验 (2)2.1 实验目的 (2)2.2 实验内容 (2)2.3 实验小结 (19)3 第二次实验 (20)3.1 实验目的 (20)3.2 实验内容 (20)3.3 实验小结 (35)4 第三次实验 (36)4.1 实验目的 (36)4.2 实验内容 (36)4.3 实验小结 (49)5 第四次实验 (50)5.1 实验目的 (50)5.2 实验内容 (50)5.3 实验小结 (62)6 第五次实验 (63)6.1 实验目的 (63)6.2 实验内容 (63)6.3 实验小结 (69)7 第六次实验 (70)7.1 实验目的 (70)7.2 实验内容 (70)7.3 实验小结 (83)8 第七次实验 (84)8.1 实验目的 (836)8.2 实验内容 (84)8.3 实验小结 (105)9 实验总结 (106)参考文献 (107)1实验概述本实验采用C语言常用的上机开发环境Visual C++6.0集成开发环境。
其关键是掌握程序的编辑、修改、编译、链接和执行的方法,以及如何调试程序,跟踪程序,通过断点控制程序,观察变量、表达式在程序运行过程中的取值,如何综合运用这些方法定位程序中的的错误和排除错误。
在实验中除了传统的编程设计题外,增加了源程序改错题,源程序完善、修改、替换题,程序的调试跟踪题等,培养学生跟踪程序、设置断点、调试程序的能力,排除程序中语法、逻辑方面错误与故障的能力,并养成细致、周密、严谨的编程作风。
实验中的源程序改错题,先给出一些含有经典、常见错误的源程序,让学生通过编译或运行发现错误,并加以改正,从而纠正初学者易犯的常见错误,实验中的源程序修改替换题,也是先给出源程序,然后要求学生按要求编写修改替换原来程序中的部分代码。
实验中的跟踪调试结果填空题,给出源程序,要求学生填写程序运行过程中的某些中间结果。
强迫学生用断点设置、单步跟踪等调试程序,观察并填写相应的中间结果。
实验中的编程设计题,只给出题目,要求学生完成从源程序的编程、编辑、修改,编译产生目标代码,链接产生可执行文件,运行程序得到正确结果的全部环节。
学习C语言与学习和使用任何一门其他程序设计语言一样。
首先要理解程序设计语言的语法和语义,然后通过上机熟悉该语言集成开发环境,掌握编辑、修改、编译、链接、跟踪、调试程序等方面的技巧。
最后,还要熟悉语言编译系统提供的库函数或类库,以便快速实现程序的设计与开发。
上机实验就是学习与掌握上面三个环节的重要途径。
上机实验之前一定要预先编好有关程序,把上机的重点放到程序的编辑、修改、编译、链接、跟踪、调试程序等方面,放到观察程序运行过程中的中间结果和运行完毕后的运行结果方面来。
最终使自己的大脑变成一台“计算机”,能够在脑袋中运行自己的程序,判断各种情况下程序的走向等等,从而提高上机的效率和程序设计的准确性。
总之,上机实验是进一步深刻了解C语言的语法、语义,掌握用C语言进行初步程序设计所需要的方法和技能的一个重要环节。
2 第一次实验2.1 实验目的1.熟悉C语言开发环境VC++6.0的使用;2.学会在VC++6.0中编译、运行、调试程序;3.熟悉表达式和标准输入与输出;4.熟悉掌握掌握各种运算符运算功能,操作数的类型,运算结果的类型及运算过程中的类型转换,重点是c语言特有的运算符,例如位运算符,问号运算符,逗号运算符;书籍运算符的优先级和结合性;5.掌握getchar,putchar,scanf和printf函数的用法;6.掌握c程序(顺序结构程序)的编写方法。
2.2 实验内容(一)修改四个源程序得到正确的结果和如下程序设计:1.编写一个程序,输入字符c,如果c是大写字母,则将c转换成对应的小写,否则c的值不变,最后输出c。
2.编写一个程序,输入无符号短整数x,m,n(0 ≤m≤ 15, 1 ≤ n≤ 16-m),取出x从第m位开始向左的n位(m从右至左编号为0~15),并使其向左端(第15位)靠齐。
3.(3)IP地址通常是4个用句点分隔的小整数(即点分十进制),如32.55.1.102。
这些地址在机器中用无符号长整形表示。
编写一个程序,以机器存储的形式读入一个互联网IP地址,对其译码,然后用常见的句点分隔的4部分的形式输出。
例如,整形676879571二进制表示就是:00101000 01011000 01011100 11010011,按照8位一组可表示为:40 88 92 211,由于CPU处理数据的差异,它的顺序是颠倒的,所有最终格式为211.92.88.40。
(二)1. 设计一个程序,在字符模式下,以某个字符拼出“王”汉字图形,显示输出。
2.输入一个华氏温度,要求输出摄氏温度。
公式为c=5/9*(f-32)。
3. 设计3种算法,完成交换整型两个变量值的程序。
4. 设计一个程序,完成如下功能:输出一个三位数,将它反向输出。
如输入127,输出721。
5. 设计2种完成计算MAX{x,y,z}的程序。
6. 设计一个程序,完成计算3个数的居中数。
改错:(红色字体为修改部分)源程序1.#include <stdio.h>#include<math.h>int main(void){float a,b,c,s,t;print("input three dege of the triangle\n") scanf("%f%f%f",&a,&b,&c);s=(a+b+c)/2t=s*(s-a)*(s-b)*(s-c);area=sqrt(t);print("area=%d\n",area);return 0;}修改后:源程序1.#include <stdio.h>#include <math.h>int main(void){float a,b,c,s,t;print("input three dege of the triangle\n"); scanf("%f%f%f",&a,&b,&c);s=(a+b+c)/2;t=s*(s-a)*(s-b)*(s-c);t=sqrt(t);printf("area=%d\n",t);return 0;}源程序2:#include <stdio.h>#define N 5;int sum(int x[],int n);int main(){int i,a[N],total;printf("Input %d integer please!\n",N); for(i=0;i<N;i++)scanf("%d",a[i]);total=sum(a,N);printf("The sum is %d\n",total);return 0;}int sum(int x[],int n);{int i,s;for(s=0;i=0;i<n;i++);s+=x[i];return s;}修改后:#include <stdio.h>#define N 10int sum(int x[],int n);int main(){int i,a[N],total;printf("Input %d integer please!\n",N); for(i=0;i<N;i++)scanf("%d",a[i]);total=sum(a,N);printf("The sum is %d\n",total);return 0;}int sum(int x[],int n){int i,s;for(s=0,i=0;i<n;i++)s+=x[i];return s;}源程序3:#include <stdio.h>#define PI 3.14159;voidmain(void){int f;short p,k;double c,r,s;/*for task 1*/printf("Input Fahrenheit:");scanf("%d",f);c=5/9*(f-32);printf("\n %d(F)=%.2(C)\n\n",f,c /*for task 2*/printf("input the radius r:"); scanf("%f",&r);s=PI*r*r;printf("\nThe acreage is %.2f\n\n",s); /*for task 3*/printf("input hex int k,p:");scanf("%x %x",&k,&p);newint=(p&0xff00)|(k&0xff00)<<8;printf("new int=%x\n\n",newint);}修改后:#include <stdio.h>#define PI 3.14159int main(void){int f,newint;short p,k;double c,r,s;/*for task 1*/printf("Input Fahrenheit:");scanf("%d",&f);c=5/9*(f-32);printf("\n %d(F)=%.2f(C)\n\n",f,c);/*for task 2*/printf("input the radius r:");scanf("%lf",&r);s=PI*r*r;printf("\nThe acreage is %.2f\n\n",s); /*for task 3*/printf("input hex short int k,p:");scanf("%hd %hd",&k,&p);newint=(p&0xff00)+(k>>8)&0x00ff;printf("newint=%hd\n\n",newint);return 0;}源程序4:#include <stdio.h>void main(){int a,b,t;printf("Input two integers:"); scanf("%d %d",&a,&b);t=a;a=b;b=t;prinf("\na=%d,b=%d",a,b);}替换下划线,语句:#include <stdio.h>void main(){int a,b;printf("Input two integers:"); scanf("%d %d",&a,&b);a=a+b;b=a-b;a=a-b;printf("\na=%d,b=%d",a,b);}程序设计:程序1:#include <stdio.h> void main(){int c ;c=getchar();if(c>='A'&&c<='Z') c=c-'A'+'a'; putchar(c);}程序2: #include <stdio.h>int main(){unsigned short int x,m,n;unsigned short int result; scanf("%hu%hu%hu",&x,&m,&n); result=x>>m<<(16-n);printf("%hu",result);return 0;}程序3:#include<stdio.h>void main(){long ip;unsigned int i=0,tem=0,c=0;scanf("%ld",&ip);for (i=0;i<32;i++){tem|=(ip%2)<<(i-8*c);if((i+1)%8==0){c<=2?printf("%3d.",tem):printf("%3d",tem); tem=0;c++;}ip=ip/2;}}(二)、题1:#include <stdio.h>void main(){printf("1111111111\n");printf(" 11 \n");printf(" 11 \n");printf(" 11111111 \n");printf(" 11 \n");printf(" 11 \n");printf("1111111111\n"); }题2:#include <stdio.h>void main (){int tem,res;printf("please input your temperature\n");scanf("%d",&tem);res=5.0/9.0*(tem-32);printf("the result is: %d\n",res);}题3:#include <stdio.h>void main (){int a,b;printf("The first Methoad:\n");printf("please input the value of a,b\n");scanf("%d%d",&a,&b);int tem;tem=a,a=b,b=a;printf("a=%d b=%d",a,b);printf("The second Methoad:\n");printf("please input the value of a,b\n");scanf("%d%d",&a,&b);a+=b;b=a-b;a=a-b;printf("a=%d b=%d",a,b);printf("The third Methoad:\n");printf("please input the value of a,b\n");scanf("%d%d",&a,&b);a^=b;b^=a;a=a^b;printf("a=%d b=%d",a,b);}题4:#include <stdio.h>void main(){int num,res;printf ("input the number :\n");scanf("%d",&num);res=100*(num%10);res+=num%100-num%10;res+=num/100;printf("the result is the :\n%d\n",res); }题5:#include <stdio.h>void main(){int a,b,c;printf("the first:\ninput three numbers\n");scanf("%d%d%d",&a,&b,&c);a=a>b?a:b;a=a>b?a:c;if (a>b){ if(a>c);elsea=c;}else{ a=b;if (b>c);elsea=c;}printf("Max=:%d\n",a);printf("the second:\ninput three numbers\n");scanf("%d%d%d",&a,&b,&c);a=(a>b)?(a>c?a:c):(b>c?b:c);printf("Max=:%d\n",a);}题6:#include <stdio.h>void main(){int a,b,c,d;printf("input three numbers:\n");scanf("%d%d%d",&a,&b,&c);d=a+b+c;a=(a>b)?(b>c?c:b):(a>c?c:a);b=(a>b)?(a>c?a:c):(b>c?b:c);d=d-a-b;printf("mid=%d\n",d);}题7:#include<stdio.h>#include<math.h>int main(void){float a,b,c,d,x1,x2,realpart,imagepart;printf("Input a,b,c:\n");scanf("%f%f%f",&a,&b,&c);if(a==0)printf("Inupt ERROR\n");else{d=b*b-4*a*c;if(d<0){realpart=-b/(2*a);imagepart=sqrt(-d)/(2*b);printf("complex roots:%g+%.3gi\n",realpart,imagepart);printf("complex roots:%g-%.3gi\n",realpart,imagepart);}else{x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);printf("x1=%f,x2=%f\n",x1,x2);}}return 0;}题8:#include <stdio.h>#include <math.h>void main(){float a,b,c,s,p;printf("input three numbers:\n");scanf("%f%f%f",&a,&b,&c);p=(a+b+c)/2;if((p*(p-a)*(p-b)*(p-c))>0){s=sqrt(p*(p-a)*(p-b)*(p-c));printf("the result :%f\n",s);}elseprintf("these numbers is'n a triangle!\n");}2.3 实验小结1.编写程序时,要注意加上“#include <stdio.h> ” ,这是一个包含基本输入输出的头文件。