当前位置:编程学习 > C/C++ >>

跪求C语言一题 要详解和流程图 感激不尽!!

单词统计

要求1 输入任意一段英文

2 统计1中各字母的出现的概率

3 具有输入输出 的界面 并对输入的数字和其它符号给予提示!!

(有详细解释就最好了,谢谢)

追问:能用流程图生成器生成一个流程图发过来吗??
答案:#include "stdio.h" 
#include "string.h"
void main()
{
char ch='\0';
char word[1000][20]; /* 最多存1000个不同单词,每个单词在20个字符内。 */
int count_word[1000]={0}; /* 每个单词对应个数 */
int i=0,j=0,k=0,flag=2,total=0,l=0,max,maxno;
float percent; /* 每个单词出现频率 */
printf("请输入一段英文文章,并以#标志结束:\n");
while((ch=getchar())!='#')
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
if(ch>='A'&&ch<='Z') ch+=32;
flag=0;
word[i][j]=ch;
j++;
}
else flag++;
if(flag==1)
{
total++;
word[i][j]='\0';
count_word[i]++;
for(k=0;k<i;k++)
{
if(strcmp(word[i],word[k])==0)
{ count_word[k]++; count_word[i]=0; i--; break; }
}
i++;
j=0;
}
}
printf("单词 出现次数 百分比\n");
printf("----------------------------------------");
for(l=0;l<5;l++)
{
max=0;
maxno=0;
for(k=0;k<i;k++)
if (count_word[k]>max)
{
maxno=k;
max=count_word[k];
}
if (max==0) break;
printf("\n%-20s",word[maxno]);
printf("%-10d",count_word[maxno]);
percent=100.0*count_word[maxno]/total;
printf("%.2f%%",percent);
count_word[maxno]=0;
}
printf("\n谢谢使用。。嘿嘿。");
getchar();
}
本程序在VC++6.0下测试通过。。有什么看不懂的可以问我。。


不知道是统计单词还是出现的字母啊,我写了一个统计字母的,不知道楼主要哪一个

本人也是小菜,还请高手指点。

/**********************以下程序在 dev c++ 下测试通过***************************/

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

int main(void)
{
int c , total = 0 ;
int i , numa1 = 65 , numa2 = 97;
int a[26] ;
float per ;

for(i = 0 ; i < 26 ; i++)
a[i] = 0 ;

printf("请输入一段英文文章,并以#标志结束:\n");

while((c = getchar()) != '#')
{
if((c <= 'z' && c >= 'a') || c <= 'Z' && c >= 'A')
total++ ;
for(i = 0 ; i < 26 ; i++ )
{
if(c == numa1 || c == numa2)
{
a[i]++ ;
break ;
}
numa1++ ;
numa2++ ;

}
numa1 = 65 ;
numa2 = 97 ;
}

printf("\n字母\t概率\n") ;
for(i = 0 ; i < 5 ; i++)
printf("----") ;
printf("\n");

for(i = 0 ; i < 26 ; i++)
{ if(a[i] == 0)
continue ;
per =100. * a[i] / total ;
printf("%c(%c) : %.2f%%\n", (i + 97),(i + 65),per);
}

system("pause");
}

上一个:求C语言输出999999999999+1234567890123的结果。。。注意长度
下一个:C语言有些单个字母所代表意义,越多越好

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,