admin 管理员组

文章数量: 1184232


2024年3月10日发(作者:linux软件更新)

28. 11

29. ABC

30. 8

31. 20

32. 6

33. 3

34. BC

35. 4

36. 5

37. 11

38. 123ABC

39. 1

40. 5

二.阅读程序写结果

1. #include <>

void main()

{

int a[2][3]={{3,2,7},{4,8,6}};

int i,j,m=0; for(i=1;i<=1;i++)

for(j=0;j<=i;j++)

m=m+a[i][j];

printf("m=%d",m);

}

2. #include <>

void main(void)

{

int array[10] = {1, 2, 4, 5, 6, 0, 0, 0, 0, 0}, i;

printf("n");

for (i=0; i<10; i++)

printf("%3d ", array[i]);

printf("n");

for (i=9; i>=2; i--)

array[i] = array[i-1];

array[2] = 3; for (i=0; i<10; i++)

printf("%3d ", array[i]);

printf("n");

}

3. #include <>

void main(void)

{

char a[]="ABCDEFGH",b[]="abCDefGh";

char *p1,*p2; int k;

p1=a; p2=b;

for(k=0;k<=7;k++)

if (*(p1+k)==*(p2+k))

printf("%c",*(p1+k));

printf("n");

}

4. #include <>

#include <>

void main()

{

char a[30]="Hello ";

char b[10]="Students";

char c[10]="Teachers";

strcat(a,c);

printf("%s,%s.n",a,b);

}

5. #include <>

void inv(int x[],int n);

void main()

{

int i, a[10]={3,7,9,11,0,6,7,5,4,2};

inv(a, 10);

printf("The array has been reverted.n");

for(i=0;i<10;i++)

printf("%d,",a[i]);

printf("n");

}

void inv(int x[], int n)

{

int t,i,j,m=(n-1)/2; for(i=0;i<=m;i++)

{

j=n-1-i;

t=x[i];

x[i]=x[j];

x[j]=t;

}

}

6. #include <>

void main()

{

float a[3][3]={1,2,3,4,5,6,7,8,9},sum=0; int i;

printf("please input rectangle element:n");

for(i=0;i<3;i++)

sum=sum+a[i][i];

printf("duijiaoxian he is %6.2f",sum);

}

7. #include <>

int main()

{

int i=1,n=3,j,k=3; int a[5]={1,4,5};

while(i<=n&&k>a[i]) i++;

for(j=n-1;j>=i;j--)

a[j+1] = a[j];

a[i] = k;

for(i=0;i<=n;i++)

printf(“%2d”,a[i]);

return 0; }

8. #include <>

void main()

{

int a[10]={0,1,2,3,4,5,6,7,8,9}, b[10],i,sum=0; for(i=1;i<10;i++)

{

b[i]=a[i-1]+a[i];

sum=sum+b[i];

}

for(i=1;i<10;i++)

if(i%3==0) sum=sum+b[i];

printf("sum=%3d",sum);

}

9. #include <>

void main()

