计算机应用基础练习题题 答案在最后
习题1
一、选择题
1.所有C函数的结构都包括的三部分是( )。
A.语句、花括号和函数体
B.函数名、语句和函数体
C.函数名、形式参数和函数体
D.形式参数、语句和函数体
2.C程序由( )组成。
A.子程序 B.主程序和子程序 C.函数 D.过程
3.下面属于C语言标识符的是( )。
A.2ab B.@f C.?b D._a12
4.C语言中主函数的个数是( )。
A.2个 B.1个 C.任意个 D.10个
5.下列关于C语言注释的叙述中错误的是( )。
A.以“/*”开头并以“*/”结尾的字符串为C语言的注释内容
B.注释可出现在程序中的任何位置,用来向用户提示或解释程序的意义
C.程序编译时,不对注释作任何处理
D.程序编译时,需要对注释进行处理
6.下列不是C语言的分隔符的是( )。
A.逗号 B.空格 C.制表符 D.双引号
7.下列关于C语言的关键字的叙述中错误的是( )。
A.关键字是C语言规定的具有特定意义的字符串,通常也称保留字
B.用户定义的标识符不应与关键字相同
C.ANSI C标准规定的关键字有64个
D.ANSI C标准规定的关键字有32个
8.在Visual C++ 6.0环境下,C源程序文件名的默认后缀是( )。
A..cpp B..exe C..obj D..dsp
9.若在当前目录下新建一个名为“LX”的工程,则在当前目录下生成的工作区文件名为( )。
A.LX.DSW B.LX.OPT C. LX.DSP D.LX.C
10.面可能不影响程序正常运行的是( )。
A.语法错误 B.逻辑错误 C.警告提示 D.算法错误
二、填空题
1.C程序是由 构成的,一个C程序中至少包含 。因此, 是C程序的基本单位。
2.C程序注释是由 和 所界定的文字信息组成的。
3.开发一个C程序要经过编辑、编译、 和运行4个步骤。
4.在C语言中,包含头文件的预处理命令以 开头。
5.在C语言中,主函数名是 。
6.在C语言中,行注释符是 。
7.在C语言中,头文件的扩展名是 。
8.在Visual C++ 6.0 IDE中,按下Ctrl键的同时按 键,可以运行可执行程序文件。
9.在Visual C 6.0环境中用RUN命令运行一个C程序时,这时所运行的程序的后缀是 。
10.C语言源程序文件的扩展名是 ;经过编译后,生成文件的后缀是
;经过连接后,生成文件的扩展名是 。
三、程序设计题
1.编写程序输出以下图案
*
* S *
*
2.试编写一个C程序,输出如下信息。
*****************
You are welcome!
--------------------------
3.编写程序,分别用scanf函数和getchar函数读入两个字符送给变量c1、c2,然后分别用putchar函数和printf函数输出这两个字符。上机运行此程序,比较用putchar函数和printf函数输出字符的特点。
第2章 习题参考答案
答:算法就是为解决一个具体的问题而使用的确定的、有限的方法和步骤。
答:算法应具备有穷性、确定性、可行性、输入、输出等五种特性。
答:①用人们日常使用的语言来描述算法,称为算法的自然语言描述法。②框图语言描述法是用一些特殊符号来描述算法流程的算法。③N-S框图语言描述法是一种将具体的功能都写在一个矩形框内,框内又可以包含其他的从属框的描述方法。④伪语言描述法是用介于自然语言和计算机语言之间的文字符号来描述算法。(优缺点略)
答:在结构化程序设计方法中有顺序结构、分支结构和循环结构三种。
参考算法如下:
① 初始化
第3章 习题参考答案
1、选择题
(1)C (2)C (3)D (4)D (5)D
(6)D (7)C (8)A (9)B (10)B
(11)A (12)A (13)C (14)B (15)A
(16)C (17)B (18) A (19)B (20)C
2、填空题
(1) a%2!=0 或a%2==1。 (2) 0 。
(3) _1 , _0_ 。 (4) 3.5 。
(5) 0 。 (6) 3.5 。
(7) 4.5 。
3、程序设计题
(1):
#include <stdio.h>
main()
{
float r=0,h=0,l=0,v=0,pi=0;
pi=3.1415926;
printf("请输入圆半径及圆柱的高,用逗号分隔:");
scanf("%f,%f",&r,&h);
l=2*pi*r;
v=pi*r*r*h;
printf("圆周长为:%6.2f \n",l);
printf("%圆柱体积为:%6.2f\n",v);
}
(2):
#include<stdio.h>
main()
{
char c1='C',c2='h',c3='i',c4='n',c5='a';
printf("原字符为:%c%c%c%c%c\n",c1,c2,c3,c4,c5);
c1+=4;
c2+=4;
c3+=4;
c4+=4;
c5+=4;
printf("加密后的字符为:%c%c%c%c%c\n",c1,c2,c3,c4,c5);
c1='T',c2='o',c3='d',c4='a',c5='y';
printf("题目(2)原字符为:%c%c%c%c%c\n",c1,c2,c3,c4,c5);
/*c1='w',c2='x',c3='y',c4='z',c5='w';*/
c1-=22;
c2-=22;
c3-=22;
c4-=22;
c5-=22;
printf("题目(2)加密后的字符为:%c%c%c%c%c\n",c1,c2,c3,c4,c5);
printf("题目(3)原字符为:%c%c%c%c%c\n",c1,c2,c3,c4,c5);
c1-=4;
c2-=4;
c3-=4;
c4-=4;
c5-=4;
printf("题目(3)加密后的字符为:%c%c%c%c%c\n",c1,c2,c3,c4,c5);
}
(3):
#include<stdio.h>
main()
{
long x=0;
int h=0,m=0,s=0;
printf("请输入秒数,为一个正整数:");
scanf("%d",&x);
h=x/3600;
m=x%3600/60;
s=x%3600%60;
printf("你输入的秒数为 %d小时%d分%d秒\n",h,m,s);
}
第4章 习题参考答案
1、选择题
(1) C (2) C (3) C (4) B (5) D
(6) B (7) C (8) A (9) A
2、填空题
(1) i<9、 i 、i++;++a; 、i++; 、9、10
(2) i<10 、 i 、a++、i++、9、10、10。
(3) 54321 。
(4) x%3==0 || x%7==0 、f=1。
3.程序阅读题
(1)pass (2) 120
4、程序设计题
(1) 参考程序:
#include <stdio.h>
int main(void)
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(number < 0)
{
number = -number;
}
printf("The absolute value is %d.\n", number);
return 0;
}
Enter a number: 10
The absolute value is 10.
Enter a number: -300
The absolute value is 300.
(2) 参考程序:
#include<stdio.h>
void main()
{
int n=0;
printf("input a string\n");
while(getchar()!='\n') n++;
printf("%d",n);
}
(3)参考程序:
#include<stdio.h>
void main()
{
int n,i;
for(n=2; n<=100; n++)
{
for(i=2; i<n; i++)
if(n%i==0) break;
if(i>=n) printf("\t%d",n);
}
}
(4)参考程序:
#include <stdio.h>
main()
{
double sum = 0;
double tmp = 1;
int mul = 1;
int i;
for(i=1;i<=50;i++)
{
tmp *= i;
sum += tmp*mul;
mul *= -1;
}
printf("1!-2!+3!-4!+.....+49!-50! = %e",sum);
}
(5)参考程序:
/* 输入10个字符,分别统计出其中空格或回车、数字和其他字符的个数。*/
#include <stdio.h>
int main(void)
{
int blank, digit, i, other; /* 定义3个变量分别存放统计结果 */
char ch;
blank = digit = other = 0; /* 置存放统计结果的3个变量的初值为零 */
printf("Enter 10 characters: "); /* 输入提示 */
for(i = 1; i <= 10; i++){ /* 循环执行了10次 */
ch = getchar(); /* 输入一个字符 */
/* 在switch语句中灵活应用break */
switch (ch) {
case ' ' : /* 语句段为空,请注意空格的表示 */
case '\n':
blank ++; /* 2个常量表达式 ' ' 和 '\n' 共用该语句段 */
break; /* 跳出switch语句 */
case '0' : case '1' : case '2' : case '3' : case '4' :
case '5' : case '6' : case '7' : case '8' : case '9' :
digit ++; /* 10个常量表达式 '0' ~ '9' 共用该语句段*/
break; /* 跳出switch语句 */
default:
other ++; /* 累加其他字符 */
break; /* 跳出switch语句 */
}
}
printf("blank=%d, digit=%d, other=%d\n", blank, digit, other);
return 0;
}
第5章 习题参考答案
1、选择题
(1)A (2)D (3)D (4)A (5)C
(6)D (7)D (8)B (9)A (10)D
2、填空题
(1) 6个字节。 (2) you&me。
(3) 21 。 (4) 6 。
3、程序设计题
(1) main()
{
int a[9],i,temp;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
for(i=0;i<5;i++)
{
temp=a[i];
a[i]=a[9-i];
a[9-i]=temp;
}
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
}
(2) main()
{
int a[8]={2,8,7,6,4,28,70,25};
int b[8]={79,27,32,41,57,66,78,80};
int c[8],i;
for(i=0;i<8;i++)
c[i]=a[i]+b[i];
for(i=0;i<8;i++)
printf("%d\t",a[i]);
printf("\n");
for(i=0;i<8;i++)
printf("%d\t",b[i]);
printf("\n");
for(i=0;i<8;i++)
printf("%d\t",c[i]);
printf("\n");
}
(3) main()
{
int a[4][4]={{25,36,78,13},{12,26,88,93},{75,18,22,32},{56,44,36,58}};
int dui1=0,dui2=0,i,j,temp;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(i==j)
dui1+=a[i][j];
if(i+j==3)
dui2+=a[i][j];
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("dui1=%d\tdui2=%d\n",dui1,dui2);
for(i=0;i<4;i++)
{
temp=a[0][i];
a[0][i]=a[2][i];
a[2][i]=temp;
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
}
第6章 习题参考答案
1、选择题
(1)A (2)B (3)C (4)B (5)D
(6)D (7)B (8)D (9)B (10)D
2、填空题
(1)
I=7;j=6;x=7;
I=2;j=7;x=5;
(2)
1 1 1
(3)
max is 2
(4)
<1>x=2 y=3 z=0
<2>x=4 y=9 z=5
<3>x=2 y=3 z=0
三、 程序设计题
1、
#include "stdio.h"
int prime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
void main()
{
int n;
scanf("%d",&n);
if(prime(n))
printf("该数是个素数\n");
else
printf("该数不是个素数\n");
}
2、
#include "stdio.h"
void fun(int a[], int b[], int n)
{
int i;
for(i=0;i<n;i++)
b[(a[i]/10)]++;
}
void main()
{
int i,a[100]={9,18,27,38,59,33,14,75,38},b[6]={0};
fun(a,b,9);
printf("The result is: ");
for (i=0; i<6;i++)
printf("%d ",b[i]);
printf("\n");
}
第7章 习题参考答案
1) A 2)B 3)C 4)C 5)A 6)D 7)A 8)D 9)A 10)C
11)B 12)C 13)D 14)A 15)B 16)D 17)D 18)A 19)D 20) B
(1) 8 4 (2) 3 5 (3) 0 (4) 7 (5) 0
(6) int * (7) –1 (8) *sn (9) *x (10) t
(11) int * (12) *z
(1) 编写一个函数,将数组中n个数按反序存放。
void inv(int *x,int n)
{ int *p,m,t,*i,*j;
m=(n-1)/2;
i=x; j=x+n-1; p=x+m;
for(;i<=p;i++,j--)
{ t=*i; *i=*j; *j=t ;}
return;
}
main()
{ int i,arr[10],*p=arr;
printf("The original array : \n");
for(i=0;i<10;i++,p++)
scanf("%d",p);
printf("\n");
p=arr; /* pay attention to reassigning for p */
inv(p,10);
printf(" The array has been inverted :\n");
for(p=arr;p<arr+10;p++)
printf("%d",*p);
printf("\n");
}
(2) 用地址法输入输出二维数组各元素。
#include <stdio.h>
main( )
{
int a[3][4];
int i,j;
for(i = 0; i<3; i++)
for(j = 0; j<4; j++)
scanf("%d",a[i]+j); / *地址法* /
for(i = 0; i<3; i++)
{
for(j = 0; j<4; j++)
printf("%4d",*(a[i]+j)); /* *(a[i]+j) 是地址法所表示的数组元素* /
printf(" ");
}
}
运行程序:
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4
5 6 7 8
9 1 0 1 1 1 2
(3) 用指针法输入输出二维数组各元素。
#include<stdio.h>
main( )
{
int a[3][4],*ptr;
int i,j;
ptr = a[0];
for(i = 0; i<3; i++)
for(j = 0; j< 4; j++)
scanf("%d", ptr++); / *指针的表示方法* /
ptr = a[0];
for(i = 0; i<3; i++)
{
for(j = 0; j<4; j++)
printf("%4d", *ptr++);
printf(" ");
}
}
运行程序:
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4
5 6 7 8
9 1 0 1 1 1 2
第8章 习题参考答案
一、选择题(10小题)
| 题号 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 答案 | D | C | B | C | B | D | B | A | D | D |
二、填空题(3小题)
| 题号 | 1 | 2 | 3 | |||
| 答案 | 成员 | 指向 | abc.a | p_abc->a | (*p_abc).a | 34 |
三、程序分析题(5小题)
| 题号 | 1 | 2 | 3 | 4 | 5 |
| 答案 | 9 | name:zhong total=170.0000 name:wang total=150.00000
| DDBBCC | Qian,p,95,92 | 20098 703.000000 |
四、程序设计题(4小题)
1、#define N 3
struct person
{ char *name;
float wages;
float floatwages;
float expenditure;
float realpay;
};
main()
{ struct person p[N];
int i=0;
for(i=0;i<N;i++)
{ scanf("%s,%f,%f,%f",p[i].name,&p[i].wages,&p[i].floatwages,&p[i].expenditure);
p[i].realpay=p[i].wages+p[i].floatwages+p[i].expenditure;
}
for(i=0;i<N;i++)
printf("%s,%f",p[i].name,p[i].realpay);
}
2、#define N 10
struct student
{ long number;
char *name;
float score[4];
};
main()
{ struct student stu[N];
int i,max_i=0;
float max
scanf("%ld,%s,%f,%f,%f",&stu[0].number,stu[0].name,&stu[0].score[0],&stu[0].score[1],&stu[0].score[2]);
max=stu[0].score[3]=stu[0].score[0]+stu[0].score[1]+stu[0].score[2];
for(i=1;i<N;i++)
{scanf("%ld,%s,%f,%f,%f",&stu[i].number,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
stu[i].score[3]=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];
if (max<stu[i].score[3])
max=stu[i].score[3],max_i=i;
}
printf("max:%s,%f",stu[max_i].name,stu[max_i].score[3]);
}
3、#include "stdio.h"
struct student
{ char *name;
float cc;
float sql;
float c;
float ave;};
main()
{ struct student stu[4];
int i=0;
for(;i<4;i++)
{ scanf("%s,%f,%f,%f",stu[i].name ,&stu[i].cc ,&stu[i].sql ,&stu[i].c);
stu[i].ave=(stu[i].cc+stu[i].sql+stu[i].c)/3.0;
}
for(i=0;i<4;i++)
printf("%s,%f",stu[i].name ,stu[i].ave);
}
4、#include "stdio.h"
#include "stdlib.h"
/*定义链表数据结构 */
typedef struct node{
float data; /*这里设数据类型为float */
struct node *next;
}Lnode;
Lnode *a,*b;
/*建立a链表 */
Lnode * newcreate(Lnode *a,int n) /*建立n个元素的a链表*/
{ int i;
float x;
Lnode *q,*s;
q=a;
for(i=0;i<n;i++)
{ printf("请输入链表的第%d个数据:",i);
scanf("%f",&x);
s=(Lnode *)malloc(sizeof(Lnode));
s->data=x;
q->next=s;
q=q->next;
}
return(a);
}
/* 求a链表的所有数据之和 */
float sum (Lnode *a)
{ float total=0.0;
Lnode *p;
p=a->next; /*p指向a的第一个结点*/
while(p)
{ total=total+(p->data);
p=p->next;
}
return total;
}
/*将b链接到a的后面*/
Lnode *relation(Lnode *a,Lnode *b)
{ Lnode *q,*p;
q=a;
p=a->next;
while(p)
{ q=p;
p=p->next;
}
q->next=b;
return(a);
}
main()
{
}
第9章 习题参考答案
1、选择题
(1)D (2)C (3)C (4)B (5)C
(6)C (7)D (8)C (9)A (10)D
(11)A (12)B (13)A
2、填空题
(1) 逻辑位运算、移位运算。 (2)按位取反。
(3) 0 。 (4) 复合赋值位运算符 。
(5)右对齐、0、0、1。 (6) 整型 。
(7) 11110000 。 (8) a^=a 。
(9) a=~(a^a) (10) x=(x|ff00)|(x&00ff) 。
(11)a=012500>>2 (12)ch=ch|32
3、程序设计题
(1) /*
题目:取一个整数a从右端开始的4~7位。
程序分析:可以这样考虑:
(1)先使a右移4位。
(2)设置一个低4位全为1, 其余全为0的数。可用~(~0<<4)
(3)将上面二者进行&.运算。
*/
#include <stdio.h>
void main()
{
unsigned a,b,c,d;
scanf("%o",&a);
b=a>>4;
c=~(~0<<4);
d=b&c;
printf("%o\n%o\n",a,d);
}
(2) #include <stdio.h>
void main()
{
struct bit
{
unsigned a:3;
unsigned b:2;
unsigned c:2;
/*unsigned类型的数据在VC6.0中为4个字节*/
unsigned x:32-(3+2+2);/*定义25位不使用的位段*/
unsigned d:4; /*d另起一个存储单元*/
unsigned f:7;
}s={1,2,3,0,4,5}; /*初始化*/
printf("%d %d %d %d %d %d\n",s.a,s.b,s.c,s.x,s.d,s.f);
}
第10章 习题参考答案
1、选择题
(1)D (2)B (3)C (4)B (5)C
(6)C、D (7)A (8)D (9)D (10)C
(11)C (12)A (13)B
2、填空题
(1)fputc(‘A’,fp); 、fputs(“Hello!”,fp); 。
(2) fscanf(fp,”%d,%d,%d”,&a,&b,&c); 。
(3) “r” 、 “w” 、 “a+” 。
(4) !feof(f1) 、 f2 、 fclose(f1) 、 fclose(f2) 。
(5) fopen(fname,”w”) 、 ch 。
(6) “r” 、 (!feof(fp)) 、 fgetc(fp) 。
(7) CCCCBBBBAAAA 。
3、程序填空题
score.dat
i+1
“%s”,name
scanf(“%d”,&age);
scanf(“%f”,&score);
“%s,%d,%f”
fclose(fp);
4、程序设计题
(1) /*
源文件名:xiti41.c
功能:读取一个文件的内容并显示出来。
*/
#include<stdio.h>
void main()
{
FILE *fp; /*定义一个文件指针*/
char c;
if((fp=fopen("C:\\boot.ini","rt"))==NULL)
{
printf("file can not open,press any key to exit!\n");
getch(); /*从键盘上任意输入一字符,结束程序*/
exit(1);
}
c=fgetc(fp); /*从文件中逐个读取字符*/
/*只要读出的字符没有到文件尾就把该字符显示在屏幕上*/
while(c!=EOF)
{
putchar(c);
c=fgetc(fp);
}
printf("\n");
fclose(fp);
}
(2) /*
源文件名:xiti42.c
功能:读取一个文件的内容并显示出来。
*/
#include<stdio.h>
void main()
{
FILE *fp; /*定义一个文件指针*/
char ch;
int i;
if((fp=fopen("C:\\boot.ini","rt"))==NULL)
{
printf("file can not open,press any key to exit!\n");
getch(); /*从键盘上任意输入一字符,结束程序*/
exit(1);
}
for(i=0;i<10;i++)
{
if(feof(fp))
break; /*如果是文件尾,则退出循环*/
ch=fgetc(fp);
putchar(ch);
}
printf("\n");
fclose(fp);
}
(3) /*
源文件名:xiti43.c
功能:写一行字符到文件中,然后读取文件的内容并显示出来。
*/
#include<stdio.h>
void main()
{
FILE *fp; /*定义一个文件指针*/
char c;
if((fp=fopen("file2.txt","w+"))==NULL)
{
printf("file can not open,press any key to exit!\n");
getch(); /*从键盘上任意输入一字符,结束程序*/
exit(1);
}
printf("Please input a string:");
c=getchar(); /*从键盘读入一个字符后进入循环*/
while(c!= '\n') /*判断读入的字符不是回车符时进入循环*/
{
fputc(c,fp); /*将字符写入文件之中*/
c=getchar(); /*继续从键盘读入下一个字符*/
}
/*用于把fp所指文件的内部位置指针移到文件头。*/
rewind(fp);
c=fgetc(fp); /*从文件中逐个读取字符*/
/*只要读出的字符没有到文件尾就把该字符显示在屏幕上*/
while(c!=EOF)
{
putchar(c);
c=fgetc(fp);
}
printf("\n");
fclose(fp);
}
(4) /*
源文件名:xiti44.c
*/
#include<stdio.h>
void main()
{
FILE *fp; /*定义一个文件指针*/
char c;
if((fp=fopen("file2.txt","w+"))==NULL)
{
printf("file can not open,press any key to exit!\n");
getch(); /*从键盘上任意输入一字符,结束程序*/
exit(1);
}
printf("Please input a string:");
c=getchar(); /*从键盘读入一个字符后进入循环*/
while(c!= '*') /*判断读入的字符不是回车符时进入循环*/
{
fputc(c,fp); /*将字符写入文件之中*/
c=getchar(); /*继续从键盘读入下一个字符*/
}
/*用于把fp所指文件的内部位置指针移到文件头。*/
rewind(fp);
c=fgetc(fp); /*从文件中逐个读取字符*/
/*只要读出的字符没有到文件尾就把该字符显示在屏幕上*/
while(c!=EOF)
{
putchar(c);
c=fgetc(fp);
}
printf("\n");
fclose(fp);
}
(5) /*
源文件名:xiti45.c
功能:从键盘输入10个浮点数,以二进制形式存入文件中。再从文件中读出数据显示在屏幕上。
*/
#include<stdio.h>
void main()
{
FILE *fp; /*定义一个文件指针*/
int i;
double a[10];
if((fp=fopen("data.txt","wb+"))==NULL)
{
printf("file can not open,press any key to exit!\n");
getch(); /*从键盘上任意输入一字符,结束程序*/
exit(1);
}
for(i=0;i<10;i++)
scanf("%lf",&a[i]);
for(i=0;i<10;i++)
fwrite(a+i,sizeof(double),1,fp);
printf("\n");
rewind(fp);
fread(a,sizeof(double),10,fp);
for(i=0;i<10;i++)
printf("%f\n",a[i]);
printf("\n");
fclose(fp);
}