您的当前位置:首页正文

华中科技大学2003-2006级信息学院《C语言程序设计》考试试题及答案(DOC)

来源:九壹网
2003级信息学院《C语言程序设计》考试试题 系 班级

姓名 学号 成绩 一. 判断下列语句或程序的对错。(“×”表示错,“√”表示对)(10分)

1 int x=y=z=20; ( × ) y,z需要在前定义 2 #define M 50; ( × ) 不能有分号 3 printf(“%\”,(40,50,60)); ( √ ) 4 float a[100],*p;

p=a++; ( × ) 数组名是地址常量,不能做++操作 5 char str[20];

str[20]=0; ( × ) str[20]单元不可用

6 int data[4]={0,1,2,3,4}; ( × ) 0,1,2,3,4是5个元素,与data[4]不匹

7 int x=’\\014’; ( √ )

8 int xyz.1=2; ( × ) xyz.1不是合法的标识符(变量名) 9 char *p=”c:\est\\prg.c”; ( × ) 修改为c:\\\est\\\\prg.c 10 int *p;

*p=20; ( × ) 指针不能用常量进行赋值

二. 计算下列表达式的值 (10分)

设unsigned int a=15,b=21,c=5,d=1; float f;

(1) f=b/c ( 4.0 ) b/c是整数除,先得到4,然后赋值给f (2) !(a+b)+c-1&&b+c/2 ( 1 ) (3) a^b+b&0x55+c<<3|015 ( 0x000f )

(4) d*=c--%b++ ( 5 ) (5) a%=(c%=2) ( 0 )

三.程序改错(10分)

1从键盘上输入三个字符串,然后按字典(从小到大)顺序进行输出 # include # include

void swap(char *,char *); //函数声明需要分号结束 main() {

char a[80],b[80],c[80];

scanf(“%s%s%s”,&a,&b,&c); //去掉&号,数组名即表示地址

if(a>b)swap(a,b); strcmp(a,b)>0 a,b比较需要用字符串比较函数 if(b>c)swap(b,c); strcmp(b,c)>0 b,c比较需要用字符串比较函数 printf(“%s\\n%s\\n%s\\n”,a,b,c); }

void swap(char *pstr1,char *pstr2) {

char *p; //char p[80];

p=pstr1; //strcpy(p,pstr1);字符串赋值需要用拷贝函数

pstr1=pstr2; //strcpy(pstr1,pstr2); pstr2=p; //strcpy(pstr2,p);

}

(2)求某班30个学生数学成绩的最高分和平均分. # include

flaot process(float *p1,int n,int *p2); 添加函数声明 main() {

float a[30],aver; int ;

for(m=0;m<30;m++) scanf(“%f”,&a[m]);

max=process(a,30,&aver);

printf(“max=%f,ave=%f\\n),max,aver); }

float process(float *p1,int n,int *p2) {

char x;

int temp; //float temp 与函数返回类型匹配 for(x=0;x<=n;x++) //for(x=0;xif (p1[x]>temp) temp=p1[x]; *p2+=p1[x]; }

p2=p2/n; //*p2=*p2/n; return temp;

}

四.程序填空

(1) 有一分数系列:2/1,3/2,/5/3,8/5,13/8,21/13……求出这数列的前50项和 # include main() {

int m;

float__ ________; // t,s=0,a=2,b=1 for(m=1;___ _______;m++) //m<=50 {

s=s+a/b; t=a;

a=_a+b_____; b=_t_; }

printf(“sum=%f\\n:,s);

}

(2) 按如图所示格式打印杨辉三角形的前10行 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 ……… main() {

int m,n,a[10][10]; for(m=0;m<10;m++) {

a[m][0]=1;

__________________; a[m][m]=1; }

for(__m=2___;m<10;m++)

for(___n=1____;__nfor(n=0;__n<=m_____________;n++) printf(“%6d”,a[m][n]); _printf(“%5d”,a[m][n]); } }

五.写输出结果 (1)

# include void main() {

int m,n,p,s; s=0;

for(m=1;m<=4;m++) {

for(n=1;n<=m;n++) { p=1;

for(n=1;n<=m;n++) p=p*n; }

s=s+p; }

printf(“s=%d\\n”,s);

}

S = 33; //1!+2!+3!+4!; (2)

# include

void auto_static(); int g_val; main() {

int m;

for(m=0;m<5;m++) auto_static(); }

void auto_static() {

int au=0;

static int st=0;

printf(“au=%d,st=%d,g_val=%d\\n”,au++,++st,g_val++); }

