代码仅供参考,望大家多多思考
10-1:
栈(STACK) 修改如下程序(10.2节的栈示例)使它存储字符而不是整数。 增加 MAIN 函数,用来要求用户输入一串圆括号或花括号,然后指出它们之间的嵌套是否正确。
Enter parentheses and/or braces: ()({})({})
Parentheses/braces are nested properly
Enter parentheses and/or braces: ((}
Parentheses/braces are NOT nested properly
#include <stdbool.h> /* C99 only */
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 100
/* external variables */
char contents[STACK_SIZE];
int top = 0;
void stack_overflow(void)
{
printf("Stack overflow\n");
exit(EXIT_FAILURE);
}
void 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(char ch)
{
if (is_full())
stack_overflow();
else
contents[top++] = ch;
}
char pop(void)
{
if (is_empty())
stack_underflow();
else
return contents[--top];
return '\0'; /* prevents compiler warning due to stack_underflow() call */
}
int main(void)
{
char ch, temp;
int n = 0;
printf("Enter parenteses and/or brance:");
while ((ch = getchar()) != '\n') {
if (ch == '(' || ch == '{')
push(ch); //推进去
else if (ch == ')' || ch == '}') {
temp = contents[--top]; //把上一个推给temp
if (ch == ')' && temp != '(') //检验
{
n = 1;
//printf("ch:%c temp:%c\n", ch, temp);
} else if (ch == '}' && temp != '{')
n = 1;
}
}
if (n == 1)
printf("Parentheses/braces are NOT nested properly\n");
else
printf("Parentheses/braces are nested properly\n");
return 0;
}
12-01:
逆序(REVERSAL) 编写程序读一条消息,然后逆序打印出这条消息
输出范例:
Enter a message: Don't get mad, get even.
Reversal is: .neve teg ,dam teg t'noD
Enter a message: Hello, world!
Reversal is: !dlrow ,olleH
提示:⼀次读取消息中的⼀个字符(⽤getchar函数),并且把这些字符存储在数组中,当数组写满或者读到字符 '\n' 时停⽌读⼊。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
int i = 0, j, a[100];
for (j = 0; j < 100; j++) {
a[j] = 0;
}
printf("Enter a message: ");
printf("life is so hard,but");
do {
ch = getchar();
a[i] = ch;
i++;
} while (ch != '\n');
for (j = i - 1; j >= 0; j--) {
printf("%c", a[j]);
}
return 0;
13-01:
最大最小单词简略版,编写程序找出一组单词中“最大”单词和“最小”单词。用户输入单词后,程序根据字典顺序决定排在最前面和最后面的单词。当用户输入4个字母的单词时,程序停止读入,假设所有单词都不超过20个字母。
输出范例1:
Enter word: dog
Enter word: zebra
Enter word: rabbit
Enter word: catfish
Enter word: walrus
Enter word: cat
Enter word: fish
Smallest word: cat
Largest word zebra
输出范例2:
Enter word: computer
Enter word: ink
Enter word: light
Enter word: bookrack
Enter word: book
Smallest word: book
Largest word: light
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char wo[40][20];
int i = 0, x, y, largest, smallest;
largest = 0;
smallest = 0;
for (x = 0; x < 20; x++) {
for (y = 0; y < 6; y++) {
wo[x][y] = 0;
}
}
do {
printf("enter word:\n");
scanf("%s", wo[i]);
i++;
} while (strlen(wo[i - 1]) != 4);
for (x = 0; x < i; x++) {
for (y = 0; y < i; y++) {
if (strcmp(wo[x], wo[y]) < 0)
smallest++;
if (strcmp(wo[x], wo[y]) == 0)
continue;
if (strcmp(wo[x], wo[y]) > 0)
largest++;
}
if (largest == 0)
printf("smallest word:%s\n", wo[x]);
if (smallest == 0)
printf("largest word:%s\n", wo[x]);
smallest = 0;
largest = 0;
}
return 0;
}