[高等教育]C程序设计实验报告2012上传版.doc_第1页
[高等教育]C程序设计实验报告2012上传版.doc_第2页
[高等教育]C程序设计实验报告2012上传版.doc_第3页
[高等教育]C程序设计实验报告2012上传版.doc_第4页
[高等教育]C程序设计实验报告2012上传版.doc_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

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

文档简介

实 验 报 告高级语言程序设计20112012学年第 二 学期学院(部)管理学院指导教师班级代号姓名/学号同组人一、实验项目名称: 综合练习数据类型、变量、运算符、循环、数组、函数声明、定义与调用,实参与形参数值传递以及其它复杂数据类型。二、实验目的综合练习数据类型、变量、运算符、循环、数组与函数编写,学会形式参数与实际参数的结合及其值的传递要点(数据类型、变量个数与顺序严格一致,忽略变量名是否一致)。把函数调用与前面章节内容如变量、数组、循环及if分支语句等进行结合运用,巩固练习。三、实验内容为下面各题分别创建一个函数,通过实参向形参传递值方式实现数值传递。 1 结合循环语句编一个一维数组输出一句有十个单词的话,在函数中使用 extern,auto,static,register等限定不同类型的变量。#include static void printResult(char ax);int main() char str = I want to learn C Language and want to be expert in this language.;char* p;printf(程序段的输出结果: );printf(n=);printf(n);p = str;while (*p != 0)printResult(*p);p+;printf(n=);printf(n);return 0; static void printResult(char x)auto char a = x;printf(%c, a);/*程序运行结果:程序段的输出结果: =I want to learn C Language and want to be expert in this language.=Press any key to continue*/2 编程输出一个3*5的整数矩阵。(二维数组)#include void printMyArray(int x);int main() int i,j;int array35 = 10, 20, 30, 40, 50, 100, 200, 300, 400, 500, 1000, 2000, 3000, 4000, 5000;printf(3*5的整数矩阵:);printf(n=);for ( i = 0; i 3; i+) printf(n);for ( j = 0; j 5; j+) printMyArray(arrayij);printf(n=);printf(n);return 0; void printMyArray(int x)printf(t%d, x);/*程序运行结果:3*5的整数矩阵:= 10 20 30 40 50 100 200 300 400 500 1000 2000 3000 4000 5000=Press any key to continue*/3 请结合使用for与if语句编一个字符数组,并将其大写改为小写,或小写改为大写。#include char change(char x);int main() int i;char array = Hi, How are you!;printf(n=);printf(n原字符数组:);printf(n%s, array);printf(n);printf(n%s, 将大写改为小写,小写改为大写的结果:);printf(n);for (i = 0; arrayi != 0; i+)/0结束符char afterChange = change(arrayi);printf(%c, afterChange);printf(n=);printf(n);return 0; char change(char x)if (x = a & x = A & x = Z)x += 32;return x;/*程序运行结果:=原字符数组:Hi, How are you!将大写改为小写,小写改为大写的结果:hI, hOW ARE YOU!=Press any key to continue*/4 请用循环语句和二维数组计算并输出全班同学(共69位)的这次英语、数学和物理成绩总分及平均分。#include void printMyArray(int english, int matchs, int physics);int main() int i;int array693 = 100, 99, 98, 99, 98, 97, 98, 97, 96, 97, 96, 95, 96, 95, 94, 95, 94, 93, 94, 93, 93, 93, 93, 92, 93, 92, 91, 92, 91, 90, 91, 90, 89, 90, 89, 88, 89, 88, 87, 88, 87, 86, 87, 86, 85, 86, 85, 84, 85, 84, 83, 84, 83, 82, 83, 82, 81, 82, 81, 80, 81, 80, 79, 80, 79, 78, 79, 78, 77, 78, 77, 76, 77, 76, 75, 76, 75, 74, 75, 74, 73, 74, 73, 72, 73, 72, 71, 72, 71, 70, 71, 70, 69, 70, 69, 68, 69, 68, 67, 68, 67, 66, 67, 66, 65, 66, 65, 64, 65, 64, 63, 64, 63, 62, 63, 62, 61, 62, 61, 60, 61, 60, 59, 60, 59, 58, 59, 58, 57, 58, 57, 56, 57, 56, 55, 56, 55, 54, 55, 54, 53, 54, 53, 52, 53, 52, 51, 52, 51, 50, 51, 50, 49, 100, 100, 100, 99, 98, 97, 98, 97, 96, 97, 96, 95, 96, 95, 94, 95, 94, 93, 94, 93, 92, 93, 92, 91, 92, 91, 90, 91, 90, 89, 90, 89, 88, 89, 88, 87, 88, 87, 86, 87, 86, 85, 86, 85, 84, 85, 84, 83, 84, 83, 82, 83, 82, 81;printf(全班同学(共69位):);printf(n=);printf(n英语t数学t物理t总分t平均分);for (i = 0; i 69; i+) printf(n);printMyArray(arrayi0, arrayi1, arrayi2);printf(n=);printf(n);return 0; void printMyArray(int english, int matchs, int physics)int total = english + matchs + physics;int average = total / 3;printf(%dt %dt%dt %dt %d, english, matchs, physics, total, average);/*程序运行结果:全班同学(共69位):=英语 数学 物理 总分 平均分100 99 98 297 9999 98 97 294 9898 97 96 291 9797 96 95 288 9696 95 94 285 9595 94 93 282 9494 93 93 280 9393 93 92 278 9293 92 91 276 9292 91 90 273 9191 90 89 270 9090 89 88 267 8989 88 87 264 8888 87 86 261 8787 86 85 258 8686 85 84 255 8585 84 83 252 8484 83 82 249 8383 82 81 246 8282 81 80 243 8181 80 79 240 8080 79 78 237 7979 78 77 234 7878 77 76 231 7777 76 75 228 7676 75 74 225 7575 74 73 222 7474 73 72 219 7373 72 71 216 7272 71 70 213 7171 70 69 210 7070 69 68 207 6969 68 67 204 6868 67 66 201 6767 66 65 198 6666 65 64 195 6565 64 63 192 6464 63 62 189 6363 62 61 186 6262 61 60 183 6161 60 59 180 6060 59 58 177 5959 58 57 174 5858 57 56 171 5757 56 55 168 5656 55 54 165 5555 54 53 162 5454 53 52 159 5353 52 51 156 5252 51 50 153 5151 50 49 150 50100 100 100 300 10099 98 97 294 9898 97 96 291 9797 96 95 288 9696 95 94 285 9595 94 93 282 9494 93 92 279 9393 92 91 276 9292 91 90 273 9191 90 89 270 9090 89 88 267 8989 88 87 264 8888 87 86 261 8787 86 85 258 8686 85 84 255 8585 84 83 252 8484 83 82 249 8383 82 81 246 82=Press any key to continue*/ 把下列程序段放在函数中,通过实参向形参传递值方式实现数值传递。在main()中调用,写出结果。5 执行下列程序,其输出结果是_int i=9;switch(i) case 9: i+=1; break; case 10: i+=1; case 11: i+=1;break; default: i+=1;printf(“%d”,i);/i=13#include void printResult();int main() printf(输出结果是: );printf(n=);printf(n);printResult();printf(n=);printf(n);return 0; void printResult()int i = 9;switch(i)case 9: i += 1; break;case 10: i += 1; case 11: i += 1;break; default: i += 1;printf(%d,i); /i=13/*程序运行结果:输出结果是: =10=*/6 以下程序段的输出结果_int i , a8 = 1, 2, 3, 4, 5, 6, 7, 8 , sum = 0 ;for( i = 0 ; i 8 ; i = i+2 ) sum = sum + a i ;printf(“sum = %dn”, sum) ;#include void printResult();int main() printf(程序段的输出结果: );printf(n=);printf(n);printResult();printf(n=);printf(n);return 0; void printResult()int i, a8 = 1, 2, 3, 4, 5, 6, 7, 8 , sum = 0;for (i = 0; i 8; i = i + 2)sum = sum + a i;printf(sum = %dn, sum);7 以下程序段的输出结果_ char a20 = “abcd”, b10= “12345” ; strcat ( a , “ ABC ”) ;strcpy( a + 5 , b ) ;printf( “ %s , %dn” , a , strlen ( a ) ) ;#include #include void printResult();int main() printf(程序段的输出结果: );printf(n=);printf(n);printResult();printf(n=);printf(n);return 0; void printResult()char a20 = abcd, b10= 12345;strcat (a, ABC ) ;/*a=abcd ABC */*strcat 字符串连接函数*/strcpy(a + 5, b) ;/*a第一个字符右移五个(即第六个字符开始)用b替换*/*strcpy 字符串复制函数*/printf( %s, %d, a, strlen(a) ;/*strlen字符串取长*/*程序运行结果:程序段的输出结果: = abcd 12345, 10=Press any key to continue*/8 运行以下程序段后sum 的值是_int i , a10 = 1, 2, 3, 4, 5, 6, 7, 8 , 9 , 10 , sum = 0 ;for( i = 3 ; i 7 ; + i ) ai = a i + 1 ; for( i = 0 ; i 10 ; + i ) sum = sum + a i ;printf(“sum = %dn”, sum) ;#include int printResult(int x10);int main() int c;int a10=1,2,3,4,5,6,7,8,9,10;printf(程序段的输出结果: );printf(n=);printf(n);c=printResult(a);printf(n=);printf(n);return 0; int printResult(int x10)int i=3;int sum=0;for ( i = 3; i 7; + i)xi = x i + 1; for (i = 0; i 10; +i)sum = sum + xi ;printf(sum = %dn, sum);return (sum);/*程序运行结果:程序段的输出结果: =sum = 59=Press any key to continue*/9 运行以下程序段后a0 的值是_main() float a10; int i; for( i=0; i3; i+) scanf(“%d”, &ai); for( i=1; i3; i+) a0=a0+ai; printf( “%fn”,a0);#include void printResult();int main() printf(please input three numbers:n);printf(n=);printf(n);printResult();printf(n=);printf(程序段的输出结果: );printf(n);return 0; void printResult()float a10; int i; for(i = 0; i 3; i+) scanf(%d, &ai); for(i = 1; i 3; i+) a0 = a0 + ai; printf(%fn, a0);/*程序运行结果:程序段的输出结果: =1234560.000000=Press any key to continue*/10 以下C语言函数的有关描述中,正确的是_A_ A 调用函数时,只能把实参的值传给形参,形参不能传给实参; B 函数既可以嵌套定义又可以递归调用 C 函数必须有返回值,否则不能使用函数 D 具有调用关系的所有函数必须放在同一个源程序文件中.11 编写一个函数,输入一个字符串,统计该字符串中字符对“ab”的个数,然后在主函数中调用它。#include void myfunction()char c,s80;int i,f,sum=0;printf(Enter a string:);gets(s);i=0;while(c=si+)!=0) if(c=a) f=1;else if(c=b&f=1)sum+;f=0;printf(There are %d ab in the string you just entered.,sum);main() myfunction(); #include void myfunction();int main() printf(程序段的输出结果: );printf(n=);printf(n);myfunction();printf(n=);printf(n);return 0; void myfunction()char c, s80;int i, f, sum = 0;printf(Enter a string:);gets(s);i=0;while (c = si+) != 0)if (c = a) f = 1;else if (c = b & f = 1) sum+;f = 0;printf(There are %d ab in the string you just entered., sum);/*程序运行结果:程序段的输出结果: =Enter a string:this is a count ab founction abcd and xabyabThere are 4 ab in the string you just entered.=Press any key to continue*/12 分析下列程序,输出结果是_ main() char arr24;strcpy(arr,you);strcpy(arr1,me);arr03=&;printf(%sn,arr);#include #includevoid printResult();int main() printf(程序段的输出结果: );printf(n=);printf(n);printResult();printf(n=);printf(n);return 0; void printResult()char arr24;strcpy(arr0, you);strcpy(arr1, me);arr03 = &;printf(%sn, arr);/*程序运行结果:程序段的输出结果: =you&me=Press any key to continue*/ 程序段有错误,strcpy(arr, you); 需要修改为:strcpy(arr0, you);13 分析下列程序,输出结果是_main() int i,a20=1,1;for(i=2;i20;i+)ai=ai-2+ai-1;for(i=0;i20;i+) if(i%5=0)printf(n);printf(%10d,ai);#include #includevoid printResult();int main() printf(程序段的输出结果: );printf(n=);printf(n);printResult();printf(n=);printf(n);return 0; void printResult()int i, a20 = 1,1;for (i = 2; i 20; i+)ai = ai - 2 + ai - 1;for (i = 0; i 20; i+)if (i%5 = 0) printf(n);printf(%10d, ai);/*程序运行结果:程序段的输出结果: = 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765=Press any key to continue*/14 下列语句中,正确的是_C_A char a3=abc,1; B char a3=abc,1; C char a3=a,”1”; D char a3=“a”,”1”; 15 若函数能被其他编译单位(源文件)引用,那么函数定义时的存储类型应选用 _ C _A auto B static C extern D register16 下面函数定义有错误的是_A_B_A f1(x1,x2) int x1,x2; 函数体 B f1(x1,x2) int x1;int x2; 函数体 C f1(int x1,x2) 函数体 D f1(int x1,int x2) 函数体17 分析下列程序,输出结果是_#includevoid main() int i,j; char c; for(i=0;i2;i+=1) c=A; for(j=i;j2;j+) c=c+j; printf(c=%cni=%dnj=%dn,c,i,j);#include void printResult();int main() printf(程序段的输出结果: );printf(n=);printf(n);printResult();printf(n=);printf(n);return 0; void printResult()int i, j;char c;for (i = 0; i 2; i += 1)c = A;for (j = i; j 2; j+)c = c + j;printf(c = %cni = %dnj = %dn, c, i, j);/*程序运行结果:程序段的输出结果: =c = Bi = 0j = 2c = Bi = 1j = 2=Press any key to continue*/printf(c=%cni=%dnj=%dn,c,i,j);/退出内外循环后的i,j值18 分析下列程序,输出结果是_/测试实参向形参传值 #include int WarningSign(int secondNumber,int firstNumber) int total; firstNumber-=10; total=secondNumber*firstNumber; return total; main() int firstNumber

温馨提示

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

评论

0/150

提交评论