{

int a[5]={2,6,1,3,4},n=5,i,j,t;

for(i=0,j=1;j

if(a[i]

{

a[j]=a[i];

i++;

}

for(i=0;i

printf("%d",a[i]);

}

10. #include <>

void funa(int x[],int n);

void main()

{

int i, a[5]={3,7,9,11,0};

funa(a, 5);

for(i=0;i<5;i++)

printf("%d,",a[i]);

printf("n");

}

void funa(int x[], int n)

{

int t,i,j,m=(n-1)/2; for(i=0;i<=m;i++)

{

j=n-1-i;

t=x[i];

x[i]=x[j];

x[j]=t;

}

}

11. #include <>

void main()

{

int a[5]={24,15,33,64,45},n=5,i,j,t;

for(i=0;i

a[i]=a[i]%10; for(i=n;i>0;i--)

printf("%d,",a[i-1]);

}

12. #include <>

void main()

{

int a[5],i,j,t;

for(i=4;i>=0;i--)

scanf("%d",&a[i]);

for(i=0;i<5;i++)

a[i]=a[i]+10; for(i=0;i<5;i++)

printf("%d,",a[i]);

}

输入:42 31 24 15 16<回车>

13. #include <>

void main()

{

char a[][5]={"Zhong","Gong", "ren"};

printf("%sn%sn", a[0],a[1]);

}

14. #include <>

void main()

{

int a[3][3]={0,1,2,3,4,5,6,7,8},i,j,t;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

a[i][j]+=a[i][0];

for(i=0;i<3;i++)

printf("%d,",a[i][i]);

}

15. #include <>

void main()

{

int a[3][3]={1,2,3,4,5,6,7,8,9},i,j;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

a[i][j]+=a[i][2];

while(i>0) {

i--;

printf("%d,",a[i][i]);

}

}

16. #include <>

void main()

{

int i,j;

char a[10]={"ABCDEFGHIJ"};

for(i=0;i<10;i++)

a[i]=a[i]+3; printf("%cn", a[6]);

}

17. #include <>

int main()

{

int i,a[10]={0,1,2,3,4,5,6,7};

for(i=1;i<9;i++)

a[i]=a[i-1]+a[i+1];

printf("%d%dn",a[5],a[8]);

return 0; }

18. #include <>

int main()

{

int i,a[]={1,2,3,4,5,6,7,8,9,10};

for(i=0;i<5;i++)

a[i]=i+2; for(i=9;i>=0;i--)

if(a[i]%3==0) printf("%dn",a[i]);

return 0; }

19. #include <>

int main()

{

int num=13,len=0,i=0,a[30];

do

{

a[len]=num%2; num=num/2;

}while(num);

for(i=0;i

printf("%d",a[len-i-1]);

printf("n");

return 0; }

20. #include <>

int main()

{

len++;

int a[5]={6,9,12,16};

int x,i=3; scanf("%d",&x);

while(i>=0 && x

{

a[i+1]=a[i];

i--;

}

a[i+1]=x;

for(i=0;i<5;i++)

printf("%5d",a[i]);

printf("n");

return 0; }

输入:10<回车>

21. #include <>

int main()

{

int a[3][3]={{2,3},{4,5,6},{7,8}};

int i=0,j=0,m;

m=a[0][0];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(a[i][j]

m=a[i][j];

printf("m=%dn",m);

return 0; }

22. #include <>

#include <>

int main()

{

int i;

char str[20]="abcdef";

strcpy(str,"opqrst");

str[5]='0';

for(i=0;i

putchar(str[i]);

printf("n");

return 0; }

23. #include <>

#include <>

int main()

{

char a[10]="AB",b[10]="LMNP";

int i=0; while(b[i])

{

a[i]=b[i];

i++;

}

a[i]='0';

puts(b);

return 0; }

24. #include <>

#include <>

int main()

{

int i;

char str1[30]="abc",str2[3][5]={"defg","hi0j","klm"};

for(i=1;i<3;i++)

strcat(str1,str2[i]);

puts(str1);

return 0; }

25. #include <>

int main()

{

int i;

char a[] = "Time", b[] = "Tom";

for(i = 0; a[i] != '0' && b[i] != '0'; i++){

if (a[i] == b[i])

if (a[i] >= 'a' && a[i] <= 'z')

printf("%c", a[i] - 32);

else

printf("%c", a[i] + 32);

else printf("*");

}

return 0; }

26. #include <>

int main()

{

char a[4][10] = {"1234", "abcd", "xyz", "ijkm"};

int i = 3; char (*p)[10] = a;

printf("output string: n");

for (p = a; p < a + 4; p++, i--)

printf("%c", *(*p + i));

return 0; }

27. #include <>

#include<>

int main()

{

char str1[30], str2[] = “your book”;

scanf(“%s”, str1);

strcpy(str1, str2);

printf(“n %sn %dn”, str1, strlen(str2));

return 0; }

输入:you are a student<回车>

28. #include <>

#include <>

int main()

{ char s1[10]=“java”,s2[10]=“basic”,s[10];

if(strcmp(s1,s2)) { strcpy(s,s1); puts(s);}

printf(“%d,%sn”,strlen(s2),strcat(s2,s1));

return 0; }

29. #include < >

#include < >

int main ()

{

char *p = “abcdefgh” , c[10] = { “XYZ” } ;

p += 3 ;

puts ( strcat ( c , p ) ) ;

printf ( “%dn” , strlen ( c ) ) ;

return 0; }

30. #include <>

int main()

{

int a[6][6], i, j;

for(i=1;i<6;i++)

for(j=1;j<6;j++)

a[i][j] = (i/j)*(j/i);

for(i=1;i<6;i++)

{

for(j=1;j<6;j++)

printf("%2d",a[i][j]);

printf("n");

}

return 0; }

31. #include <>

int main()

{

int i, f[10];

f[0]= f[1] = 1; for(i=2;i<10;i++)

f[i] = f[i-2] + f[i-1];

for(i=0;i<10;i++){

if(i%4 == 0) printf("n");

printf("%3d",f[i]);

}

return 0; }

32. #include <>

#include <>

int main()

{

char a[]="morming", t;

int i, j=0; for(i=1;i<7;i++)

if(a[j]

t = a[j];

a[j] = a[7];

a[7] = a[j];

puts(a);

return 0; }

33. #include <>

#include <>

void main()

{

char s[12]=“abcdef”;

scanf(“%s”,s);

strcat(s,”xyz”);

printf(“%sn”,s);

}

34. #include <>

void main()

{

int a[3][3]={{3,4},{5,6},{7,8}};

int i,j,t=0; for(i=1;i<3;i++)

for(j=0;j

t=t+a[i][j];

printf(“t=%dn”,t);

}

35. #include <>

void main( )

{

int i,t,a[5]={1,2,3,4,5};

t=a[0];

for(i=1;i<5;i++)

a[i-1]=a[i];

a[4]=t;

for(i=0;i<5;i++)

printf("%d",a[i]);

printf("n");

}

36. #include <>

void mystrcpy( char s1[ ] , char s2[ ]);

void main( )

{

char a[50]="I am a teacher.";

char b[]="You are a student.";

printf( " a = %s n" , a );

mystrcpy( a , b );

printf( " a = %s n" , a );

}

void mystrcpy( char s1[ ] , char s2[ ])

{

int i = 0 ;

while( s2[i] != '0' )

{

s1[i] = s2[i];

i++;

}

s1[i] = '0' ;

}

37. #include <>

void main()

{

int i,j,k=0,a[3][3]={1,2,3,4,5,6};

for(i=0;i<3;i++)

for(j=i;j<3;j++) k=k+a[i][j];

printf(“¡ã%d”¡À,k);

}

38. #include <>

void main()

{

int i,j=3,a[ ]={1,2,3,4,5,6,7,8,9,10};

for(i=0;i<5;i++)

a[i]=i*(i+1);

for(i=0;i<4;i++)

j+=a[i]*3; printf("%dn",j);

}

39. #include <>

void main()

{

char c, string[81]= "How are you";

int i, n=0, k=0; for(i=0; c=string[i]; i++)

{

if(c==' ') k=0; else if(k == 0)

k = 1; n ++;

}

}

printf("%dn" , n );

}

40. #include <>

#include <>

void main( )

{

char s[16]=“123450ttt”;

printf(“%d,%dn”,strlen(s),sizeof(s));

}

解答:

1. m=12

2. 1 2 4 5 6 0 0 0 0 0

1 2 3 4 5 6 0 0 0 0

{

3. CDG

4. Hello Teachers,Students.

5. The array has been reverted.

2,4,5,7,6,0,11,9,7,3,

6. please input rectangle element:

duijiaoxian he is

7. 1 3 4 5

8. sum=114

9. 22121

10. 0,11,9,7,3,

11. 5,4,3,5,4,

12. 26,25,34,41,52

13. ZhongGong

Gong

14. 0,10,20

15. 18,11,4

16. J

17. 2027

18. 9

6

6

3

19. 1101

20. 6 9

21. m=0

10 12 16

22. opqr

23. LMNP

24. abchiklm

25. t*M

26. 4cyi

27. your book

9

28. java

9,basicjava

29. XYZdefgh

8

30. 1 0 0 0 0

0 1 0 0 0

0 0 1 0 0

0 0 0 1 0

0 0 0 0 1

31. 1 1 2 3

5 8 13 21

32. mo

33. hijxyz

34. t=20

35. 23451

36. a= I am a teacher.

a= You are a student.

37. 17

38. 63

39. 3

40. 5,16

三.补充程序

1. 求一个3*3矩阵对角线元素之和。

#include ""

int main()

{

float a[3][3],sum=0; int i,j;

printf("please input rectangle element:n");

for(i=0; ________________________;i++)

for(j=0;j<3;j++)

scanf("%f",________________________);

for(i=0; ________________________;i++)

________________________;

printf("duijiaoxian he is %6.2f",sum);

return 0; }

2. 将一个数组逆序输出.

#include ""

#define N 5

int main()

{

int a[N]={9,6,5,4,1},i,temp;

printf("n original array:n");

for(i=0;i

printf("%4d",________________________);

for(i=0; ________________________;i++)

{

temp=________________________;

a[i]=a[N-i-1];

a[N-i-1]= ________________________;

}

printf("n sorted array:n");

for(i=0;i

printf("%4d",a[i]);

return 0; }

3. 一个学习小组有5个人,每个人有三门课的考试成绩。求全组分科的平均成绩和各科总

均成绩。

#include ""

int main()

{

int i,j,s=0,a[5][3];

float ,l,v[3];

printf("input scoren");

for(i=0; ________________________;i++)

{

for(j=0;j<5;j++)

{

scanf("%d",________________________);

s=s+a[j][i];

}

v[i]=s/; ________________________;

}

l=(v[0]+v[1]+v[2])/; printf("math:%fnc

________________________,v[2

]);

printf("total:%fn",l);

return 0; }

languag:%fndbase:%fn",v[0],

4. 将字符串st2内容连接到串st1中,并计算串st1字符长度。

________________________

#include ""

int main()

{

int len;

char st1[30]="My name is ";

char st2[10];

printf("input string st2 name:n");

gets(________________________);

________________________;

len=________________________;

puts(st1);

printf(“串st1字符长度%dn”,len);

return 0; }

5. 将十进制整数base转换成2进制。

#include ""

int main ( )

{

int i=0,base,j,num[20] ;

scanf(“%d”,&base) ;

do {

num[i]= ________________________;

base=________________________;

i++;

} while (base!=0);

for (________________________;________________________ ;j--)

printf(“%d”,num[j]) ;

return 0; }

6. 输入一个2×3矩阵,并3行2列输出矩阵。

#include<>

int main()

{

int i,j;

float s[2][3];

printf(“please input 6 numbers:n”);

for(i=0; ________________________;i++)

for(j=0;j<3;j++)

________________________;

printf(“n the final matrix is:n”);

for(i=0;i<3;i++)

{

________________________

printf("%f",s[j][i]);

________________________

}

return 0; }

7. 从键盘输入10个学生的成绩,求平均分并输出大于平均分的所有成绩。#include<

>

int main( )

{

int i;

________________________

float a=; printf (" Input 10 student's scores:n ");

for(i=0; i<10; i++)

________________________;

for(i=0; i<10; i++)

a=a+s[i];

________________________;

printf("average=%2.1f n scores(> average):n",a);

for(i=0; i<10; i++)

if (s[i]>a) ________________________;

return 0; }

8. 将一个2行3列数组a的元素,存到一个3行2列二维数组b中,并输出数组a,b.

#include<>

int main ( )

{

int a[2] [3]={{1, 2,3},{4, 5,6}};

int b[3] [2], i, j;

printf("array a: n");

for (i=0; i<=1; i++)

{

for (j=0; j<=2; j++)

{

printf("%5d", a[i][j]);

________________________;

}

printf("n");

}

printf("array b:n");

for (i=0; ________________________; i++)

{

for (j=0; ________________________; j++)

printf("%5d", b[i][j]);

________________________;

}

return 0; }

9. 计算矩阵上三角阵(不包括对角线元素)各元素之和,并8行2列输出矩阵a.

#include<>

int main()

{

int a[4][4]={{2,3,4},{5,6,7},{8,9,1}};

int i,j,t=0; for(I=0;i<4;i++)

for(____________________; ________________________;j++)

t+=________________________;

printf("t=%dn",t);

for(i=0;i<4;i++)

{

for(j=0;j<4;j++)

{

printf(“%3d”, a[i][j]);

if((j+1)%2==0)printf(“n”);

}

________________________;

}

return 0; }

10. 计算和存储数列前20项,每行输出5项。

#include ""

int main()

{

________________________;

int i;

a[0]=a[1]=1; for(________________________;i<20;i++)

a[i]=a[i-1]+a[i-2];

for(i=0; ________________________;i++)

{

printf(“%6d”,a[i]);

________________________;

}

return 0; }

解答:

1. i<3

&a[i][j]

i<3

sum+=a[i][j]

2. a[i]

i

a[i]

temp

3. i<3

a[j][i]

s=0

v[1]

4. #include""

st2

strcat(st1,st2);

strlen(st1);

5. base%2;

base/2;

j=i-1

j>=0

6. i<2

scanf("%f",&s[i][j]

for(j=0;j<2;j++)

printf("n");

7. int s[10];

scanf("%d",&s[i]);

a=a/10;

printf("%5d",s[i]);

8. b[j][i]=a[i][j];

i<3

j<2

printf("n");

9. j=i+1

j<4

a[i][j]

if((i+1)%2==0)printf("n");

10. int a[20]

i=2

i<20

if((i+1)%5==0)printf("n");

四.改错题

解答:

五.编程一

解答:

六.编程二

解答:

第八章 结构体

一.填空

1. 引用结构体变量成员的一般形式是:结构体变量名__________。

2. 定义枚举类型的关键字是__________。

3. 设char 、int、float分别占1、2、4个字节,假设有如下定义:

struct student

{ char name[10];

int nl;

float ave;

}qq;

则变量qq在内存中所占的字节数是__________。

4. 设char 、int、float分别占1、2、4个字节,假设有如下定义:

union student

{ char name[10];

int nl;

float ave;

}qq;

则变量qq在内存中所占的字节数是__________。

5. 设char 、int、float分别占1、2、4个字节,假设有如下定义:

union stu

{ char a;

int b;

float c;

};

struct {

float c;

union stu x;

} y;

那么变量y占用的存储空间分别为__________个字节。

6. 设有定义 struct date

{ int day ;

char month ;

int year ;

} dd , *pd = &dd ;

请写出:使用指针变量pd引用结构体成员 dd .day 的另一种描述形式__________。

7. 结构体变量各成员占用的内存单元起始地址__________。(填“相同”或“不同”)

8. 共用体变量各成员占用的内存单元起始地址__________。(填“相同”或“不同”)

9. 设char 、int、float分别占1、2、4个字节,假设有如下定义:

union stud_type

{

char name[5];

int num;

float score;

}];

union stud_type stu[10]];

则程序运行时,系统将为stu分配__________个字节的内存空间。

10. 设char 、int、float分别占1、2、4个字节,假设有如下定义:

struct stud_type

{

char name[5];

int num;

float score;

};

struct stud_type stu[10];

则程序运行时,系统将为stu分配__________个字节的内存空间。

11. 设char、float和double型数据占用的内存空间字节数分别是1、4和8,则变量stu1在

存中占用的字节数是__________。

struct stud_type

{

char name[10];

float score[5];

double average;

}stu1;

12. 写出一个单链表结点的结构体类型定义:结点名为worker,分量num为整型,分量next

指向下一结点的指针________________________________________。

13. 写出一个货品信息的结构体类型定义:结点名为goods_type,第一个分量货品号num

为整

型,第二个分量货品名name为长度为20 的字符型数组,第三个货品库存量amount为整

型。__

______________________________________。

14. 在C语言中,可以给某个已有的数据类型重新命名。将int型重新命名为INTEGER,应

该表

示为______________________________。

15. 在C语言中,给某个已有的数据类型重新命名的形式为:__________ 类型名 新名称。

16. 结构体变量成员的引用方式是使用__________运算符。

17. 结构体指针变量成员的引用方式是使用__________运算符。

18. 设struct student{ int no;char name[12];float score[3]; } sl,* p = &sl;用指针

法给sl的成员no赋值1234的语句是____________________。

19. typedef的功能是____________________。

20. 设char 、int分别占1、2个字节,假设有如下定义:

union student { int n;char a[100]; } b; 则sizeof(b)的值是__________。

解答:

1. •成员名

2. enum

3. 16

4. 10

5. 8

6. pd->day 或(*pd).day

7. 不同

8. 相同

9. 50

10. 110

11. 38

12. struct worker { int num; struct worker *next; };

13. struct goods_type { int num; char name[20];int amount;};

14. typedef int INTEGER;

15. typedef

16. •

17. ->

18. p->no=1234或(*p).no=1234

19. 给某个已有的数据类型重新命名

20. 100

二.阅读程序写结果

1. #include <>

struct stri_type

{

char ch1; char ch2; struct

{

int a;

int b;

}ins;

};

int main()

{

struct stri_type ci;

='a'; ='A';

printf("%d,%cn", return 0; }

2. #include <>

struct stud_type

{

char num[11];

char name[11];

float score[3];

float average;

};

int main()

{

struct stud_type stu={"200601","wang",,,};

=[0]+[1]+[2])/3; printf("average=%5.2fn",;

return 0; }

3. #include <>

union out

{

int a[2];

struct

{

int b;

int c;

}in;

int d;

};

int main()

{

union out e;

int i;

=3; for(i=0;i<2;i++)

printf("%5d",[i]);

printf("n");

return 0; }

4. #include <>

#define PI struct cir_type

{

float r;

double area;

};

int main()

{

struct cir_type cir={},*p=○

p->area=PI*p->r*p->r;

printf("area=%.3lfn",p->area);

return 0; }

5. #include <>

struct int_char

{

int i;

char ch;

};

int main()

{

struct int_char x={9,'z'};

printf("%dt%5cn",+20,;

return 0; }

6. #include <>

int main()

{

struct data

{

int m;

int n;

union

{

int y;

int z;

}da;

};

struct data intdata;

=3;=6;

7. #include <>

int main()

{

int i;

float sum_average=0;

{

int num;

char name[10];

int score[2];

float average;

};

printf("%5d%5dn",

struct student

return 0; }

struct student data[2]={{1,"zhangsan",60,70},{2,"Lisi",89,90}};

for(i=0;i<2;i++)

{

data[i].average=(data[i].score[0]+data[i].score[1])/;

sum_average=sum_average+data[i].average;

}

sum_average=sum_average/2; for(i=0;i<2;i++)

if(sum_average>data[i].average)

printf("%5d%10s%5.2f",data[i].num,data[i].name,data[i].average);

return 0; }

8. #include <>

struct int_data

{ int d1,d2;};

int main()

{

struct int_data data[2]={{2,3},{5,6}};

int i;

int sum=10; for(i=0;i<2;i++)

{

scanf("%d%d",&data[i].d1,&data[i].d2);

sum=data[i].d1+data[i].d2+sum;

}

printf("sum=%d n",sum);

return 0; }

输入:20 30 40 50<回车>

9. #include <>

int main( )

{

union exa

{

struct

{

int a;

int b;

}out;

int c;

int d;

}e;

=2; =5; printf("%d,%dn",

10. #include <>

return 0; }

struct st

{

int a;

int b;

union

{

int e;

int f;

}c;

};

int main( )

{

struct st y;

=10; =

printf("%d,%dn", return 0; }

11. #include <>

struct sta

{

int a;

char b;

};

struct stb

{

int a;

char b;

struct sta c;

};

int main( )

{

struct stb y;

=10; ='X';

printf("%d,%cn",

12. #include <>

union st

{

int a[2];

int b[2];

int c;

return 0; }

};

int main( )

{

union st y;

[0]=10; [1]=20;

=30; printf("%d,%dn",[0],[1]);

return 0; }

13. #include <>

struct two

{

int n;

char ch;

};

void main( )

{

struct two ex1={5,'t'};

printf("%d,%cn",+10,;

}

14. #include <>

struct two

{

int n;

char ch;

};

void func(struct two ex2);

void main()

{

struct two ex1={6,'v'};

func(ex1);

printf("%d,%cn",,;

getchar();

}

void func(struct two ex2) {

= +20; = -1; }

15. #include <>

union exa{

struct{

int a;

int b;

}out;

int c;

int d;

};

void main()

{

union exa e;

=1; =3;

16. #include <>

struct stu

{

int a;

int b;

struct poi

{

int x;

int y;

}ins;

};

void main()

{

struct stu outs;

=11; =4;

17. #include <>

struct abc

{

int a, b, c;

};

int main()

{

struct abc s[2] = {{1,2,3},{4,5,6}};

int t;

t = s[0].a + s[1].c;

printf("t=%d n",t);

return 0; }

printf("%d,%dn", }

printf("%d,%d", }

18. #include <>

struct st

{

int x, y;

} data[2] = {1, 10, 2, 20};

int main()

{

struct st *p = data ;

printf("%dn", p->y ) ;

printf("%dn", (++p)->x ) ;

return 0 ;

}

19.

#include <>

struct n

{

int x;

char c;

};

void func(struct n b)

{

= 20; = 'y';

}

int main()

{

struct n a = {10, 'x'};

func(a);

printf("%d,%c", , ;

return 0; }

20.

#include <>

int main()

{

struct EXAMPLE

{

struct

{

int x;

int y;

} in;

int a;

int b;

} e;

= 1; = 2; = * ;

= + ;

printf("%d,%d",

解答:

1. 162,A

2. average=

3. 3 2

4. area=

5. 29 x

6. -3 -3

7. 1

8. sum=150

9. 5,20

10. 10,10

11. 20,X

12. 30,20

13. 15,s

14. 6,v

15. 3,3

return 0; }

16. 15,7

17. t=7

18. 10 2

19. 10,x

20. 2,3

三.补充程序

1. 该程序完成的功能是从屏幕上输入日期(年、月、日),计算其为该年的第几天。

#include<>

struct date_type

{ int year;

int month;

int day;

};

int main()

{

int days=0,i;

struct date_type date;

int mon_day[12]={31,28,31,30,31,30,31,31,30,31,30,31};

printf("请输入日期(年 月 日):n");

scanf("%d%d%d",_____________________________________________);

for(i=1;i<;i++)

_______________________________;

days=days+;

if(%4==0&&%100!=0||%400==0)&&>=3)

____________________________;

printf(" 所输入日期是该年第%d天n",_________________________);

return 0; }

2. 该程序完成的功能是从屏幕上输入日期(年、月、日),计算其为该年的第几天。

#include<>

struct date_type

{ int year;

int month;

int day;

};

int main()

{

int days=0,i;

_)

___________________________;

int mon_day[12]={31,28,31,30,31,30,31,31,30,31,30,31};

printf("请输入日期(年 月 日):n");

scanf("%d%d%d",&,&,&;

for(i=1;________________;i++)

days=mon_day[i-1]+days;

days=days+;

if(%4==0&&%100!=0||%400==0)&&_______________

days=days+1;

printf(" 所输入日期是该年第%d天n",_______________);

return 0; }

3. 该程序完成的功能是从屏幕上输入日期(年、月、日),计算其为该年的第几天。

#include<>

struct date_type

{ int year;

int month;

int day;

};

int main()

{

int days=0,i;

struct date_type date;

int mon_day[12]={31,28,31,30,31,30,31,31,30,31,30,31};

printf("请输入日期(年 月 日):n");

scanf("%d%d%d",_________________________________);

for(i=1;____________________;i++)

days=mon_day[i-1]+days;

days=days+;

if(%4==0&&%100!=0||___________________)&&>=3) days=days+1;

printf(" 所输入日期是该年第%d天n",______________________);

return 0; }

4. 该程序实现从键盘输入1名职工的信息:职工号、姓名、月工资。由print函数完成这名

职工信息的输出。

#include<>

struct employee_type

{

char num[20];

char name[20];

float pay;

};

void print(struct employee_type emp1);

int main()

{

struct employee_type emp;

printf("请输入职工信息(职工号 姓名 月工资):n");

scanf("%s%s%f",____________________________);

print(_____________________);

return 0; }

void print(__________________________)

{

printf("****职工号 姓名 月工资****n");

printf("%8s%8s%10.2fn",_____________________________);

}

5. 该程序完成从键盘输入10名学生信息(学号、姓名、成绩),用一个排序函数sort完成

学生成绩降序排列,并输出学生成绩排行榜。

#include <>

#define N 10

struct stud_type

{ char name[10];

int num;

float score;

};

void sort(struct stud_type stu[], int n);

int main()

{ int i;

struct stud_type stu[N];

printf("Please input %d student’s name num score:n",N);

for(i=0;i

scanf("%s%d%f", ___________________________);

sort(stu,N);

printf("The sort is:n");

for(i=0;i

printf("%8s%5d%8.2fn",_________________________);

return 0; }

void sort(__________________)

{ int i,k,j;

struct stud_type t ;

for(i=0;i

{ k=i;

for(j=i+1;j

if(stu[k].score

_________________;

if (k!=i)

{ t=stu[i];

stu[i]=stu[k];

stu[k]=t;

}

}

}

6. 该程序完成从键盘输入10名学生信息(学号、姓名、成绩),用一个排序函数sort完成

学生成绩降序排列,并输出学生成绩排行榜。

#include <>

#define N 10

struct stud_type

{ char name[10];

int num;

float score;

};

void sort(struct stud_type stu[], int n);

int main()

{ int i;

struct stud_type stu[N];

printf("Please input %d student’s name num score:n",N);

for(i=0;i

scanf("%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score);

sort(____________);

printf("The sort is:n");

for(i=0;i

printf("%8s%5d%8.2fn",stu[i].name,stu[i].num,stu[i].score);

return 0; }

void sort(struct stud_type stu[],int n)

{ int i,k,j;

struct stud_type t ;

for(i=0;i

{ k=i;

for(j=i+1;j

if(stu[k].score

k=j ;

if (k!=i)

{__________;

__________;

__________;

}

}

}

7. 该程序完成从键盘输入10名学生信息(学号、姓名、成绩),用一个排序函数sort完成

学生成绩降序排列,并输出学生成绩排行榜。

#include <>

#define N 10

struct stud_type

{ char name[10];

int num;

float score;

};

void sort(struct stud_type stu[], int n);

int main()

{ int i;

________________________;

printf("Please input %d student’s name num score:n",N);

for(i=0;i

scanf("%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score);

sort(stu,N);

printf("The sort is:n");

for(i=0;i

printf("%8s%5d%8.2fn",stu[i].name,stu[i].num,stu[i].score);

return 0; }

void sort(___________________)

{ int i,k,j;

struct stud_type t ;

for(i=0;i

{ __________;

for(j=i+1;j

if(stu[k].score

____________;

if (k!=i)

{ t=stu[i];

stu[i]=stu[k];

stu[k]=t;

}

}

}

8. 该程序完成从键盘输入10名学生信息(学号、姓名、成绩),用一个排序函数sort完成

学生成绩降序排列,并输出学生成绩排行榜。

#include <>

#define N 10

struct stud_type

{ char name[10];

int num;

float score;

};

void sort(struct stud_type stu[], int n);

int main()

{ int i;

struct stud_type stu[N];

printf("Please input %d student’s name num score:n",N);

for(i=0;i

scanf("%s%d%f", __________________);

sort(stu,N);

printf("The sort is:n");

for(i=0;i

printf("%8s%5d%8.2fn",stu[i].name,stu[i].num,stu[i].score);

return 0; }

void sort(___________________)

{ int i,k,j;

________________ ;

for(i=0;i

{ k=i;

for(j=i+1;j

if(stu[k].score

k=j ;

if (___________)

{ t=stu[i];

stu[i]=stu[k];

stu[k]=t;

}

}

}

9. 该程序完成从键盘输入10名学生信息(学号、姓名、成绩),用一个排序函数sort完成

学生成绩降序排列,并输出学生成绩排行榜。

#include <>

#define N 10

struct stud_type

{ char name[10];

int num;

float score;

};

void sort(struct stud_type stu[], int n);

int main()

{ int i;

_______________________;

printf("Please input %d student’s name num score:n",N);

for(i=0;i

scanf("%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score);

sort(stu,N);

printf("The sort is:n");

for(i=0;i

printf("%8s%5d%8.2fn",__________________________);

return 0; }

void sort(struct stud_type stu[],int n)

{ int i,k,j;

struct stud_type t ;

for(i=0;i

{ k=i;

for(j=i+1;j

if(stu[k].score

____________;

if (k!=i)

{ t=stu[i];

stu[i]=stu[k];

___________;

}

}

}

10. 从键盘输入一整数,显示与之对应的星期值,当输入-1时结束。

#include<>

int main()

{

enum week_type

{

mon=1,tue,wed,thu,fri,sta,sun

}workday;

int i;

do

{

printf("Please input integer(end:-1)n");

scanf("%d",________________);

workday=(________________)i;

switch(________________)

{

case sun:printf("Sundayn");

break;

case mon:printf("Mondayn");

break;

case tue:printf("Tuesdayn");

break;

case wed:printf("Wednesdayn");

break;

case thu:printf("Thursdayn");

break;

case fri:printf("Fridayn");

break;

case sta:printf("Saturdayn");

break;

default:printf("Input errorn");

break;

case -1:printf("Goodbye!n");

}

}while(________________);

return 0; }

解答:

1. &,&,&

days=mon_day[i-1]+days

days=days+1

days

2. struct date_type date

i<

>=3

days

3. &,&,&

i<

%400==0

days

4. ,,&

emp

struct employee_type emp1

,,

5. stu[i].name ,&stu[i].num,&stu[i].score

stu[i].name,stu[i].num,stu[i].score

struct stud_type stu[],int n

k=j

6. stu,N

t=stu[i]

stu[i]=stu[k]

stu[k]=t

7. struct stud_type stu[N]

struct stud_type stu[], int n

k=i

k=j

8. stu[i].name ,&stu[i].num,&stu[i].score

struct stud_type stu[],int n

struct stud_type t

k!=i

9. struct stud_type stu[N]

stu[i].name,stu[i].num,stu[i].score

k=j

stu[k]=t

10. &i

enum week_type

workday

i!=-1

四.改错题

解答:

五.编程一

解答:

六.编程二

解答:

第九章 指针

一.填空

1. 假定在一维数组b[10]中,元素b[5]的指针为p,则p+4所指向的元素为__________。

2. 指针变量是一种专门存放__________的变量。

3. 函数中的形参和调用时的实参都是变量时,传递方式为值传递;形参和实参都是数组名

时,传递方式为__________。

4. 设有数组a[10];则数组元素a[3]的地址可以写成__________。

5. 定义int b[10]; 若元素b[5]的指针为p1,元素b[9]的指针为p2,则p2- p1的值为______

____。

6. int *p, a; 则语句 p=&a; 中的运算符&是__________符号。

7. 设int a[]={0,1,2,3,4,5,6,7,8,9},*p=a,则*p+4= __________。

8. 定义int a[10]={11,12,13,14,15}, *p=&a[5]; 假设&a[0]和&a[1]的值分别是2000和200

2,则p的值是__________。

9. 对变量的访问方式有直接访问和间接访问两种。其中,将变量a的地址存放在另外一个

量p中,访问时先从p中取出变量a的地址,再按照a的地址访问a的值,该方式称为

__________

访问。

10. int *p 的含义是____________________。

11. 定义int a[10]={11,12,13,14,15}, *p=&a[5]; 则表达式a[0]+*p的值是__________。

12. 定义int a[10]={1,3,5,7,9,11,13,15,17,19},*p=a;执行语句printf("%d",*p+7);输出

结果是__________。

13. 定义int m=5,n=9,t,*p=&m,*q=&n;顺序执行语句t=*p;*p=*q;*q=t;此时m的值是 _______

___。

14. 已知 int a[5] = { 2, 3, 4, 5, 6 }; int *p = a+2; 则表达式 *p*a[3] 的值是_____

_____。

15. 假定在一维数组array[10]中,元素array[5]的指针为p,则p-4所指向的元素为________

__。

16. 数组名代表的是____________________。

17. 若int a[3][3]={1,2,3,4,5,6,7,8,9};则*(*(a+2)+1)的值是__________。

18. 若int a[ ][3]={1,2,3,4,5,6,7,8,9};则*(*a+1)的值是__________。

19. 若有int a[3]={10,12,30};则a+1是数组元素__________的地址。

20. 函数调用时参数有值传递和__________传递两种参数传递方式。

解答:

1. b[9]

2. 地址

3. 地址传递

4. &a[3]或a+3

5. 4

6. 取地址

7. 4

8. 2010

9. 间接

10. 定义指针变量p,p指向整型变量。

11. 11

12. 8

13. 9

14. 20

15. array[1]

16. 数组元素的首地址

17. 8

18. 2

19. a[1]

20. 地址

二.阅读程序写结果

1. #include <>

int main()

{

int i=0,a[5]={11,12,13},*q;

for(q=a;q

{

*q+=5; printf("%5d",a[i]);

}

printf("n");

return 0; }

2. #include <>

void fun(int *p);

int main()

{

int a[5]={1,2,3,4,5},*r=a;

fun(r);

printf("%dn",*r);

return 0; }

void fun(int *p)

{

p=p+2; printf("%dn",*p);

}

3. #include <>

int main()

{

int a[3][3]={0,1,2,3,4,5,6,7,8};

int *p,(*q)[3];

int i=0; for(q=a;q

{

for(p=*q;p<=*q+i;p++)

printf("%5d",*p);

printf("n");

}

return 0; }

4. #include <>

#include <>

int main()

{

char str[20]="I am a student.",*p=str;

char *q="You are a teacher.";

p=p+7; q=q+10; strcpy(p,q);

puts(str);

return 0; }

5. #include <>

void swap(int x, int y)

{

int z;

z=x;

x=y;

y=z;

}

void pswap(int *x, int *y)

{

int z;

z=*x;

*x=*y;

*y=z;

}

void main()

{

int a=3, b=2; printf("first:a=%d, b=%d n", a, b);

swap(a,b);

printf("second:a=%d,b=%dn", a,b);

pswap(&a,&b);

printf("third:a=%d,b=%d",a,b);

}

6. #include <>

void f(int a, int b, int *c, int *d)

{

a=30; b=40; *c=a+b;

*d=*d-a;

}

void main()

{

int a=10,b=20,c=30,d=40; f(a,b,&c,&d);

printf("%d,%d,%d,%d",a,b,c,d);

}

7. #include <>

void ast(int *cp, int *dp)

{

int x=4,y=3; *cp=++x+y;

*dp=x-y;

}

void main()

{

int c, d;

ast(&c,&d);

printf("%dn%dn", c, d);

}

8. #include <>

void main()

{

char s[]="ABCD",*p;

for(p=s+1;p

printf("%sn",p);

}

9. #include <>

void main()

{

char *p="I am a student.";

p=p+3; printf("%s",p);

}

10. #include <>

void main( )

{

char *p="This is a programe.";

p=p+10; printf("%s",p);

}

11. #include <>

void add(int *p, int n)

{

int *pend = p + n;

for( ; p

*p += 10; }

void main( )

{

int a[5]={1,2,3,4,5}, *q = a;

add(q, 5);

for(q=a; q < a+5; q++)

printf("%4d", *q);

}

12. #include <>

int f(int x,int *y);

void main()

{

int a,b,c;

a=3; b=5; c=f(a,&b);

printf("%d,%d,%dn",a,b,c);

}

int f(int x,int *y)

{

int a=2; x=x+a;

*y=x+a;

return(a);

}

13. #include <>

void fun (int x , int *y );

int main()

{

int a = 2 , b = 3 , *c = &b;

fun(a, c);

printf("a=%d, b=%d, c=%dn", a, b, *c);

return 0; }

void fun( int x , int *y )

{

int a = 4; *y = x + a;

x = *y;

}

14. #include <>

void sub(int x, int y, int *z)

{

*z = y - x;

}

int main()

{

int a, b, c;

sub(10, 5, &a);

sub(7, a, &b);

sub(a, b, &c);

printf(" % 4d, % 4d, % 4dn", a, b, c);

return 0; }

15. #include <>

int main()

{

char a[] = "language", *p;

p = a;

while (*p != 'u')

{

printf(" % c", *p - 32);

p++;

}

return 0; }

16. #include <>

int stre(char str[])

{

int num = 0; while(*(str + num) != '0')

num++;

return num;

}

int main()

{

char str[10], *p = str;

gets(p);

printf(" %dn", stre(p));

return 0; }

输入:happy<回车>

17. #include <>

void f(int x, int *y)

{

x=x%10; *y=*y/10+x;

}

int main()

{

int a=88,b=99,*c;

c=&b;

f(a,c);

printf("%d,%dn",a,b);

return 0; }

18. #include <>

void main()

{

char a[][7]={ "Teacher","Tom" , "Good"};

char *p=a[0];

p=p+3; printf("%sn%sn", a[0],p);

}

19. #include <>

void main()

{

char a[][7]={ "Teacher","Tom" , "Good"};

char (*p)[7]=a;

printf("%sn%sn", a[0],++p);

}

20. #include <>

void ast(int x, int y, int *a, int *b)

{

*a=x+y;

*b=*a-y;

}

void main()

{

int a, b, c, d;

a=10; b=20; ast(a,b,&c,&d);

printf("%d,%dn", c, d);

}

解答:

1. 16 17 18

2. 3

1

3. 0

3 4

6 7 8

4. I am a teacher.

5. first:a=3, b=2

second:a=3,b=2

third:a=2,b=3

6. 10,20,70,10

7. 8

2

8. BCD

CD

D

9. m a student.

10. programe.

11. 11 12 13 14

12. 3,7,2

5 5

15

13. a=2, b=6, c=6

14. -5, -12, -7

15. L A N G

16. 5

17. 88,17

18. TeacherTom

cherTom

19. TeacherTom

Tom

20. 30,10

三.补充程序

1. 该程序实现输入两个整数,按先小后大的顺序输出,调用函数实现交换位置。

#include<>

void swap ( int *x, int *y );

int main ( )

{

int x, y;

printf ( "请输入两个数(空格分隔):" );

scanf ( "%d %d", &x, &y );

if ( x > y )

swap ( _________ );

printf ( " %4d %4d n", x, y );

return 0;

}

void swap ( int *p1, int *p2 )

{

int p;

_________;

_________;

_________;

}

2. 该程序实现输入两个整数,按先小后大的顺序输出,调用函数实现交换位置。

#include<>

void swap ( int *x, int *y );

int main ( )

{

int x, y;

printf ( "请输入两个数(空格分隔):" );

scanf ( "%d %d", &x, &y );

if (_________ )

swap ( &x, &y );

printf ( " %4d %4d n", ______________ );

return 0;

}

void swap ( _____________)

{

_________;

p = *p1;

*p1 = *p2; *p2 = p;

}

3. 该程序实现输入两个整数,按先小后大的顺序输出,调用函数实现交换位置。

#include<>

void swap ( _______________ );

int main ( )

{

int x, y;

printf ( "请输入两个数(空格分隔):" );

scanf ( "%d %d", __________________ );

if ( x > y )

swap ( ______________ );

printf ( " %4d %4d n", x, y );

return 0;

}

void swap ( int *p1, int *p2 )

{

_________________ ;

p = *p1;

*p1 = *p2; *p2 = p;

}

4. 该程序实现输入两个整数,按先小后大的顺序输出,调用函数实现交换位置。

#include<>

void swap ( _______________ );

int main ( )

{

int x, y;

printf ( "请输入两个数(空格分隔):" );

scanf ( "%d %d", &x, &y );

if (__________)

swap ( &x, &y );

printf ( " %4d %4d n",________________);

return 0;

}

void swap ( int *p1, int *p2 )

{

int p;

___________;

*p1 = *p2; *p2 = p;

}

5. 该程序实现输入两个整数,按先小后大的顺序输出,调用函数实现交换位置。

#include<>

void swap ( int *x, int *y );

int main ( )

{

int x, y;

printf ( "请输入两个数(空格分隔):" );

scanf ( "%d %d", &x, &y );

if ( x > y )

swap (__________);

printf ( " %4d %4d n",_____________ );

return 0;

}

void swap ( int *p1, int *p2 )

{

int p;

p = *p1;

_________;

_________;

}

6. 利用指针变量自增的方式输出一维数组b中的所有元素。

#include <>

#define SIZE 3

int main()

{

int i, b[]={1,2,3};

int *p;

for (_______;________; _________)

printf("*p=%dn",__________);

}

7. 输入一个八进制数并转换为十进制数。八进制转换为十进制数的方法是按权相加,即将

八进制每位上的数乘以位权,然后相加之和即是十进制数。利用数组存放八进制数的各位数

码。

#include <>

#define N 6

int main ()

{

int n, temp ;

char s [ N ], * p = s;

printf ( "请输入八进制数:" );

gets (_____);

for ( _________; (* p ) != ' 0 '; _________)

{

temp = * p - '0';

n = n * 8 + temp;

}

printf ( "对应的十进制数:%dn", _______________ );

return 0; }

8. 输入一个八进制数并转换为十进制数。八进制转换为十进制数的方法是按权相加,即将

八进制每位上的数乘以位权,然后相加之和即是十进制数。利用数组存放八进制数的各位数

码。

#include <>

#define N 6

int main ()

{

int n, temp ;

char s [ N ], * p = s;

printf ( "请输入八进制数:" );

gets ( _________ );

for ( n = 0; ______________; p ++ )

{

temp = * p - '0';

_____________;

}

printf ( "对应的十进制数:%dn", ________ );

return 0; }

9. 判断一个字符串是否为回文(指从左至右和从右至左读都是一样的字符串)。

#include <>

#define N 11 int main ( )

{

int i = 0, n = 0;

char a [ N ] = " ";

char * p1, * p2; printf ( "请输入小于%d位的整数: ",N-1 );

scanf ( "%s", a );

for (_______; (*p2) != ' 0 '; p2 ++ )

n ++;

p1 = a;

p2 = a+n-1; for ( i = 1; p1 < p2; i ++ )

{

if (__________ )

break;

___________;

___________;

}

if ( i > n / 2 )

printf ( "Yes, %s是回文!n", a );

else

printf ( "No, %s不是回文!n", a );

return 0 ;

}

10. 判断一个字符串是否为回文(指从左至右和从右至左读都是一样的字符串)。

#include <>

#define N 11 int main ( )

{

int i = 0, n = 0;

char a [ N ] = " ";

char * p1, * p2; printf ( "请输入小于%d位的整数: ",N-1 );

scanf ( "%s", a );

for (p2=a; (*p2) != ' 0 '; p2 ++ )

________;

p1 = a;

p2 = a+n-1; for ( i = 1; p1 < p2; i ++ )

{

if (*p1!=*p2) ________;

_________;

_________;

}

if ( i > n / 2 )

printf ( "Yes, %s是回文!n", a );

else

printf ( "No, %s不是回文!n", a );

return 0 ;

}

解答:

1. &x, &y

p = *p1

*p1 = *p2

*p2 = p

2. x > y

x, y

int *p1, int *p2

int p

3. int *x, int *y

&x, &y

&x, &y

int p

4. int *x, int *y

x > y

x, y

p = *p1

5. &x, &y

x, y

*p1 = *p2

*p2 = p

6. p=b

p

7. p

8. p

(* p ) != ' 0 '

n = n * 8 + temp;

n

n = 0

p ++

n

p++

*p

9. p2 = a

* p1 != * p2

p1 ++

p2 --

10. n ++

break

p1 ++

p2 --

四.改错题

解答:

五.编程一

解答:

六.编程二

解答:

第十章 文件

一.填空

解答:

二.阅读程序写结果

解答:

三.补充程序

解答:

四.改错题

1. 下列给定程序中,函数fun()的功能是:从字符串s中,取出所有大写字母。程序的功能

是从文件中读出字符串,进行如上处理后,输出到屏幕上。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

#include <>

void fun(char s)

{

int i, j;

for(i=j=0;s[i]!='n';i++)

if(s[i]>='A'|| s[i]<='Z')

s[j]=s[i];

s[j]='0';

}

int main()

{

char s[80];

FILE *fp;

if(fp=fopen("d:", "r")==NULL)

{

printf("Cannot open filen Press any key to exit!");

return 1; }

fgets(s,30,fp);

fun(s);

printf("The string after deleted: ");

puts(s);

printf("nn");

return 0;

fclose(fp);

}

2. 下列给定程序中,函数fun()的功能是:从字符串s中,将大写字母转换成小写字母。程序

的功能是从键盘读入一个字符串,进行如上处理后,写入D盘上指定文件中。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

#include <>

void fun(char s[])

{

int i;

for(i=0;s[i]!='n';i++)

if(s[i]>='A'&& s[i]<='Z')

s[i]=s[i]+32; s[i]='0';

}

int main()

{

char *s;

FILE fp;

gets(s);

if((fp=fopen("d:", "w"))=NULL)

{

printf("Cannot open filen Press any key to exit!");

return 1; }

fun(s);

fputs(fp, s);

printf("nn");

fclose(fp);

return 0; }

3. 程序功能说明:该程序完成从文本文件中读入10名学生信息(学号、姓名、成绩),并

格式输出显示到屏幕上。

#include <>

#define N=10

struct stud_type

{ char name[10];

int num;

float score; }

void main()

{ int i;

struct stud_type stu[N];

FILE *fp;

If((fp=fopen(''d:'', ''w'')) ==NULL)

{

printf(''Cannot open filen Press any key to exit!'');

getch();

}

for(i=0;i

scanf(fp, "%s%d%f", &stu[i].name ,&stu[i].num,&stu[i].score);

for(i=0;i

printf("%8s%5d%8.2fn",stu[i].name,stu[i].num,stu[i].score);

fclose(fp);

4. 程序功能说明:从键盘上输入10名学生信息(学号、姓名、成绩),写入磁盘文件D盘

件中。

#include <>

#define N 10

struct stud_type

{ char *name;

int num;

float score; }

void main()

{ int i;

struct stud_type stu[N];

FILE *fp;

If((fp=fopen(''d:'', ''w'')) ==NULL)

{

printf(''Cannot open filen Press any key to exit!'');

getch();

}

for(i=0;i

scanf("%s%d%f", &stu[i].name ,&stu[i].num,&stu[i].score);

for(i=0;i

printf(fp,"%8s%5d%8.2fn",stu[i].name,stu[i].num,stu[i].score);

fclose(fp);

}

5.

从键盘输入10个学生的信息,然后把它们存入磁盘文件中。程序中有5处错误,

请改正

#include<>

#define N 10 struct student

{

char name[10];

int num;

int age;

char addr[15];

} s[N];

void save()

int main()

{

int i;

printf("please input student information:n");

for(i=0;i

scanf("%s%d%d%s",s[i].name,&s[i].num,&s[i].age,s[i].addr);

save();

printf("data is savedn");

return 0; }

void save()

{

FILE fp;

int i;

if((fp=fopen("","wb"))==NULL)

{

printf("cannot open filen");

return;

}

for(i=0;i

if (fwrite(&s[i],sizeof(struct student),1,fp)=0)

出错*/

printf("file write errorn");

fclose(fp);

}

6. 以下程序中有五处错误,请改正。

#include <>

#include <>

int main( )

{

FILE fp;

char ch;

if((fp = fopen("d:","w+")) == NULL)

{

printf("Cannot open filen Press any key exit!");

getch( );

return 1;

}

printf("input a string:n");

ch = getchar( );

while(ch != n)

/*写文件,并判断是否

{

fputs(ch,fp);

ch = getchar( );

}

rewind(fp);

ch = fgetc(fp);

while(ch != "EOF")

{

putchar(ch);

ch = fgetc(fp);

}

printf("n");

fclose(fp);

return 0;

}

7. 以下程序中有五处错误,请改正。

#include <>

#include <>

int main( )

{

FILE *fp;

char ch;

if(fp = fopen("d:","w+") == NULL)

{

printf("Cannot open filen Press any key exit!");

getch( );

return 1;

}

printf("input a string:n");

ch = getchar( );

while(ch != ‘0’)

{

fputc(fp, ch);

ch = getchar( );

}

rewind(fp);

ch = fgetc(fp);

while(ch !=end)

{

putchar(ch);

ch = fgetc(fp);

}

printf("n");

fclose( );

return 0;

}

8. 将d:langtc文件夹下的文件内容输出到屏幕上。有5处错误,请改正。

#include<>

#include<>

int main( )

{ char ch;

FILE fp;

if ((fp=fopen(“d:langtc”,”r”))=NULL)

{ printf(“Cannot open file n”);

break;

}

while ((ch=getchar())!=EOF)

putchar(ch);

fclose(ch);

return 0; }

9. 将d:langtc文件夹下的文件内容全部以小写字母输出到屏幕上。

#include<>

#include<>

void main( )

{ char ch;

FILE *fp;

if ((fp=fopen("d:langtc","r"))==NULL)

{ printf("Cannot open file n");

return 1;

}

while (ch=fgetc(fp)!=EOF)

{

if ((ch>='A'||ch <='Z')) ch=ch+32; puts(ch);

}

fclose(fp);

return 0; }

10. 以下程序中用户由键盘输入一个文件名,然后输入一串字符(用#结束输入)存放到此

件文件中形成文本文件,并将字符的个数写到文件尾部。

#include <>

#include<>

void main()

{

FILE *fp;

char ch,fname[32]; int count=0; printf("Input the filename :");

scanf("%s",&fname[32]);

if ((fp=fopen("fname","w+"))==NULL)

{

printf("Can't open file:%s n",fname);

exit(0);

}

printf("Enter data:n");

while ((ch=getchar())!="#") {

fputc(fp, ch);

count++;

}

fprintf("n%dn",count);

fclose(fp);

}

11. 下面程序的功能是从filename1中将文件拷贝到filename2,其中源文件和目标文件名均

从键盘输入。有五处错误,请改正。

");

#include <>

#include<>

void main()

{

FILE *fp1,fp2; char ch,*fname1,*fname2; printf("Input the source file name :

scanf("%s",fname1);

if ((fp1=fopen(fname1,"r"))==NULL)

{

printf("Can't open source file:%s n",fname1);

exit(0);

}

printf("Input the targate file name :");

scanf("%s",fname2);

if ((fp2=fopen(fname2,"w"))==NULL)

{

printf("Can't open targate file:%s n",*fname2);

exit(0);

}

while((ch=fgetc(*fp1))=EOF)

fputc(ch,fp2);

fclose(fp1);

fclose(fp2);

}

12. 下面程序的功能是从filename1中将内容读出,显示在屏幕上。其中文件名由键盘输入。

有五处错误,请改正。

#include <>

#include<>

void main()

{

FILE fp1; char ch,*fname1; printf("Input the file name :");

scanf("%s",fname1);

if ((fp1=fopen(fname1,’r’))==NULL)

{

printf("Can't open source file:%s n",fname1);

exit(0);

}

while(ch=fgetc(fp1)!=EOF)

put(ch);

fclose(fp1);

}

13. 下列程序中存在5处C语言语法错误,请识别并改正。程序的功能是在已建立的文件

write

中追加一个字符串,并将文件整个内容输出。

#include <>

#include<>

int main

{

FILE *fp;

char ch,st[20];

if((fp=fopen("d:langtc","w"))==NULL)

{

printf("Cannot open file strike any key exit!");

getchar();

return 1; }

printf("input a string:n");

scanf("%c",st);

fgets(fp, st);

rewind();

while(ch=fputc(fp)!=EOF)

putchar(ch);

printf("n");

fclose(fp);

return 0;

}

14. 以下程序的功能:把文件的内容输出到屏幕上。(共5个错误点)

#include<>

#include<>

int main()

{

char *fp;

char ch;

fp=fopen("", "w");

if(fp=NULL)

{

printf("nCannot open filenStrike any key exit!n");

getchar();

exit(1);

}

ch=fgetc(fp);

if(ch!=EOF)

{

printf("%c",ch);

ch=fgetc(fp);

}

fclose(*fp);

}

15. 以下程序的功能:把文件和文件的内容相连,输出到屏幕上。

共5个错误点,请改正。

#include<>

#include<>

int main()

{

FILE *fp1,fp2;

char ch;

fp1=fopen("", "w");

fp2=fopen("", "r");

if(fp1==NULL&& fp2==NULL)

{

printf("nCannot open filenStrike any key exit!n");

getchar();

exit(1);

}

ch=fgetc(fp2);

if(ch!=EOF)

{

fputc(ch,fp1);

ch=fgetc(fp2);

}

fclose(fp1,fp2);

return 0;

}

16. 下面程序的功能是统计文件中字符的个数,其中有5处错误,请查找并改正(每个2分)

#include<>

#include<>

void main()

{

FILE *fp; int count;

char ch;

*fp=fopen("", "w");

if(*fp=NULL)

{

printf("nCannot open filenStrike any key exit!n");

getchar();

exit(1);

}

ch=fgetc(fp);

while(ch!=EOF)

{

count++;

ch=fgetc(fp);

}

printf("n字符个数=%fn",count);

fclose(fp);

return 0; }

17. 以下程序的功能:把文件的内容读出,压缩掉其中的空格,存入文件target.

txt中,其中有5出错误,请检查并改正。

#include<>

#include<>

int main()

{

FILE *fp1,fp2;

char ch;

fp1=fopen("", "r");

fp2=fopen("", "w");

if(fp1=NULL|| fp2=NULL)

{

printf("nCannot open filenStrike any key exit!n");

getchar();

exit(1);

}

ch=fgetc(fp1);

if (ch!=EOF)

{

while (ch!=' ') fputc(ch,fp2);

ch=fgetc(fp1);

}

fclose(fp1,fp2);

return 0;

}

18. 程序功能说明:该程序完成从文本文件中读入10名学生信息(学号、姓名、成绩),求

学生平均分,显示在屏幕上。其中有5处错误,请检查并改正。

#include <>

#define N 10;

struct stud_type

{ char name[10];

int num;

float score; };

void main()

{ int i;

struct stud_type stu[N];

float s=0; FILE *fp;

If((fp=fopen(''d:'', ''w'')) ==NULL)

{

printf(''Cannot open filen Press any key to exit!'');

getch();

}

for(i=0;i

{

fscanf(fp, "%s%d%f", &stu[i].name ,&stu[i].num,&stu[i].score);

s=s+&stu[i].score;

}

for(i=0;i

printf("%8s%5d%8.2fn",stu[i].name,stu[i].num,stu[i].score);

printf("平均分=%8.2fn", s);

}

19. 以下程序的功能:把字符串“hello world”及文件的内容相连,输出到文件

中。(共5个错误点,请改正)

#include<>

#include<>

int main()

{

FILE *fp1,*fp2;

char ch;

char str= "hello world";

fp1=fopen("d:", "r");

fp2=fopen("d:", "w");

if(fp1==NULL&& fp2==NULL)

{

printf("nCannot open filenStrike any key exit!n");

getchar();

exit(1);

}

fputs(str, fp2);

while(ch=fgetc(fp1)!=EOF)

{

fputc(ch,fp2);

}

fclose(fp1); fclose(fp2);

return 0;

}

20. 下面程序的功能是统计文件中大写字符的个数,其中有5处错误,请查找并改正。

#include<>

#include<>

int main()

{

FILE *fp; int count;

char ch;

*fp=fopen("", "r");

if(*fp=NULL)

{

printf("nCannot open filenStrike any key exit!n");

getchar();

return 1;

}

ch=fgetc(fp);

if (ch!=EOF)

{

if (ch>=’A’&&ch<=’Z’) count++;

ch=fgetc(fp);

}

printf("n大写字符个数=%fn",count);

fclose();

return 0; }

解答:

1. (1)if((fp=fopen(''d:'', ''r''))==NULL)

(2)void fun(char *s)

(3)for(i=j=0; s[i]!= ´0´;i++)

(4)if(s[i]>='A'&& s[i]<='Z')

(5) s[j++]=s[i];

2. (1)for(i=0;s[i]!='0';i++)

(2)char s[80];

(3)FILE *fp;

(4)if((fp=fopen("d:", "w"))==NULL)

(5)fputs( s,fp);

3. (1)#define N 10 (2)struct stud_type

{ char name[10];

int num;

float score; };

(3) If((fp=fopen(''d:'', ''r'')) ==NULL)

(4), (5) fscanf(fp, "%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score);

4. (1)char name[30] (2)struct stud_type

{ char name[10];

int num;

float score; };

(3) If((fp=fopen(''d:'', ''w'')) ==NULL)

(4) scanf("%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score);

(5) fprintf(fp,"%8s%5d%8.2fn",stu[i].name,stu[i].num,stu[i].score);

5. void save();

scanf("%s%d%d%s",&s[i].name,&s[i].num,&s[i].age,s[i].addr);

FILE *fp;

if (fwrite(&s[i],sizeof(struct student),1,fp)=0) fclose();

6. ① FILE *fp;

② if((fp = fopen("d:","w+")) == NULL)

③ while(ch !=’ n’)

④ fputc(ch,fp);

⑤ while(ch != EOF)

7. if((fp = fopen("d:","w+") )== NULL)

while(ch != ‘n’)

fputc(ch, fp);

while(ch !=EOF)

fclose(fp );

8. FILE *fp;

if ((fp=fopen("d:langtc","r"))==NULL)

return 1; while ((ch=fgetc(fp))!=EOF)

fclose(fp);

9. int main( )

if ((fp=fopen("d:langtc","r"))==NULL)

while ((ch=fgetc(fp))!=EOF)

if ((ch>='A'&&ch <='Z')) ch=ch+32; putchar(ch);

10. scanf("%s",fname);

if ((fp=fopen( fname,"w+"))==NULL)

while ((ch=getchar())!='#')

fputc(ch,fp);

fprintf( fp,"n%dn",count);

11. 1、FILE *fp1,*fp2; 2、char ch,fname1[32],fname2[32];

3、printf("Can't open targate file:%s n",fname2);

4、5、while((ch=fgetc(fp1))!=EOF)

12. 1、FILE *fp1 ;

2、char ch,fname1[32];

3、if ((fp1=fopen(fname1, "r"))==NULL)

4、while((ch=fgetc(fp1))!=EOF)

5、putchar(ch);

13. 1、if((fp=fopen("d:langtc","a+"))==NULL) ;

2、scanf("%s",st);

3、fgets(st, fp);

4、rewind(fp);

5、while((ch=fputc(fp))!=EOF)

14. FILE *fp;

fp=fopen("", "r");

if(fp= =NULL)

while(ch!=EOF)

fclose(fp);

15. FILE *fp1,*fp2; fp1=fopen("", "a+");

if(fp1==NULL|| fp2==NULL)

while(ch!=EOF)

fclose(fp1); fclose(fp2);

16. int main()

int count=0; fp=fopen("", "r");

if(fp==NULL)

printf("n字符个数=%dn",count);

17. FILE *fp1,*fp2; if(fp1==NULL|| fp2==NULL)

while(ch!=EOF)

if (ch!=' ') fputc(ch,fp2);

fclose(fp1); fclose(fp2);

18. (1)#define N 10 (2)If((fp=fopen(''d:'', ''r'')) ==NULL)

(3)fscanf(fp, "%s%d%f", stu[i].name ,&stu[i].num,&stu[i].score);

(4) s=s+stu[i].score;

(5) fclose(fp);

19. char str[]= "hello world";

fp1=fopen("d:", "r");

fp2=fopen("d:", "w");

if(fp1==NULL|| fp2==NULL)

while((ch=fgetc(fp1))!=EOF)

20. fp=fopen("", "r");

if(fp==NULL)

while(ch!=EOF)

printf("n大写字符个数=%dn",count);

fclose(fp);

五.编程一

解答:

六.编程二

解答:

/


本文标签: 程序 输出 变量 文件 功能