华中科技大学标准C语言程序设计及应用习题答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第二章
一.选择题
D 5. D
100011 001111
二.判断题
1.错
2.错
3.错
4.错
三.填空题
1. B 66
2. n1=%d\nn2=%d\n
3. 0
四.计算
1
(1)x|y = 0x002f
(2)x^y = 0x0026
(3)x&y = 0x0009;
(4)~x+~y = 0xffc6
(5)x<<=3 0x0068
(6)y>>=4 0x0002
2
(1)6
(2)50
(3)1
(4)–16
(5)1
(6)20
3
(1)0
(2)1
(3)1
(4)4
(5)8
(6)1
4
(1)12
(2)0
(3)1
(4)27
(5)1
(6)6
(7)24
(8)27
(9)–29
5
(1)0
(2)1
(3)1
(4)–3
(5)2
五.程序分析题
程序1
b=20 a=3600
程序2
第三章一.填空题
=6
3.(1) scanf("%c",&c);
(2) c-32 更好的答案:c-('a'-'A') main()
{
int a,b;
printf("please input a & b:"); scanf("%d%d",&a,&b);
printf("\nmax is %d\n",(a>b)a:b); }
int max(int x,int y);
main()
{
int a,b,c,max;
printf("please input a & b & c:"); scanf("%d%d%d",&a,&b,&c);
max=a>ba:b;
max=c>maxc:max;
printf("\nmax is %d\n",max);
}
main()
{
int i=0,sum=0;
while(i<=100)
{
sum+=i;
i++;
}
printf("1+2+3+......+100=%d\n",sum); }
main()
{
int i;
int a=10,b=-3;
int c;
printf("%6d%6d",a,b);
for(i=2;i<10;i++)
{
c=3*b+a;
printf("%6d",c);
a=b;
b=c;
}
printf("\n");
}
main()
{
int i;
while(1)
{
printf("please input a data(0:exit):");
scanf("%d",&i);
if(i==0)
break;
if(i%2==0)
printf("the data %d is a even number.\n",i); else
printf("the data %d is a odd number.\n",i); }
}
#include <>
main()
{
int i;
int a=8,b=1;
int sum=0;
for(i=0;i<10;i++)
{
b+=3;
sum += a;
a+=b;
printf("a%8d b:%8d\n",a,b); }
printf("The Sum Of Is:%d\n",sum); }
#include <>
main()
{
float x,y;
printf("please input x:");
scanf("%f",&x);
if(x<
y=x;
else if(x<10)
y=2*x-1;
else
y=3*x-11;
printf("y=%f\n",y);
}
#include <>
main()
{
long a,i,b,a1;
while(1)
{
printf("please input data(1-99999):"); scanf("%ld",&a);
printf("a:%ld\n",a);
if(a<=0||a>=100000)
break;
i=0;
a1=0;
while(a!=0)
{
b=a%10;
printf("%8d",b);
a/=10;
i++;
a1=a1*10+b;
}
printf("\n i:%ld a1:%ld\n",i,a1); }
}
#include <>
#include <>
#include <>
main()
{
int a,b,i,k=0;
randomize();
a=random(1001);/*create a random data(0-1000)*/ for(i=0;i<20;i++)
{
printf("please guess a number:");
scanf("%d",&b);
if(a>b)
{
k++;
printf("\n%d:Smaller,guess again!\n",k);
}
else if(a<b)
{
k++;
printf("\n%d:Bigger,guess again!\n",k);
}
else
{
printf("\nYou guess right,congraturation!") ; printf("\nYou have guessed %d times",k);
break;
}
}
if(k==20)
printf("\nsorry,you failed!");
}
#include <>
main()
{
int a,b,c;
int num;
for(a=0;a<10;a++)
{
for(b=0;b<10;b++)
{
for(c=1;c<10;c++)
{
num=a*100+b*10+c;
if((num%3==0)&&(a==5||b==5||c==5)) printf("%8d",num);
}
}
}
printf("\n\n\n");
}
#include <>
main()
{
int i;
int a,b;
printf("please input a,b:");
scanf("%d%d",&a,&b);
for(i=a<ba:b;i>0;i--)
{
if(a%i==0&&b%i==0)
{
printf("The max=%d",i); break;
}
}
for(i=a>ba:b;i>0;i++)
{
if(i%a==0&&i%b==0)
{
printf("\nThe min=%d",i); break;
}
}
#include <>
main()
{
int a,k=0;
printf("please input data:"); scanf("%d",&a);
while(a%2==0)
{
a=a/2;
k++;
}
printf("\nthe number of factor(2) is %d",k); }
main()
{
long i,t=1;
long sum=0;
for(i=1;i<=10;i++)
{
t*=i;
sum+=t;
printf("%ld!=%ld\n",i,t);
}
printf("sum:%ld\n",sum);
}
#include <>
void main()
{
int i,x=0;
for(i=9;i>=1;i--)
{
x=2*(x+1);
}
printf("The first day:%d",x); }
#include <>
#define PI
main()
{
float r,h;
float v;
printf("please input r,h:"); scanf("%f%f",&r,&h);
v=3*PI*r*r*h;
printf("V=%.2f",v);
}
#include<>
#include<>
main()
{
long int sn=0;
long int m=0,t=0;
int a,n,i;
printf("please input a n:");
scanf("%d%d",&a,&n);
for(i=0;i<n;i++)
{
m=m*10+a;
sn+=m;
}
printf("a+aa+aaa+...+aa...a=%ld\n\n",sn); }
#include <>
main()
{
int k;
printf("please input k:");
do
{
scanf("%d",&k);
if(k>=0&&k<=6)
break;
}while(1);
switch(k)
{
case 0:printf("Sunday.\n");break;
case 1:printf("Monday.\n");break;
case 2:printf("Tuesday.\n");break; case 3:printf("Wednesday.\n");break; case 4:printf("Thursday.\n");break; case 5:printf("Friday.\n");break;
case 6:printf("Saturday.\n");break; }
}
#include <>
main()
{
int i;
double x,a,b=1,sum=1;
printf("please input x:");
scanf("%lf",&x);
a=x;
for(i=1;a/b>=1e-6;i++) {
sum=sum+a/b;
a=a*x;
b=(i+1)*b;
}
printf("\nsum=%lf",sum); }
#include <>
#include <>
main()
{
float a,x1,x2;
printf("input a:");
scanf("%f",&a);
x1=;
while(1)
{
x2=2*(x1+a/x1);
if(fabs(x2-x1)<1e-5)
break;
x1=x2;
}
printf("sqrt(a)=%f",x2);
}
第四章写出下列程序输出结果
1 a=1no 1 a=0no 1 a=1no 1 a=0
2.
a=0 b=0 c=0
a=1 b=0 c=0
a=2 b=0 c=0
3.
main:x=5,y=1,n=1
func:x=6,y=21,n=11
main:x=5,y=1,n=11
func:x=8,y=31,n=21
第五章6.(1)j+=2
(2)a[i]<a[j]
7.(1)s[i++]!='\0'
(2)s[i-1]
(同题4)
[i]>='0'&&s[i]<='9'
12. (1)'\0' (2)str1[i]-str2[i]
#include<>
int fun();
int fun()
{
int a[3][3],sum;
int i,j;
sum=0;/*error*/
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);/*error*/ }
for(i=0;i<3;i++)
sum=sum+a[i][i];
printf("sum=%d\n",sum);
}
void main()
{
fun();
}
#include <>
void main( void )
{
float Num[10];
float Input,GetNum[11];
int i,j;
float a=6;
for(i=0; i<10; i++)
{
Num[i]=a;
a=a+;
}
Loop1: printf("Please input a Number(0-80) Input=");
scanf("%f",&Input);
if((Input<0)||(Input>70)==1) goto Loop1;
for(i=0; i<10; i++ )
{
if(Input<Num[i])
goto Loop2;
}
Loop2: for(j=0; j<i; j++ ) GetNum[j]=Num[j]; GetNum[j]=Input;
for(j=i; j<=10; j++,i++) GetNum[j+1]=Num[i];
for(j=0; j<11; j++ )
printf("% ",GetNum[j]);
}
#include ""
#include ""
main()
{
int a1,a2,a3,a4,a5,a6,a7,a8,a9;
int a[3];
int i;
for(;;)
{
for (i=0;i<3;i++)
{
a[i]=rand()%3;
}
while((a[0]!=a[1]) && (a[0]!=a[2]) && (a[1]!=a[2]))/*get three different numbers 0,1,2*/
{
a1=a[0]+1;/*divide 1~9 into three groups,a1~a3,a4~a6,a7~a9*/
a2=a[1]+1;
a3=a[2]+1;
a4=a1+3;
a5=a2+3;
a6=a3+3;
a7=a1+6;
a8=a2+6;
a9=a3+6;
/* make sure that each line and each row is made up with three members in different group.*/
/* such as: a9 a1 a5
a2 a6 a7
a4 a8 a3 */
if(((a1+a5+a9) == (a2+a6+a7)) && ((a1+a5+a9) == (a3+a4+a8))
&& ((a1+a6+a8) == (a5+a7+a3)) && ((a1+a5+a9) == (a2+a4+a9)))
{
printf("%d %d %d\n%d %d %d\n%d %d %d\n",a9,a1,a5,a2,a6,a7,a4 ,a8,a3);
return;
}
}
}
}
#include <>
void main(void)
{
char input1[100],input2[100],input3[100];
int i,Eng=0,eng=0,num=0,blank=0,other=0;
printf("Input 3 rows of character,each row don't exceed 80 characters:\n");
gets(input1);
printf("The second row:\n");
gets(input2);
printf("The third row:\n");
gets(input3);
/*test the first row*/
for(i=0; i<100; i++)
{
if(input1[i]=='\0')
goto Loop1;
else if(('A'<=input1[i])&&(input1[i]<='Z')==1)
Eng++;
else if(('a'<=input1[i])&&(input1[i]<='z')==1)
eng++;
else if(('0'<=input1[i])&&(input1[i]<='9')==1) num++;
else if(input1[i]==32)
blank++;
else other++;
}
/*test the second row*/
Loop1: for(i=0; i<100; i++)
{
if(input2[i]=='\0')
goto Loop2;
else if(('A'<=input2[i])&&(input2[i]<='Z')==1) Eng++;
else if(('a'<=input2[i])&&(input2[i]<='z')==1) eng++;
else if(('0'<=input2[i])&&(input2[i]<='9')==1) num++;
else if(input2[i]==32)
blank++;
else other++;
}
/*test the third row*/
Loop2: for(i=0; i<100; i++)
{
if(input3[i]=='\0')
goto Loop3;
else if(('A'<=input3[i])&&(input3[i]<='Z')==1)
Eng++;
else if(('a'<=input3[i])&&(input3[i]<='z')==1)
eng++;
else if(('0'<=input3[i])&&(input3[i]<='9')==1) num++;
else if(input3[i]==32)
blank++;
else other++;
}
Loop3: printf("Upper english character:%d\nLower english character:%d\nNumber:%d\nBlank:%d\nOther
characters:%d\n",Eng,eng,num,blank,other);
}
#include <>
void main(void)
{
char str1[80],str2[40];
int i,j,k;
/*Input two string*/
printf("Please input the first string\n str1=");
gets(str1);
printf("Please input the second string\n str2=");
gets(str2);
/*Get the end of str1*/
for(i=0; i<80; i++)
{
if(str1[i]=='\0')
break;
}
/*Copy str2 to str1*/
for(j=i,k=0; str2[k]!='\0';k++,j++)
str1[j]=str2[k];
str1[i+k]='\0';
puts(str1);
}
#include <>
struct student
{char name[20];
int score;
}stu[5],stu1;
void main(void)
{ int i,j;
printf("Input student's score and name(5),seperate using the character of ',' :\n");
for(i=0; i<5; i++)
scanf("%d,%s",&stu[i].score,&stu[i].name);
/*sorting*/
for(i=0; i<5; i++)
for(j=0; j<4-i; j++)
if(stu[j].score>stu[j+1].score)
{stu1=stu[j];stu[j]=stu[j+1];stu[j+1]=stu1;} printf("After sotred,score and name:\n");
printf("%d,%s\n",stu[i].score,stu[i].name); }
#include<>
main()
{ int a[3][3],i,j,m,n,o,p;
printf("Please input a 3*3 shuzu:\n");
for (i=0;i<=2;i++)
for (j=0;j<=2;j++)
scanf("%d",&a[i][j]);
for (i=0;i<=2;i++)
{ m=(a[i][0]>a[i][1]) a[i][0]:a[i][1];
n=(m>a[i][2]) m: a[i][2];
}
{ o=(a[0][j]<a[1][j]) a[0][j]:a[1][j];
p=(o<a[2][j]) o: a[2][j];
}
if (n==p)
printf("Andian is %d\\n",p);
else
printf("There is no andian.\\n");
}
#include <>
void main(void)
{ int a[4][3],b[3][4],i,j;
for(i=0; i<4; i++)
{ for(j=0; j<3; j++)
scanf("%d",&a[i][j]);
}
for(i=0; i<4; i++)
{
printf("\n");
for(j=0; j<3; j++)
printf("%5d ",a[i][j]); }
for(i=0; i<4; i++)
{
for(j=0; j<3; j++) b[j][i]=a[i][j];
}
printf("\n");
for(i=0; i<3; i++)
{
printf("\n");
for(j=0; j<4; j++)
printf("%5d ",b[i][j]); }
}
#include <>
#include<>
void main(void)
{ char str[6];
int i,j=0,m=0,n=0;
gets(str);
while(str[j]!='\0') j++;
for(i=j-1;i>=0;i--)
{
m=(str[i]-'0')*(pow(8,j-i-1)); n+=m;
}
printf("shijinzhi %d",n);
}
#include <>
void main(void)
{
char input1[100];
int i,num=0,blank=0;
printf("Please input a row character:\n");
gets(input1);
/*test the first row*/
for(i=0; i<100; i++)
{
if(input1[i]=='\0')
goto Loop;
else if(('A'<=input1[i])&&(input1[i]<='Z')==1) num++;
else if(('a'<=input1[i])&&(input1[i]<='z')==1) num++;
else if (input1[i]==32)
blank++;
}
Loop: printf("Character:%d\nBlank:%d\n",num,blank);
}
第六章
一、选择题
:p中记录的是a的地址,*p访问地址a的值
:指针数组
:指针可以进行是否相等判断,空指针可以进行指针变量初始化,指针可以偏移;
:
、D:a[5]不存在,若*&a[4]则正确;C,D正确;
:
:
:
二、填空题
1.*(p+3) 2 22
2.cdefg
bcdefg
abcdefg
7
3.6385
三、程序分析题
1.p = s[0] ;错误,应改为:p = s;或 p = &s[0];
2.int x , *p;错误,应改为:double x, *p;
3.*p = x ;错误,应改为:p = &x ;
4.声明顺序错误;应该改为:
void main(void)
{
int a;
int *p = &a;
a = 10;
printf(“%d\n”,*p);
}
#include <>
#include <>
main()
{
char ch[2][5] = {"6937","8254"},*p[2];
int i,j,s=0;
for(i=0;i<2;i++)
p[i] = ch[i];
for(i=0;i<2;i++)
for(j=0;p[i][j]>'\0';j+=2)
s = 10*s + p[i][j] - '0';
printf("%d\n",s);
}
#include <>
#include <>
main()
{
int count = 0;
char str1[100],str2[100];
char *p1,*p2;
printf("please enter string 1:"); gets(str1);
printf("please enter string 2:"); gets(str2);
printf("str1:%s\n",str1);
printf("str2:%s\n",str2);
p1 = str1;
p2 = str2;
while(*p1!='\0'&&*p2!='\0')
{
if(*p1++==*p2++)
count++;
}
printf("count:%d\n",count);
}
#include <>
#include <>
main()
{
int i,j,k;
int a[3];
int temp;
printf("please enter array a[3]:"); scanf("%d%d%d",&a[0],&a[1],&a[2]); for(i=0;i<2;i++)
{
k = i;
for(j= i+1;j<3;j++)
{
if(a[j]<a[i])
k = j;
}
if(k!=i)
{
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
for(i=0;i<3;i++)
printf("%d\t",a[i]);
printf("\n");
}
#include <>
#include <>
#define N 100
main()
{
char s1[N], s2[N],*p;
int m;
printf("please enter string1:"); gets(s1);
p = s1;
printf("enter m:");
scanf("%d",&m);
strcpy(s2,p+m);
printf("string2:%s\n",s2);
}
#include <>
#include <>
main()
{
char s[100]="iuiui012asdd90k890y098kkkk1234",*p; char data[100][100]={{0}};
int count=0;
int i;
p = s;
/*
printf("please enter a string:");
gets(p);
*/
printf("sssss:%s\n",s);
while(*p!='\0')
{
i=0;
while(*p>='0'&&*p<='9') {
data[count][i] = *p; i++;
p++;
}
if(i!=0)
count++;
p++;
}
printf("count:%d\n",count); for(i=0;i<count;i++)
printf("%s\n",data[i]); }
#include <>
#define SIZE 4
main()
int data[SIZE][SIZE],i,j,d; int max,m=0,n=0;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
scanf("%d",&data[i][j]); }
}
for(i=0;i<SIZE;i++)
{
for(j=i+1;j<SIZE;j++)
{
d = data[i][j];
data[i][j] = data[j][i]; data[j][i] = d;
}
}
max = data[0][0];
for(i=0;i<SIZE;i++)
printf("\n");
for(j=0;j<SIZE;j++)
{
printf("%4d",data[i][j]);
if(data[i][j]>=max)
{
max = data[i][j];
m = i;
n = j;
}
}
}
printf("\nmax:%4d m:%d n:%d\n",max,m,n);
}
#include <>
#include <>
main()
{
char *p[] = {"test","capital","index","large","small"};
char **pstr = p;
int a,b,n = 5;
char *temp;
for(a=0;a<n-1;a++)
{
for(b=a+1;b<n;b++)
{
if(strcmp(pstr[a],pstr[b])>0) {
temp = pstr[a];
pstr[a] = pstr[b];
pstr[b] = temp;
}
}
}
for(a=0;a<n;a++)
printf("%s\n",pstr[a]);
}
#include <>
#include <>
main()
{
char s[100],temp;
char *p = s;
int i,length;
printf("please enter a string: "); gets(s);
while(*p++!='\0');
length = p - s -1;
printf("string length:%d\n",length); for(i=0;i<length/2;i++)
{
temp = s[i];
s[i] = s[length-i-1];
s[length-i-1] = temp;
}
printf("%s\n",s);
}
第七章一,选择题
二.程序填空题
1.
(1) a[k]
(2) a[k]
(3) a[k]
2.
(1) a[i]
(2) j
(3) i+1或6
三.改错题
1.
#include<>
#include<>
void swap(char *,char *); /*此处有错误,函数声明时要加分号*/ main()
{
char a[80],b[80],c[80];
scanf("%s%s%s",a,b,c); /*此处有错误,a,b,c分别为数组a[80],b[8],c[80]的首地址*/
if(strcmp(a,b)>0) swap(a,b); /*此处有错误,字符串比较应该使用strcmp函数*/
if(strcmp(b,c)>0) swap(b,c); /*此处有错误,原因同上*/
if(strcmp(a,b)>0) swap(a,b); /*原算法不能实现排序,需要添加此语句*/
printf("%s\n%s\n%s\n",a,b,c);
}
<>
#include <>
void swap(char *pstr1,char *pstr2)
{
char p[80];
strcpy(p,pstr1);
strcpy(pstr1,pstr2);
strcpy(pstr2,p);
}
main()
{
char a[80]="ccc",b[80]="bbb",c[80]="aaa";
/* scanf("%s%s%s",a,b,c); */
if(strcmp(a,b)>0)
swap(a,b);
if(strcmp(b,c)>0)
swap(b,c);
if(strcmp(a,b)>0)
swap(a,b);
printf("%s\n%s\n%s\n\n\n",a,b,c);
}
<>
int SquSum(int,int);
main()
{
int a,b;
int c;
printf("enter 2 integer:");
scanf("%d%d",&a,&b);
c = SquSum(a,b);
printf("a=%d b:%d a2+b2=%d\n",a,b,c); }
int SquSum(int x,int y)
{
int z;
z = x*x+y*y;
return z;
}
<>
int GongYueShu(int,int);
int GongBeiShu(int,int);
main()
{
int a,b;
int gbs,gys;
printf("enter 2 integer:");
scanf("%d%d",&a,&b);
gys = GongYueShu(a,b);
gbs = GongBeiShu(a,b);
if(gys<0)
printf("There has not gong yue shu!!!!!!\n"); else
printf("max gong yue shu:%d\n",gys);
printf("min gong bei shu:%d\n",gbs);
}
int GongYueShu(int x,int y) {
int i,gys=-1,min;
min = x;
if(x>y)
min = y;
for(i=2;i<=min;i++)
{
if((x%i==0)&&(y%i==0)) gys = i;
}
return gys;
}
int GongBeiShu(int x,int y) {
int i,gbs,max;
max = x;
if(y>x)
max = y;
for(i=max;i<=x*y;i++)
{
if((i%x==0)&&(i%y==0))
{
gbs = i;
break;
}
}
return gbs;
}
<>
#include <>
void Delete_Ch(char *,char); main()
{
char str[80],ch;
printf("please a string:"); gets(str);
printf("please a char:"); scanf("%c",&ch);
Delete_Ch(str,ch);
printf("%s\n\n",str);
}。