C语言程序设计教程 第六章 课后习题参考答案

合集下载

C语言程序设计_第三版_谭浩强主编第6—8章课后习题答案

C语言程序设计_第三版_谭浩强主编第6—8章课后习题答案

C语言程序设计_第三版_谭浩强主编第6—8章课后习题答案C语言第6—8章课后习题答案第六章循环语句6.1输入两个正数,求最大公约数和最小公倍数.#includevoid main(){ int a,b,num1,num2,temp;printf("请输入两个正整数:\");scanf("%d,%d",&num1,&num2);if(num1<num2)< bdsfid="73" p=""></num2)<>{temp=num1;num1=num2;num2=temp;}a=num1,b=num2;while(b!=0){temp=a%b;a=b;b=temp;}printf("它们的最大公约数为:%d\",a);printf("它们的最小公倍数为:%d\",num1*num2/a);}编译已通过6.2输入一行字符,分别统计出其中英文字母,空格,数字和其它字符的个数. 解:#includevoid main(){char c;int letters=0,space=0,degit=0,other=0;printf("请输入一行字符:\");while((c=getchar())!='\'){if(c>='a'&&c<='z' || c>'A'&&c<='Z')letters++;else if(c==' ')space++;else if(c>='0'&&c<='9')digit++;elseother++;}printf("其中:字母数=%d 空格数=%d 数字数=%d 其它字符数=%d\",letters,space, digit,other);}6.3求s(n)=a+aa+aaa+…+aa…a之值,其中a是一个数字,n表示a的位数。

C程序设计第六章答案

C程序设计第六章答案

