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

第四题

#include 
#include

int main(void)
{
	int ch;
	printf("Enter phone number: ");

	while ((ch = getchar()) != '\n') 
	{
		if (ch <= 'Z' && ch >= 'A')
		{
			switch (ch)
			{
			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;
		}
		putchar(ch);
	}
	system("pause");
	return 0;
}


第五题
只做了大写情况

#include 
#include

int main(void)
{
	int ch, sum = 0;
	printf("Enter a word: ");
	while ((ch = getchar()) != '\n') 
	{
			switch (ch)
			{
			case 65: case 69: case 73:case 76: case 78: case 79: case 82: case 83: case 84: case 85:
				sum += 1; break;
			case 68: case 71:
				sum += 2;break;
			case 66: case 67: case 77: case 80:
				sum += 3;break;
			case 70: case 72: case 86:case 87: case 89:
				sum += 4; break;
			case 75:
				sum += 5;break;
			case 74: case88:
				sum += 8;break;
			case 81: case 90:
				sum += 10; break;
			}
			//continue;
	}
	printf("Scarabble value: %d\n",sum);
	system("pause");
	return 0;
}


第六题

#include 
#include

int main(void)
{
	printf("int:%d\n", sizeof(int));
	printf("short:%d\n", sizeof(short));
	printf("long:%d\n", sizeof(long));
	printf("float:%d\n", sizeof(float));
	printf("double:%d\n", sizeof(double));
	printf("long double:%d\n", sizeof(long double));
	system("pause");
	return 0;
}


第七题

#include 
#include

int main(void)
{
	int a1, a2, b1, b2, c1, c2;
	char ch;
	printf("Enter two fractions separated by a plus sign:");
	scanf_s("%d/%d%c%d/%d", &a1, &a2,&ch, &b1, &b2);
	if (ch == '+')
	{
		c1 = a1*b2 + b1*a2;
		c2 = a2*b2;
	}
	else if (ch == '-')
	{
		c1 = a1*b2 - b1*a2;
		c2 = a2*b2;
	}
	else if (ch == '*')
	{
		c1 = a1*b1;
		c2 = a2*b2;
	}
	else if (ch == '/')
	{
		c1 = a1*b2;
		c2 = a2*b1;
	}
	else
		printf("ERRO"); 
	printf("The result is: %d/%d\n", c1, c2);
	system("pause");
	return 0;
}


第八题
第九题

#include 
#include
#include 
int main(void)
{
	int h, m;
	char ch,ch1;
	printf("Enter a 12-hour time:");
	scanf_s("%d:%d %c%c",&h,&m,&ch,1,&ch1,1);
	if (toupper(ch) == 'P')
		h += 12;
	printf("Equivalalent 24-hour time:%d:%d ", h, m);
	system("pause");
	
	第十题
	

#include
#include
#include
int main(void)
{
int ch,sum = 0;
printf(“Enter a sentence:”);
while ((ch=toupper(getchar())) != ‘\n’)
{
if (ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’)
sum += 1;
}
printf(“ssssss %d\n”, sum);
system(“pause”);
return 0;
}

第十一题

#include 
#include
#include 
int main(void)
{
	int ch,ch1,i=0,sum = 0;
	printf("Enter a sentence:");
	ch1 = toupper(getchar());
	while ((ch=getchar()) != '\n')
	{
		if (ch == ' ' || i == 1)
		{
			printf("%c", ch);
			i = 1;
		}
	}
	printf( ",%c\n", ch1);
	system("pause");
	return 0;
}
第十二题

#include
#include
#include
int main(void)
{
float a,b;
int ch;
printf(“Enter:”);
scanf("%f", &a);
while ((ch = getchar()) != ‘\n’)
{
scanf("%f", &b);
switch (ch)
{
case ‘+’:
a = a + b;break;
case ‘-’:
a = a - b;break;
case ‘*’:
a = a * b;break;
case ‘/’:
a = a / b; break;
}
}
printf("%f\n", a);
system(“pause”);
return 0;
}

第十三题

#include
#include
#include
int main(void)
{
float i=0,j=0;
float r;
int ch;
printf(“Enter:”);
while ((ch = getchar()) != ‘\n’)
{
if (ch == ’ ')
i++;
else
j++;
}
r =j / (i + 1);
printf("%.2f\n", r);
system(“pause”);
return 0;
}

第十四题

#include
#include
#include
int main(void)
{
double y=1, y1=0,y2=1;
float x;
printf(“Enter x:”);
scanf("%f", &x);
printf(“Enter y:”);
scanf("%f", &y);
while (fabs(y2 - y1) >= (0.00001*y1))
{
y2 = y;
y = (y + x / y)/2;

}
printf("%f",y1);
system("pause");
return 0;

}


你可能感兴趣的:(c)