6.8之后补上
6.2
#include <stdio.h>
void main()
{
int i, j;
printf("This program prints a table of squares. \n");
printf("Enter number of entries in table: ");
scanf_s("%d", &j);
getchar();
for (i = 1; i <= j; i++) {
printf("%10d%10d\n", i, i * i);
if (i % 24 == 0) {
printf("Press Enter to continue...");
getchar();
}
}
}
6.3
#include <stdio.h>
void main()
{
double n, sum = 0.0;
printf("This program sums a series of integers.\n");
printf("Enter inetgers (0 to terminate): ");
scanf_s("%lf", &n);
while (n != 0.0) {
sum += n;
scanf_s("%lf", &n);
}
printf("The sum is:%f\n", sum);
}
6.4
#include <stdio.h>
void main()
{
int num;
printf("Enter phone number(Capital letter): ");
while ((num = getchar()) != '\n') {
if (num <= 'Z' && num >= 'A') {
switch (num) {
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", num);
}
}
6.5
#include <ctype.h>
#include <stdio.h>//toupper函数的头文件
int main(void)
{
int sum = 0;
char ch;
printf("Enter a word: ");
while ((ch = getchar()) != '\n')
switch (toupper(ch)) {//toupper函数把小写变为大写
case 'D': case 'G':
sum += 2; break;
case 'B': case 'C': case 'M': case 'P':
sum += 3; break;
case 'F': case 'H': case 'V': case 'W': case 'Y':
sum += 4; break;
case 'K':
sum += 5; break;
case 'J': case 'X':
sum += 8; break;
case 'Q': case 'Z':
sum += 10; break;
default:
sum++; break;
}
printf("Scrabble value: %d\n", sum);
}
6.6
#include<stdio.h>
void main()
{
printf("int大小=%d\n", (int)sizeof(int));
printf("short大小=%d\n", (int)sizeof(short int));
printf("long大小=%d\n", (int)sizeof(long int));
printf("float大小=%d\n", (int)sizeof(float));
printf("double大小=%d\n", (int)sizeof(double));
printf("long double大小=%d\n", (int)sizeof(long double));
}
6.7
#include <stdio.h>
void main()
{
int num1, denom1, num2, denom2, result_num, result_denom;
char ch;
printf("Enter two fractions separated by operator: ");
scanf_s("%d/%d", &num1, &denom1);
ch = getchar();
scanf_s("%d/%d", &num2, &denom2);
switch (ch) {
case '+':
result_num = num1 * denom2 + num2 * denom1;
result_denom = denom1 * denom2;
break;
case '-':
result_num = num1 * denom2 - num2 * denom1;
result_denom = denom1 * denom2;
break;
case '*':
result_num = num1 * num2;
result_denom = denom1 * denom2;
break;
case '/':
result_num = num1 * denom2;
result_denom = num2 * denom1;
break;
}
printf("The result is %d/%d", result_num, result_denom);
}
6.9
#include<stdio.h>
void main()
{
int hour, min;
char ch;
printf("Enter a 12-hour time:");
scanf_s("%d:%d", &hour, &min);
while ((ch = getchar()) != '\n') {
switch (ch) {
case 'p':
case 'P':hour = hour + 12;
break;
case 'A':
case 'a':hour = hour+1-1;
break;
}
}
printf("Equivalent 24-hour time: %d:%d\n", hour, min);
}
6.10
#include<stdio.h>
void main()
{
printf("Enter a sentence: ");
char ch;
int sum = 0;
while ((ch = getchar()) != '\n') {
switch (tolower(ch)) {
case 'a': case 'e': case 'i': case 'o': case 'u':
sum++;
break;
}
}
printf("You sentence contains %d vowels", sum);
}
6.11
#include<stdio.h>
void main()
{
printf("Enter a first and last name: ");
char first;
scanf_s(" %c", &first);
while (getchar() != ' ');
char last;
while ((last = getchar()) != '\n')
if (last != ' ')
putchar(last);
printf(", %c.\n", first);
}
6.12
#include<stdio.h>
void main()
{
double num, result;
printf("Enter an expression: ");
scanf_s("%lf", &result);
char ch = getchar();
do {
scanf_s("%lf", &num);
switch (ch) {
case '+': result += num; break;
case '-': result -= num; break;
case '*': result *= num; break;
case '/': result /= num; break;
}
} while ((ch = getchar()) != '\n');
printf("Value of expression: %.1f\n", result);
}
6.13
#include<stdio.h>
void main()
{
char ch;
float i = 0.0, j = 0.0;
printf("Enter a sentence:");
while ((ch = getchar()) != '\n')
{
i++;
if (ch == ' ') {
j++;
i--;
}
}
printf("Average word length:%.1f\n", i / j);
}
6.14
#include<stdio.h>
#include<math.h>
void main()
{
double x, y, root = 1.0;
printf("Enter a positive number: ");
scanf("%lf", &x);
do {
y = root;
root = (y + x / y) / 2;
} while (fabs(y - root) >= 0.00001 * y);
printf("Square root: %.5f\n", root);
}
6.15
#include<stdio.h>
void main()
{
int n, mult = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
mult *= i;
}
printf("Factorial of %d: %d\n", mult);
}