实验六数组练习6.2代码如下:#include <iostream>using namespace std;int main(){int num, max = 0, count = 1;//user input 6 numbersfor (int i = 0; i < 6; i++){cout << "Enter a number: ";cin >> num;//if number entered > max, max = num, count = 1 again.if (num > max){max = num;count = 1;}else if (num == max){count++;}}//displaycout << "The largest number is " << max <<endl;cout << "The largest number appears " << count << " times\n";return 0;}练习6.4代码如下:#include <iostream>using namespace std;int main (){int score[40];int upcount = 0, downcount = 0, equalcount = 0, count = 0,i = 0, j = 0, sum = 0;//input the arrays, and when user enter a minus, stop inputingdo{cout << "Please enter less than 40 students' score, if you want to stop entering you can enter a minus.\n";cout << "You have entered " << count << " scores\n";cin >> j;if ((j >= 0) && (j <= 100)){score[i] = j;j++;count++;i++;}else if (j > 100){cout << "You should enter a correct score";}}while (j >= 0);//compute the sum of the arraysfor (i = 0; i < count; i++){sum += score[i];}//compute the averageint average = sum / count;for (i = 0; i < count; i++){if (score[i] > average){upcount++;}else if (score[i] == average){equalcount++;}else if (score[i] < average){downcount++;}}//display the resultcout << "The summer scores among " << count << " students is " << sum << endl;cout << "The average scores among " << count << " students is "<< average << endl;cout << "There are(is) " << upcount << " student's(s') scores are(is) higher than average.\n";cout << "There are(is) " << equalcount << " student's(s') scores are(is) equal to average.\n";cout << "There are(is) " << downcount << " student's(s') scores are(is) lower than average.\n";return 0;}练习6.6代码如下:#include <iostream>#include <iomanip>using namespace std;bool isPrime (int num);int main (){int prime[50];for (int i = 0, num = 2; i < 50; num++){if ( isPrime(num) ){prime[i] = num;i++;}}for (int i = 0, count = 0; i < 50; i++){cout << setw(6) << prime[i];count++;if (10 == count){cout << endl;count = 0;}}system("pause");return 0;}//this function called isPrime is used to judge an integer is prime or not... bool isPrime (int num){bool isPrime = true;for (int i = 2; i <= sqrt( (double)num ); i++){if (0 == num %i){isPrime = false;break;}}return isPrime;}练习6.8代码如下:#include <iostream>using namespace std;int average(int array[], int size);double average(double array[], int size);int main (){int array1[6] = {1,2,3,4,5,6};double array2[7] = {6.0,4.4,1.9,2.9,3,4,3.5};//display the resultcout << average(array1,6) << endl << average(array2,7) << endl;return 0;}//the kind of integerint average(int array[], int size){double sum = 0;for (int i = 0; i < size; i++){sum += array[i];}return sum / size;}//the kind of integerdouble average(double array[], int size) {double sum = 0;for (int i = 0; i < size; i++){sum += array[i];}return sum / size;}练习6.10代码如下:#include <iostream>using namespace std;void min (int array[], int size);int main (){int array[8] = {2,2,4,5,10,100,2,2};min(array,8);return 0;}//打印最小元的下标void min (int array[], int size){int min = array[0];int xiabiao[10];int j = 0;for (int i = 0; i < size; i++){if ( array[i] <= min ){min = array[i];xiabiao[j] = i;j++;}}int k = j;j = 0;//保存最小元下标的个数cout << "The min of the array(s) is " << min << endl;cout << "The subscript of the minnest number is(are) ";for (; j < k; j++){cout << xiabiao[j] << endl;}}练习6.12代码如下:#include <iostream>using namespace std;void reverse (int soure[], int size);//swapvoid reverse (int soure[], int size){for (int i = 0; i < (size / 2); i++){swap ( soure[i], soure[size-1-i] );}}//check the void reverse is right or notint main (){int array[6] = {1,2,3,4,5,6};reverse (array, 6);for (int i = 0; i < 6; i++){cout << array[i] << endl;}return 0;}练习6.14代码如下:#include <iostream>#include <ctime>using namespace std;int main (){int num[100000];srand ( time(0) );for (long i = 0; i < 100000; i++){num[i] = rand();}//生成关键字int key = rand();cout << "关键字是" << key << endl;//开始计时long startTime1 = time(0);for (int i = 0; i < 100000; i++){if (key == num[i]){cout <<"关键字的下标是"<< i <<endl;}}long endTime1 = time(0);long time1 = endTime1 - startTime1;cout << "顺序搜索时间是" << time1 <<"秒\n\n";//二分搜索int low = 0;int high = 99999;//对数组进行排序,现在开始第二次计时long startTime2 = time(0);for (long i = 9999;i >= 1; i--){int xiabiao = 0, lagest = num[0];for (long j = 1;j <= i; j++){if ( num[j] > lagest ){lagest = num[j];xiabiao = j;}}//swapif ( xiabiao != i){num[xiabiao] = num[i];num[i] = lagest;}}while (high >= low){int mid = (low + high) / 2;if (key < num[mid]){high = mid - 1;}else if (key == num[mid]){cout << "关键字下标是" << mid << endl;break;}else{low = mid + 1;}}long endTime2 = time(0);long time2 = endTime2 - startTime2;cout << "排序和二分搜索花费时间是" << time2 << "秒\n\n";system("pause");return 0;}练习6.16代码如下:#include <iostream>using namespace std;void turn ( double arrays[], int size);int main (){double arrays[7] = {6.0,4.4,1.9,2.9,3.4,2.9,3.5};turn(arrays, 7);for (int i = 0; i < 7; i++){cout << arrays[i] << " ";}system("pause");return 0;}//起泡排序void turn ( double arrays[], int size){bool changed = true;do{changed = false;for (int i = 0; i < size - 1; i++){if (arrays[i] > arrays[i+1]){swap(arrays[i], arrays[i+1]);changed = true;}}} while (changed);}练习6.18代码如下:#include <iostream>using namespace std;int main (){int arrays[4][4] = {{1,2,4,5},{6,7,8,9},{10,11,12,13},{14,15,16,17}};int sum = 0;for (int i = 0, j = 0; i < 4; i++,j++){sum += arrays[i][j];}cout << "主对角线之和为" << sum << endl;system("pause");return 0;}练习6.20代码如下:#include <iostream>using namespace std;void turn ( int arrays[], int size);int main(){int time[8][7] = {{2,4,3,4,5,8,8},{7,3,4,3,3,4,4},{3,3,4,3,3,2,2},{9,3,4,7,3,4,1},{3,5,4,3,6,3,8},{3,4,4,6,3,4,4},{3,7,4,8,3,8,4},{6,3,5,9,2,7,9}};int sumofRow[8] = {0,0,0};for (int row = 0; row < 8; row++){for (int i = 0; i < 7; i++){sumofRow[row] += time[row][i];}}turn(sumofRow, 8);for (int i = 7; i >= 0; i--){cout << sumofRow[i] << endl;}system("pause");return 0;}//排序void turn ( int arrays[], int size){bool changed = true;do{changed = false;for (int i = 0; i < size - 1; i++){if (arrays[i] > arrays[i+1]){swap(arrays[i], arrays[i+1]);changed = true;}}} while (changed);}练习6.22代码如下:#include <iostream>using namespace std;//compute the sumvoid mutiplyMatrix (int a[][5],int b[][5],int c[][5],int rowsize){for (int i = 0; i < rowsize; i++){for (int j = 0; j < rowsize; j++){for (int k = 0; k < rowsize; k++){c[i][j] += (a[i][k] + b[k][j]);}}}}int main (){int a[5][5] = {{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}};int b[5][5] = {{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}};int c[5][5] = {{0,0,0,0,0},{0},{0},{0},{0}};mutiplyMatrix(a,b,c,5);for (int i = 0; i < 5; i++){for (int j = 0; j < 5; j++){cout << c[i][j] << endl;}}system("pause");return 0;}练习6.24代码如下:#include <iostream>#include <ctime>using namespace std;int main(){srand( time(0) );int chess[8][8];//随机输入0和1,并输出8*8列表for (int i = 0; i < 8; i++){for (int j = 0; j < 8; j++){chess[i][j] = rand() % 2;cout << chess[i][j];}cout << endl;}//计算每行的和for (int row = 0; row < 8; row++){int sumofrow = 0;for (int column = 0; column < 8; column++){sumofrow += chess[row][column];}if (0 == sumofrow){cout << "All 0s on row" << row + 1 << endl;}else if (8 == sumofrow){cout << "All 1s on row" << row + 1 << endl;}}//计算每列的和for (int column = 0; column < 8; column++){int sumofcolumn = 0;for (int row = 0; row < 8; row++){sumofcolumn += chess[row][column];}if (0 == sumofcolumn){cout << "All 0s on column" << column + 1 << endl;}else if (8 == sumofcolumn){cout << "All 1s on column" << column + 1 << endl;}}//计算两个对角线的和int sumofsubdiagonal1 = 0, sumofsubdiagonal2 = 0;for (int row = 0, column = 0; row < 8; row++,column++){sumofsubdiagonal1 += chess[row][7 - column];sumofsubdiagonal2 += chess[row][column];}if ( 0 == sumofsubdiagonal1 ){cout << "All 0s on subdiagonal1" << endl;}else if ( 8 == sumofsubdiagonal1 ){cout << "All 1s on subdiagonal1" << endl;}if ( 0 == sumofsubdiagonal2 ){cout << "All 0s on subdiagonal2" << endl;}else if ( 8 == sumofsubdiagonal2 ){cout << "All 1s on subdiagonal2" << endl;}system("pause");return 0;}练习6.26代码如下:#include <iostream>using namespace std;int factors(int num, int table[][2]){/*从i=2开始除,若不能被i整除则i++;若能被i整除则输出i,且num变成num除以i的商,重新把2赋值给i,循环。

