C语言作业习题讲解.ppt_第1页
C语言作业习题讲解.ppt_第2页
C语言作业习题讲解.ppt_第3页
C语言作业习题讲解.ppt_第4页
C语言作业习题讲解.ppt_第5页
已阅读5页,还剩56页未读 继续免费阅读

下载本文档

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

文档简介

作业习题讲解 动态存储DynamicMemoryAllocation 申请内存函数malloc格式 void malloc unsignedsize 功能 申请长度为size字节的内存区返值 若申请成功 返回所分配的内存区首字节的地址 若申请失败 返回NULL说明 在实际申请内存时 要强制转换 例 申请长度为10的整型数组空间int p p int malloc 10 sizeof int 包含在头文件stdlib h 释放内存函数free格式 voidfree void p 功能 释放p所指的内存空间返值 无说明 p所指的内存区域必须是用函数malloc申请的 否则调用时可能会破坏系统 例 动态申请10个元素的整型数组 include includevoidassign inta intm intk for k 0 k m k a k k 10 for k 0 k m k printf d a k voidmain int p p int malloc 10 sizeof int printf nResult assign p 10 free p 第一次上机作业 1 1 大写字母转换成小写字母if c A c Z a A 32getchar putchar 1 2 求平方根math hsqrt scanf f x 2f 1 3 TemperatureConversionc 5 0 9 f 32 1 4 计算旅途时间 1 5 数字加密 第二次上机作业 2 1 4个数排序if语句t a a b b t 2 2 求1 1 2 1 n 嵌套for循环 2 3 编程计算表达式 data1opdata2的值 switch语句 2 4 用二分法求方程2x3 4x2 3x 6 0在 a b 之间的根double lfwhile 1 break for s 0 i 1 i n i for t 1 j 1 j i j t j s 1 t scanf d c d 2 5 cCalendarDisplay voidmain while 1 ComputedaysfromJan1 Inputvalidity 1 12 whatdayisfirstdayofthismonth Displaycalendarofthismonth while 1 printf Wouldyouliketocontinue y n fflush stdin ch getchar if ch N ch n break intdayinmonth 12 31 28 31 30 31 30 31 31 30 31 30 31 days 0 for i 0 i month 1 i days dayinmonth i intfday 1 thefirstdayof2007ismonday fday days fday 7 printf n printf SUNMONTUEWEDTHUFRISAT n for i 0 i fday i printf for i 1 i dayinmonth month 1 i if i fday 7 0 printf 7d n i elseprintf 7d i printf n 第三次上机作业 3 4 CountCapitalConsonantLetters charstr N MAXLEN for i 0 count 0 i A 3 1 BinarySearch 3 2 Sorting 3 5 Stringconnection welcometo 0 str1 c h i n a 0 c h i n a 0 gets str1 gets str2 for i 0 str1 i 0 i for j 0 str2 j 0 str1 i str2 j str1 i 0 puts str1 3 3 Statistics NOMTENPHSUMAVE 90 197879227692N292919027391Y390818225384N defineN30 defineM3ints N M 4 for i 0 i N i printf Inputnumber scanf d for i 0 i 90 count s i M 2 s i M 1 M if count M s i M 3 Y elses i M 3 N 3 6 c十六进制转换十进制 Pf4 1 0 str num f 4 1 0 3 6 c十六进制转换十进制 Pf4 1 0 str num f 4 1 0 for number 0 i 0 num i 0 i if num i A 3 7 矩阵运算 sum 0 for i 0 i n i for j 0 j n j if i n 1 j n 1 i j n 1 continue elsesum a i j 4 1 c数组转置 第四次上机作业 inta N N printf Inputn n 6 scanf d printf Displaythearray n for i 0 i n i for j 0 j n j printf 4d a i j printf n transpose a n voidtranspose inta N intn voidtranspose int a N intn 4 1 c数组转置 for i 0 i n i for j 0 j n j t a i j a i j a j i a j i t 1 5 9 13 2 3 4 i 0 第四次上机作业 6 10 14 7 8 4 1 c数组转置 for i 0 i n i for j 0 j n j t a i j a i j a j i a j i t 1 9 13 3 4 i 1 5 2 2 5 6 10 14 7 8 4 1 c数组转置 for i 0 i n i for j 0 j n j t a i j a i j a j i a j i t 1 9 13 3 4 i 2 5 2 2 5 3 9 7 10 11 12 15 6 10 14 7 8 4 1 c数组转置 for i 0 i n i for j 0 j n j t a i j a i j a j i a j i t 1 9 13 3 4 i 3 5 2 2 5 3 9 7 10 11 12 15 4 13 14 8 15 12 16 4 1 c数组转置 for i 0 i n i for j 0 j i j t a i j a i j a j i a j i t i 0 4 1 c数组转置 for i 0 i n i for j 0 j i j t a i j a i j a j i a j i t 5 2 i 1 4 1 c数组转置 for i 0 i n i for j 0 j i j t a i j a i j a j i a j i t 5 9 2 3 i 2 10 7 4 1 c数组转置 for i 0 i n i for j 0 j i j t a i j a i j a j i a j i t 5 9 13 2 3 4 i 3 10 7 14 8 15 12 4 2 学生信息 voidsort charname LEN floats inti j k floatt charstring LEN for i 0 is k k j if k i t s i s i s k s k t strcpy string name i strcpy name i name k strcpy name k string input sort display search voidmain intnum charname N LEN student LEN floatscore N input name score display name score sort name score display name score printf Inputstudentnameyouwanttosearch fflush stdin gets student num search name student if num 1 puts SearchFailure elseprintf s sscoreis 2f n student score num voidinput charname LEN floats inti printf Inputnameandscore n for i 0 i N i scanf s name i scanf f intsearch charname LEN chars inti for i 0 i N i if strcmp name i s 0 returni return 1 4 3 c字符串处理 SAMPLEOUTPUT 1 Reverseastring 2 Deleteacharacterinastring Pleaseinputyourchoice 1Enterastring holidayReversedstringis yadilohWouldyouliketocontinue y n Y1 Reverseastring 2 Deleteacharacterinastring Pleaseinputyourchoice 2Enterastring oneworld onedreamEnteracharacteryouwanttodelete oDeletedstringis newrld nedreamWouldyouliketocontinue y n N voidmain while 1 printf Wouldyouliketocontinue y n fflush stdin choice2 getchar if choice2 N choice2 n break printf 1 Reverseastring n printf 2 Deleteacharacterinastring n printf Pleaseinputyourchoice scanf d switch choice1 case1 break case2 break Reversethestring a k j b c i j 0 1 2 3 i 10 9 8 7 Algorithm whilej i exchangethecorrespondingcharacter for i 0 str i 0 i i for j 0 j i j i c str i str i str j str j c i strlen str i voidstrreverse chars Deleteacharacterinastring oneworld onedream 0 str c o n e w r l d n e d r e a m 0 voiddelchar chars charc Deleteacharacterinastring c o n e w r l d n e d r e a m 0 n e w r l d n e d r e a m 0 4 4 分别用函数和带参数的宏 从三个数中找出最大数 带参数的宏方式实现 include defineMAX a b c a b a b c a b a b cvoidmain inta b c printf Input3integers n scanf d d d 5 1 c数据移位有n n 10 个整数 使前面各数顺序向后移m m n 个位置 最后m个数变成最前面的m个数 如图所示 编写函数shift a n m 实现上述功能 在主函数中输入n个整数和输出调整后的n个数 第五次上机作业 n m n m n m n m n m n m n m n m n m n m n m n m 5 1 c数据移位有n n 10 个整数 使前面各数顺序向后移m m n 个位置 最后m个数变成最前面的m个数 如图所示 编写函数shift a n m 实现上述功能 在主函数中输入n个整数和输出调整后的n个数 include includevoidshift inta intn intm voidmain inti n m int a printf Inputn n 10 scanf d voidshift inta intn intm inti j t for i 0 i 0 j a j 1 a j a 0 t 5 2 c字符串排序在主函数中输入10个不等长的字符串放入二维字符数组中 编写函数sort 利用指针数组对其排序 在主函数中输出排好序的字符串 include include defineN10 defineM80voidsort char s voidmain chars N M strp N inti printf Input10strings n for i 0 i N i gets s i for i 0 i N i strp i s i sort strp printf Outputsortedstrings n for i 0 i N i puts strp i voidsort char s inti j k char p for i 0 i0 k j f i j p s k s k s i s i p 2 Darrayvs Pointerarrays chara 5 9 a 0 a 1 a 2 a 3 a 4 char a 5 a 0 a 1 a 2 a 3 a 4 5 2 c字符串排序在主函数中输入10个不等长的字符串放入二维字符数组中 编写函数sort 利用指针数组对其排序 在主函数中输出排好序的字符串 include include include defineM80voidsort char s intn voidmain char strp inti n printf Howmanystrings n scanf d voidsort char s intn inti j k char p for i 0 i0 k j f i j p s k s k s i s i p 5 3学生成绩统计 defineN30 defineM5ints N M 3 for i 0 i N i s i 0 i 1 printf d i 1 for j 1 j M j scanf d ave average s findfail s findgood s voidfindfail ints M 3 inti j count sum for i 0 i 2 s i M 2 N intaverage inta M 3 inti sum 0 for i 0 i N i sum a i 1 return sum N voidfindgood ints M 3 inti j count for i 0 i 85 count if count M s i M 1 90 s i M 2 Y 5 4 cMatrixMultiply 30 36 42 50 5 4 cMatrixMultiply 30 36 42 50 66 81 96 116 5 4 cMatrixMultiply 30 36 42 50 66 81 96 116 102 126 150 182 5 4 cMatrixMultiply 30 36 42 50 66 81 96 116 102 126 150 182 138 171 204 248 5 4 cMatrixMultiply 30 36 42 50 66 81 96 116 102 126 150 182 138 171 204 248 for i 0 i m i for j 0 j m j c i j 0 for k 0 k n k c i j a i k b k j 5 5 cCountKeywords IFheisill theni elseyouneedandgood IfheThenelseokandhellohowareandelse i f 0 1 5 5 cCountKeywords IFheisill theni elseyouneedandgood IfheThenelseokandhellohowareandelse h e 0 1 5 5 cCountKeywords IFheisill theni elseyouneedandgood IfheThenelseokandhellohowareandelse i s 0 1 5 5 cCountKeywords IFheisill theni elseyouneedandgood IfheThenelseokandhellohowareandelse i l l 1 0 5 5 cCountKeywords IFheisill theni elseyouneedandgood IfheThenelseokandhellohowareandelse t h e 1 n 0 1 5 5 cCountKeywords IFheisill theni elseyouneedandgood IfheThenelseokandhellohowareandelse 2 2 3 3 voidmain chartext LEN keys M N intn i num M 0 gets text scanf d voidcount char text charkeys N intn int num inti j k charwords N for i 0 text i 0 j 0 while text i text i text i i while text i 第六次上机作业 成绩处理定义一个结构体类型 从键盘输入20个学生的学号和数学 英语 计算机成绩 要求 1 计算并输出每一个学生三门课的平均成绩 2 计算并输出每门课程的平均成绩 3 查找并输出平均成绩最高的学生的记录 4 按平均成绩从高到低输出学生的成绩单 学号 数学 英语 计算机成绩和平均成绩 typedefstructstudent longnum floatscore M floataverage STU 第六次上机作业 成绩处理定义一个结构体类型 从键盘输入20个学生的学号和数学 英语 计算机成绩 要求 1 计算并输出每一个学生三门课的平均成绩 2 计算并输出每门课程的平均成绩 3 查找并输出平均成绩最高的学生的记录 4 按平均成绩从高到低输出学生的成绩单 学号 数学 英语 计算机成绩和平均成绩 文件将上面成绩处理程序中从键盘读入的学生原始数据保存到文件studata dat文件中 从文件中读入数据后 按平均成绩排序 并将排好序的数据保存到文件sorted dat中 typedefstructstudent longnum floatscore M floataverage STU voidmain intn i j STU s printf Pleaseinputthenumberofstudents n scanf d voidcalstuave intn STU s inti j for i 0 i n i s i average 0 for j 0 j M j s i average s i score j s i average M for i 0 i n i printf Number ldAverage 2f n s i num s i average voidcalcourseave intn STU s floatave M inti j for j 0 j M j ave j 0 for i 0 i n i ave j s i score j ave j n printf MathAverage 2f nEnglishAverage 2f nComputerAverage 2f n ave 0 ave 1 ave 2 voidfind intn STU s inti max 0 for i 1 is max average max i puts thehighestaveragestudentis n printf ld 2f 2f 2f 2f n s max num s max score 0 s max score 1 s max score 2 s max average voidsort intn STU s inti j k STUt for i 0 is k average k j if i k t s i s i s k s k t voidsave intn STU s char fname FILE fp inti if fp fopen fname wb NULL printf Fileopenerror n exit 0 fwrite voidload int n STU s char fname FILE fp inti if fp fop

温馨提示

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

评论

0/150

提交评论