C语言程序设计实验四.pdf

上传人:zh****71 文档编号:126719206 上传时间:2020-03-27 格式:PDF 页数:22 大小:176.06KB
返回 下载 相关 举报
C语言程序设计实验四.pdf_第1页
第1页 / 共22页
C语言程序设计实验四.pdf_第2页
第2页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《C语言程序设计实验四.pdf》由会员分享,可在线阅读,更多相关《C语言程序设计实验四.pdf(22页珍藏版)》请在金锄头文库上搜索。

1、电子科技大学计算机 学院 标 准 实 验 报 告 实验 课程名称C 语言程序设计 电子科技大学教务处制表 电 子 科 技 大 学 实验报告四 学生姓名 学 号 指导教师 王瑞锦 实验地点 实验日期 年月日 一 实验室名称 软件实验室 二 实验项目名称 函数的应用及预处理 三 实验学时 2 四 实验原理 使用 Turbo C 软件 简称TC 在微型计算机上 对其程序进行编辑 编译 连接与 运行 Turbo C 是一个集成环境 它具有方便 直观 易用的界面和丰富的库函数 通过 上机练习掌握在TC 环境下编辑 编译 连接 和运行C 程序的方法和过程 五 实验目的 1 掌握 C语言中定义函数的方法 2

2、 掌握函数传值调用的方法 3 掌握函数传址调用的方法 4 掌握递归函数的设计方法 5 掌握命令行参数的使用方法 6 掌握函数在不同文件中的编译 链接方法 六 实验内容 编程实验 完成以下上机内容 调试运行程序并完成报告 1 教材第七章习题7 4 2 编写一个实现冒泡法排序的函数 并在主函数中从键盘上输入6 个数后进行排序 输出 3 教材第七章习题7 11 4 教材第七章习题7 15 5 教材第七章习题7 24 6 求解汉渃塔 tower of Hanoi 问题 在一块平板上立有3 根立柱 从左到右分 别标记为 A B C 最初在A柱上放有6个大小不等的圆盘 并且大盘在下面 小盘在上面 要求将这

3、些盘从A移到 C 可以借助B柱 条件是 每次只能移动一个盘 并且不允许把 大盘放在小盘的上面 提示 利用函数的递归调用 七 实验器材 设备 元器件 pc 硬件要求 CPU PII 以上 64M 内存 1OOM 硬盘空间即可 软件要求 DOS3 0 以上 Windows98 Me XP NT 2000 八 实验步骤 实验编程与运行结果 编写一个求两个整数的最小公倍数的函数 两个整数由键盘输入 用主函数调用这个函 数 并输出结果 程序文件名为7 4 c 源程序清单如下 include lcd int a int b int temp num1 num2 num1 a num2 b while nu

4、m2 0 temp num1 num2 num1 num2 num2 temp return a b num1 main int t x y printf Please input two integers scanf d d if x y t x x y y t printf lcd d d d n x y lcd x y 运行结果 编写一个实现冒泡法排序的函数 并在主函数中从键盘上输入6 个数后进行排序输出 程序文件名为testf3 c 源程序清单如下 main int i j a 7 int temp clrscr for i 1 i 7 i printf nInput a d i sc

5、anf d for i 1 i 6 i for j 1 j 7 j if a j a j 1 temp a j a j a j 1 a j 1 temp for i 1 i 7 i printf n 5d a i 运行结果 编写一个通过函数调用把输入的小写字符串变为大写字符串的程序 程序文件名为7 11 c 源程序清单如下 include lowch to upch char str while str 0 if str a str main char str printf Please input a string gets str lowch to upch str puts str 运行

6、结果 编写一个求三个整数中最小值整数的函数 主程序要求三个整数由命令行参数输入 调 用该函数并输出结果 程序文件名为7 15 c 源程序清单如下 include include include int search min int a int b int c if a b if a c return a else return c else if b c return b else return c main int argc char argv int a b c if argc 4 printf Needs arguments n printf Command line example x

7、t7 15 3 2 1 n exit 0 a atoi argv 1 b atoi argv 2 c atoi argv 3 printf min d d d d n a b c search min a b c 运行结果 编写一个程序 先定义一个将字符大写 小写和数字进行分类的带参数宏 然后对从键 盘输入的一系列字符进行分类计数 并输出计数的结果 程序遇到字符 结束 程序文件名为7 24 c 源程序清单如下 include define INUP c c A void hanoi int n char one char two char three if n 1 move one three

