题目:输入一段含标点的英文语段(若干行,以Ctrl+Z结束)
统计这段话中出现的所有词语,要求按照字典顺序输出词语,每输出一个词换一行(相同的词语只出现一次)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAXSIZE 10000
#define WORDSIZE 20
bool IsWord(char c); //字符C是否为字母 a~z A~Z
void SortStr(char word[MAXSIZE][WORDSIZE], int n); //字符串数组排序
bool IsBig(char w1[WORDSIZE], char w2[WORDSIZE]); //w1字符串是否应该排在w2字符串后面
void Exchange(char *w1, char *w2); //交换在字符串数组中的两个个字符串
void Print(char word[MAXSIZE][WORDSIZE], int n); //跳过冗余输出字符串
bool IsSame(char w1[WORDSIZE], char w2[WORDSIZE]); //判断两个字符串是否相同
int main(){
char str[MAXSIZE] = {'\0'}; //记录输入的字符数据
int len = 0;
char ch;
while( (ch = getchar()) != EOF){
str[len++] = ch;
}
char word[MAXSIZE][WORDSIZE] = {'\0'}; //将输入分解成单个单词存储
int i,j,k;
i = j = k =0;
while(k < len){
if(IsWord(str[k])){
word[i][j++] = str[k];
++k;
}else{
if(str[k] == '-'){
++k;
continue;
}
else{
word[i][j] = '\0';
while(!IsWord(str[k]))
++k;
++i; j = 0;
}
}
}
//printf("%s\n", str);
SortStr(word, i); //排序
Print(word, i); //不冗余输出
return 0;
}
bool IsWord(char c){ //字符C是否为字母 a~z A~Z
if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return true;
return false;
}
void SortStr(char word[MAXSIZE][WORDSIZE], int n){ //字符串数组排序
int i,j;
for(i = 0; i < n - 1; i++){
for(j = i + 1; j < n; j++){
if(IsBig(word[i], word[j])){ //w1字符串是否应该排在w2字符串后面
Exchange(word[i], word[j]); //交换在字符串数组中的两个个字符串
}
}
}
}
bool IsBig(char w1[WORDSIZE], char w2[WORDSIZE]){ //w1字符串是否应该排在w2字符串后面
int len1 = strlen(w1);
int len2 = strlen(w2);
int l = (len1 < len2) ? len1 : len2;
int i;
for(i = 0; i < l;){
if(tolower(w1[i]) < tolower(w2[i]))
return false;
else if(tolower(w1[i]) == tolower(w2[i]))
i++;
else if(tolower(w1[i]) > tolower(w2[i]))
return true;
}
if(i == l){
if(len1 < len2)
return false;
else
return true;
}
}
void Exchange(char *w1, char *w2){ //交换在字符串数组中的两个个字符串
char temp[WORDSIZE];
strcpy(temp,w1);
strcpy(w1,w2);
strcpy(w2,temp);
}
void Print(char word[MAXSIZE][WORDSIZE], int n){ //跳过冗余输出字符串
if(n == 0)
return ;
printf("%s\n", word[0]);
char *temp = word[0];
for(int i = 1; i < n;i++){
if( !IsSame(word[i], temp) ){
printf("%s\n", word[i]);
temp = word[i];
}
}
}
bool IsSame(char w1[WORDSIZE], char w2[WORDSIZE]){ //判断两个字符串是否相同
int len1 = strlen(w1);
int len2 = strlen(w2);
if(len1 != len2)
return false;
else{
for(int i = 0; i < len1; i++){
if(tolower(w1[i]) != tolower(w2[i]))
return false;
}
}
return true;
}
输入:
I like for you to be still, it is as though you were absent, and you hear me from far away, and my voice does not touch you.
It seems as though your eyes had flown away, and it seems that a kiss had sealed your mouth.
As all things are filled with my soul you emerge from the things, filled my soul.
You are like my soul, a butterfly of dream, and you are like the word Melancholy.
I like for you to be still,and you seem far away.
It sounds as though you were lamenting, a butterfly cooing like a dove.
And you hear me from far away and my voice does not touch you:
Let me come to be still in your silence.
And let me talk to you with your silence that is bright as a lamp, simple as a ring.
con-tinue
ab-solute
输出: