C语言程序设计实验指导上交大课前练习Word文档下载推荐.docx
《C语言程序设计实验指导上交大课前练习Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C语言程序设计实验指导上交大课前练习Word文档下载推荐.docx(31页珍藏版)》请在冰豆网上搜索。
undeclaredidentifier变量sum没有定义
intsum二a+b;
1
errorC2086:
c‘:
red己finition
变量c重复定义
c二a+b+sum;
b—3suni-:
-8
④在同一个函数体内,变量要先定义后使用,但不能重复定义程序3:
①1
errorC2106:
'
二'
:
leftoperandmustbe1一value
赋值符号左边参数错误
c二a+b;
2c=4
3赋值符号左边只能是变量,不能是常量或表达式
(1)t=a;
(2)a二b;
;
(3)printf(a=%d\tb=%d\n,a,b);
(1)intn
(2)”%d”,&
n
(1)floats
(2)(float)a/b
(3)%f
(1)#include<
math・h>
(2)pow(x,y)
(3)a%lfw,s
1.D
4.B
6.D
8.A
9.C
10.A
11.A
12.B
13.B
14.C
15.B
16.A
17.C
18.8
19.5和4
20.2.000000
21.double(删除最后“语句”两字)
22・double
23.a=3(回车)
实验三if选择结构语句
•
9
11
(1)
n%2=0
(2)
11%2!
=0
(3)
a>
=J0?
&
a<
=9
(4)
=>
z>
(5)
=?
A?
(6)
(a>
z,)
II(a>
A>
&
a<
=N)
(7)
(x<
-10)
II
((x>
10)
(x<
=20))
(8)a%b!
=O
(9)a*b>
4.-1
5.D
6.103040
7.C
9.D
(1)scaiif(H%d,\&
t);
(2)if(t%5=0&
t%7==0)
(3)else
(1)charv;
(2)if(x==O)
(1)if(x==m)
(2){printf(/zWrong\nz,);
(3)printf("
Toolow\n,z);
}
math・h>
(2)("
b+sqrt(d))/(2*a);
(3)sqrt(fabs(d))/(2*a);
(1)c>
a&
c<
-u
(2)c>
v'
-z'
(3)c二c+5-26;
=,a&
A'
='
Z'
2.C
4.A
5.B
8.C
9.B
10.D
12.D
13.D
14.A
1520
17.D
18.A
19.D
20.B
21.C
22.!
23.D
24.C
25.D
26.B
实验四switch开关语句
1・411
if((k==l川(k==2)
prmtf(na\nn);
elseif((k=3)||(k=4)||(k=5))pnntf「b\ir);
else
switch(x)
case1:
case2:
case3:
priiitf(Mx\iiH);
break;
case19:
case20:
case21:
printf(ny\iiH);
bieak;
default:
prmtf(nz\nM);
(1)switch(x)
(2)case4:
(3)default:
(1)a二s/250;
(2)case0:
d=0;
d=15;
(1)elsep=l;
(2)a二num/1000;
(3)num%10
(1)grade/10
(2)break
(3)default
(1)x<
(2)a=x/10
(3)a
(1)month
(2)days=31
(3)(year%4==0&
yearWIOO!
二0)||(year%400二二0)
1.C
2.D(将程序最后一条语句改为:
printf("
a=%d,b=%d\ii"
a,b);
)
5.pass
中
及格不及格
6.2
实验五while>
do...while循环结构语句
1.0
2.10
6.A
(1)longk=l;
(2)num/=10;
(3)while(num);
(1)intm二0,mc=0,j;
(2)if((k%7==0)||(k%ll==0))
(3)m=m+k
(1)max二min二grade;
(2)while(grade>
=0)
(3)if(grade<
min)
(1)t%2=0
(2)m=m*10+t;
(3)ii=n/10;
(1)ch=getchai()
(2)ch>
=,0,&
ch<
(3)(ch>
=,a,&
ch<
=,z,)||(ch>
=A&
=Z)
3・
(1)fabs(t)
(2)s/n
(3)pi*4
1.A
2.A
3.D
7.A
8.30
实验六for循环结构语句
3.C
4.fbr(i=100;
i!
=0;
i~)printffpj;
8.k<
=n
k++
9.A
10.C
11.C
(1)intsum=l,s0=0,sl=0,s2=l,s;
(2)for(n=4;
n<
=10:
n++)
(3)s2=s;
(1)for(i=l;
i<
=y;
i++)
(2)t*=x;
(3)t=t%1000;
(1)inti,count=0;
(2)辻((i%7二二0||i%U==0)&
(i%77!
二0))
(3)count++;
(1)s二0.0
(2)scanf(“%d"
&
n)
(3)s+l.O/sl
(1)i%100
(2)a+b+c=x
(3)t++
(1)2
(2)n
(3)n%i==0
2.B
实验七循环控制语句
课前习题
2.循环语句
3.break
5.20
6.66
7.8
(1)inti,j,k,sum=0;
(2)for(i=l;
=3;
(3)if((k>
=l&
k<
=6)&
(i!
=0)&
(j!
=0)||(k==0))
(1)for(j二2;
j<
i;
j++)
(2)if(i%j=0)
(3)if(j>
=i)
(1)m=n;
(2)while(m!
=0&
m>
=c)
(3)if(m=0)
(1)i<
10
(2)j%3!
(3)continue;
(1)total二0;
(2)sum二0;
(3)total+sum;
(1)j=(y%100)/10;
(2)i!
=j&
i!
=k&
k!
=j
(3)t%5==0
6.B
1
9.107
10.x=]6y=8
实验八函数的初步应用
return
void
8.D
(1)intfun(inta,intb,intc)
(2)if(a+b>
c&
b+c>
a&
a+c>
b)
(3)elseif(a==bb==c|a==c)
(1)if(n==l)
(2)c=age(n~l)+2;
(3)printf(,z%d\n/z,age(5))
(1)while(n!
=i)
(2)if(n%i==O)
(3)n=n/i;
■■■■
(1)i*i==x
(2)return0
(3)f(i+100)&
f(i+168)
(1)x
(2)sl+s2+s3=15
(1)f<
t
(2)returnf
(3)fun(n)
7.B
12.A
14.D
15.10,20
16.13111217.2
18.31
19.-31
20.221
21.22
22.main函数
23.etumvoid
24.voidint
实验九函数的其他应用
1.B
7.55
(1)#defineS)temp=x;
x=y;
y二temp
2Ju(e)
8寸u(t)
〈Itdqgxpnloul#
(2)(q)ip)(q(0)03QU-rH七p#(e)
A*x(1x)13QU-rH±
p#(T)
trwe能(M)
Qs©
。
(2)&
吕3++它二Qs©
(0)(01毛20.t;
・:
(I)(e)AIa)*(g)-(日巴^芦)主©
5(e)0HCO賈日)(日)AidQUUQP#(T)
MS(g)-du!
Q4.q<
04U-PH(e)
穴us密
14.auto
15.6720
16.48
17.
18.5,25
19.6
20.3
实验H维数组
1-3.BDD
4.248
6.mtss[s];
7.3715
8.654,321
(1)scaiif(H%f\a);
改为&
a);
(2)niax=inin=avg=a[1];
改为max=niiii=avg=a[O];
(3)if(niiii<
a[i])改为if(iniii>
a[i])或if(a[i]<
inin)
(1)while(x[-H-i]!
=0);
改为while(x[i++]!
(2)while(x[i]=0)改为wliile(x[i]=0)
(3)sum\=c;
改为suin/=c;
(1)s=count=l;
改为s=count=0;
(2)scanf(”%d”,a[i]);
改为scanff%d”,&
a[i]);
(3)break;
改为continue;
(4)count=%fn,\s,count);
改为priiitf(ns=%dcount=%d\nM,s,count);
(1)k=p
(1)#iiiclude<
math.h>
(2)aver=s/*N
(3)s=fabs(a[i]-aver)
(4)pnntf(“%f;
t)
(1)s=i*3+j*5
(2)break
(3)a[k]!
=0&
s!
1-2.DC
3.1=1、x[I-l]
4.012,201
5.47,17
6.15,12
7.6,2
8.234,556
9.023,235,71113
实验计
二维数组
2.96.8
3-6.BBDB
7.x<
4、y<
3、z==3
8.0、10、210、3210
(1)sl=s2=s3=s4=0改为sl=0,s2=0,s3=(Xs4=0
(2)s2=s2+a[m][j]改为s2=s2+a[m-l][j]
(3)fbr(i=0;
i<
m;
i++)改为fbi(i=l;
i++)
(4)s=sl-s2-s3-s4改为s=sl+s2+s3+s4
(1)inta[3][3],sum改为inta[3][3],sum=0
(2)scanf(n%dH,a[i][j])改为scanff%d”,&
a[i][j])
(3)sum=sum^a[i][j]改为sum=sum+a[i][i]
(4)sum=%fai改为sum=%d\n
(1)intij,s,a[3][3],sum改为intij,s,a[3][3],sum=O
(2)fbr(i=O;
3;
i++)改为for(i=0;
i=i+2)
(3)sum=*a[i][j]改为sum*=a[i]Ij]
(1)sum=O
⑵a[i][i]
(1)1<
M
(2)j<
N
⑶a[i]U]
(2)(j+2)%3
1-3.CDD
3.8
4.suml=38,suni2=34
6.A
7.16
8.100,120,358
9.1b1331
10.
13610
259
48
7
实验十二数组与函数
1-3.ACB
4.改变、不改变
(1)a[].iiitn)改为doublefun(inta[],intn)
(2)inti,n;
改为iiiti;
(3)doubles;
改为doubles=0;
(4)s=s+a(i);
改为s=s+a[i];
(5)ave=fun(a[],n);
改为ave=fun(a,n);
(1)floatj=0;
改为floatxa=0;
=n改为j<
(3)j<
(4)returnxa改为returnc
(1)sum=ariay[O]改为sum=O
(2)iiitscore[10]改为floatscore[10]
(3)scanf(n%f\score)改为scanf(H%f\&
score)
(4)aver=average(score[10])改为avei-average(score)
3.-1.2
4.22
5.3、6、9
6.1、a[i-l][j.l]+a[i-l][j]xj<
=i
7.1、121、1331
8.134567.3456、EndofmamO?
9.012、345
10.234、123、234
实验十三字符数组与字符串
1-5.BCDBD
5.sticmp(sl,s2)=0
(1)charp[]改为chaip[20]
(2)n=l;
改为n=0;
(3)p[n]=、(r改为ptnJI^O1
(4)length何20])改为length(s)
(1)max=0改为max=p[0]
(2)max>
p[i]改为max<
p[i]
(3)i=k改为k=i
(1)s[i]T(y
(2)S[1]=W
(3)s[i]+=1
(4)s
(1)strlen(s)
(2)s[i]>
=A&
s[i]<
=Z
(3)fiui(s)
1-5.DAACB
6.7
7.GL
8.diane
9.France^England>
ChinavAustralia
10.Simon、Niku>
Josiah.George
11.strlen⑸、s[i]>
a‘&
s[i]<
=,z\s[i]+32>
"
\0\s2[j]!
=s2[n2-j-l]>
n3.f(s)
实验十四指针与一维数组
1.DDDAC
2.5
(1)prmtf(n%d役p+i)改为pnntf(・%d笃*(p+i))
(2)m=0改为m=i
(3)k=m改为m=k
(1)niath.h
(2)&
b
(3)*pb
(1)n
(2)*(p+i-l)*3-l
1.4
2-11.BBBACDACAA
12.2.2
13.3.7
14.4.16.64
15.36.214
16.26.22、46
实验十五指针与字符串
1-6.CBAAAA
(_)程序改错
(1)iiit*a=upper改为iiit*a=&
uppei
(2)iiit*b=lower改为iiit*b=&
lowei;
(3)a++改为(*a)++
(4)b++改为(*b)++
(1)*p!
=\0・
(2)*p<
=tZ,
(3)*p+=32
(1)s
(2)*p!
(3)*p-,0r
1-9.BDCDDCBAD
10.gae
11.65、7、8、76
12・b5、c7、hlO
13.432156、432651
14.jib.gfb、dcba
实验十六指针与函数
1-2.BB
3.100200
4.B
(1)scanf(”%d"
p)改为scanf("
%d"
p++)
(2)*p<
*niax改为*p>
*max
(3)if(max=an)改为if(niax==arr)
(4)while(i=0;
n;
i++);
改为fbi(i=0:
(1)iiitnum[]改为imnum[5]
(2)num[i]=0改为num[k]=0
(3)switch(s)改为switch(*s)
(4)num[k]++改为num[i]++
(1)iiit*x
(2)x[i]==x[i+l]
(3)n-1
(4)f==l
1-4.DCCD
5.10,15,1540
6.2143
6.i<
n-l.a[i]>
a[j卜"
%d笃p+i、”%d:
*(p+i)
8.4、5、64
9.36、94
10.*pl=15.*p2=0
11.43210.98765
12.10、15、19
实验十七结构体、共同体和枚举类型
(1)enumday(sun,mon,tu®
wed,thu,fri,sat};
(2)scaiif(H%s,\s);
(3)prmtf(n明天是%s\n”,stT[d+l]);
(1)};
(2)structemployeeem[4]={{l,"
wang"
25},{2,"
li"
38},{3,"
zhang"
30},{4,"
qian"
45}};
(3)if(em[i].age>
35)
3
(1)strcpy(s1.name
(2)sl.sex-F'
(3)piiiitf(n%s\n,\s1.name);
(1)stnictDate
t.year.&
t.niomh,&
t・day
(3)t・year,t・month,t・day
(1)stu[i].score
(2)stu[i]・score<
=stu[j]・score
(3)sunvN
(1)S
(2)x[i].sex=M,
(3)n
课后习题
6.3
7.0,6,7,8,15
7.4,3
9.4
10.01
11.7
12.