C语言程序设计现代方法_第二版,CH13第十三章编程题(Programming_Projects)

CH13_1

#include <stdio.h>
#include <string.h>

int main(void)
{
    char a[20];
    char smallest_word[20], largest_word[20];

    printf("Enter word: ");
    scanf("%s", a);

    strcpy(smallest_word, a);
    strcpy(largest_word, a);

    while (strlen(a) != 4) {
        printf("Enter word: ");
        scanf("%s", a);

        if (strcmp(a, smallest_word) < 0) {
            strcpy(smallest_word, a);
        }
        else if (strcmp(a, largest_word) > 0) {
            strcpy(largest_word, a);
        }

    }

    printf("Smallest word: %s\n", smallest_word);
    printf("Largest word: %s\n", largest_word);

    return 0;
}

CH13_2
(a)

#include <stdio.h>
#include <string.h>

#define MAX_REMIND 50   /* maximum number of reminders */
#define MSG_LEN 60      /* max length of reminder message */

int read_line(char str[], int n);

int main(void)
{
    char reminders[MAX_REMIND][MSG_LEN + 3];
    char day_str[3], msg_str[MSG_LEN + 1];
    int day, i, j, num_remind = 0;

    for (;;) {
        if (num_remind == MAX_REMIND) {
            printf("-- No space left --\n");
            break;
        }

        printf("Enter day and reminder: ");
        scanf("%2d", &day);

        if (day == 0)
            break;
        else if (day < 0 || day > 31) {
            printf("ERROR\n");
            while (getchar() != '\n');
            continue;
        }

        sprintf(day_str, "%2d", day);
        read_line(msg_str, MSG_LEN);

        for (i = 0; i < num_remind; i++)
            if (strcmp(day_str, reminders[i]) < 0)
                break;
        for (j = num_remind; j > i; j--)
            strcpy(reminders[j], reminders[j - 1]);

        strcpy(reminders[i], day_str);
        strcat(reminders[i], msg_str);

        num_remind++;
    }

    printf("\nDay Reminder\n");
    for (i = 0; i < num_remind; i++)
        printf(" %s\n", reminders[i]);

    return 0;
}

int read_line(char str[], int n)
{
    int ch, i = 0;

    while ((ch = getchar()) != '\n')
        if (i < n)
            str[i++] = ch;
    str[i] = '\0';
    return i;
}

(b)

#include <stdio.h>
#include <string.h>

#define MAX_REMIND 50   /* maximum number of reminders */
#define MSG_LEN 60      /* max length of reminder message */

int read_line(char str[], int n);

int main(void)
{
    char reminders[MAX_REMIND][MSG_LEN + 3];
    char day_str[9], msg_str[MSG_LEN + 1];
    int day, i, j, num_remind = 0;
    int hour,minute;

    for (;;) {
        if (num_remind == MAX_REMIND) {
            printf("-- No space left --\n");
            break;
        }

        printf("Enter day and reminder: ");
        scanf("%2d", &day);
        if (day == 0)
            break;

        scanf("%d:%d", &hour, &minute);

        sprintf(day_str, "%2d %2d:%.2d", day,hour,minute);
        read_line(msg_str, MSG_LEN);

        for (i = 0; i < num_remind; i++)
            if (strcmp(day_str, reminders[i]) < 0)
                break;
        for (j = num_remind; j > i; j--)
            strcpy(reminders[j], reminders[j - 1]);

        strcpy(reminders[i], day_str);
        strcat(reminders[i], msg_str);

        num_remind++;
    }

    printf("\nDay Reminder\n");
    for (i = 0; i < num_remind; i++)
        printf(" %s\n", reminders[i]);

    return 0;
}

int read_line(char str[], int n)
{
    int ch, i = 0;

    while ((ch = getchar()) != '\n')
        if (i < n)
            str[i++] = ch;
    str[i] = '\0';
    return i;
}

(c)

#include <stdio.h>
#include <string.h>

#define MAX_REMIND 50   /* maximum number of reminders */
#define MSG_LEN 60      /* max length of reminder message */

int read_line(char str[], int n);