C语言程序设计(第3版)第6章习题参考答案

C语言程序设计(第3版)第6章习题参考答案
printf("\nplease input the first idiom: ");
gets(stridiom1);
printf("\nplease input the second idiom: ");
gets(stridiom2);
fnConcat(stridiom1,stridiom2);
printf("\nthe result is: %s",stridiom1)=0;
while(b[j] != '\0') /*把第二个字符串的内容连接到第一个字符串的后面*/
{
a[i+j] = b[j];
j++;
}
a[i+j]= '\0'; /*添加字符串结束符*/
}
void main()
{ char stridiom1[80],stridiom2[80];
i++;
}
a[j]= '\0'; /*添加字符串结束符*/
}
void main()
{ char stridiom1[80];
printf("\nplease input the first idiom: ");
gets(stridiom1);
fnConvert(stridiom1);
printf("\nthe result is: %s",stridiom1);
scanf("%d",&n);
printf("%ld",fnF(n));
getch();
}
(4)编一函数判别某一数是否为素数,若是,返回值为1,否则,返回值为0。

C语言程序设计第六章数组习题及答案

C语言程序设计第六章数组习题及答案

1.以下对一维整型数组a的定义,正确的是_。

(2分)A.int a(10) ;B.int n = 10 , a[n] ;C.int n ;scanf( "%d" , &n ) ;int a[n] ;D.int a[10] ;2.若有定义:int a[10] ;,则对a数组元素的正确引用是_。

(2分)A.a[10]B.a[3.5]C.a(5)D.a[10-10]3.对定义int a[10] = {6 , 7 , 8 , 9 , 10} ; 的正确理解是_。

