C语言程序设计现代方法第二版 第八章答案

1#include 
 
int main (void)
{
	float num;           // 具体换成不同的基本类型 
	 float res = 1;
	
	printf ("Enter a positive integer: ");
	scanf ("%f", &num);
	
	for (int i = 1; i <= num; i++) {
		res *= i;
	}
	
	printf ("Factorial of %f:%f", num, res);
	
	return 0;
}

2#include
#define N 10
int main(void)
{
	int y[N]={0};
	long x;
	printf("Enter a number:");
	scanf("%ld",&x);
	int i,q;
	while(x>0){
		i=x%10;
		y[i]++;
		x=x/10;
	}
	printf("Dight:       ");
	for(q=0;q<N;q++){
		printf("%2d",q);
	}
	printf("\n");
	printf("Occurrences: ");
	for(q=0;q<N;q++){
		printf("%2d",y[q]);
	}
	return 0;
}
		
3#include
#define N 10


int main(void){
	long x;
	do{
	printf("Enter a number:");
	scanf("%ld",&x);
	if(x<=0)break;
	int y[N]={0};
	int i;
	while(x>0){
		i=x%10;
		y[i]++;
	    x=x/10;
	}
	printf("Repeated digit(s):");
	for(i=0;i<N;i++){
		
		if(y[9]<=1&&y[8]<=1&&y[7]<=1&&y[6]<=1&&y[5]<=1&&y[4]<=1&&y[3]<=1&&y[2]<=1&&y[1]<=1){printf("没有重复的\n");break;}
		else{printf("有重复的\n");break;}
			}
	}while(1);
	return 0;
}

4#include
#define N 10


int main(void){
	long x;
	do{
	printf("Enter a number:");
	scanf("%ld",&x);
	if(x<=0)break;
	int y[N]={0};
	int i;
	while(x>0){
		i=x%10;
		y[i]++;
	    x=x/10;
	}
	printf("Repeated digit(s):");
	for(i=0;i<N;i++){
		
		if(y[9]<=1&&y[8]<=1&&y[7]<=1&&y[6]<=1&&y[5]<=1&&y[4]<=1&&y[3]<=1&&y[2]<=1&&y[1]<=1){printf("没有重复的\n");break;}
		else{printf("有重复的\n");break;}
			}
	}while(1);
	return 0;
}

5#include
#define N 10


int main(void){
	long x;
	do{
	printf("Enter a number:");
	scanf("%ld",&x);
	if(x<=0)break;
	int y[N]={0};
	int i;
	while(x>0){
		i=x%10;
		y[i]++;
	    x=x/10;
	}
	printf("Repeated digit(s):");
	for(i=0;i<N;i++){
		
		if(y[9]<=1&&y[8]<=1&&y[7]<=1&&y[6]<=1&&y[5]<=1&&y[4]<=1&&y[3]<=1&&y[2]<=1&&y[1]<=1){printf("没有重复的\n");break;}
		else{printf("有重复的\n");break;}
			}
	}while(1);
	return 0;
}

6#include
#include
#define N 100
int main(void)
{

	char ch[N];
	int i=-1;
	printf ("Enter message: ");
	do{
		i++;
		ch[i]=getchar();
		i++;
		
	}while(ch[--i]!='\n');


	printf("In B1FF-speak:");
	for(int q=0;q<i;q++){
		ch[q]=toupper(ch[q]);
		switch(ch[q])
		{
		case 'A': printf ("4"); break;
			case 'B': printf ("8"); break;
			case 'E': printf ("3"); break;
			case 'I': printf ("1"); break;
			case 'O': printf ("0"); break;
			case 'S': printf ("5"); break;
			
			default : printf ("%c", ch[q]); break;
		}

	}
	
	printf ("!!!!!!!!!!");
	
	return 0;
	
}
		

7#include 
 
#define N 5
 
int main(void)
{
	int a[N][N] = {0};
	int i, j;
	 
	for (i = 0; i < N; i++) {
		printf("Enter row %d:  ", i + 1);
		for (j = 0; j < N; j++) {
			scanf("%d", &a[i][j]);
		} 
	}
	
	int sum=0;
	
	printf("Row totals:  ");
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			sum+=a[i][j];
		}
		printf("%d ", sum);
		sum = 0;
	}
	
	printf("\nColumn totals:  ");
	for (j = 0; j < N; j++) {
		for (i = 0; i < N; i++) {
			sum += a[i][j];
		}
		printf("%d ", sum);
		sum = 0;
	}
	
	return 0;
 } 

8#include 
 
#define N 5
 