int main(void)
{
    char reminders[MAX_REMIND][MSG_LEN + 3];
    char day_str[9], msg_str[MSG_LEN + 1];
    int month,day, i, j, num_remind = 0;
    int hour,minute;

    for (;;) {
        if (num_remind == MAX_REMIND) {
            printf("-- No space left --\n");
            break;
        }

        printf("Enter month/day and reminder: ");
        scanf("%d", &month);
        if (month == 0)
            break;

        scanf("/%d", &day);

        sprintf(day_str, "%2d/%.2d", month, day);
        read_line(msg_str, MSG_LEN);

        for (i = 0; i < num_remind; i++)
            if (strcmp(day_str, reminders[i]) < 0)
                break;
        for (j = num_remind; j > i; j--)
            strcpy(reminders[j], reminders[j - 1]);

        strcpy(reminders[i], day_str);
        strcat(reminders[i], msg_str);

        num_remind++;
    }

    printf("\n Date Reminder\n");
    for (i = 0; i < num_remind; i++)
        printf("%s\n", reminders[i]);

    return 0;
}

int read_line(char str[], int n)
{
    int ch, i = 0;

    while ((ch = getchar()) != '\n')
        if (i < n)
            str[i++] = ch;
    str[i] = '\0';
    return i;
}

CH13_3

#include <stdbool.h>   
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM_SUITS 4
#define NUM_RANKS 13

int main(void)
{
  bool in_hand[NUM_SUITS][NUM_RANKS] = {false};
  int num_cards, rank, suit;
  const char *rank_code[] = {"Two", "Three", "Four", "Five", "Six",
                             "Seven", "Eight", "Nine", "Ten", "Jack",
                             "Queen", "King", "Ace"};

  const char *suit_code[] = {"Clubs", "Diamonds", "Hearts", "Spades"};

  srand((unsigned) time(NULL));

  printf("Enter number of cards in hand: ");
  scanf("%d", &num_cards);

  printf("Your hand:");
  while (num_cards > 0) {
    suit = rand() % NUM_SUITS;   
    rank = rand() % NUM_RANKS;   
    if (!in_hand[suit][rank]) {
      in_hand[suit][rank] = true;
      num_cards--;
      printf("\n%s of %s", *(rank_code + rank), *(suit_code + suit));
    }
  }

  return 0;
}

CH13_4

#include <stdio.h>

int main(int argc, char *argv[])
{
    for (int i = argc - 1; i >= 1; i--) {
        printf("%s ", argv[i]);
    }

    return 0;
}

CH13_5

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int sum = 0;

    for (int i = argc - 1; i >= 1; i--) {
        sum += atoi(argv[i]);
    }

    printf("Total: %d", sum);

    return 0;
}

CH13_6

#include <stdio.h>
#include <ctype.h>

#define NUM_PLANETS 9

int str_cmp(const char* s1, const char* s2);

int main(int argc, char* argv[])
{
    char* planets[] = { "Mercury", "Venus", "Earth",
                       "Mars", "Jupiter", "Saturn",
                       "Uranus", "Neptune", "Pluto" };
    int i, j;

    for (i = 1; i < argc; i++) {
        for (j = 0; j < NUM_PLANETS; j++) {
            if (str_cmp(argv[i], planets[j])) {
                printf("%s is planet %d\n", argv[i], j + 1);
                break;
            }
        }
            
        if (j == NUM_PLANETS) {
            printf("%s is not a planet\n", argv[i]);
        }
    }

    return 0;
}

int str_cmp(const char* s1, const char* s2)
{
    for (; toupper(*s1) == toupper(*s2); s1++, s2++) {
        if (!*s1) {
            return 1;
        }
    }

    return 0;
}

CH13_7

#include <stdio.h>
//此程序转换范围为1-99
int main(void)
{
    int number, tens, ones;
    char* x[] = { "one.", "two.", "three", "four.", "five.",
                          "six.", "seven.", "eight.", "nine." };

    char* xy[] = { "ten.", "eleven.", "twelve.", "thirteen.",
                           "fourteen.", "fifteen.", "sixteen.", "seventeen.",
                           "eighteen.", "nineteen." };

    char* yy[] = { "twenty-", "thirty-", "forty-", "fifty-",
                          "sixty-", "seventy-", "eighty-", "ninety-" };

    printf("Enter a two-digit number: ");
    scanf("%d", &number);

    if (number < 1 || number > 99) {
        printf("ERROR");
        return 0;
    }

    tens = number / 10;
    ones = number % 10;

    printf("You entered the number: ");

    if (tens == 0) 
        printf("%s", x[ones - 1]);
    else if (tens == 1) 
        printf("%s", xy[ones]);
    else 
        printf("%s%s", yy[tens - 2], x[ones - 1]);

    return 0;
}

