成
绩
Guangxi University of Science and Technology
C语言程序设计实验报告
院 〔系〕: 计算机学院
专 业: 通信工程
班 级: 通信132班
学 生 姓 名: 王睿
学 号: 202100402042
课 时 间: 20212021〔1〕
指 导 老 师: 王智文
实验 C程序运行环境简单程序运行
实验目
〔1〕解计算机系统根操作方法学会独立该系统
〔2〕解该系统编辑编译连接运行C 程序
〔3〕通运行简单C 程序初步解C 程序特点
二实验容
1程序功:计算x*y值结果输出程序中8处错误尝试改正机调试〔程序文件名sy1_2cpp存盘〕
#include
void main
{ int x2y3a
Ax*y
print (ada)
printf(\n’)
}
2 程序功:输入圆半径计算圆面积结果输出程序中9处错误尝试改正机调试〔程序文件名sy1_3cpp存盘〕
#include stdioh
int mian()
float r area
printf(input r:\n)
scanf(fr)
area314159r*r
printf(rf areaf\narea)
3 编写程序实现输入三整数显示出中值
三实验分析总结
⑴ 总结C程序结构书写规
⑵ 阅读:附件AVisual C++环境运行C程序方法
特提示:程序编译时发现错误时应逐改正改错误重新编译时错误会引起错误信息
四相关实验程序列表
1
#include
void main()
{ int x2y3a
ax*y
printf (ada)
printf(\n)
}
2
#include
int main()
{float r area
printf(input r:\n)
scanf(fr)
area314159*r*r
printf(rf areaf\narea)
}
3
#include
int main()
{
int max(int xint yint z)
int abcd
scanf(ddd&a&b&c)
dmax(abc)
printf(maxd\nd)
return 0
}
int max(int xint yint z)
{
int w
if(x>y)wx
else wy
int q
if(w>z)qw
else qz
return(q)
}
五实验心
通次学学会编辑编译连接运行C 程序初步解C 程序特点感快乐会努力根底学
实验二 根数类型运算表达式
实验目
〔1〕掌握C 语言数类型熟悉定义整型实型变量赋值方法
〔2〕掌握类型数间赋值规律
〔3〕 学会C 关算术运算符包含运算符表达式特加〔++〕减〔〕运算符
〔4〕进步熟悉C 程序编辑编译连接运行程
二实验容
1 [变量定义求完成程序]
面出运行程序缺少局部语句请右边提示补充完整缺少语句
#include stdioh
main()
{
*定义整型变量a b*
*定义实型变量i j*
a5
b6
i314 ji*a*b
printf(adbdifjf\n a b i j)
}
[具体操作步骤]
〔1〕代码补充完整
〔2〕VC++运行通写出运行结果
2 [显示屏显示指定字符]
求编写程序显示屏显示容〔全半角字符包含空格〕:C\ABCTXT
[提示] 注意转义字符程序中
3[赋值表达式赋值语句写出程序运行结果]
阅读面程序写出运行结果:
#include stdioh
main()
{ float a
int b c
char d e
a35
ba
c330
dc
e'\141'
printf(fddcc abcde)
}
运行结果:
[提示]赋值语句具类型转换功会改变数
4 [根运算写出程序运行结果]
阅读面程序写出运行结果:
#include stdioh
main()
{ int a b c
float d15 e f
a357
b1510
cb++
e1510
fd10
printf(dddfff abcdef)
}
运行结果:
[提示]法分整普通法分
三实验分析总结
⑴ 总结定义C程序数类型运算符优先级赋值表达式作
⑵ 程序4中b++改写++b结果会变化?
四相关实验程序列表
1
#include stdioh
int main()
{
int a5
int b6
float i314
float ji*a*b
printf(adbdif\n a b i)
printf(jf\nj)
return 0
}
2
#include stdioh
int main()
{
printf(C\\ABCTXT)
}
3
#include stdioh
main()
{ float a
int b c
char d e
a35
ba
c330
dc
e'\141'
printf(fddcc abcde)
}
4
#include stdioh
main()
{ int a b c
float d15 e f
a357
b1510
cb++
e1510
fd10
printf(dddfff abcdef)
}
五实验心
通次学学会整型实型变量赋值方法C 关算术运算符包含运算符表达式会稳固学知识继续努力
实验三 根输入输出
实验目
〔1〕熟练掌握putchargetcharprintfscanf 函数方法
〔2〕掌握种类型数输入输出方法正确种格式转换符
二实验容
1 [字符输入输出]
编程实现键盘输入字符屏幕输出该字符
参考程序:
#include stdioh
main()
{ char ch
ch getchar()
putchar(ch)
}
2 [加法计算]
编程实现键盘输入加法式输出正确结果〔两加数均整数〕
参考程序:
#include stdioh
main()
{ int a b
scanf(d*cd &a&b)
printf(d a+b)
}
3[求圆面积]
键盘输入圆半径r请计算该圆面积〔注:π取314159结果保存两位数位外程序r 0 10000 范围情况输出正确答案〕
参考程序:
#include stdioh
main()
{ float arear
scanf(f&r)
area314159*r*r
printf(02farea)
}
运行结果:
4 [计算摄氏温度值]
键盘输入华氏温度值求格式输出应摄氏温度值精确数点两位
数学公式描述:
参考程序:
#include
main()
{ float fc
scanf(f&f)
c509*(f32)
printf(2fc)
}
运行结果:
三实验分析总结
⑴ 实验运行结果分析
⑵ 程序2中*c含义什?
(3) 程序4中c509*(f32)什50果改5运行结果会什?
四相关实验程序列表
1
#include stdioh
int main()
{ char ch
ch getchar()
putchar(ch)
putchar('\n')
}
2
#include stdioh
main()
{ int a b
scanf(d*cd &a&b)
printf(d\n a+b)
}
3
#include stdioh
main()
{ float arear
scanf(f&r)
area314159*r*r
printf(02f\narea)
}
4
#include
main()
{ float fc
scanf(f&f)
c509*(f32)
printf(2f\nc)
}
五实验心
通次学熟悉putchargetcharprintfscanf 函数方法种类型数输入输出够正确种格式转换符感受学c语言乐趣
实验四 序结构程序设计
实验目
〔1〕熟练掌握序结构程序设计设计方法
〔2〕掌握赋值语句正确方法
〔3〕学进步调试程序
二实验容
1 [sizeof运算符方法]
程序测试C中类型数占存字节数运行体会sizeof运算符方法
sizeof运算结果int型整数表示求解象存储占存字节数
#include
void main()
{ int a1
printf(Size of char is d\n sizeof(char))
printf(Size of short is d\n sizeof(short))
printf(Size of int is d\n sizeof(int))
printf(Size of long is d\n sizeof(long))
printf(Size of float is d\n sizeof(float))
printf(Size of double is d\n sizeof(double))
printf(Size of bool is d\n sizeof(bool))
printf(\n)
printf(Size of a is d\n sizeof(a))变量类型占存字节数
printf(Size of 20*10 is d\n sizeof(20*10))表达式运算结果类型占字节数
Size of char is 1
Size of short is 2
Size of int is 4
Size of long is 4
Size of float is 4
Size of double is 8
Size of bool is 1
Size of a is 4
Size of 20*10 is 8
size of Liu Dehua is 10
Press any key to continue
printf(size of Liu Dehua is d\n sizeof(Liu Dehua))字符串占存字节数
}
参考结果
2 [分析程序输出结果]
阅读分析程序输出结果机调试实际输出分析结果否致
#include
void main()
{ char ch65
int a1234
long g12345678
float f123456789
double d987654321E5
printf(dc5d3c\nchchchch)
printf(08d0+8d8do7x10ldld \naaaagg)
printf(0103f103f103ffeg\nffffff)
printf(0+155lf155lf155lflfle\nddddd)
printf(10s\n20s\n103s\ns\nHelloworldHelloworldHelloworldHelloworld)
}
参考结果
65A 65 A
00001234+0001234 12342322 bc614e 123456782367460
000123457123457 1234571234567871234568e+002123457
+009876543210098765432100 987654321009876543210009876543e+005
Helloworld
Helloworld
Hel
Helloworld
3[程序修改]
程序功:公式计算输出x值中ab值键盘输入
x2ab(a+b)2
请纠正程序中存错误程序实现功
#include
void main()
{ int ab
float x
scanf(ddab)
xab(a+b)(a+b)
printf(xd\nx)
}
4 [编写调试程序]
写程序测试带数floatdouble型数printf函数输出时数局部截断四舍五入?
三实验分析总结
⑴ 实验运行结果分析
⑵ 程序1中Liu Dehuasizeof值什109?
四相关实验程序列表
1
#include
void main()
{ int a1
printf(Size of char is d\n sizeof(char))
printf(Size of short is d\n sizeof(short))
printf(Size of int is d\n sizeof(int))
printf(Size of long is d\n sizeof(long))
printf(Size of float is d\n sizeof(float))
printf(Size of double is d\n sizeof(double))
printf(Size of bool is d\n sizeof(bool))
printf(\n)
printf(Size of a is d\n sizeof(a))变量类型占存字节数
printf(Size of 20*10 is d\n sizeof(20*10))表达式运算结果类型占字节数
printf(size of Liu Dehua is d\n sizeof(Liu Dehua))字符串占存字节数
}
2
#include
void main()
{ char ch65
int a1234
long g12345678
float f123456789
double d987654321E5
printf(dc5d3c\nchchchch)
printf(08d0+8d8do7x10ldld \naaaagg)
printf(0103f103f103ffeg\nffffff)
printf(0+155lf155lf155lflfle\nddddd)
printf(10s\n20s\n103s\ns\nHelloworldHelloworldHelloworldHelloworld)
}
3
#include
void main()
{
int ab
float x
scanf(dd&a&b)
x(float)a*b((a+b)*(a+b))
printf(xf\nx)
}
4
#include
int main()
{float a15555555555
double b15555555555
printf(ff\nab)
return 0
}
五实验心
通次学根掌握序结构程序设计设计方法赋值语句正确方法会努力熟悉便更设计程序
实验五 选择结构程序设计
实验目
〔1〕解C 语言表示逻辑方法〔0 代表假〞非0 代表真〞〕
〔2〕学会正确逻辑运算符逻辑表达式
〔3〕熟练掌握if 语句switch 语句
〔4〕结合程序掌握简单算法
二实验容
1 [判断点否圆]
键盘输入点坐标求编程判断点否单位圆点圆输出Y圆输出N数点3位精度进行判断
[提示]〔1〕面点圆关系分圆圆圆外三种题求判断否圆〔2〕判断两实数相等采判断两实数差绝值规定误差精度〔题0001〕方法实现
参考程序:
#include stdioh
#include mathh
main()
{ float ab
scanf(ff&a&b)
if(fabs(a*a+b*b1)<1e3)
printf(Y\n)
else printf(N\n)
}
2 [求数位数]
键盘输入9 位正整数求输出位数
[提示]划定位数二位数…九位数范围然if … else if … else 语句实现判断
参考程序:
#include stdioh
main()
{ int nplace
scanf(ld&n)
if(n>99999999) place9
else if(n>9999999) place8
else if(n>999999) place7
else if(n>99999) place6
else if(n>9999) place5
else if(n>999) place4
else if(n>99) place3
else if(n>9) place2
else place1
printf(ld\nplace)
}
3[程序修改]
程序功:输入1字母输出该字母前序字母该字母该字母序字母例:输入g输出fgh输入a输出zab输入M输出LMN输入Z输出YZA
#include
void main()
{ char chc1c2
printf(Enter a character)
chgetchar()
if((ch>'a')||(ch<'z'))
ch32
c1ch1
c2ch+1
if(ch'A') c1ch+25
else if(ch'Z') c2ch25
putchar(c1)
putchar(ch)
putchar(c2)
putchar('\n')
}
请纠正程序中存错误程序实现功
4 [字符变换]
键盘输入5 字符中写字母变成写类型字符变输入序逐输出
[提示]面代码实现键盘读入字符题意处理输出
char a
agetchar()
if(a>'A' && a<'Z') aa+32
putchar(a)
现题目求处理5 字符做呢?请思考……
5 [简单计算器]
面程序实现简单运算器〔保存两位数点〕果键盘输入10+50计算机输出结果6000果输入8*6计算机输出4800果输入204计算机输出500果输入86计算机输出200请空处填适代码运行通提交
#include stdioh
main()
{ float abc
char op
scanf(fcf )
switch (op)
{ case '+'
case ''
case '*'
case ''
default printf(error)
return
}
printf(result c)
}
6 [编写调试程序]
输入三角形三边长abc求面积三角形面积公式:Area中S(a+b+c)2〔求判断边长值合法性三角形构成性〕
参考程序:
#include stdioh
#include mathh 数学函数库头文件
void main()
{
double abcsarea
printf (请输入三角形三边长:\n)
scanf(lflflf&a&b&c)
if(a<0||b<0||c<0) 边边长等0
printf(边长必须正\n)
else
if(a+b>c && b+c>a && a+c>b) 意两边第三边
{
s(a+b+c)2
areasqrt(s*(sa)*(sb)*(sc)) double sqrt(double x)求方根值
printf(arealf\narea)
}
else
printf(构成三角形\n)
}
三实验分析总结
⑴ 实验运行结果分析
⑵ 程序1中误差精度〔设置000001〕样修改程序?
⑶ 程序4中求反序输出写字母样修改程序?
四相关实验程序列表
1
#include stdioh
#include mathh
main()
{ float ab
scanf(ff&a&b)
if(fabs(a*a+b*b1)<1e3)
printf(Y\n)
else printf(N\n)
}
2
#include
main()
{ int nplace
scanf(ld&n)
if(n>99999999) place9
else if(n>9999999) place8
else if(n>999999) place7
else if(n>99999) place6
else if(n>9999) place5
else if(n>999) place4
else if(n>99) place3
else if(n>9) place2
else place1
switch(place)
{
case 9printf(ld\n)break
case 8printf(ld\n)break
case 7printf(ld\n)break
case 6printf(ld\n)break
case 5printf(ld\n)break
case 4printf(ld\n)break
case 3printf(ld\n)break
case 2printf(ld\n)break
case 1printf(ld\n)break
default(enter date error\n)
}
}
3
#include
void main()
{ char chc1c2
printf(Enter a character)
chgetchar()
if(((ch>'b')&&(ch<'y'))||((ch>'B')&&(ch<'Y')))
{c1ch1
c2ch+1}
else if((ch'A')||(ch'a')) {c1ch+25c2ch+1}
else if((ch'Z')||(ch'z')) {c2ch25c1ch1}
else printf(reEnter a character)
putchar(c1)
putchar(ch)
putchar(c2)
putchar('\n')
}
4
#include
main()
{
char abcde
scanf(ccccc&a&b&c&d&e)
if(a<'Z' && a<'A') aa+32
if(a<'Z' && a<'A') aa+32
if(a<'Z' && a<'A') aa+32
if(a<'Z' && a<'A') aa+32
if(a<'Z' && a<'A') aa+32
printf(cccccabcde)
}
5
#include stdioh
int main()
{ float abc
char op
scanf(fcf&a&op&b)
switch (op)
{ case '+'ca+bbreak
case ''cabbreak
case '*'ca*bbreak
case ''cabbreak
default printf(error)
break
}
printf(result2fc)
}
6
#include stdioh
#include mathh 数学函数库头文件
void main()
{
double abcsarea
printf (请输入三角形三边长:\n)
scanf(lflflf&a&b&c)
if(a<0||b<0||c<0) 边边长等0
printf(边长必须正\n)
else
if(a+b>c && b+c>a && a+c>b) 意两边第三边
{
s(a+b+c)2
areasqrt(s*(sa)*(sb)*(sc)) double sqrt(double x)求方根值
printf(arealf\narea)
}
else
printf(构成三角形\n)
}
五实验心
通次学解C 语言表示逻辑方法学会正确逻辑运算符逻辑表达式根掌握掌握if 语句switch 语句会快熟悉培养独立思考力
实验六 循环结构程序设计〔〕
实验目
〔1〕熟悉掌握while 语句dowhile 语句for 语句实现循环方法
〔2〕掌握程序设计中循环实现常算法〔穷举迭代递推等〕
二实验容
1 [计算阶]
输入正整数n计算n结果长整型数表示〔注n1*2*3**n〕
参考程序:
#include
main()
{ long in1a
scanf(ld&a)
for(i1i
}
2 [计算数列]
数列1357911……现求键盘输入n计算输出该数列前n 项
参考程序:
#include
main()
{ long nsum0it1
scanf(ld&n)
for(i1i
tt+2
}
printf(ld\nsum)
}
#include
main()
{ long nsum
scanf(ld&n)
sumn*n
printf(ldsum)
}
3[累加行字符中数字]
键盘输入行字符〔总字符数1100均回车符表示结束〕中数字字符代表数值累加起输出结果
[提示]〔1〕面程序段逐读入键盘输入行字符
char ch
while((chgetchar())'\n')
{……}
〔2〕数字字符转应数值ach'0'
参考程序:
#include
main()
{ char c
int s0a
while((cgetchar())'\n')
{ if(c>'0'&&c<'9')
{ ac48
ss+a
}
}
printf(ds)
}
4 [求公约数]
键盘输入两正整数mn〔mn 长整数表示〕计算公约数
[[提示]公约数整m 整n 数题目求满足条件
参考程序:
#include
main()
{ long rmntemp
scanf(ldld&m&n)
while(m0)
{ rnm
nm
mr
}
printf(ld\nn)
}
#include
main()
{ long mntis
scanf(ldld&m&n)
tm>nnm
for (iti>1i)
{ if (mi0&&ni0)
{ si break }
}
printf(ds)
}
5 [计算数列]
数列:编程实现键盘输入n计算输出数列前n 项〔结果保存四位数〕
参考程序:
#include
main()
{ int itn
float a2b1s0
scanf(d&n)
for(i1i
taaa+bbt
}
printf(4f\ns)
}
6 [百万富翁]
百万富翁遇陌生陌生找谈换钱方案该方案:天m 元第天需分钱第二天m 元2 分钱第三天m 元4分钱次类推天钱前天两倍直月(38)天百万富翁快乐欣然接受契约现求编写程序键盘输入m计算少天百万富翁开始亏钱
参考程序:
#include
#include
main()
{ int nmi
scanf (d&m)
for (i1i<38i++)
if (001*(pow(2i1)1)i*m>0) break
printf(di1)
}
三实验分析总结
⑴ 实验运行结果分析
⑵ 程序2中数列1357911……改1917253341……样修改程序?
⑶ 程序5中换数列〔:〕样修改程序?
四相关实验程序列表
1
#include
main()
{ long in1a
scanf(ld&a)
for(i1i
}
2
#include
main()
{ long nsum0it1
scanf(ld&n)
for(i1i
tt+2
}
printf(ld\nsum)
}
#include
main()
{ long nsum0it1
scanf(ld&n)
for(i1i
tt+8
}
printf(ld\nsum)
}
3
#include
main()
{ char c
int s0a
while((cgetchar())'\n')
{ if(c>'0'&&c<'9')
{ ac48
ss+a
}
}
printf(ds)
}
4
#include
main()
{ long rmntemp
scanf(ldld&m&n)
while(m0)
{ rnm
nm
mr
}
printf(ld\nn)
}
5
#include
main()
{ int itn
float a2b1s0
scanf(d&n)
for(i1i
taaa+bbt
}
printf(4f\ns)
}
修改
#include
main()
{int sign1
int in
float a1b1s0pi0
scanf(d&n)
for(i1i
ss+ab
bb+3
ss*(sign)
}
printf(4f\ns)
}
6
#include
#include
main()
{ int nmi
scanf (d&m)
for (i1i<38i++)
if (001*(pow(2i1)1)i*m>0) break
printf(di1)
}
五实验心
通次学掌握while 语句dowhile 语句for 语句实现循环方法程序设计中循环实现常算法感受算法程序设计中重性
实验七 循环结构程序设计〔二〕
实验目
〔1〕进步熟悉掌握while 语句dowhile 语句for 语句实现循环方法
〔2〕掌握程序设计中重循环
二实验容
1 [印菱形]
键盘输入正数n求输出2*n+1 行菱形图案求菱形左边紧屏幕左边
*
***
*****
*******
*****
***
*
参考程序:
#include stdioh
#include mathh
main()
{ int nijk
scanf(d&n)
for(i1i<2*n+1i++)
{ kabs(n+1i)
for(j1j
printf(\n)
}
}
2 [找出满足求数字]
输出1 9999 中7 整少位数字5 数字输出时行数字序输出
[提示]判断数中否数字5首先掌握拆数字方法种行算法:
整数aa10a 位数然aa10a 中位数字述程重复原数a 中位数字
参考程序:
#include stdioh
main()
{ int i j
for(i7 i<9999 ii+7)
{ ji
while(j0)
{ if(j105) break
jj10
}
if(j0) printf(d\n i)
}
}
3[求水仙花数]
输出水仙花数〞谓水仙花数〞指3位数位数字立方等该数身:15313+53+33
三实验分析总结
⑴ 实验运行结果分析
四相关实验程序列表
1
#include stdioh
#include mathh
main()
{ int nijk
scanf(d&n)
for(i1i<2*n+1i++)
{ kabs(n+1i)
for(j1j
printf(\n)
}
}
2
#include stdioh
main()
{ int i j
for(i7 i<9999 ii+7)
{ ji
while(j0)
{ if(j105) break
jj10
}
if(j0) printf(d\n i)
}
}
3
#include
void main()
{
int ijkn
printf(water flower numbles is)
for (n100n<1000n++)
{
in100
j(ni*100)10
kn10
if(ni*i*i+j*j*j+k*k*k)
printf(d\nn)
}
}
五实验心
通次学稳固次学while 语句dowhile 语句for 语句实现循环方法根学会程序设计中重循环
实验八 数 组
实验目
〔1〕掌握维数组二维数组定义赋值输入输出方法
〔2〕掌握数组关算法
〔3〕掌握字符数组字符串函数
二实验容
1 [倒序]
键盘输入10 整数倒序输出〔数字间空格分隔〕
2 [印矩阵]
键盘输入3×4 矩阵求输出转置矩阵
参考程序:
#include
main()
{ int a[3][4]b[4][3]ij
for(i0i<3i++)
for(j0j<4j++)
{ scanf(d&a[i][j])
b[j][i]a[i][j]
}
for(i0i<4i++)
{ for(j0j<3j++)
printf(d b[i][j])
printf(\n)
}
}
3[泡排序]
键盘输入10 数泡法〞10 数排序格式求输出代码请填充完整数字间空格分隔
#incude stdioh
main()
{ int a[10] i j t
for(i0i<10i++)
scanf(d___________)
for( ___________)
{ for(j0j<____j++)
if (___________)
{___________}
}
for(i0i<10i++)
printf(d a[i])
}
参考程序:
#include
main()
{ int a[10]
int ijt
for(i0i<10i++)
scanf(d&a[i])
for(i0i<9i++)
{ for(j0j<9ij++)
if(a[j]>a[j+1])
{ ta[j] a[j]a[j+1] a[j+1]t }
}
for(i0i<10i++)
printf(d a[i])
}
4[ 统计数字数]
键盘输入20 整数统计数字数
参考程序:
#includestdioh
main()
{ int a[20]
int itp0
for(i0i<20i++)
{ scanf(d&a[i])
for(t0t
if(ti)
p++
}
printf(dp)
}
5[计算高精度加法]
键盘输入两位数长整数〔行数数长超80 位〕试计算输出两数
#include stdioh
#include stringh
main()
{ int a[100]{0}b[100]{0}c[100]{0}
char s[101]
int i0n10n20max0e0
gets(s)
n1strlen(s)
for(in11i>0i) a[n11i]s[i]'0'
gets(s)
n2strlen(s)
for(in21i>0i) b[n21i]s[i]'0'
if(n1>n2) maxn1
else maxn2
for(i0i
e(a[i]+b[i]+e)10
}
if(c[max]>0) printf(dc[max])
for(imax1i>0i)
printf(dc[i])
}
三实验分析总结
⑴ 实验运行结果分析
(2)程序1中改n数样修改程序?
四相关实验程序列表
1
#includestdioh
#includemathh
main()
{ int a[10]
int i
for(i0i<10i++)
scanf(d&a[i])
for(i9i>0i)
printf(d\na[i])
return 0
}
2
#include
main()
{ int a[3][4]b[4][3]ij
for(i0i<3i++)
for(j0j<4j++)
{ scanf(d&a[i][j])
b[j][i]a[i][j]
}
for(i0i<4i++)
{ for(j0j<3j++)
printf(d b[i][j])
printf(\n)
}
}
3
#include
main()
{ int a[10]
int ijt
for(i0i<10i++)
scanf(d&a[i])
for(i0i<9i++)
{ for(j0j<9ij++)
if(a[j]>a[j+1])
{ ta[j] a[j]a[j+1] a[j+1]t }
}
for(i0i<10i++)
printf(d a[i])
}
4
#includestdioh
main()
{ int a[20]
int itp0
for(i0i<20i++)
{ scanf(d&a[i])
for(t0t
if(ti)
p++
}
printf(dp)
}
5
#include stdioh
#include stringh
main()
{ int a[100]{0}b[100]{0}c[100]{0}
char s[101]
int i0n10n20max0e0
gets(s)
n1strlen(s)
for(in11i>0i) a[n11i]s[i]'0'
gets(s)
n2strlen(s)
for(in21i>0i) b[n21i]s[i]'0'
if(n1>n2) maxn1
else maxn2
for(i0i
e(a[i]+b[i]+e)10
}
if(c[max]>0) printf(dc[max])
for(imax1i>0i)
printf(dc[i])
}
五实验心
通次学掌握数组关算法字符数组字符串函数明白维二维数组方法会继续努力
实验九 字符数组应
实验目
〔1〕掌握字符数组字符串函数
〔2〕掌握字符串处理关算法
二实验容
1 [定义存贮字符串字符数组]
#include stringh
void main()
{ char s[80] *定义字符数组s*
strcpy(s abcdefghijklmn)
printf(s s)
}
2 [字符串输入输出]
面程序实现键盘读入字符串然输出屏幕请填充必语句
#include stdioh
void main()
{
char s[50]
printf(What's your name)
gets(s) *键盘读入字符串*
printf(Your name is )
printf(s s) *印字符串*
}
3[字符串合]
键盘输入3 字符串〔字符串回车符做结束标志〕3 字符串输入先序合字符串s中请填空程序完整
#include stdioh
#include stringh
main()
{
char s[100]
char a[30]
gets(a) strcat(s a)
gets(a) strcat(s a)
gets(a) strcat(s a)
*写行代码*
printf(s s)
}
4[回文串]
读入行字符串〔80 字符回车结束〕判断该字符串否回文串〔左右拼写右左拼写样〕输出Y否输出N
参考程序:
#include stdioh
#include stringh
main()
{ int i len
char buf[100]
gets(buf)
len strlen(buf)
for(i0 i
break
if(i>len2)
printf(Y)
else
printf(N)
}
5[寻找字符串]
键盘输入两字符串〔假设第字符串必包含第二字符串第字符串ABCDEF第二CDECDE 包含ABCDEF 中〕现求编程输出第二字符串第行字符串中出现位置〔果第二字符串第字符串中出现屡次前出现准〕
[提示]方法1:建立双重循环外层循环变量指示第串查找起始位置层循环起始位置开始判断第二字符中否出现处方法2:字符串函数strstr()
参考程序:
#include stdioh
main()
{ int ij
char a[80] b[80]
gets(a) gets(b)
for(i0a[i]'\0'i++)
{ for(j0b[j]'\0'j++)
if(a[i+j]b[j]) break
if(b[j]'\0') break
}
if (a[i]'\0') printf(di+1)
else printf(Not Found)
}
三实验分析总结
⑴ 实验运行结果分析
四相关实验程序列表
1
#include stdioh
void main()
{ char s[80]{'a''b''c''d''e''f''g''h''i''g''k''l''m''n'} *定义字符数组s*
scanf(s abcdefghijklmn)
printf(s s)
}
2
#include stdioh
void main()
{
char s[50]
printf(What's your name)
gets(s) *键盘读入字符串*
printf(Your name is\n)
printf(s s) *印字符串*
}
3
#include stdioh
#include stringh
main()
{
char s[100]
char a[30]
gets(a) strcat(s a)
gets(a) strcat(s a)
gets(a) strcat(s a)
*写行代码*
printf(s s)
}
4
#include stdioh
#include stringh
main()
{ int i len
char buf[100]
gets(buf)
len strlen(buf)
for(i0 i
break
if(i>len2)
printf(Y)
else
printf(N)
}
5
#include stdioh
main()
{ int ij
char a[80] b[80]
gets(a) gets(b)
for(i0a[i]'\0'i++)
{ for(j0b[j]'\0'j++)
if(a[i+j]b[j]) break
if(b[j]'\0') break
}
if (a[i]'\0') printf(di+1)
else printf(Not Found)
}
五实验心
通次学明白字符数组字符串函数字符串处理关算法掌握字符数组字符串函数会更加努力
实验十 函数根应
实验目
〔1〕掌握定义函数方法
〔2〕掌握函数实参形参应关系值传递〞方式
〔3〕掌握函数嵌套调递调方法
〔4〕掌握全局变量局部变量动态变量静态变量概念方法
二实验容
1 [函数定义]
面辗转相法求公约数程序请补充完整程序中函数定义调
#include stdioh
int f(int m int n)
{
int r
while ((rmn)0)
{
mn
nr
}
return n
}
main()
{
int a b n
scanf(dd &a &b)
printf(d\n f(a b) )
}
2 [编写函数计算阶]
面程序实现键盘读入整数n计算输出n请补充完整计算阶函数
#include stdioh
long fanc(int a)
{ long in1
for(i1i
}
main()
{ int n
scanf(d &n)
printf(ld fanc(n))
}
3[函数中变量]
写出面程序运行结果:
int f1(int x)
{ static int z3y0
y++
z++
return(x+y+z)
}
main()
{ int a1k
for(k0k<3k++) printf(4df1(a))
}
程序运行结果:
6 8 10
4[编写递调函数实现十进制数转二进制数]
面程序实现键盘输入正整数(100000000)输出应二进制数〔原码表示〕
请填空:
#include stdioh
void fun( int i )
{ if (i>1)
fun(i2)
printf(d i2)
}
main()
{ int n
scanf(d &n)
fun(n)
}
三实验分析总结
⑴ 实验运行结果分析
四相关实验程序列表
1
#include stdioh
int f(int m int n)
{
int r
while ((rmn)0)
{
mn
nr
}
return n
}
main()
{
int a b n
scanf(dd &a &b)
printf(d\n f(a b) )
}
2
#include stdioh
long fanc(int a)
{ long in1
for(i1i
}
main()
{ int n
scanf(d &n)
printf(ld fanc(n))
}
3
#include
int f1(int x)
{ static int z3y0
y++
z++
return(x+y+z)
}
main()
{ int a1k
for(k0k<3k++) printf(4df1(a))
}
4
#include stdioh
void fun( int i )
{ if (i>1)
fun(i2)
printf(d i2)
}
main()
{ int n
scanf(d &n)
fun(n)
}
五实验心
通次学学会掌握定义函数方法函数实参形参应关系函数嵌套调递调方法感受学c语言快乐
实验十 指针结构体
实验目
〔1〕通实验进步掌握指针概念会定义指针变量
〔2〕正确数组指针指数组指针变量
〔3〕正确字符串指针指字符中指针变量
〔4〕掌握结构体类型变量定义
二实验容
1 [交换两数输出]
面程序交换两数两数输出请填空
#include stdioh
void swap( int *p1 int *p2 )
{ int temp
temp*p1
*p1*p2
*p2temp
}
int main()
{ int ab int *pa*pb
scanf(dd &a &b)
pa&a pb&b
if(aprintf(d d\nab)
}
2 [函数实现求字符串长度]
面程序指针方式传递参数函数实现求字符串长度请填空完成
#include stdioh
#include stringh
int f(char *p)
{ return strlen(p)
}
int main()
{ char s[80]
int i
scanf(s s)
if(s)
printf(d i)
}
3[定义结构体类型]
求定义名student 结构体类型包含成员:
〔1〕字符数组name存放10 字符
〔2〕字符变量sex记录性
〔3〕整数类型变量num记录学号
〔4〕float 类型变量score记录成绩
代码完整
*定义结构体类型*
struct student
{ char name[20]
char sex
int num
float score
}
main()
{
struct student stu
scanf(s stuname)
scanf(*cc &stusex)
scanf(d &stunum)
scanf(f &stuscore)
printf(s\n stuname)
printf(c\n stusex)
printf(d\n stunum)
printf(f\n stuscore)
}
三实验分析总结
⑴ 实验运行结果分析
四相关实验程序列表
1
2
3
五实验心
实验十二 文 件
实验目
〔1〕学会文件翻开关闭读写等文件操作函数
二实验容
1 [文文件操作_字符读入]
前目录中存文件名data1in文文件现求fopen 函数命令翻开该文件读出里面字符遇写字母变写字母字符变字符序屏幕输出请填空完成程序
〔data1in 容〕
Hello my Dear
Have a GooD Time
〔屏幕输出结果〕
hello my dear
have a good time
程序请填空
#include stdioh
main()
{ FILE *fp
char ch
if(( fpfopen(data1inr) ) NULL)
return 0
while( (chfgetc(fp))EOF )
{ if ('A'
putchar(ch)
}
fclose(fp)
}
[提示] 提交前测试代码否正确源文件目录创立名data1in 文文件文件中入字母便测试代码否正确
2 [文文件操作_字符写入]
键盘输入意字符〔连着三写字符bye 做结束标志〕字符〔包括bye〕写
入新建文件answertxt 中〔注:文件放前目录〕请完成该功
〔键盘输入容〕
He can you write the code
Yes you canbye
〔程序执行文件answertxt 中容〕
He can you write the code
Yes you canbye
参考程序:
#include stdioh
main()
{ FILE *fp
char ch ch1' ' ch2' ' ch3' '
if((fpfopen(answertxt w))NULL)
return 1
while((chgetchar())EOF)
{ fputc(ch fp)
ch1ch2ch2ch3ch3ch
if (ch1'b'&&ch2'y'&&ch3'e')
break
}
fclose(fp)
}
3[文文件操作_单词排序]
前目录文件data1in〞文件里存放〔总数超10000 〕英文单词〔英文单词会超10 字文字符〕行单词未排序现求文件中单词字典序排序
然排序单词写入新建文件answertxt 中〔注:文件存放前目录〕请完成程序实现该功
〔data1in 文件中原容〕
hello
bye
yes
〔程序执行文件answertxt 中容〕
bye
hello
yes
参考程序:
#include stdioh
#include stringh
main()
{ FILE *fp1*fp2
char str[1000][11]str1[11]
int n0ij
if((fp1fopen(data1inr))NULL)
return 0
if((fp2fopen(answertxtw))NULL)
return 0
while(fscanf(fp1sstr[n])>0) n++
for(i0i
if(strcmp(str[i]str[j])>0)
{
strcpy(str1str[i])
strcpy(str[i]str[j])
strcpy(str[j]str1)
}
}
for(i0i
fclose(fp1)
fclose(fp2)
}
三实验分析总结
⑴ 实验运行结果分析
四相关实验程序列表
五实验心
文档香网(httpswwwxiangdangnet)户传
《香当网》用户分享的内容,不代表《香当网》观点或立场,请自行判断内容的真实性和可靠性!
该内容是文档的文本内容,更好的格式请下载文档