int main(void)
{
	int a[N][N] = {0};
	int i, j;
	 
	for (i = 0; i < N; i++) {
		printf("学生 %d:  ", i + 1);
		for (j = 0; j < N; j++) {
			scanf("%d", &a[i][j]);
		} 
	}
	int sum1=0;
	int max=0,min=0;
	int sum2=0;
	int q=0;
	for(i=0;i<N;i++){
		
	printf("学生 %d:  \n", i + 1);
	for( q=0;q<N;q++){
		sum1+=a[i][q];}
	printf("平均分:%d\n",sum1/N);
	printf("总分分:%d\n",sum1);
	sum1=0;}
	for(i=0;i<N;i++){
		min=a[0][i];
		for(q=0;q<N;q++){
			sum2+=a[q][i];
			if(a[q][i]>max){
				max=a[q][i];}
			
			if(a[q][i]<min){
				min=a[q][i];}
		
			   
		}
		printf("第%d门平均分:%.2f\n",i+1,(double)sum2/N);
		printf("第%d最高分=%d\n",i+1,max);
		printf("第%d最低分=%d\n",i+1,min);}
	return 0;
}

9:

#include 
 
#define N 5
 
int main(void)
{
	int a[N][N] = {0};
	int i, j;
	 
	for (i = 0; i < N; i++) {
		printf("学生 %d:  ", i + 1);
		for (j = 0; j < N; j++) {
			scanf("%d", &a[i][j]);
		} 
	}
	int sum1=0;
	int max=0,min=0;
	int sum2=0;
	int q=0;
	for(i=0;i<N;i++){
		
	printf("学生 %d:  \n", i + 1);
	for( q=0;q<N;q++){
		sum1+=a[i][q];}
	printf("平均分:%d\n",sum1/N);
	printf("总分分:%d\n",sum1);
	sum1=0;}
	for(i=0;i<N;i++){
		min=a[0][i];
		for(q=0;q<N;q++){
			sum2+=a[q][i];
			if(a[q][i]>max){
				max=a[q][i];}
			
			if(a[q][i]<min){
				min=a[q][i];}
		
			   
		}
		printf("第%d门平均分:%.2f\n",i+1,(double)sum2/N);
		printf("第%d最高分=%d\n",i+1,max);
		printf("第%d最低分=%d\n",i+1,min);}
	return 0;
}
			
	
10:
#include 
#include 
 
int main(void)
{
	const int lea[8] = {480, 583, 679, 767, 840, 945, 1140, 1305};
	const int arr[8] = {616, 712, 811, 900, 968, 1075, 1280, 1438};
	
	int hours, minutes;
	int time;
	
	printf ("Enter a 24-hour time:");
	scanf ("%d:%d", &hours, &minutes);
	//将时间换算成分钟 
	time = hours * 60 + minutes;
	
	int mix = 1440, m;
	
	//找到距离最近的时间,并记录其下标 
	for (int i = 0; i < 8; i++) {
		if (abs(lea[i] - time) < mix) {
			mix = abs(lea[i] - time);
			m = i;
		}
	}
	
	
	if (lea[m] / 60 > 12) {
		if (arr[m] / 60 > 12) {
			printf("CLosest departure time is %d:%.2d p.m.", lea[m] / 60 - 12, lea[m] % 60);
			printf(", arriving at %d:%.2d p.m.", arr[m] / 60 -12, arr[m] % 60);
		}
	} else {
		if (arr[m] / 60 > 12) {
			printf("CLosest departure time is %d:%.2d a.m.", lea[m] / 60, lea[m] % 60);
			printf(", arriving at %d:%.2d p.m.", arr[m] / 60 -12, arr[m] % 60);
		} else {
			printf("CLosest departure time is %d:%.2d a.m.", lea[m] / 60, lea[m] % 60);
			printf(", arriving at %d:%.2d p.m.", arr[m] / 60, arr[m] % 60);
		}
	}
	
	return 0;
 } 