(2分)A.将5个初值依次赋给a[1]--a[5]B.将5个初值依次赋给a[0]--a[4]C.将5个初值依次赋给a[6]--a[10]D.因为数组长度与初值个数不相同,所以此语句不正确4..若有定义:int a[3][4]; , 则对a数组元素的正确引用是_。

(2分)A.a[3][4]B.a[1,3]C.a[1+1][0]D.a(2)(1)5.以下对二维数组a初始化正确的语句是_。

(2分)A.int a[2][ ]={{0 , 1 , 2}, {3 , 4 , 5}};B.int a[ ][3]={{0, 1, 2}, {3, 4, 5}};C.int a[2][4]={{0, 1 , 2}, {3 , 4}, {5}};D.int a[ ][3]={{0, 1, 2}, { }, {3, 4}};6.对二维数组a进行如下初始化:int a[ ][3]={0 , 1 , 2 , 3 , 4 , 5};则a[1][1]的值是_。

(2分)A.0B.3C.4D.17.下面程序段的运行结果是_。

(2分)#include<stdio.h>int main( ){int i , x[3][3] = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9} ;for( i = 0 ; i < 3 ; i++ )printf( "%2d" , x[i][2-i] ) ;return 0 ;}A.1 5 9B.1 4 7C.3 5 7D.3 6 98.以下对数组s的初始化,错误的是_。

《C语言程序设计教程》第三版课后习题参考答案

《C语言程序设计教程》第三版课后习题参考答案

《C语言程序设计教程》第三版课后习题参考答案C语言程序设计教程第三版课后习题参考答案第一章:C语言概述1.1 C语言的特点答案:C语言是一种通用的、面向过程的程序设计语言,具有高效、简洁、灵活等特点。

它提供了丰富的程序设计元素和功能,适用于各种不同的应用领域。

1.2 C语言程序的基本结构答案:C语言程序由预处理指令、函数声明、函数定义、变量声明和语句组成。

其中,预处理指令用来引入头文件或定义宏,函数声明用来声明函数的名称和参数,函数定义用来实现函数的功能,变量声明用来声明变量的类型和名称,语句用来表达具体的计算过程。

1.3 C语言的数据类型答案:C语言提供了多种数据类型,包括基本类型(整型、浮点型、字符型等)和派生类型(数组、指针、结构体等)。

每种数据类型在内存中占据一定的存储空间,并具有特定的取值范围和操作规则。

1.4 C语言的运算符和表达式答案:C语言支持各种运算符和表达式,例如算术运算符(+、-、*、/等)、关系运算符(>、<、==等)、逻辑运算符(&&、||、!等)等。

通过运算符和表达式可以进行各种数值计算和逻辑判断。

第二章:基本数据类型与运算2.1 整型数据类型答案:C语言提供了不同长度的整型数据类型,包括有符号整型(int、long等)和无符号整型(unsigned int、unsigned long等)。

整型数据类型可以表示整数值,并具有不同的取值范围。

2.2 浮点型数据类型答案:C语言提供了浮点型数据类型(float、double等),用来表示带小数部分的实数值。

浮点型数据可以表示较大或较小的数值,并具有一定的精度。

2.3 字符型数据类型答案:C语言提供了字符型数据类型(char),用来表示单个字符。

字符型数据可以用于表示各种字符(包括字母、数字、符号等)。

2.4 布尔型数据类型答案:C语言不直接支持布尔型数据类型,但可以使用整型数据类型来表示布尔值(0表示假、非零表示真)。

C语言第6章习题答案

C语言第6章习题答案
(2) #include <stdio.h> #include <stdlib.h> void inputdata(int a[],int n); void process(int a[],int n); void outputdata(int a[],int n); int main() {
int a[10],n=10; inputdata(a,n); outputdata(a,n); process(a,n); outputdata(a,n); system("Pause"); return 0; } void inputdata(int a[],int n) //0~99 之间的数 {
string_b=You are a student.
(5) A (10) D (15) A
(16) A
string_a=I am a teacher. string_b=I am a teacher. 4、程序填空 (1) *p != '\0', *p-'0', j— (2) i <strlen(str), j=i, k+1 (3)a+i, (char)(n%10) + '0' (4)*pk = i, a,n,i+1,pk (5) s1++, *s2, s1=p 5、编程题 (1) #include <stdio.h> int main() { int a=3,b=7,c=2; int *ptra = &a,*ptrb = &b,*ptrc = &c; int temp; printf("a=%d,b=%d,c=%d\n",a,b,c); printf("*ptra=%d,*ptrb=%d,*ptrc=%d\n",*ptra,*ptrb,*ptrc); if ( *ptra > *ptrb ) {