au=0,st=1,g_val=0 au=0,st=2,g_val=1 au=0,st=3,g_val=2 au=0,st=4,g_val=3 au=0,st=5,g_val=4

(3)

# include main() {

int i,j,a[][3]={2,4,6,1,2,3}; int *p[2]={a[0],a[1]}; for(i=0;i<2;i++) {

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

printf(“%3d\”,*(p[i]+j)); printf(“\\n”); } }

2 4 6 1 2 3 (4)

# include # include struct person

{

char name[20]; int count; };

void main() {

struct person leader[3]={{“li”,0},{“zhang”,0},{“wang”,0}}; char name[20],m,n; for(m=1;m<7;m++) {

scanf(“%s”,name); for(n=0;n<3;n++)

if(!strcmp(name,leader[j].name)) {

leader[j].count++; break; } }

printf(“\\n”);

for(m=0;m<3;m++) {

printf(“%s:%d\\n”,leader[m].name,leader[m].count); } }

当程序运行时,键入情况如下: li wang lei li wang zhang

写出程序的输出结果。

基本上是书中例题8.3 p238-239 li:2 zhang:1 wang:2 (5)

#include #define NULL 0 void main() {

char **pp;

char *name[]={“fortran”,”basic”,”pascal”,””}; int m;

pp=name;

for(m=0;**pp!=NULL;m++,pp++) printf(“%s\\n”,*pp+m); }

fortran asic scal

六.编写程序(35分) (1) 一个程序实现这样的功能:商店卖西瓜,10斤以上(包括10斤,

以下同)的每斤0.8元,8斤以上10斤以下的每斤0.6元,8斤以下的每斤0.4元。输入西瓜的重量和顾客所付的金额,输出应付货款和应找钱数。(9分)

#include void main() {

float weight,a,b,c;