CH13_8

#include <stdio.h>
#include <ctype.h>

int compute_scrabble_value(const char* word);

int main(void)
{
    char word[10] = { 0 };

    printf("Enter a word: ");
    scanf("%s", &word);

    printf("Scrabble value: %d\n", compute_scrabble_value(word));

    return 0;
}

int compute_scrabble_value(const char* word)
{
    int sum = 0;

    while (*word) {
        switch (toupper(*word++)) {
        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;
        }
    }

    return sum;
}

CH13_9

#include <stdio.h>
#include <ctype.h>

int compute_vowel_count(const char* sentence);

int main(void)
{
	char str[30];

	printf("Enter a sentence: ");
	scanf("%s", &str);

	printf("Your sentence contains %d vowels.", compute_vowel_count(str));

	return 0;
}

int compute_vowel_count(const char* sentence)
{
	int n = 0;
	char t;
	while (t = tolower(*sentence++)) {
		if (t == 'a' || t == 'e' || t == 'i' || t == 'o' || t == 'u') {
			n++;
		}
	}

	return n;
}

CH13_10

#include <stdio.h>
#include <ctype.h>

void reverse_name(char* name);

int main(void)
{
	char name[20] = { 0 };

	printf("Enter the name:");
	gets(name);

	reverse_name(name);

	return 0;
}

void reverse_name(char* name)
{
	char ch_ = toupper(*name++);

	while (*name++ != ' ');
	while (*name++ == ' ');

	printf("%s, %c\n", --name, ch_);
}

CH13_11

#include <stdio.h>

double compute_verage_word(const char* sentence);

int main(void)
{
	char sentence[50] = { 0 };

	printf("Enter a sentence:");
	gets(sentence);

	printf("Average word length: %.2f\n", compute_verage_word(sentence));

	return 0;
}

double compute_verage_word(const char* sentence)
{
	char ch;
	int n = 0;
	int m = 1;

	while (ch=*sentence++) {	
		if (ch != ' ') {
			n++;
		}
		else {
			m++;
		}
	}

	return (double)n / m;
}

CH13_12

#include <stdio.h>
#include <ctype.h>

void my_read(char* sentence, char* ch);

int main(void)
{
	char sentence[30][20] = { 0 };
	char ch;
	int i = 1;

	printf("Enter a sentence: ");
	my_read(&sentence[0][0], &ch);

	while (ch != '.' && ch != '?' && ch != '!') {
		my_read(&sentence[i][0], &ch);
		i++;
	}

	for (int j = --i; j >= 0; j--) {
		printf("%s ", sentence[j]);
	}
	printf("\b%c\n", ch);

	return 0;
}

void my_read(char* sentence, char* ch)
{
	while (toupper((*ch = getchar())) >= 'A' && toupper(*ch) <= 'Z') {
		*sentence++ = *ch;
	}
}

CH13_13

#include <stdio.h>

void encrypt(char* message, int shift);

int main(void)
{
	char message[100] = { 0 };
	int n;

	printf("Enter message to be encrypted: ");
	gets(message);

	printf("Enter shift amout (1~25): ");
	scanf("%d", &n);

	encrypt(message, n);

	printf("Encrypted message: ");
	puts(message);

	return 0;
}

void encrypt(char* message, int shift)
{
	while (*message) {
		if (*message >= 'A' && *message <= 'Z') {
			*message = (*message - 'A' + shift) % 26 + 'A';
		}
		else if (*message >= 'a' && *message <= 'z') {
			*message = (*message - 'a' + shift) % 26 + 'a';
		}
		message++;
	}
}

CH13_14

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

bool are_anagrams(const char* word1, const char* word2);

int main(void)
{
	char word1[30] = { 0 };
	char word2[30] = { 0 };

	printf("Enter first word: ");
	gets(word1);

	printf("Enter Second word: ");
	gets(word2);

	if (are_anagrams(word1, word2)) {
		printf("The words are anagrams.");
	}
	else {
		printf("The words are not anagrams.");
	}

	return 0;
}

bool are_anagrams(const char* word1, const char* word2)
{
	int ch[26] = { 0 };

	while (*word1) {
		if (isalpha(*word1)) {
			ch[toupper(*word1) - 'A']++;
		}
		word1++;
	}

	while (*word2) {
		if (isalpha(*word2)) {
			ch[toupper(*word2) - 'A']--;
		}
		word2++;
	}

	int key = 0;
	for (int i = 0; i < 26; i++) {
		if (ch[i] == 0) {
			key++;
		}
	}

	if (key == 26) {
		return true;
	}
	else {
		return false;
	}
}