11:
#include
#include
#define N 15
int main(void)
{
	char ch[N];

	int i;
	printf("Enter phone number:");
	for( i=0;i<N;i++){
	ch[i]=getchar();
	if(ch[i]=='\n')break;
	}
	printf("In numeric form:");
	for(int q=0;q<=i;q++){
		ch[q]=toupper(ch[q]);
		if (ch[q]<= 'Z' && ch[q] >= 'A'){
switch (ch[q]) {
				case 65: case 66: case 67:
					printf ("2");
					break;
				case 68: case 69: case 70:
					printf ("3");
					break;
				case 71: case 72: case 73:
					printf ("4");
					break;
				case 74: case 75: case 76:
					printf ("5");
					break;
				case 77: case 78: case 79: 
					printf ("6");
					break;
				case 81: case 82: case 83: case 80:
					printf ("7");
					break;
				case 84: case 85: case 86: case 87:
				 	printf ("8");
					break;
				case 88: case 89: case 90:
					printf ("9");
					break; 
				
}continue;}
printf("%c",ch[q]);}

	

	return 0;
}
12:
#include
#include
#define N 15
int main(void)
{
	char ch[N];

	int i;
	printf("Enter phone number:");
	for( i=0;i<N;i++){
	ch[i]=getchar();
	if(ch[i]=='\n')break;
	}
	printf("In numeric form:");
	for(int q=0;q<=i;q++){
		ch[q]=toupper(ch[q]);
		if (ch[q]<= 'Z' && ch[q] >= 'A'){
switch (ch[q]) {
				case 65: case 66: case 67:
					printf ("2");
					break;
				case 68: case 69: case 70:
					printf ("3");
					break;
				case 71: case 72: case 73:
					printf ("4");
					break;
				case 74: case 75: case 76:
					printf ("5");
					break;
				case 77: case 78: case 79: 
					printf ("6");
					break;
				case 81: case 82: case 83: case 80:
					printf ("7");
					break;
				case 84: case 85: case 86: case 87:
				 	printf ("8");
					break;
				case 88: case 89: case 90:
					printf ("9");
					break; 
				
}continue;}
printf("%c",ch[q]);}

	

	return 0;
}
13:
#include
int main(void)
{
	char ch[20];
	int i,e;
	printf("Enter a first and last name:");
	for( i=0;i<20;i++){
		ch[i]=getchar();
		
		if(65<ch[i]&&ch[i]<90)
			e=i;
		if(ch[i]=='\n')
			break;
	}
	printf("You enered the name:");

	for(;e<i;e++){
		if(ch[e]!=' ')
			printf("%c",ch[e]);

		continue;}
	
	printf(", ");
	for(int q=0;q<=i;q++){
		if(ch[q]!=' '){
			printf("%c",ch[q]);
			break;}
		continue;}
	return 0;
}
14:
#include
int main(void)
{
	char ch,a[100];

	printf("Enter a sentence:");
	int i,e,t=0;
	int q;
	for( i=0;i<100;i++){
		a[i]=getchar();
		if(a[i]=='?'||a[i]=='!'||a[i]=='.'){
			ch=a[i];
			}
		
	     if(a[i]=='\n')
			 break;
	}
	
	printf("Reversal of sentence:");
	for( q=i-2;;){

		if(a[q]!=' '){
		t++;
			q--;}
		if(a[q]==' '){
		   e=q;
		   for(;(e+1)<=(q+t);e++){
			   printf("%c",a[e+1]);
		   }printf(" ");
		   q--;
		   t=0;
		}
		  if(q==0)
		  {
			for(int y=0;y<=t;y++){
				printf("%c",a[y]);
			 }
			break;
		  }
	}

	printf("%c",ch);
	return 0;
}


15:


	#include 
 
int main(void)
{	
	//sen为原句子 
	char sen[80], sen1[80];
	int n;
	
	//初始化数组
	int i;
	printf("Enter message to be encrypted: ");
	for (i = 0; ; i++) {               
		sen[i] = getchar();
		if (sen[i] == '\n') break;
	} 
	
	printf("Enter shift amout (1~25): ");
	scanf("%d", &n);
	
	printf("Encrypted message: ");
	for (int j = 0; j < i + 1; j++) {
		if (sen[j] <= 'z' && sen[j] >= 'a') {
			printf("%c", ((sen[j] - 'a') + n) % 26 + 'a');
		} else if (sen[j] <= 'Z' && sen[j] >= 'A') {
			printf("%c", ((sen[j] - 'A') + n) % 26 + 'A');
		} else {
			printf("%c", sen[j]);
		}
	}
	
	return 0;
 } 

16:
#include
#include
int main(void)
{
	char a[100];
	char ch[26]={0};
	int i,e;
	char b[100];
	printf("Enter first word:");
	for(i=0;i<100;i++){
		a[i]=getchar();
		a[i]=tolower(a[i]);
		if(a[i]=='\n')break;
	     if(isalpha(a[i])!=0){
			e=a[i]-'a';
		    ch[e]++;
		 }
			 
	}
	printf("Enter second word:");
	for(i=0;i<100;i++){
		b[i]=getchar();
		b[i]=tolower(b[i]);
		if(b[i]=='\n')break;
	     if(isalpha(b[i])!=0){
			e=b[i]-'a';
		    ch[e]--;
		 }
			 
	}
	
	for(i=0;i<26;i++){
		if(ch[i]!=0){
			printf("The words are  no anagrams\n");
			break;}
		  if(i==25)
		  {
			  if(ch[i]==0){
				  printf("The words are anagrams\n");break;}
			  else {
				  continue;
			  }
			
		  }}

	return 0;
}
17:
#include
#include
int main(void)
{
	char a[100];
	char ch[26]={0};
	int i,e;
	char b[100];
	printf("Enter first word:");
	for(i=0;i<100;i++){
		a[i]=getchar();
		a[i]=tolower(a[i]);
		if(a[i]=='\n')break;
	     if(isalpha(a[i])!=0){
			e=a[i]-'a';
		    ch[e]++;
		 }
			 
	}
	printf("Enter second word:");
	for(i=0;i<100;i++){
		b[i]=getchar();
		b[i]=tolower(b[i]);
		if(b[i]=='\n')break;
	     if(isalpha(b[i])!=0){
			e=b[i]-'a';
		    ch[e]--;
		 }
			 
	}
	
	for(i=0;i<26;i++){
		if(ch[i]!=0){
			printf("The words are  no anagrams\n");
			break;}
		  if(i==25)
		  {
			  if(ch[i]==0){
				  printf("The words are anagrams\n");break;}
			  else {
				  continue;
			  }
			
		  }}

	return 0;
}

你可能感兴趣的:(C语言程序设计现代方法第二版 第八章答案)