8、 else hanoi n 1 one three two move one three hanoi n 1 two one three main int m printf Input the number of diskes scanf d printf The step to moving 3d diskes n m hanoi m A B C 运行结果 九 总结及心得体会 十 对本实验过程及方法 手段的改进建议 报告评分 指导教师签字 电子科技大学计算机 学院 标 准 实 验 报 告 实验 课程名称C 语言程序设计 电子科技大学教务处制表 电 子 科 技 大 学 实验报告五 学生姓名 学

9、 号 指导教师 王瑞锦 实验地点 实验日期 年月日 一 实验室名称 软件实验室 二 实验项目名称 指针 三 实验学时 2 四 实验原理 使用 Turbo C 软件 简称TC 在微型计算机上 对其程序进行编辑 编译 连接与 运行 Turbo C 是一个集成环境 它具有方便 直观 易用的界面和丰富的库函数 通过 上机练习掌握在TC 环境下编辑 编译 连接 和运行C 程序的方法和过程 指针一般指向一个函数或一个变量 在使用一个指针时 一个程序既可以直接使用这 个指针所储存的内存地址 又可以使用这个地址里储存的变量或函数的值 和 int n1 n2 n3 int p1 p2 p3 printf inp

10、ut three interger n1 n2 n3 scanf d d d p1 p2 p3 if n1 n2 swap p1 p2 if n1 n3 swap p1 p3 if n2 n3 swap p2 p3 printf Now the order is d d d n n1 n2 n3 void swap int p1 int p2 int temp temp p1 p1 p2 p2 temp include include void main void swap char char char str1 20 str2 20 str3 20 printf input three li

11、ne n gets str1 gets str2 gets str3 if strcmp str1 str2 0 swap str1 str2 if strcmp str1 str3 0 swap str1 str3 if strcmp str2 str3 0 swap str2 str3 printf Now the order is n printf s n s n s n str1 str2 str3 void swap char p1 char p2 char p 20 strcpy p p1 strcpy p1 p2 strcpy p2 p 题 2 输入 10 个整数 将其中最小的数

12、和第一个数对换 把最大的数与最后一个数对换 编写三个程序 1 输入十个数 2 进行处理 3 输出十个数 include void main void input int void max min value int void output int int number 10 input number max min value number output number void input int number int i printf input 10 numbers for i 0 i 10 i scanf d void max min value int number int max m

13、in p temp max min number for p number 1 p max max p temp number 9 number 9 max max temp else if p min min p temp number 0 number 0 min min temp void output int number int p printf Now they are for p number p number 10 p printf d p printf n 题 3 有 n 个人围城一圈 从第一个人开始报数 从1 到 3 报数 凡报到3 的人退出圈子 问最后留下的是原来的第几号

14、的那个人 include void main int i k m n num 50 p printf input number of person n scanf d p num for i 0 i n i p 1 i 1 以 1 至 n 为序给每人编号 i 0 k 0 m 0 while m n 1 当退出人数比n 1 少时 执行循环体 if p i 0 k if k 3 p i 0 k 0 m i if i n i 0 报数到尾后 i 恢复为 0 while p 0 p printf The last one is N0 d n p 题 4 输入一行文字 找出其中大写字母 小写字母 空格

15、数字以其他的字符各有多少 include void main int upper 0 lower 0 digit 0 space 0 other 0 i 0 char p s 20 printf input string while s i getchar n i p while p n if A p else if a p else if p space else if p 0 digit else other p printf upper case d lower case d upper lower printf space d digit d other d n space digit

16、 other 5 将一个 5 5 的矩阵中最大的元素放在中心 4 个角分别放4 个最小的元素 顺序为从坐 到右 从上到下顺序依次从小到大存放 编写一个函数实现之 用main 函数调用 include void main void change int int a 5 5 p i j printf input martix n for i 0 i 5 i for j 0 j 5 j scanf d p change p printf Now martix n for i 0 i 5 i for j 0 j 5 j printf d printf n void change int p 交换函数的实现 int i j temp int pmax pmin int pmax pmin pmax p pmin p pmax a 0 0 pmin a 0 0 for i 0 i 5 i 寻找最大值和最小值 for j i j 5 j if pmax p 5 i j if pmax p 5 i j if pmin a i j pmin p 5 i j pmin a i j temp p 12 最大

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 其它文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号