c 程序设计教程习题6_8章参考答案

c  程序设计教程习题6_8章参考答案

习题及实验解答第六章一、选择题1、应为4(无正确选项)2、D3、B C4、D5、A6、A7、C8、B9、A 10、A二、程序填空1、stu[i].score k=j p[k]=p[i] p[i]=temp2、p!=NULL p->data p->next3、struct node * s->data=ch r=s NULL三、编程题1、#include "iostream.h"struct staff{char num[6];char name[8];float salary[3]; //分项工资float gs; //实得工资}s[100];void main( ){int i,j,n;cin>>n; /*输入职工人数*/for(i=0;i<n;i++){cin>>s[i].num>>s[i].name;for(j=0;j<3;j++)cin>>s[i].salary[j];}cout<<"NO. salary\n";for(i=0;i<n;i++){s[i].gs= s[i].salary[0]+ s[i].salary[1]- s[i].salary[2] ;cout<<s[i].num<<'\t'<<s[i].gs<<endl;}}2、#include "iostream.h"#include "stdio.h"struct str{char ch;struct str *next;};void main(){char s[100];int i=0,num=0;struct str *insert,*head=NULL,*p;gets(s);while(s[i]!='\0'){insert=new str;insert->ch=s[i];if(head==NULL){head=insert ;head->next=NULL;}else{insert->next=head ;head=insert;}i++;}p=head;while(p!=NULL){if(p->ch>='A'&&p->ch<='Z')num++;cout<<p->ch;p=p->next;}cout<<endl<<"num="<<num<<endl; }3、#include "iostream.h"#include "stdio.h"struct node{char ch;int count;struct node *next;};void main( ){struct node *head,*p1,*p2,*insert;char c;head=NULL;while((c=getchar())!='\n'){p1=head;while(p1!=NULL&&c>p1->ch){p2=p1;p1=p1->next;}if(p1==NULL){insert=new node;insert->ch=c;insert->count=1;if(head==NULL){insert->next=head;head=insert;}else{insert->next=NULL;p2->next=insert;}}else if(c==p1->ch)p1->count++;else{insert=new node;insert->ch=c;insert->count=1;if(p1==head){insert->next=head;head=insert;}else{insert->next=p1;p2->next=insert;}}}p1=head;while(p1!=NULL){cout<<p1->ch<<' '<<p1->count<<'\t';p1=p1->next;}}4、#include "stdio.h"#include "iostream.h"struct node{int coef;int expn;struct node *next;};struct node *creat(){struct node *head,*tail,*p;int c,e;head=NULL;cin>>c>>e;while(c!=0) //约定以输入系数为0作为多项式的结束{p=new node;p->coef=c;p->expn=e;if(head==NULL)head=p;elsetail->next=p;tail=p;cin>>c>>e;}tail->next=NULL;return head;}void print(struct node *h){struct node *p;p=h;while(p!=NULL){if(p->next!=NULL)cout<<p->coef<<'x'<<p->expn<<'+';else{if(p->coef!=0&&p->expn!=0)cout<<p->coef<<'x'<<p->expn<<endl;else if(p->coef!=0)cout<<p->coef<<endl;elsecout<<endl;}p=p->next;}}void main(){struct node *h1,*p1,*h2,*p2,*h3,*p3,*newnode,*t3;h1=creat();print(h1);h2=creat();print(h2);p2=h2;h3=NULL;p1=h1;p2=h2;p3=h3;t3=h3;while(p1!=NULL&&p2!=NULL){newnode=new node;if(p1->expn>p2->expn){newnode->coef=p1->coef;newnode->expn=p1->expn;p1=p1->next;}else if(p1->expn<p2->expn){newnode->coef=p2->coef;newnode->expn=p2->expn;p2=p2->next;}else{newnode->coef=p2->coef+p1->coef;newnode->expn=p2->expn;p2=p2->next;p1=p1->next;}if(h3==NULL){h3=newnode;t3=newnode;}elset3->next=newnode;t3=newnode;}if(p1==NULL)t3->next=p2;elset3->next=p1;print(h3);}第七章一、选择题1、D2、B3、B4、A5、B6、A7、D8、B9、C 10、B二、程序填空1、fname, “w”(ch=getchar())!=’#’count++2、(c=fgetc(fp)) length++ length=03、”wb”&emp, sizeof(employer),1,fp fclose(fp) “rb”&emp, sizeof(employer),1,fp三、编程题1、#include "iostream.h"#include "stdlib.h"#include "stdio.h"void main(){FILE *fp1,*fp2;char ch;fp1=fopen("f1.txt","a");if(fp1==NULL){cout<<"can't open f1.\n";exit(1);}if((fp2=fopen("f2.txt","r"))==NULL){cout<<"can't open f2.\n";exit(1);}while(1){ch=fgetc(fp2);if(feof(fp2))break;cout<<ch;fputc(ch,fp1);}fclose(fp1);fclose(fp2);}2、#include "iostream.h"#include "stdlib.h"#include "stdio.h"void main(){FILE *fp;int a[10],i,j;if((fp=fopen("d1.dat","w"))==NULL){cout<<"can't open d1.\n";exit(1);}for(i=0;i<10;i++)a[i]=rand();for(i=0;i<9;i++)for(j=0;j<9-i;j++)if(a[j]<a[j+1]){int t=a[j];a[j]=a[j+1];a[j+1]=t;}for(i=0;i<10;i++)fprintf(fp,"%d\t",a[i]);fclose(fp);}3、#include "iostream.h"#include "stdlib.h"#include "stdio.h"#include "string.h"void main(){FILE *fp;char s[100];int a[26]={0},i;if((fp=fopen("f.txt","r"))==NULL){cout<<"can't open f.\n";exit(1);}fgets(s,100,fp);strlwr(s);i=0;while(s[i]!='\0'){if(s[i]>='a'&&s[i]<='z')a[s[i]-'a']++;i++;}for(i=0;i<26;i++)cout<<(char)('a'+i)<<":"<<a[i]<<endl;fclose(fp);}4、#include "iostream.h"#include "stdlib.h"#include "stdio.h"struct student{char num[8];char name[20];double s[3],ave;}st[5];void main(){student stu[5];FILE *fp;int i;if((fp=fopen("stu","wb"))==NULL){cout<<"can't open stu.\n";exit(1);}for(i=0;i<5;i++){cin>>stu[i].num>>stu[i].name>>stu[i].s[0]>>stu[i].s[1]>>stu[i].s[2];stu[i].ave=(stu[i].s[0]+stu[i].s[1]+stu[i].s[2])/3;}fwrite(stu,sizeof(student),5,fp);fclose(fp);}第八章一、选择题1、A2、A3、C4、C5、6、D7、B8、D9、C 10、B11、D 12、A 13、B 14、D 15、D二、阅读程序写结果1、con1 calledcon2 calledcon3 calleda=0,b=0a=10,b=10a=10,b=202、0 51 52 53 54 53、4564、10,106,67,95、x=0x=10x=7三、编程题1、#include "iostream.h"#include "string.h"class Cat{private:int age;double weight;char color[10];public:void set(int a,double w,char c[10]){age=a;weight=w;strcpy(color,c);}void print(){cout<<"age:"<<age<<"\tweight:"<<weight<<"\color:"<<color<<endl;}int getage(){return age;}double getweight(){return weight;}char *getcolor(){return color;}};void main(){Cat c1;int a;double w;char c[10];cin>>a>>w>>c;c1.set(a,w,c);c1.print();}2、#include "iostream.h"#include "string.h"#include "stdio.h"class Mystring{private:char *str;public:Mystring(){ }Mystring(char *s){str=new char[100];strcpy(str,s);}void set(char *s){str=new char[100];strcpy(str,s);}void print(){cout<<str<<endl;}int length( ){int i=0;while(str[i]!='\0')i++;return i;}char *stringcat(Mystring s2){int i=0,j=0;while(str[i]!='\0')i++;while(s2.str[j]!='\0')str[i++]=s2.str[j++];str[i]='\0';return str;}};void main(){Mystring s1;char s[100];gets(s);s1.set(s);s1.print();gets(s);Mystring s2(s);s2.print();cout<<s1.stringcat(s2);}3、#include "iostream.h"#include "string.h"#include "stdio.h"class Point{private:double x,y;public:Point(){ }Point(double x1,double y1){x=x1;y=y1;}void set(double x1,double y1){x=x1;y=y1;}void move(double x1,double y1){x=x+x1;y=y+y1;}void print(){cout<<"("<<x<<","<<y<<")"<<endl;}double getx(){ return x;}double gety(){ return y;}};class Circle:public Point{private:double r;public:void set(double x1,double y1,double r1){Point::set(x1,y1);r=r1;}void print(){Point::print();cout<<r<<endl;}double getr(){ return r;}double s(){ return 3.14*r*r;}};void main(){Point p1;Circle c1;double x1,y1,r1;cin>>x1>>y1;p1.set (x1,y1);p1.print ();cin>>x1>>y1;p1.move (x1,y1);p1.print ();cin>>x1>>y1>>r1;c1.set (x1,y1,r1);c1.print ();cout<<c1.s()<<endl;}4、#include "iostream.h"class Point{private:int x,y;public:Point(){ }Point(int x1,int y1){ x=x1; y=y1;}friend Point operator+(Point &p1,Point &p2){Point p(p1.x+p2.x,p1.y+p2.y);return p;}void print(){cout<<"("<<x<<","<<y<<")"<<endl;}};void main(){Point p1(1,2),p2(2,3),p3;p3=p1+p2;p3.print();}5、#include "iostream.h"#include "stdio.h"#include "string.h"class Teacher{private:char name[20];public:void virtual input(){cout<<"input name:";cin>>name;}virtual double wage()=0;void virtual print(){cout<<name<<'\t';}};class Professor:public Teacher{private:int cnum; //课时数public:void input(){Teacher::input ();cout<<"input cnum:";cin>>cnum;}double wage(){return (3000+40*cnum);}void print(){Teacher::print ();cout<<wage()<<endl;}};class ViceProfessor:public Teacher {private:int cnum; //课时数public:void input(){Teacher::input ();cout<<"input cnum:";cin>>cnum;}double wage(){return (2500+30*cnum);}void print(){Teacher::print ();cout<<wage()<<endl;}};class Lecture:public Teacher{private:int cnum; //课时数public:void input(){Teacher::input ();cout<<"input cnum:";cin>>cnum;}double wage(){return (2000+25*cnum);}void print(){Teacher::print ();cout<<wage()<<endl;}};void main(){Teacher *t;Professor *p;ViceProfessor *vp;Lecture *l;char ptitle[20],ch;//职称do{cout<<"input ptitle(professor,viceprofessor or lecture):"<<endl;gets(ptitle);if(!strcmp(ptitle,"professor")){p=new Professor;t=p;}else if(!strcmp(ptitle,"viceprofessor")){vp=new ViceProfessor;t=vp;}else{l=new Lecture;t=l;}t->input ();t->print ();cout<<"continue(y/n)?";cin>>ch;}while(ch=='y');}。

