C语言程序设计现代方法第二版,第八章课后编程习题

第一题

#include 
#include
int main(void)
{
	long n;
	int  didit_seen[10] = {0};
	int didit,i=0,j;
	printf("Enter a number:");
	scanf("%ld", &n);
	while (n != 0)
	{
		didit = n % 10;
		didit_seen[didit] += 1;
		n = n / 10;
		i++;
	}
	printf("\nRepeated digits:");
	for (j = 0; j <= 9; j++)
	{
		if (didit_seen[j] > 1)
			printf("%d ",j);
	}
	printf("\n");
	system("pause");
	return 0;
}


第二题

#include 
#include
int main(void)
{
	long n;
	int  didit_seen[10] = {0};
	int didit,i=0,j;
	printf("Enter a number:");
	scanf("%ld", &n);
	while (n != 0)
	{
		didit = n % 10;
		didit_seen[didit] += 1;
		n = n / 10;
		i++;
	}
	printf("Digit:\t\t0\t1\t2\t3\t4\t5\t6\t7\t8\t9\t\n");
	printf("Repeated digits:");
	for (j = 0; j <= 9; j++)
	{
			printf("%d\t", didit_seen[j]);
	}
	printf("\n");
	system("pause");
	return 0;
}


第三题

#include 
#include
int main(void)
{
	long n=1;
	
	while (n >= 0)
	{
		int  didit_seen[10] = { 0 };
		int didit = 0, i = 0, j;
		printf("Enter a number:");
		scanf("%ld", &n);
		while (n != 0)
		{
			didit = n % 10;
			didit_seen[didit] += 1;
			n = n / 10;
			i++;
		}
		printf("Digit:\t\t0\t1\t2\t3\t4\t5\t6\t7\t8\t9\t\n");
		printf("Repeated digits:");
		for (j = 0; j <= 9; j++)
		{
			printf("%d\t", didit_seen[j]);
		}
		printf("\n");
	}
	system("pause");
	return 0;
}


第六题

#include 
#include
#include 
int main(void)
{
	char input[100];
	int i = 0, j = 0;
	printf("Enter message: ");
	while ((input[i] = getchar()) != '\n')
	{
		i++;
	}
	printf("In B1F'F-speak:");
	for (j = 0; j < i; j++)
	{
		input[j] = toupper(input[j]);
		switch (input[j]) {
		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", input[j]); break;
		}
	}
	printf("!!!!!!!!!!\n");
	system("pause");
	return 0;
}


第七题

#include 
#include
#include 
int main(void)
{
	int num[5][5];
	int rol[5], col[5],i;
	for (i = 0; i < 5; i++)
	{
		printf("Enter row %d:", i+1);
		scanf("%d %d %d %d %d",&num[i][0],&num[i][1], &num[i][2], &num[i][3], &num[i][4]);
		rol[i] = num[i][0] + num[i][1] + num[i][2] + num[i][3] + num[i][4];
	}
	printf("Row totals: %d %d %d %d %d\n", rol[0], rol[1], rol[2], rol[3], rol[4]);
	for (i = 0; i <5; i++)
	{
		col[i] = num[0][i] + num[1][i] + num[2][i] + num[3][i] + num[4][i];
	}
	printf("Column totals: %d %d %d %d %d\n", col[0], col[1], col[2], col[3], col[4]);
	system("pause");
	return 0;
}

第八题

#include 
#include
#include 
int main(void)
{
	int num[5][5];
	int stusum[5], i, j, min = 100 , max = 0;
	double stuave[5];
	for (i = 0; i < 5; i++)
	{
		printf("Enter row %d:", i+1);
		scanf("%d %d %d %d %d",&num[i][0],&num[i][1], &num[i][2], &num[i][3], &num[i][4]);
		stusum[i] = num[i][0] + num[i][1] + num[i][2] + num[i][3] + num[i][4];
		stuave[i] = float(stusum[i]) / 5;
	}
	printf("stusum: %d %d %d %d %d\n", stusum[0], stusum[1], stusum[2], stusum[3], stusum[4]);
	printf("subave : %.2f %.2f %.2f %.2f %.2f\n", stuave[0], stuave[1], stuave[2], stuave[3], stuave[4]);
	for (i = 0; i <5; i++)
	{
		for (j = 0; j < 5;j++)
		{
			if (num[j][i] > max)
				max = num[j][i];
			if (num[j][i] < min)
				min = num[j][i];
		}
		printf("sub %d min is %d,max is %d\n", i+1, min,max);
	}
	
	system("pause");
	return 0;
	}

第九题

#include
#include 
#include
#include 
int main(void)
{
	char num[10][10];
	int n = 0, i, j, way, m = 1, h = 0;
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			num[i][j] = '.';
		}
	}
	num[0][0] = 'A';
	i = 0; j = 0;
	for (; m < 26; m++)
	{

		srand((unsigned)time(NULL));
		while (n == 0)
		{
			Sleep(3);
			way = rand() % 4;
			if (((i == 0) || (num[i - 1][j] != '.')) && (way == 0))//0
				way = 1;
			else if (((j == 9) || (num[i][j + 1] != '.')) && (way == 1))//1
				way = 2;
			else if (((i == 9) || (num[i + 1][j] != '.')) && (way == 2))//2
				way = 3;
			else if (((j == 0) || (num[i][j - 1] != '.')) && (way == 3))//3
				way = 0;
			else
				n = 1;
			h++;;
		}
		if (h > 4)
			break;
		n = 0;
		h = 0;
		switch (way)
		{
		case 0:num[i - 1][j] = 'A' + m; i -= 1; break;
		case 1:num[i][j + 1] = 'A' + m; j += 1; break;
		case 2:num[i + 1][j] = 'A' + m; i += 1; break;
		case 3:num[i][j - 1] = 'A' + m; j -= 1; break;
		default:break;
		}
	}
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			printf("%c ", num[i][j]);
		}
		printf("\n");
	}
	system("pause");
	return 0;
}

