C语言读程序写结果练习题

更新时间:2023-07-28 01:00:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

C语言读程序写结果练习题

读程序写结果

一、数组

1. #include <stdio.h>

void main()

{

}

2. #include <stdio.h>

void main()

{

float b[6]={1.1,2.2,3.3,4.4,5.5,6.6},t; int i;

t=b[0];

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

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

b[5]=t;

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

printf(“%6.2f”,b[i]); }

void main()

{ int p[7]={11,13,14,15,16,17,18},i=0,k=0;

while(i<7 && p[i]%2)

{ k=k+p[i]; i++;}

printf("k=%d\n",k);

}

4. void main()

{int a[3][3]={1,3,5,7,9,11,13,15,17}; int sum=0,i,j;

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

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

{a[i][j]=i+j; int a[8]={1,0,1,0,1,0,1,0},i; for(i=2;i<8;i++) a[i]+= a[i-1] + a[i-2]; printf(“%5d”,a[i]); for(i=0;i<8;i++) 3. #include <stdio.h>

C语言读程序写结果练习题

if (i==j)

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

}

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

}

5. void main()

{int a[4][4],i,j,k;

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

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

a[i][j]=i-j;

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

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

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

printf("\n");

}

}

6. #include <stdio.h>

main()

{inti,s;

char s1[100],s2[100];

printf("input string1:\n");gets(s1); printf("input string2:\n");gets(s2); i=0;

while ((s1[i]==s2[i])&&(s1[i]!='\0')) i++;

if ((s1[i]=='\0')&&(s2[i]=='\0')) s=0; else s=s1[i]-s2[i];

printf("%d\n",s);

}

输入数据 aid

and

7. void main()

{

} charch[3][5]={ "AAAA","BBB","CC"}; printf("\"%s\"\n",ch[1]);

C语言读程序写结果练习题

1. 1 0 2 2 5 7 13 20

2. 2.20 3.30 4.40 5.50 6.60 1.10

3. k=24

4. sum=6

5.

1 0

2 1 0

3 2 1 0

6. -5

7. “BBB”

二、指针

1、

charcchar(char ch) { if (ch>=‘A’&&ch<=‘Z')

ch=ch-‘A'+‘a';

returnch; } voidmain() { char s[]="ABC+abc=defDEF",*p=s; while(*p) { *p=cchar(*p); p++; } printf("%s\n",s); }

2、

voidmain() {int a[5]={2,4,6,8,10},*p,* *k; p=a; k=&p; printf("%d",*(p++)); printf("%d\n",* *k); }

3、

funa(inta,int b ) { return a+b;} funb(inta,int b ) { return a-b;} sub(int(*t)(),intx,int y )

C语言读程序写结果练习题

voidmain() { int x ,(*p)(); p=funa; x=sub(p,9,3); x+=sub(funb,8,3); printf("%d\n",x); }

4、

voidmain() { char *s="12134211";

int v[4]={0,0,0,0} ;

intk,i; for(k=0;s[k];k++) { switch(s[k]) { case '1':i=0; case '2':i=1; case '3':i=2; case '4':i=3; } v[i]++; } for(k=0;k<4;k++)printf("%d",v[k]); }

5、# include "ctype.h"

void space(char *str) { int i ,t ; charts[81]; for(i=0,t=0 ;str[i]!='\0' ;i+=2) if(! isspace(*(str+i))&&(*(str+i)!='a')) ts[t++]=toupper(str[i]); ts[t]='\0' ;strcpy(str,ts); } voidmain() { char s[81]={"abcdefg"} ;

C语言读程序写结果练习题

}

6、

#include<stdio.h> sub1(char a ,char b) { char c ; c=a ;a=b ;b=c ;} sub2(char * a ,char b) { char c ; c=*a ;*a=b ;b=c ;} sub3(char * a ,char *b){ char c ; c=*a ;*a=*b ;*b=c ;} voidmain() { char a ,b ; a='A' ;b='B' ;sub3(&a ,&b); putchar(a);putchar(b); a='A' ;b='B' ;sub2(&a ,b); putchar(a);putchar(b); a='A' ;b='B' ;sub1(a ,b); putchar(a);putchar(b); }

7、

8、

voidprtv(int * x) {printf("%d\n",++*x); } voidmain() { int a=25 ;prtv(&a); } voidmain() { char b[ ]="ABCDEFG"; char *chp=&b[7]; while(--chp>&b[0]) putchar(*chp); putchar('\n'); }

9、

intaa[3][3]={{2},{4},{6}}; voidmain() { int i ,*p=&aa[0][0];

C语言读程序写结果练习题

{ if(i==1)aa[i][i+1]=*p+1; else ++p; printf("%d",*p); } }

10、

1、abc+abc=defdef 2、24

3、17

4、0008

5、CEG

6、BABBAB

7、GFEDCB 8、26 9、00

10、120 intf(int b[ ],int n) { int i ,r=1; for(i=0;i<=n;i++) r=r*b[i]; return r; } voidmain() { intx,a[]={2,3,4,5,6,7,8,9}; x=f(a,3); printf("%d\n",x); }

三、结构体 1、

#include <stdio.h> structabc { int a, b, c; }; main() { structabc s[2]={{1,2,3},{4,5,6}}; int t;

C语言读程序写结果练习题

2、

3、

4、

5、

printf("%d \n",t); } #include <stdio.h> structstu { intnum;char name[10];int age;}; void fun(structstu *p) { printf("%s\n" ,(*p).name); } voidmain() { structstu students[3]={{9801,"Zhang",20} ,{9802,"Wang", 19} , {9803,"Zhao",18} }; fun(students+2); } #include <stdio.h> voidmain() { enum team { my ,your=4 ,his ,her=his+10}; printf("%d%d%d%d\n",my,your,his,her); } #include <stdio.h> structst { intx;int *y;} *p ; intdt[4]={10,20,30,40}; structstaa[4]={50,&dt[0],60,&dt[1],70,&dt[2],80,&dt[3] }; void main() { p=aa; printf("%d, ", ++p->x); printf("%d, ",(++p)->x); printf("%d\n",++(*p->y)); } #include <stdio.h> unionmyun {struct

C语言读程序写结果练习题

6、

7、

8、 int k; } a; voidmain() { a.u.x=4; a.u.y=5; a.u.z=6; a.k=0; printf("%d\n",a.u.x); } #include <stdio.h> struct STU { charnum[10]; float score[3]; }; voidmain() { structSTU s[3]={{“20021”,90,95,85},{“20022”,95,80,75}, {“20023”,100,95,90}},*p=s; int i; float sum=0; for(i=0;i<3;i++) sum=sum+p->score[i]; printf(“%6.2f\n”,sum); } #include <stdio.h> #include <stdlib.h> struct NODE { intnum; struct NODE *next; }; voidmain() { struct NODE *p,*q,*r; p=(struct NODE*)malloc(sizeof(struct NODE)); q=(struct NODE*)malloc(sizeof(struct NODE)); r=(struct NODE*)malloc(sizeof(struct NODE)); p->num=10; q->num=20; r->num=30; p->next=q;q->next=r; printf(“%d\n”,p->num+q->next->num); }

C语言读程序写结果练习题

typedef union student { char name[10]; longsno; char sex; float score[4]; }STU; voidmain() { STU a[5]; printf(“%d\n”,sizeof(a)); }

1、 6

2、 Zhao

3、 04515

4、 51, 60, 21

5、 0

6、 270.00

7、 40

8、 80

本文来源:https://www.bwwdw.com/article/ssim.html

Top