printf(\"请输入重量和所付金额:\"); scanf(\"%f%f\ if(weight<8.0) { b = weight*0.4; c = a - b; }

else if(weight<10) { b = 8*0.4+(weight-8)*0.6; c = a - b; } else { b = 8.0*0.4+2*0.6+(weight-10)*0.8; c = a - b; }

printf(\"应付货款:%f 应找钱数:%f\\n\}

(2) 编写一个函数,求两个在整数的最大公约数;主函数完成两个整数

的输入,调用所编函数和输出所求的最大公约数。(9分)

(3) 编写一个函数,删除给定字符串中的指定字符,如给定字符串

“abcdfrc”,删除指定的字符‘c’后,字符串变成“abdfr”;主函数完成给定字符串和指定字符的输入,调用所编函数,输出处理后的字符串。注意:不得使用全局变量,注意程序结构。(9分)

(4) 编程处理某班30个学生4门课的成绩,它们是数学,物理,英语

和计算机,按学号依次输入学生的学号,姓名,性别(用1表示男

生,0表示女生)和4门课成绩。要求以清晰的格式从高分到低分的顺序打印平均分高于全班总平均分的男生的成绩单。(8分) 要求:输入.输出,计算和排序分别用程序实现,主函数只是调用这些函数。不得使用全局变量,注意程序结构。

#include #define NUM 5 struct Student {

int number; char name[20]; int sex; float maths; float phys; float eng; float comp; float aver; };

Input(struct Student *stu) {

int i;

for(i=0;iAver(struct Student *stu,float *total_aver) {

int i;

float total=0; float sum=0;

for(i=0;i{ total = stu[i].maths+stu[i].phys+stu[i].eng+stu[i].comp; stu[i].aver= total/4; sum += total; }

*total_aver = sum/(4*NUM); }

Sort(struct Student *stu) {

int i,j;

struct Student temp; for(i=0;istu[j].aver) { temp = stu[i]; stu[i] = stu[j]; stu[j] = temp; } } }

Output(struct Student *stu,float total_aver) {

int i;

printf(\"total_aver:%f\\n\

printf(\"num name sex math aver\\n\");

for(i=0;i=total_aver) printf(\"%d %s %d %f %f %f %f %f\\n\ stu[i].number, stu[i].name, stu[i].sex, stu[i].maths, stu[i].phys, stu[i].eng, stu[i].comp, stu[i].aver);

phys english computer } }

void main() {

struct Student stu[NUM]; float total_aver; Input(stu);

Aver(stu,&total_aver); Sort(stu);

Output(stu,total_aver); }

2004级信息学院《C语言设计》考试试题

一、判断下列语句或程序的对错。 10分 √ 1 int x=y=z=’0’; (×) y,z没有定义 2 #include ; (×) 不能有分号,#开头的结尾均不能有分号; 3 printf(“%s\\n”,”c language”); (√) 4 float a[100];

int *p=a; (×) 数据类型不匹配 5 char str[20];

6 int data[4]={0,1,2,3,4}; (×)五个元素,但是只有四个单元 7 float x=1.45e+310L; (×)数值越界

8 int xyz-1=2; (×) 9 int x=‘\\xae’ ; (√) 10 int *p,a[2][3] ;

p=a ; (×) 数据类型不匹配

二 计算下列表达式的值 10分 设 unsigned int a=10,b=17,c=5,d=3; float f ;

(1)f=b/c ( 3.0 ) (2)!(a+b)+c-1&&b+c/2 ( 1 ) (3)(a^b)+(c>>1+d) ( 0x1b ) (4)a+=b%=a=b ( 17 ) (5)a=2,b=a*++b ( 36 ) 三 程序改错 10分 (1) 求两个浮点数的平方和及平方差 #include

float calculate (float x,float y,float *sub);添加函数原型声明

main () {

float a,b;

float add_reasult, sub_result; scanf (“%f,%f”,a,b);

add_result=calculate(a,b,&sub_result);

printf( “a*a+b*b=%d,a*a-b*b=%d\\n”,add_result,sub_result); }

float calculate (float x,float y,float *sub) 添加函数类型 {

float *temp; 应该直接定义为变量float temp; sub=a*a-b*b ; *sub=a*a-b*b; temp = a*a+b*b;

return *temp; return temp }

(2) 统计N 个字符中大写字母和数字字符的个数 #include #define N 5

Count(char *str,int *result); 添加函数声明

main () {

char string[N][80]; char i;

int Capital_Count=0,Num_Count=0;需要初始化为0

for(i=0;iscanf( “%s”,&string[i]) ; 去掉&符 for(I=0;ICapital_Count+=Count(string[I],&Num_Count); Printf(“Capital count :=%d,numbercount=%d\\n”

,Capital_Count,Num_Count) ;

}

Count(char *str, int *result) {

int temp,I ; int temp=0,i; temp应该初始化为0 for(I=0;I<80;I++) {

If(str[I]>=’A’&& str[I]<=’Z’) Temp++;

If(str[I]>’0’||str[I]<’9’) *result++; }

return temp;

}

四 程序填空 10分

(答案参考书中p85~86)

3

(1)利用公式 sin x=x-x /3!+x

x=0.5,n=20 #include main() {

float y,s,x,d,t; int n,I,j;

scanf(“%d%f”,&n,&x); s=1.0;

____________________________; for(I=2;Id=t=__________________________; for(j=1;_______________;j++) {

d=________________; t=________________; }

s=(-1)*s;

y+=_____________________; }

(2)利用库函数char *strstr(char *sl,char *s2)在给定字符串中查找子串最后(最右)一次出现的位置。如果S2并没有出现在S1的任何地方,函数返回一个NULL

指针。如果第二个参数是一个空字符串,函数就返回S1; 注:库函数char strstr(char*s1,char*s2),这个函数在S1中查找子字符 串S2第一次出现的起始位置,并返回一个指向该位置的指针。如果S2并没有出现在S1的任何地方,函数返回一个NULL指针。如果第二个参数是一个空字符串,函数返回S1;

(答案见书中p196~197)

#include #include

void main(void) {

char str[80]=”ABCdabcdfgabc”; char *p;

p=my_strrstr(str,”abc”); printf(“%s \\n”,p); p=my_strrstr(str,” “); printf(“%s\\n”,p); }

char *my_strrstr(char *s1,char*s2) {

char *last; char *current;

_________________________; if(________________________) {

last=current=_____________; While(______); {

last=current;

current=_______; } }

return last; }

五.写输出结果(20分)

(1)

#include void fun(int*,int); void main() {

int a[]={5,6,7,8},i; fun(a,4);

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

printf(\"%d\\n\}

void fun(int *b,int n) {

int i;

for(i=0;i} 0 2 4 6 (2)

#include void main() {

int i,j,max;

int row=0,column=0;

int a[3][3]={{1,2,3},{2,-3,4},{9,4,7}}; max=a[0][0]; for(i=0;i<3;i++) for(j=0;j<3;j++) {

if(a[i][j]>max) {

max=a[i][j]; row=i+1; column=j+1; } }

printf(\"max=%d,row=%d,column=%d\\n\}

(书中例题5.5,p123) max=9,row=3,column=1 (3)

#include int n=1; void func();

void main() {

static int x=5; int y; y=n;

printf(\"main:x=%d,y=%d,n=%d\\n\ func();

printf(\"main:x=%d,y=%d,n=%d\\n\}

void func() {

static int x=4; int y=10; x=x+2; n=n+2; y=y+n;

printf(\"func:x=%d,y=%d,n=%d\\n\}

main:x=5,y=1,n=1 func:x=6,y=13,n=3 main:x=5,y=1,n=3

(4)

#include #include struct person {

char name[20]; int count; };

void main() {

struct person leader[3]={{\"li\ char name[20],m,n; for(m=1;m<7;m++) {

scanf(\"%s\ for(n=0;n<3;n++)

if (!strcmp(name,leader[n].name)) {

leader[n].count++; break; } }

printf(\"\\n\"); for(m=0;m <3;m++)

printf(\"%s:%d\\n\}

当程序运行时,键入情况如下: Li Wang Lei

Li Wang Zhang

写出程序的输出结果。(基本上是书中例题) Li:2 Zhang:1 Wang:2

(5)

#include #include

void main() {

char *name[]={\"capital\ int a,b,n=4; char *temp;

for(a=0;aif(strcmp(name[a],name[b])>0) {

temp=name[a]; name[a]=name[b]; name[b]=temp ; }

} 在此之前是书中的例题7.19 for(a=0;aprintf(\"%s\\n\ 输出时应该能够识别指针及偏移情况 }

capital ndex rge ll

六、 编写程序 (35分)

2

(1) 求一元二次方程ax +bx+c=0的根,实系数a,b,c从终端输入,只考虑两个不同实根和两个相同的实根 (9分) (书中例题3.4,p66`67) #include #include

void main() {

float a,b,c; float x1,x2; float x3,x4; float m;

printf(\"input the numbers:a,b,c\"); scanf(\"%f%f%f\

if(a==0) {

printf(\"the input is error!\\n\"); return; }

m=b*b-4*a*c;

if(m>0) {

x1=(-b+sqrt(m))/(2*a); x2=(-b-sqrt(m))/(2*a);

printf(\"x1:%.2f x2:%.2f\\n\ }

else if(m==0) {

x1=x2=(-b+sqrt(m))/(2*a); printf(\"x1=x2=%.2f\\n\ } else {

x3=-b/(2*a);

x4=sqrt(m)/(2*a);

printf(\"x1=%.2f+%.2fi\\n\ printf(\"x2=%.2f-%.2fi\\n\ } }

(2)编写一个函数,求s=a+aa+aaa+--------+aaaaaaaaa-----a,其中a是一个数字,例如2+22+222+2222(此时n=4)。主函数a和n的输入,调用所函数和输出所求的 累加和;编写的函数完成计算。 (9分) 注意:不得使用全局变量,注意程序结构 (书中习题3:4。16) #include

#include

long Cal(long a,long n); main() {

long sn=0; long a,n;

printf(\"please input a n:\"); scanf(\"%d%d\ sn = Cal(a,n);

printf(\"a+aa+aaa+...+aa...a=%ld\\n\\n\}

long int Cal(long a,long n) {

int i;

long sn=0,m=0; for(i=0;im=m*10+a; sn+=m; }

return sn; }

(3)从十个字符串中找出所要的某个字符串,若能找到则把他删除,然后输出新字符串;若未找到则输出“”can not fond”. (9分) #include #include main() {

char s[10][80]; char s2[80]; int i,j; int num=0;

printf(\"please enter 10 string:\\n\"); for(i=0;i<10;i++) gets(s[i]);

printf(\"please enter s2 string:\"); gets(s2);

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

if(strcmp(s[i],s2)==0) {

for(j=i;j<9-num;j++) {

strcpy(s[j],s[j+1]); } i--; num++; } }

for(i=0;i<10-num;i++) puts(s[i]); }

(4)一个班有N个同学,修5门课从键盘输入他们的性名、学号、性别和成绩。 1)按平均成绩从高到底打印全班的成绩单。 2)求第三门课的平均分

3)找出平均分在90以上或全部功课在85以上的女生。

要求:输入、输出、计算、排序和查找分别用函数实现,主函数只是调用这些函数。不得使用全局变量。 #include #define N 5 struct Student {

char name[20]; int number; int sex;

float score[5]; float aver; };

void Input(struct Student *stu);

void Average(struct Student *stu,float *aver); void Sort(struct Student *stu);

void Search(struct Student *stu,float score);

main() {

struct Student stu[N]; float score=85.0; float aver3; Input(stu); Sort(stu);

Average(stu,&aver3);

printf(\"average3 is %.2f\\n\

Search(stu,score); }

void Input(struct Student *stu) {

int i,j;

float aver=0; for(i=0;iaver=0;

printf(\"please enter name:\"); gets(stu[i].name);

printf(\"please enter number sex:\");

scanf(\"%d%d\ printf(\"please enter score(5):\"); for(j=0;j<5;j++) {

scanf(\"%f\ aver +=stu[i].score[j]; }

stu[i].aver = aver/5; getchar(); } }

void Sort(struct Student *stu) {

int i,j;

struct Student temp; for(i=0;ifor(j=i+1;jif(stu[i].avertemp=stu[i]; stu[i]=stu[j]; stu[j]=temp; } } }

printf(\"Name Numb Sex score(5) aver\\n\");

for(i=0;i{

printf(\"%8s %d %d %.2f %.2f %.2f %.2f %.2f %.2f\\n\

stu[i].name, stu[i].number, stu[i].sex,

stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],

stu[i].aver); } }

void Average(struct Student *stu,float *average3) {

float sum3=0; int i;

for(i=0;isum3 += stu[i].score[2]; *average3=sum3/N; }

void Search(struct Student *stu,float score) {

int i,j; int flag =1;

printf(\"\\naver>90 score>85\\n\");

printf(\"Name Numb Sex score(5) aver\\n\"); for(i=0;ifor(j=0;j<5;j++)

flag = flag&&(stu[i].score[j]>score); if(stu[i].aver>90.0&&flag) {

printf(\"%8s %d %d %.2f %.2f %.2f %.2f %.2f %.2f\\n\

stu[i].name, stu[i].number, stu[i].sex,

stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],s

tu[i].score[4],

stu[i].aver);

} }

printf(\"\\n\\n\"); }

2005级信息学院《C语言程序设计》考试试题

一、 判断下列语句或程序的对错。(“╳”表示错,“√”表示对)(10分) 1 float s=0,s1.1=0; ( ╳ ) 2 #define M=100 ( ╳ ) int a[M];

3 char *p[]=”\\”c test\\” ”; ( ╳ ) 4 if((a=b)>0)t=a; ( √ ) 5 char str1[20]==” china”,str2[20]; ( ╳ ) str2=str1;

6 int i,*p=&i; ( √ ) 7 float a[100],*p; ( √ )

p=a+1;

8 printf(“%d\\n”,(30,50,80)); ( √ ) 9 int x,y; ( √ )

y=20,x=y+’a’;

10 int (*p)[20],a[20]; ( √ ) p=(int (*)[20])a;

二、 计算下列表达式的值(10分) 设unsigned int a=7,b=17,c=5,d=3; float x=2.5,y=4.7;

(1)x+a%3*(int)(x+y)%2/4 ( 2.5 ) (2)!(a<=b)||(bb)?++a:++b ( 3 ) (5) a/2.0*b-c/3+d ( 61.5 )

三、程序改错,用标记指出错误,并在旁边将错误的地方进行修正(10分)

(1) 求某班30个学生英语成绩的平局分 #include

float calculate (float *p,int n); /*添加函数声明*/

main () {

float a[30],aver; int m;

for(m=0;m<30;m++) scanf(“%d”,a+m); aver=calculate(a,30); printf(“aver=%f\\n”,aver);

}

float calculate (float *p,int n) /*添加函数的数据类型*/ {

char x;

int temp; /*int temp=0 需要设置初始值为0;

for(x=0;x<=n;x++) /*for(x=0;xreturn temp/n; }

(2)找N个字符串中最大字符串 #include #define N 5

char *process (char p[][80],int n);添加函数声明

main() {

char string[N][80],*p; char i;

for(i=0;iscanf(“%s”,&string[i]); /* scanf(“%s”,string[i]); 除掉取地址符*/ p=process(string,N);

printf(Max string :=%s\\n”,p); }

char *process (char *p[][80],int n) /*去掉*号,或改为char p[][]*/ {

int i;

char *pmax;

for(i=1;iif(p[i]0*/ pmax=p[i];

return pmax; }

四 程序填空(10分) (1) 利用公式 sin x 设x=0.4时sin x的值 # include main() {

float x,sn,un; int i,j;

x=0.4;

un=_________________________; sn=__________________________; i=1;

while(un=1e-5) {

un=_________________________; sn=__________________________; i++; }

printf(“x=%f,sinx=%f\\n”,x,sn); }

(2)以下itoa(int a,char *)是一个将整数转换为对应的数字串的函数,reverse(char*)是一个将字符串翻转函数,被itoa调用,main()函数是用来测试itoa函数的. (书中例题:p197) #include #define LENGTH 6 void reverse(char *); void itoa(int,char *);

void main() {

int n;

char s[LENGTH];

printf(“input a integer:\\n”); scanf(“%d”,&n); itoa(n,s);

printf(“string:%s”,s); }

void itoa(int n,char *p) {

int i,sign;

if((sign=n)<0) n= -n; i=0; do {

_p[i++]=n%10+’0’; }while((n/=10)>0);

if(sign<0)

p[i++]= ‘-‘; p[i]=‘\\0’; reverse(p); }

void reverse(char *) {

int i,j,k;

for(i=0,j=strlen(p)-1;ik=p[i]; p[i]=p[j]; p[j]=k; } }

五, 写出结果 (1)

#include

void main() {

int i,j,p,s; s=0;

for(i=1;i<=4;i++) { p=1;

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

p=p*j; s=s+p; }

printf(s=\"%d\\n\ }

S = 33 (2)

#include void mian() {

int i,j,a[5][5]; for(i=0;i<5;i++) {

a[i][i]=1; a[i][0]=1; }

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

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

a[i][j]=a[i-1][j-1]+a[i-1][j]; for(i=0;i<5;i++) {

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

printf(\"%4d\ printf(\"\\n\"); } }

(打印杨辉三角) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 (3)

#include int d=1; void func(); void main(); {

func(); func(); func(); }

void func() {

static int a;

register int b=0; int c=0;

printf(\"a=%4d\b=%4d\c=%4d\d=%4d\\n\

}

a=0 b=0 c=0 d=1 a=1 b=0 c=0 d=2 a=2 b=0 c=0 d=3

(4)

#include struct Student {

char No[10]; char name[20]; char sex; int age; };

void main() {

struct Student stu[3]={{\"10101\Lin\

fan\

struct Student *p; int m;

for(p=stu+1,m=0;m<2;m++)

printf(“%8s:%12s:%d\\n”,p->No+3,p->name,p->age); }

02: zhang fan : 19 02: zhang fan : 19 (5)

#include #include void main() {

char *name[]={“capital”,”index”,”large”,”small”}; int a,b,n=4; char *temp;

for(a=0;aif(strcmp(name[a],name[b])<0) {

temp=name[a];

name[a]=name[b]; name[b]=temp; }

}

for(a=2;aprintf(“%s\\n”,name[a]+a); } dex ital

六 编写程序 (35分)

1)(9分)售货价格随购货数量而异,买10个以上(含10,下同)为90折,买20个以上为85折,买30个以上为80折,买50个以上为6折,设商品的价格为65元,要求输入购买商品的个数后,打印出总货款。 #include #define PRICE 65 main() {

int number;

double payment;

printf(\"please enter the number:\"); scanf(\"%d\

if(number<=10) payment = PRICE*number*1.0; else if(number<=20) payment = PRICE*number*0.90; else if(number<=30) payment = PRICE*number*0.85; else if(number<=50) payment = PRICE*number*0.80; else payment = PRICE*number*0.60;

printf(\"购买产品数:%d 总货款:%lf\\n\} 2)(9分)编写一个函数,求x=a1/2,其中求平方根的迭代公式为 xn+1=1/2(xn+a/xn),要求前后两次求出的差的绝对值小于10-6,编写的函数完成求平方根运算。主函数完成整数a的输入,调用所编函数,输出所求平方根; 注意:不得使用全局变量,注意程序结构 #include #include float Root(float a) {

float x1=1.0,x2; while(1)

{ x2=1.0/2*(x1+a/x1); if(fabs(x2-x1)<1e-6) break; x1=x2; }

return x2; }

main() {

float a,x;

printf(\"input a:\"); scanf(\"%f\ x = Root(a);

printf(\"sqrt(%f)=%f\\n\\n\} 3)(9分)缩写一个函数,统计字符串中子串出现的次数,如在字符串“10101000101”中出现子串“101”的个数为2;主函数完成字符串和子串的输入,调用所编函数得到子串出现的次数,并输出出现的次数。 注意:不得使用全局变量,注意程序结构 #include #include

int mystrstr(char *s1,char *s2) {

char *last; char *current; int number=0; int len;

len = strlen(s2); last = s1; if(*s2!='\\0') { last = current = strstr(s1,s2); while(current!=NULL) { last = current; current = strstr(last+len,s2); number++; } }

return number; }

main() {

char s1[80],s2[80]; int num;

printf(\"please input s1:\"); gets(s1);

printf(\"please input s2:\"); gets(s2);

num = mystrstr(s1,s2);

printf(\"%s have %s: %d times\\n\}

4)输入10个字符串,然后排序输出。排序的原则由键盘输入的数来决定,为0,将输入的字符串按整数值大小从小到大排序,否则按字典顺序排序。要求:输入、输出、排序分别用函数实现,主函数只是调用这些函数。不得使用全局变量,注意程序结构。

(参见书中p198~201)这里的答案与书中有点不同,希望大家能够掌握多种方法

#include #include

#define NUM 10

void Input(char p[][80],int n); void Sort(char p[][80],int n); void Output(char p[][80],int n);

void main() {

char str[NUM][80]; Input(str,NUM); Sort(str,NUM); Output(str,NUM); }

void Input(char p[][80],int n) {

int i;

for(i=0;i}

void Sort(char p[][80],int n) {

int i,j;

char temp[80]; for(i=0;i0) { strcpy(temp,p[i]); strcpy(p[i],p[j]); strcpy(p[j],temp); } }

void Output(char p[][80],int n) {

int i;

for(i=0;i2006级信息学院《c语言程序设计》考试试题

系: 班级:

姓名: 学号: 成绩:

一、 判断下列语句或程序的对错。(“x”表示错,“√”表示对)(10分)

1 int x=0, y=z=0; ( x ) 2 #include ( √ ) 3 printf(“%d\\n”, “c language”); ( x ) 4 int x[100];

float *p = x; ( x ) 5 char *str;

str = “string”; ( x ) 6 int data[ ] = {1,2,3,4}; ( √ ) 7 char x = ‘A’;

float y = x ; ( x ) 8 int xyz_1 = 2; ( √ ) 9 int x = ‘\\xef’; ( √ ) 10 float *p, a[2][3];

p= *a; ( √ )

二、 计算下列表达式的值(10分)

设unsigned int a = 10, b = 17, c = 5, d = 3; float f;

(1) f = b%c ( 2.0 ) (2) !(a+b)+ c - 1||b+c/3 ( 1 )

(3) (a&b) + (c<<1|d) ( 0x0b )或11 (4) a -= b /= a+b ( 10 ) (5) a = 2, b = a*b++ ( 34 )

三、 程序改错(10分)

(1) 从键盘输入三个整数, 然后按从大到小的顺序进行输出 #include

main() {

int a, b , c;

scanf(“%d,%d,%d”, a, b, c ); &a,&b,&c if (a>b) swap( &a , &b); ff(b>c) swap(&b, &c);

printf(“%f\\n%f\\n%f\\n”, a, b, c); %d\\n%d\\n%d\\n }

void swap( int *p1, int *p2) {

int *temp; int temp; temp = p1; *p1

p1 = p2 ; *p1 = *p2; p2 = temp; *p2 }

(2) 找出N个字符串最大字符串和最小字符串 #include #include

char *process ( char p[ ][80], int n , char *pmin); 添加函数原型声明

#define N 5

main() {

char string[N][80,*pmax,*pmin; char I;

for(i=0;iscanf(“%s”,&string[i]); string[i] pmax = process(string, N, pmin);

printf(“Max string : = %s , Min string : = %s\\n”, pmax,pmin);

}

char *process ( char *p[ ][80], int n , char *pmin) char p[ ][80] {

int i;

char *pmax = pmin = p[0]; for( i =1;iif(p[i]0 字符串比较需要用strcmp pmax = p [i]; strcpy(pmax,p[i]); 字符串赋值需要用strcpy; for(i=1;iif(p[i]>pmin) strcmp(p[i],pmin)<0 pmin = p[i]; strcpy(pmin,p[i]); return pmax; }

四、 程序填空(10分)

(1) 利用公式 ∏/4 = 1 – 1/3 + 1/5 – 1/7 + …….计算∏的值,直到最后一项的

绝对值小于10-6为止

#include main() {

int s;

float n,t,pi;

n=1,pi=0,s=1,t=1 while( fabs(t)<1e-6 ) {

pi= pi + t;

n = n + 2 ;

s = (-1)*s ; t = s/n ;

}

pi = 4*pi ;

printf(“pi = % 10.6f\\n”,pi); }

(2) 编写一个函数,删去给定字符串中的数字字符 #include

char *pro_str(char *s); void main(void) {

char str[80];

printf(“input string:\\n”);

gets(str);

puts( pro_str(s) ); }

char *pro_str(char *s) {

char *temp = s ;

while( *s!=’\\0’ ) {

if(*s >=’0’&& *s <=’9’)

strcpy(s,s+1) ; else s++; }

return temp ; }

五、写输出结果

(1)

#include void fun(int *, int ); void main() { int a[] = {5,6,7,8},i; fun(a, 4); for(i=1;i<4;i++) printf(\"%d\\n\}

void fun(int *b, int n) { int i; for(i=0;i#include void main( ) {

int valueA[2][3] = {{1, 0, 5}, {0, 3, 4}}; int valueB[3][2] = {{-4, 9}, {6, -1}, {1, 2}};

int valueC[2][2] = {0, 0, 0, 0};

int i, j, k;

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

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

valueC[i][j] += ValueA[i][k]*valueB[k][j]; } }

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

printf(“\\n”);

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

printf(“%5d”, valueC[i][j]; } }

(矩阵的乘积) 1 19 22 5 (3)

#include int n;

void func( ); void main( ) {

printf(\"main: n = %d\\n\func( ); func( );

printf(\"main: n=%d\\n\}

void func( ) {

static int n = 0; register int x = 0; int y = 0;

printf(\"func : n = %d, x = %d, y = %d\\n\}

main: n = 0

func: n=0,x=1,y=0 func: n=1,x=1,y=0 main:n=0 (4)

#include struct Key {

char *keyword; int keyno; };

void main( ) { struct Key kd[3] = {{\"are\struct Key *p; int a; char chr; p = kd;

a = ++p->keyno; printf(\"a = %d\\n\a = (++p)->keyno; printf(\"a = %d\\n\

p = kd;

chr = *p->keyword;

printf(\"char = %c\\n\}

a = 124 a = 456 chr = a (5)

#include #include void main() {

char *name[ ] = {\"capital\ char **pp; int a;

pp = name; for(a=0; a<4; a++,pp++) printf(\"%s\\n\}

capital ndex rge ll

六、编写程序(35分)

1)给出一百分制成绩,要求输出成绩等级‘A’,‘B’, ‘C’, ‘D’, ‘E’, 90分以上为’A’, 80-89分为‘B’,70-79分为‘C’,, 60-69分为‘D’, 60分以下为‘E’。(9分) #include main() { int score; printf(\"please enter the score:\"); scanf(\"%d\ if(score>=90) printf(\"the score:%d-----%c\\n\ else if(score>=80) printf(\"the score:%d-----%c\\n\ else if(score>=70) printf(\"the score:%d-----%c\\n\ else if(score>=60) printf(\"the score:%d-----%c\\n\ else printf(\"the score:%d-----%c\\n\}

2)编写一个函数,求两个整数的平方和和平方差;主函数完成两个整数的输入,然后调用所编写的函数和输出所求的平方和和平方差;(9分) #include

float Func(float a,float b,float *c); main() { float a,b; float add2; float min2; printf(\"please enter the a b:\"); scanf(\"%f%f\ add2 = Func(a,b,&min2); printf(\"a2+b2:%f,a2-b2:%f\\n\}

float Func(float a,float b,float *c) { float add2; add2 = a*a+b*b; *c = a*a-b*b; return add2; }

3)编写一个函数,删除给定字符串中的指定的某一字符串,如在字符串“yamsderdsfam”中删除指定的字符串”am”后为”ysderdsf”;主函数完成字符串和需要删除的字符串的输入,调

用所编写的函数得到删除后的字符串。(9分) 注意:不得使用全局变量,注意程序结构; #include #include

char *del_str(char *s1,char *s2); void main() { char string[80]; char s2[80]; printf(\"input string:\"); gets(string); printf(\"input s2:\"); gets(s2); puts(del_str(string,s2)); }

char *del_str(char *s1,char *s2) { char *temp=s1; char *current=s1; while(*current!='\\0') { current=strstr(current,s2); strcpy(current,current+strlen(s2)); } return temp; }

4)一个班有N个学生,修5门功课,从键盘输入他们的姓名、学号、性别和成绩。 (1)按每个学生的平均成绩从低到高的顺序打印全班的成绩单; (2)求第三门课男生的平均分;

(3)找出平均成绩在50分以下或全部功课成绩在60分以下的女生。(8分)

要求:输入、输出、计算、排序和查找分别用函数实现,主函数只是调用这些函数。不得使用全局变量,注意程序结构;

因篇幅问题不能全部显示,请点此查看更多更全内容

Top