第1个回答 2006-10-29
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
//在你捻贴的时候不要覆盖程序本身就创建好的#include "stdafx.h" 会出错的 这个是我自己写的 交个朋友就加QQ150535011
struct Type
{
int a;//记录此单词个数
char b;//记录是何字母开始的单词
}phrase[26];//一个结构体数组,分别记录单词种类和个数 也可以通过申明2个数组实现
void main()
{
char str[100];//数组a用来记录各种单词的个数 数组str用来接受字符串
void Record(char str[100]);//返回你总共个数
printf("Please input a passage:\n");
gets(str);
strlwr(str);//此函数作用是将大写字母转换为小写的,也可以自己写,根据题目要求还是换成小写的
Record(str);
}
void Record(char str[])
{
int i,j,k=1,n=1;
struct Type max;//用此做为中介,来进行排序用
for(i=0;str[i]!='\0';i++)
{
if((str[i]<='z')&&(str[i]>='a'))//用来判断是不是第一个字母k==1为第一个字母
{
if(k==1)
{
for(j=1;j<=phrase[0].a&&str[i]!=phrase[j].b;j++);//用结构体第一元素来记录 已经被记录单词的种类个数
//并且来判断是否此类型单词,已经被记录过
if(str[i]!=phrase[j].b)//没有相同的记录,因此开辟数组记录此单词
{
phrase[n].b=str[i];
phrase[n].a=1;
phrase[0].a++;//使的数组中元素个数增加一
n=n+1;//使数每当使用过之后 自加一
}
else
{
phrase[j].a++;
}
k=0;
}
}
else if((str[i+1]<='z')&&(str[i+1]>='a')&&((str[i]>'z')||(str[i]<'a')))
{//此来判断下一个是不是新单词的开始
k=1;
}
}
for(i=1;i<=phrase[0].a;i++)//使用冒泡法
{
for(j=phrase[0].a;j>=i;j--)
{
if(phrase[j-1].a<phrase[j].a)
{
max=phrase[j];
phrase[j]=phrase[j-1];
phrase[j-1]=max;
}
}
printf("Words begin with:%c\t%d\n",phrase[i].b,phrase[i].a);
}
printf("%c",phrase[phrase[0].a-1].b);
printf("Total words:%d\n",phrase[0].a);
}