C语言程序设计教程课后习题答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第1章
1-3 CAB
4 .c .obj .exe
5 /* */
6 ;
7 算法
8 ①中级语言:C语言具有高级语言的先进思想又能直接对存储器进行操作,能进行位运算,能实现汇编语言的大部分功能,生成目标代码质量高,程序执行效率高。
②结构化语言:C语言用函数作为程序模块,以实现程序的模块化,语言简洁、紧凑,具有结构化的特点。
③可移植性好:C语言不包含依赖硬件的输入输出机制,使C语言本身不依赖于硬件系统,可移植性好。
9 #include<>
main( ) { ; }
10 #include “”
main()
{
printf(“This is my first C Program!”);
}
第2章
1.yes
2.-3
3.2,1
4.1)a!=b||a<=c 2)x>=4||x<=-4
5.x>20&&x<30||x<-100
6.#include <>
main()
{
int x;
printf(“please input an integar:”);
scanf("%d",&x);
if(x%5==0&&x%7==0) printf("yes\n");
else printf("no\n");
}
7.
#include <>
main()
{
int year,month;
printf("please input the year and month:");
scanf("%d%d",&year,&month);
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:printf("this month have 31 days.");
break;
case 4:
case 6:
case 9:
case 11:printf("this month have 30 days.");
break;
case 2:if(year%4==0&&year%100!=0||year%400==0)
{
printf("this month have 29 days.");
break;
}
else
{
printf("this month have 28 days.");
break;
}
}
}
8.
#include <>
main()
{
float money;
int year;
printf("\nplease input the money and the year:");
scanf("%f%d",&money,&year);
if(year==1) money+=money**12*year;
if(year==2) money+=money**12*year;
if(year==3||year==4) money+=money**12*year;
if(year>=5&&year<=7) money+=money**12*year;
if(year>=8) money+=money**12*year;
printf("the money is:%f",money);
}
第3章
1
#include ""
main()
{ float x[10],sum=0;
int i=0;
printf("please input 10 numbers(-10e6<x<10e6)\n");
while(i<10)
{ scanf("%f",&x[i]);
sum=sum+1/x[i];
i=i+1;
}
printf("the sum of 10 numbers is %.2f\n",sum);
}
2
#include ""
main()
{ int x[100],sum=0,aver,i=0;
printf("please input numbers until 0\n");
scanf("%d",&x[i]);
while(x[i]!=0)
{ sum=sum+x[i];
i=i+1;
scanf("%d",&x[i]);
}
printf("the sum is %d\n",sum);
printf("the average is %.2f\n",float(sum)/i);
}
3
#include ""
#include ""
main()
{ int i=1,j,f;
long int s=0;
while(i<=10)
{ for(f=1,j=1;j<=i;j++)
f=f*j;
s=s+pow(-1,i-1)*f;
i=i+1;
}
printf("s= %ld\n",s);
}
4
#include ""
#include ""
main()
{ int i=0;
float s=;
while(2*i+1<=101)
{ s=s+pow(-1,i)/float(2*i+1);
i=i+1;
}
printf("s= %f\n",s);
}
5
#include ""
#include""
main()
{ int i,j,k,n=0;
for(i=1;i<=9;i++)
for(j=0;j<=9;j++)
for(k=0;k<=9;k++)
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
printf("the %d:%d\n",++n,i*100+j*10+k);
printf("the all is %d.\n",n--);
}
6
#include ""
main()
{ int n,i,j;
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
printf("*");
printf("\n");
}
}
7
#include ""
main()
{ char a[81];
int i,j=0,k=0,m=0;
printf("please input a $(length<=80)\n");
for(i=0;(a[i]=getchar())!='\n';i++)
{ if(a[i]>=65&&a[i]<=90)
j++;
else if(a[i]==32)
k++;
else m++;
}
printf("the upper letter is %d\n",j);
printf("the blank is %d\n",k);
printf("the other is %d\n",m);
}
8
#include ""
main()
{ int i,j,k,m=0;
for(i=0;i<=20;i++)
{ for(j=0;j<=50;j++)
for(k=0;k<=100;k++)
if(i*5+j*2+k==100)
printf("the %d wag is 1:%d,2:%d,5:%d\n",++m,k,j,i);
}
printf("the wags is %d\n",m--);
}
9
#include ""
main()
{ int i,j,k,m=0;
for(i=0;i<=3;i++)
{ for(j=0;j<=3;j++)
for(k=0;k<=6;k++)
if(i+j+k==8)
printf("the %d wag is white:%d,red:%d,black:%d\n",++m,i,j,k);
}
printf("the wags is %d\n",m--);
}
10
#include ""
main()
{ long int n;
int i,j,k;
printf("please inpur n:");
scanf("%ld",&n);
if(n%2==1||n<=1)
{ printf("the input is error.\n");
return(1);
}
for(i=2;i<=n/2;i++)
{ for(j=2;j<i;j++)
if(i/j*j==i)
break;
if(j==i)
{for(k=2;k<n-i;k++)
if((n-i)/k*k==(n-i))
break;
if(k==n-i)
printf("%ld=%d+%d\n",n,i,n-i);
}
}
}
11
#include ""
main()
{int i,j,k,m;
for(i=1;i<=9;i++)
for(j=0;j<=9;j++)
for(k=0;k<=9;k++)
for(m=0;m<=9;m++)
if((i*1000+j*100+k*10+m)*9==(i+j*10+k*100+m*1000))
printf("the number is:%d\n",i*1000+j*100+k*10+m);
}
第4章
1-5 ABDCB 6-10 BCBCA 11-13 ABA
14
#include ""
main()
{
float a1,b1,a2,b2,re1,im1,re2,im2,re3,im3;
printf("Please input the first number:");
scanf("%f %f",&a1,&b1);
printf("Please input the second number:");
scanf("%f %f",&a2,&b2);
re1=a1+a2;
im1=b1+b2;
printf("(%f+j%f)+(%f+j%f)=%f+j%f\n",a1,b1,a2,b2,re1,im1);
re2=a1-a2;
im2=b1-b2;
printf("(%f+j%f)-(%f+j%f)=%f+j%f\n",a1,b1,a2,b2,re2,im2);
re3=a1*a2-b1*b2;
im3=a1*b2+a2*b1;
printf("(%f+j%f)*(%f+j%f)=%f+j%f\n",a1,b1,a2,b2,re3,im3);
}
第5章
1-5 CACCD 6-7 DA
8
#include ""
main()
{
char str[100];
int num1=0,num2=0,i=0;
scanf("%s",str);
while(str[i]!='\0')
{
if(str[i]>='A'&&str[i]<='Z'||str[i]>='a'&&str[i]<='z')
num1++;
else if(str[i]>='0'&&str[i]<='9')
num2++;
i++;
}
printf("num1=%d,num2=%d",num1,num2);
}
9笔者在程序中直接给数组元素赋了值,读者可以修改为用scanf函数读入数值;另外,还可以用第7章宏定义的方法设定数组的长度,从而实现对元素个数不同的数组进行排序。
#include ""
main()
{
int a[5]={2,4,1,3,0},i,j,temp;
for(i=0;i<=4;i++)
printf("%d,",a[i]);
printf("\n");
for(j=1;j<=4;j++)
for(i=j;i<=4;i++)
if(a[i]<a[j-1])
{
temp=a[j-1];
a[j-1]=a[i];
a[i]=temp;
}
for(i=0;i<=4;i++)
printf("%d,",a[i]);
}
第6章
1-5 AADBA 6 C
7 0 2 4 6 8 10 12 14 16 18
0 2 4 6 8 10 12 14 16 18
8 a+b=9
9 5+4+1=10
10+4+2=16
20+4+3=27
10
加密:
#include ""
main()
{
char string[100],i=0,number=0;
scanf("%s",string);
while(string[i++]!='\0')
number++;
fun1(string,number);
printf("%s",string);
}
fun1(char str[],int num)
{
int i;
for(i=0;i<=num;i++)
if(str[i]>='A'&&str[i]<='V'||str[i]>='a'&&str[i]<='v'||str[i]>='0'&&str[i]<='5') str[i]+=4;
else if(str[i]>'V'&&str[i]<='Z')
str[i]-=22;
else if(str[i]>'v'&&str[i]<='z')
str[i]-=22;
else if(str[i]>'5'&&str[i]<='9')
str[i]-=6;
}
解密
#include ""
main()
{
char string[100],i=0,number=0;
scanf("%s",string);
while(string[i++]!='\0')
number++;
fun2(string,number);
printf("%s",string);
}
fun2(char str[],int num)
{
int i;
for(i=0;i<=num;i++)
if(str[i]>='E'&&str[i]<='Z'||str[i]>='e'&&str[i]<='z'||str[i]>='4'&&str[i]<='9')
str[i]-=4;
else if(str[i]>='A'&&str[i]<'E')
str[i]+=22;
else if(str[i]>='a'&&str[i]<'e')
str[i]+=22;
else if(str[i]>='0'&&str[i]<'4')
str[i]+=6;
}
第8章
一、下列语句是否合法
语句:int *p = &x, x = 13;
点评:不合法,因为x变量没有定义。
在执行int *p = &x 时,x变量的定义在后面。
参考:int x = 13, *p = &x;
语句:int *p = &x = 13;
点评:不合法,因为x变量没有定义。
在执行int *p = &x = 13 时,x变量没有定义。
参考:int x = 13, *p; p = &x;
语句:int x = 13,*p = x;
点评:不合法,不能把x变量的值赋给指针p。
应该把变量x的地址值赋给指针p。
参考:int x = 13, *p; p = &x;
语句:int y[5] = {1,2,3,4,5},*p = y[3];
点评:不合法,不能把数组元素y[3]的值赋给指针p,应该把数组元素y[3]的地址值赋给指针p。
参考:int y[5] = {1,2,3,4,5},*p ; p = &y[3];
语句:int *p = y,y[5] = {1,2,3,4,5};
点评:不合法,因为数组y没有定义。
参考:int y[5] = {1,2,3,4,5},*p; p = y;
语句:int y[5] = {1,2,3,4,5},*p = &y;
点评:不合法,不能把数组y的地址值赋给指针p。
数组名y本身是数组y的地址常量值。
参考:int y[5] = {1,2,3,4,5},*p; p = y;或int y[5] = {1,2,3,4,5},*p; p = &y[0];
语句:int *c = &ch,ch = "Hello World";
点评:不合法,ch变量没有定义,字符串"Hello World"不能赋给变量ch。
参考:char *ch = "Hello World!";或char *ch;ch = "Hello World";
语句:char ch[12] = "Hello World",*c = ch;
点评:合法。
思考:char ch[12] = "Hello World!!!",*c = ch;该语句错误,字符个数超出数组ch定义的宽度。
语句:char *c = ch[12] = "Hello World"
点评:不合法,数组ch没有定义。
数组ch没有第12个元素。
参考:char *c,ch[12] = "Hello World";c = ch;
语句:char *c,c = 'X',c = &c;
点评:不合法,不能把常量'X'的值赋给指针,不能把指针c的地址值赋给指针c自己。
参考:char *c,ch; ch = 'X',c = &ch;
二、综合练习题参考答案
1.将一字符串复制到另一字符串中。
#include<>
void main()
{ char str1[128],str2[128] = {"Welcome to C Language"};
char *cp1,*cp2;
cp1 = str1;
cp2 = str2;
while(*cp1++ = *cp2++);
puts(str1);
}
2.将一字符串与另一字符串相连接。
#include<>
void main()
{ char str1[128] = {"Welcome "},str2[128] = {"to C Language"};
char *cp1,*cp2;
int i = 0,n = 0;
while(n++,str1[++i] != 0);
cp1 = str1+n;
cp2 = str2;
while(*cp1++ = *cp2++);
puts(str1);
}
3.将字符数组中大写字符改为小写字符,将小写字符改为大写字符。
#include<>
void main()
{ char str1[128] = {"Welcome to C Language"};
char *cp1 = str1;
while(*cp1)
{ if(*cp1 >= 'A' && *cp1 <= 'Z')
*cp1 = *cp1+32;
else if(*cp1 >= 'a' && *cp1 <= 'z')
*cp1 = *cp1-32;
else
*cp1 = *cp1;
cp1++;
}
puts(str1);
}
4.从一字符串中取出部分字符串复制到另一字符串中。
#include<>
void main()
{ char ch[128],*cp,str[128],*sp;
int b,n,l=0,i;
cp=ch;
sp=str;
gets(cp);
scanf("%d%d",&b,&n);
while(l++,*(ch+l)!=0);
if(n<l)
{ for(i=0;i<n;i++)
*(sp+i)=*(cp+b+i);
*(sp+i)='\0';
puts(str);
}
else
printf("无数据可以复制");
}
5.完成一整型数组的排序。
#include<>
#include<>
void main()
{ int x[10],i,j,*p,t;
p = x;
for(i = 0;i<10;i++)
x[i] = rand();
for(i = 0;i<9;i++)
for(j = 0;j<9;j++)
if(*(p+j)>*(p+j+1))
{ t = *(p+j);
*(p+j) = *(p+j+1);
*(p+j+1) = t;
}
for(i = 0;i<10;i++)
printf("%d\t",*p++);
}
6.将一个字符串中的字符反序排列。
#include<>
void main()
{ char ch[128],c,*cp1,*cp2;
int i,n = 0;
gets(ch);
cp1 = ch;
cp2 = ch;
while(*cp2!= 0)
{ n++;
cp2++;
}
cp2--;
n = n/2;
for(i = 0;i<n;i++)
{ c = *cp1;
*cp1 = *cp2;
*cp2 = c;
cp1++;
cp2--;
}
puts(ch);
}
7.编函数完成将一字符串中的字符全部改为大写。
#include<>
void cove(char *cp)
{ while(*cp!= 0)
{ if(*cp>= 'a' && *cp<= 'z')
*cp = *cp-32;
cp++;
}
}
void main()
{ char ch[128],*cp;
cp = ch;
gets(cp);
cove(cp);
puts(ch);
8.编函数完成从一字符串左边取若干字符复制到另一字符串中。
#include<>
void lm(char *lcp,char *bcp,int n)
{ int i;
for(i = 0;i<n;i++)
*(bcp+i) = *(lcp+i);
*(bcp+i) = '\0';
}
void main()
{ char ch[128],*cp,str[128];
int n,l =0;
gets(ch);
scanf("%d",&n);
while(l++,*(ch+l)!= 0);
if(n<l)
{ cp = ch;
lm(cp,str,n);
puts(str);
}
else
printf("无数据可以复制");
}
第9章
1
#include ""
struct date
{ int year;
int month;
int day;
};
main()
{ struct date re;
int
day[2][12]={{31,29,31,30,31,30,31,31,30,31,30,31},{31,28,31,30,31,30,31,31,30,31,30,31}};
int i,days=0;
printf("input the day(year:month:day):\n");
scanf("%d:%d:%d",&,&,&;
if(%4==0&&%100!=0)||%400==0)
for(i=1;i<;i++)
days+=day[1][i-1];
else
for(i=1;i<;i++)
days+=day[0][i-1];
days+=;
printf("the days is %d\n",days);
}
2
#include ""
main()
{enum weekday{Sun,Mon,Tue,Wed,Thu,Fri,Sat} day; int data;
printf("input the data:");
scanf("%d",&data);
data+=2;
day=(enum weekday)(data%7);
switch(day)
{ case Mon:printf("It is Monday.\n");break;
case Tue:printf("It is Tuesday.\n");break;
case Wed:printf("It is Wednesday.\n");break;
case Thu:printf("It is Thursday.\n");break;
case Fri:printf("It is Friday.\n");break;
case Sat:printf("It is Saturday.\n");break;
case Sun:printf("It is Sunday.\n");break;
default:printf("error\n");
}
}
3
#include ""
struct result
{ int notpass;
int pass;
int good;
};
main()
{ struct result re, *p=&re;
int i,temp;
void output(struct result *);
=0;
=0;
=0;
printf("input 12 integers;\n");
for(i=0;i<12;i++)
{ scanf("%d",&temp);
if(temp<0||temp>100)
{ printf("error!\n");
return(0);
}
else if(temp<60)
++;
else if(temp>=80)
++;
else ++;
}
output(p);
return(1);
}
void output(struct result *p)
{ printf("the result:\n");
printf(" mark: 0-59 60-79 80-100\n");
printf("number: %d %d %d\n",p->notpass,p->pass,p->good); }
4
#include ""
struct stat
{ int length;
int blank;
int letter;
int number;
};
main()
{ struct stat re[2];
char temp;
int k;
void output(struct stat *);
for(k=0;k<2;k++)
{ re[k].length=0;
re[k].blank=0;
re[k].letter=0;
re[k].number=0;
}
for(k=0;k<2;k++)
{ printf("input the %dst $;\n",k+1);
while((temp=getchar())!='\n')
{ re[k].length++;
if(temp==' ')
re[k].blank++;
else if((temp>=65&&temp<=90)||(temp>=97&&temp<=122))
re[k].letter++;
else if(temp>=48&&temp<=57)
re[k].number++;
}
}
output(re);
return(1);
}
void output(struct stat *p)
{ if(p->length==(p+1)->length)
printf("the length of 2 strings is equal.\n");
else
printf("the length of 2 strings is different.\n");
if(p->blank==(p+1)->blank)
printf("the amounts of blanks in 2 strings is equal.\n");
else
printf("the amounts of blanks in 2 strings is different.\n");
if(p->letter==(p+1)->letter)
printf("the amounts of letters in 2 strings is equal.\n");
else
printf("the amounts of letters in 2 strings is different.\n");
if((*p).number==(p+1)->number)
printf("the amounts of numbers in 2 strings is equal.\n");
else
printf("the amounts of numbers in 2 strings is different.\n");
}
第10章
1、C文件的特点是:C系统在处理文件时,并不区分类型,都看成是字符流,按字节进行处理。
输入输出字符流的开始和结束只由程序控制而不受物理符号(如回车符)的控制。
因此C语言文件也叫流式文件。
流式文件准许对一个字符进行存取, 这增加了数据处理的灵活性。
缓冲文件系统:缓冲文件系统自动在内存区开辟一个确定大小的缓冲区,使数据输入输出都先通过缓冲区。
非缓冲文件系统:非缓冲文件系统不自动开辟确定大小的缓冲区,而是由程序员为每个文件设定一个缓冲区。
2、文件类型指针是缓冲文件系统的一个重要概念。
文件类型指针是指向描述文件信息结构体的结构体变量,用于文件操作。
在C语言中,对普通数据文件的所有操作都必须依靠文件类型指针来完成。
要想对文件进行操作,首先必须将想要操作的数据文件与文件指针建立联系,然后通过这些文件指针来操作相应的文件。
3、打开文件,是建立文件的各种有关信息,并使文件指针指向该文件,以便进行其它操作。
关闭文件则断开指针与文件之间的联系,也就禁止再对该文件进行操作。
4、C语言程序中,文件操作顺序为:打开-读/写-关闭。
5、
#include <>
#define NULL 0
main()
{
FILE *fp;
char ch;
int linenum,chnum,i=0,l=0;
fp=fopen("","r");
if(fp==NULL)
{
printf("File can't be opened.\n");
exit(1);
}
ch=fgetc(fp);
linenum=0;
chnum=0;
while(ch!=EOF)
{
while(ch!='\n')
{
chnum++;
ch=fgetc(fp);
}
linenum++;
if (i<chnum)
{
i=chnum;
l=linenum;
chnum=0;
}
ch=fgetc(fp);
}
printf("\nthe longest line is the %d line,and there are %d characters.",l,i);
fclose(fp);
}
第11章
1、
#include <>
#include <>
#define NULL 0
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LNode* create_LinkList(int n)/*创建链表*/ {
LNode *s,*head,*L;
ElemType x;
int k=0;
L=(LNode *)malloc(sizeof(LNode));
if(L==0)
{
printf("error!");
exit(1);
}
else
{
L->next=NULL;
}
head=L;
while(k<n)
{
scanf("%d",&x);
s=(LNode *)malloc(sizeof(LNode));
if(s==0)
{
printf("error!");
exit(1);
}
s->data=x;
s->next=NULL;
L->next=s;
L=s;
k++;
}
return (head);
}
void print_LinkList(LinkList L)/*输出链表*/ {
LNode *h;
h=L;
while(h->next!=NULL)
{
printf(" %d ",h->next->data);
h=h->next;
}
}
int num_LinkList(LinkList L)
{
int i=0;
while(L->next!=0)
{
i++;
L=L->next;
}
return i;
}
LNode* Insert(LinkList L,ElemType x)
{
int k,i;
LNode *s,*z,*l;
s=L->next;
k=num_LinkList(L);
for(i=0;i<k;i++)
if(s->data<x)
{l=s;
s=s->next;
}
else break;
z=(LNode *)malloc(sizeof(LNode));
if(z==0)
{
printf("error!");
exit(1);
}
z->data=x;
if(s==NULL) {l->next=z;z->next=NULL;}
else
if(i==0)
{L->next=z;z->next=s;}
else
{l->next=z;z->next=s;}
return L;
}
main()
{
LNode *la;
ElemType x;
int num;
clrscr();
printf("please input the elements of la,small to big:\n");
la=create_LinkList(10);
printf("\nthe elements of the la are:");
print_LinkList(la);
printf("\nplease input the inserted element x:");
scanf("%d",&x);
la=Insert(la,x);
print_LinkList(la);
}
2、
#include <>
#include <>
#define NULL 0
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LNode* create_LinkList()/*创建链表*/
{
LNode *s,*head,*L;
ElemType x;
L=(LNode *)malloc(sizeof(LNode));
if(L==0)
{
printf("error!");
exit(1);
}
else
{
L->next=NULL;
}
head=L;
scanf("%d",&x);
while(x!=0)
{
s=(LNode *)malloc(sizeof(LNode));
if(s==0)
{
printf("error!");
exit(1);
}
s->data=x;
s->next=NULL;
L->next=s;
L=s;
scanf("%d",&x);
}
return (head);
}
void print_LinkList(LinkList L)/*输出链表*/ {
LNode *h;
h=L;
while(h->next!=NULL)
{
printf(" %d ",h->next->data);
h=h->next;
}
}
LinkList Unit(LinkList L1,LinkList L2)
{
LinkList L3,p1,p2,p3;
p1=L1;
p2=L2;
p1=p1->next;
p2=p2->next;
L3=p3=L1;
while(p1&&p2)
{
if(p1->data<=p2->data)
{
p3->next=p1;
p3=p1;
p1=p1->next;
}
else
{
p3->next=p2;
p3=p2;
p2=p2->next;
}
}
p3->next=p1p1:p2;
free(L2);
return L3;
}
main()
{
LinkList la,lb,lc;
int num;
clrscr();
printf("\nplease input the elements of la,small to is the end:\n");
la=create_LinkList();
printf("\nthe elements of the la are:");
print_LinkList(la);
printf("\nplease input the elements of lb,small to is the end:\n");
lb=create_LinkList();
printf("\nthe elements of the lb are:");
print_LinkList(lb);
lc=Unit(la,lb);
printf("\nthe elements of the united lc are:");
print_LinkList(lc);
}
3、栈的操作原则:先进后出;
队列的操作原则:先进先出。