第十题

#include 
#include
#include 
int main(void)
{
	int f[8] = { 480,583,679 ,767,840,945,1140,1305};
	int a[8] = {616,712,811,900,968,1075,1280,1438};
	int h, m, t, i, min = 200;
	printf("Enter time:");
	scanf("%d:%d",&h,&m);
	t = h * 60 + m;;
	for (i = 0; i < sizeof(a); i++)
	{
		if (abs(f[i] - t) < min)
		{
			min = abs(f[i] - t);
			m = i;
		}
	}
	printf("asss:%d/n", f[m]);
	printf("leaving time:%d:%d", f[m] / 60, f[m] % 60);

	system("pause");
	return 0;
}

第十一题

#include 
#include

int main(void)
{
	int ch, i = 0;
	char a[15] = {0};
	printf("Enter phone number: ");

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



第十二题

#include 
#include

#include 

int main(void)
{
	int a[26] = { 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 };
	int sum = 0;
	char ch;

	printf("Enter a word: ");

	while ((ch = getchar()) != '\n')
	{
		if (toupper(ch) < 'A' || toupper(ch) > 'Z') 
		{
			printf("Illegal input\n");
			break;
		}
		else
		{
			sum += a[toupper(ch) - 'A'];
		}
	}
	printf("Scrabble value: %d", sum);
	system("pause");
	return 0;
}

第十三题

#include 
#include
#include 
int main(void)
{
	char f, l[20];
	int ch,n = 0,i=0,j;
	printf("Enter a name:");
	f = toupper(getchar());
	while ((ch = getchar()) != '\n')
	{
		
		if (ch == ' ' || n == 1)
		{
			l[i] = ch;
			n = 1;
			i++;
		}
		
	}
	printf("You enerewed name:");
	for (j = 0; j < i; j++)
	{   if(l[j] != ' ')
			printf("%c", l[j]);
	}
	printf(", %c.\n", f);
	system("pause");
	return 0;
}

第十四题

#include 
#include
int main(void)
{
	char s[100],ch;
	int i,j,b[30],a1,a2,n=0;
	printf("Enter a sentence:");
	for (i = 0; i < 100; i++)
	{
		s[i] = getchar();
		if (s[i] == ' ')
		{
			b[n] = i;
			n++;
		}
		if (s[i] == '.' || s[i] == '?' || s[i] == '!'  )
		{
			b[n] = i;
			n++;
			ch = s[i];
			break;
		}
	}
	printf("Reversal of sentence:");
	for (j = n-1; j >= 0 ;)
	{
		if (j == 0)
		{
			for (i = 0; i < b[0]; i++)
				printf("%c", s[i]);
		}
		else
		{
			a1 = b[j - 1];
			a2 = b[j];
			for (i = a1 + 1; i < a2; i++)
				printf("%c", s[i]);	
			printf(" ");
		}
		j--;
	}
	printf("%c\n",ch);
	system("pause");
	return 0;
}

第十五题

#include 
#include
int main(void)
{
	char ch[80],in;
	int i = 0,n,j,m;
	printf("Enter message to be encrypted:");
	while((in = getchar()) != '\n')
	{
		ch[i] = in;
		i++;
	}
	printf("Enter shift amount(1-25):");
	scanf("%d", &n);
	printf("Encrypted message:");
	for (j = 0; j <= i; j++)
	{
		if ((ch[j] >= 'a') && (ch[j] <= 'z'))
			ch[j] = ((ch[j] - 'a') + n) % 26 + 'a';
		else if ((ch[j] >= 'A') && (ch[j] <= 'Z'))
			ch[j] = ((ch[j] - 'A') + n) % 26 + 'A';
		printf("%c", ch[j]);
	}
	printf("\n");
	system("pause");
	return 0;
}

第十六题

#include 
#include
#include 
int main(void)
{
	char word1[26], word2[26];
	int i, a[26] = {0},n=1;
	printf("Enter first word:");
	for ( i = 0;;i++)
	{
		word1[i] = toupper(getchar());
		if (word1[i] == '\n')
			break;
		a[word1[i] - 'A']++;
	}
	printf("Enter second word:");
	for (i = 0;; i++)
	{
		word2[i] = toupper(getchar());
		if (word2[i] == '\n')
			break;
		a[word2[i] - 'A']--;
	}
	for (i = 0; i < 26; i++)
	{
		if (a[i] != 0)
		{
			printf("The two words are not anagrams");
			n = 0;
			break;
		}
	}
	if (n != 0)
		printf("\nThe two words are anagrams");
	system("pause");
	return 0;
}

第十七题

#include 
#include
int main(void)
{
	int n, i = 0, j, m, ii, jj, a[99][99] = {0};
	printf("Enter the siae oh magic square:");
	scanf("%d", &n);
	j = n / 2;
	printf("j:%d\n", j);
	a[i][j] = 1;
	for (m = 2; m <= (n*n); m++)
	{
		ii = i;
		jj = j;
		i = (i + n - 1) % n;
		j = (j + n +1) % n;
		if (a[i][j] == 0)
			a[i][j] = m;
		else
		{
			i = (ii + n + 1) % n;
			j = jj;
			a[i][j] = m;
		}
	}
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
			printf("\t%d", a[i][j]);
		printf("\n");
	}
	system("pause");
	return 0;
}

你可能感兴趣的:(c)