C语言程序设计(第二版)-习题答案 第6章习题答案

C语言程序设计(第二版)-习题答案  第6章习题答案

《C语言程序设计(Visual C++6.0环境)》习题答案习题六一、思考题1、编写程序,将10个整形数2、4、6,…18,20赋给一个数组,然后使用指针输出显示该数组各元素的值。

#include “stdio.h”main(){int i,*p;int a[10]={2,4,6,8,10,12,14,16,18,20};p=a;for(i=0;i<10;i++)printf(“%3d”,*(p+i));}2、编写程序,将两个字符串连接起来。

#include<stdio.h>#define N 120main(){ char s1[N+N],s2[N],*p,*q;printf("输入2个字符串\n");scanf("%s%s",s1,s2);for(p=s1;* p!='\0'; p++);for(q=s2;*p++=*q++;);printf("两字符串连接后:%s\n",s1);}3、输入5个字符串,按英文字典排序从小到大顺序输出。

#include "stdio.h"#include "string.h"void sort(char *name[],int count){char *temp;int i,j,min;for(i=0;i<count-1;i++){min=i;for(j=i+1;j<count;j++)if(strcmp(name[min],name[j])>0)min=j;if(min!=i){temp=name[i];name[i]=name[min];name[min]=temp;}}}main(){char *name[5]={"BASIC","FORTRON","PASAL","C","FOXBASE"};int i=0;sort(name,5);for(;i<5;i++)printf("%s\n",name[i]);}4、编一程序,输入月份号,输出该月的英文月名。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
P1581求三个实数最大值
#include<stdio.h>
float max(float,float,float);
int main()
{
float a,b,c,m;
printf("请输入三个实数:");
scanf("%f %f %f",&a,&b,&c);
printf("最大数为%f\n",max(a,b,c));
}
P473进制转换
#include<stdio.h>
void trans(int n,int base);
int main()
{
int n,base;
printf("请输入要转换的十进制数:");
scanf("%d",&n);
printf("请输入转换的进制:");
scanf("%d",&base);
#include<math.h>
int isPrime(int n);
int zhi(int m);
void fenjie(int );
int main()
{
int n;
printf("请输入一个正整数:");
scanf("%d",&n);
if(isPrime(n))
{
printf("%d=1*%d\n",n,n);
}
P1584无暇素数
#include<stdio.h>
#include<math.h>
int nixvshu(int n);
int isPrime(int n);
int main()
{
int n,t;
printf("无暇素数:\n");
for(n=100;n<=999;n++)
{
t=nixvshu(n);
else
return(Y(n-1)+Y(n-2)+Y(n-3));
}
int main()
{
int n,k=0;
for(n=0;n<=19;n++)
{
printf("%d\t",Y(n));
k++;
if(k%5==0)
printf("\n");
}
return 0;
}
P1246分解质因数
#include<stdio.h>
{
int i;
for(i=2;i<=sqrt(n);i++)
if(n%i==0) return 0;
return n;
}
P1587递归函数
#include<stdio.h>
int Y(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
if(n==2)
return 2;
return 0;
}
float max(float a,float b,float c)
{
float t;
if(a>b&&a>c)
t=a;
else if(b>a&&b>c)
t=b;
else
t=c;
return t;
}
P1582求最大公约数最小公倍数
#include<stdio.h>
intfun1(int a,int b);
void wan(intn);
void main()
{
int n;
for(n=1;n<1000;n++)
wan(n);
printf("\n");
}
void wan(int n)
{
int i,s=0;
for(i=1;i<=n/2;i++)
{
if(n%i==0)
s=s+i;
}
if(n==s)
printf("%d\t",n);
trans(n,base);
printf("\n");
return 0;
}
(方法一)
void trans(int n,int base)
{
if(n)
{
trans(n/base,base);
if(n%base>=10)
switch(n%base)
{
case 10:printf("A");break;
case 11:printf("B");break;
if(isPrime(n)&&isPrime(t))
printf("%d\t",n);
}
printf("\n");
return 0;
}
int nixvshu(int n)
{
int x=0;
while(n)
{
x=x*10+n%10;
n=n/10;
}
return x;
}
int isPrime(int n)
{
int r;
char c;
if(n)
{
trans(n/base,base);
r=n%base;
if(r>=10)
c='A&'0'+r;
printf("%c",c);
}
}
{
if(isPrime(m)&&(n%m==0))
break;
}
return m;
}
void fenjie(int n)
{
int m;
printf("%d=%d",n,zhi(n));
while(n>zhi(n))
{
m=zhi(n);
n=n/m;
printf("*%d",zhi(n));
}
}
P471输出闰年
}
return 0;
}
int f(int year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
return year;
else
return 0;
}
P472输出回文数
#include<stdio.h>
int fun(int n);
int main()
{
int n,k=0;
#include<stdio.h>
int f(int year);
int main()
{
int year,k=0;
for(year=1900;year<=2000;year++)
{
if(f(year))
{
printf("%d\t",year);
k++;
if(k%5==0)
printf("\n");
}
for(n=10;n<=2000;n++)
{
if(n == fun(n))
{
printf("%d\t",n);
k++;
if(k%5==0)
printf("\n");
}
}
return 0;
}
int fun(int n)
{
int i=0;
while(n>0)
{
i=i*10+n%10;
n=n/10;
}
return i;
case12:printf("C");break;
case 13:printf("D");break;
case 14:printf("E");break;
case 15:printf("F");break;
}
else
printf("%d",n%base);
}
}
(方法二)
void trans(int n,int base)
{
int t,r;
if(a<b)
{
t=a;
a=b;
b=t;
}
while((r=(a%b))!=0)
{
a=b;
b=r;
}
return b;
}
intfun2(int a,int b)
{
int n;
n=(a*b)/fun1(a,b);
return n;
}
P1583求完全数
#include<stdio.h>
}
else
{
fenjie(n);
printf("\n");
}
return 0;
}
int isPrime(int n)
{
int i;
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0) return 0;
}
return1;
相关文档
最新文档