CH13_15

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#define STACK_SIZE 100

int contents[STACK_SIZE];
int top = 0;
void make_empty(void);
int stack_overflow(void);
int stack_underflow(void);
bool is_empty(void);
bool is_full(void);
void push(int i);
int pop(void);
int evaluate_RPN_expression(const char* expression);

int main(void)
{
    char* expression[30] = { 0 };

    make_empty();

    while (1) {
        printf("Enter an RPN expression: ");
        gets(expression);

        printf("%d\n\n", evaluate_RPN_expression(expression));
    }

    return 0;
}

int evaluate_RPN_expression(const char* expression)
{
    int m, n;

    while (*expression != '=') {
        switch (*expression) {
        case '0': push(0); break;
        case '1': push(1); break;
        case '2': push(2); break;
        case '3': push(3); break;
        case '4': push(4); break;
        case '5': push(5); break;
        case '6': push(6); break;
        case '7': push(7); break;
        case '8': push(8); break;
        case '9': push(9); break;
            //引入变量m,n;可以调节出栈元素运算顺序;若直接 push(pop() + pop());
            //对于减法和除法会产生运算错误
        case '+': m = pop(); n = pop(); push(n + m); break;
        case '-': m = pop(); n = pop(); push(n - m); break;
        case '*': m = pop(); n = pop(); push(n * m); break;
        case '/': m = pop(); n = pop(); push(n / m); break;

        case ' ':; break;
        default: printf("END\n"); return 0; break;
        }
        expression++;
    }

    return pop();
}

int stack_overflow(void)
{
    printf("Stack overflow\n");
    exit(EXIT_FAILURE);
}

int stack_underflow(void)
{
    printf("Stack underflow\n");
    exit(EXIT_FAILURE);
}

void make_empty(void)
{
    top = 0;
}

bool is_empty(void)
{
    return top == 0;
}

bool is_full(void)
{
    return top == STACK_SIZE;
}

void push(int i)
{
    if (is_full()) {
        stack_overflow();
    }
    else {
        contents[top++] = i;
    }
}

int pop(void)
{
    if (is_empty()) {
        stack_underflow();
    }
    else {
        return contents[--top];
    }
}

CH13_16

#include <stdio.h>

#define MAX_LEN 100 

void reversal(char* message);

int main(void)
{
    char message[MAX_LEN] = { 0 };

    printf("Enter message message: ");
    gets(message);

    reversal(message);

    printf("Reversal is: ");
    puts(message);

    return 0;
}

void reversal(char* message)
{
    char* p = message;
    char* q = message;
    while (*message) {
        q = message++;
    }

    char t;
    for (; p < q; p++, q--) {
        t = *p;
        *p = *q;
        *q = t;
    }
}

CH13_17

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

#define MAX_LEN 100     

bool is_palindrome(const char* message);

int main(void)
{
    char message[MAX_LEN] = { 0 };

    printf("Enter message message: ");
    gets(message);

    if (is_palindrome(message)) {
        printf("Palindrome\n");
    }
    else {
        printf("Not message palindrome\n");
    }

    return 0;
}

bool is_palindrome(const char* message)
{
    char* p = message;
    char* q = message;
    while (*message) {
        q = message++;
    }

    while (p < q) {
        if (toupper(*p) >= 'A' && toupper(*p) <= 'Z' && toupper(*q) >= 'A' && toupper(*q) <= 'Z') {
            if (*p != *q) {
                return false;
            }
            p++;
            q--;
        }
        else if (toupper(*p) >= 'A' && toupper(*p) <= 'Z') {
            q--;
        }
        else if (toupper(*q) >= 'A' && toupper(*q) <= 'Z') {
            p++;
        }
    }

    return true;
}

CH13_18

#include <stdio.h>

int main(void)
{
    const char* months[] = {
        "January", "February", "March", "April",
        "May", "June", "July", "August",
        "September", "October", "November", "December"
    };
    int month, day, year;

    printf("Enter a date (mm/dd/yyyy): ");
    scanf("%d/%d/%d", &month, &day, &year);
    printf("You entered the date : %s %d, %d\n", *(months + (month - 1)), day, year);
}
  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值