永发信息网

c语言字符串查询的问题

答案:1  悬赏:80  手机版
解决时间 2021-07-31 16:08
  • 提问者网友:聂風
  • 2021-07-31 04:17

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
void GloMenu();
void Menu();
void Calculate();
void SortFUp();
void SortFDown();
void SortLUp();
void SortLDown();
int find(char *str1,char *str2);
void Paint(struct LetterArr arr[]);
struct LetterArr
{
char ch;
int freq;
float per;
}la[26]={{'a',0,0},{'b',0,0},{'c',0,0},{'d',0,0},{'e',0,0},{'f',0,0},{'g',0,0},{'h',0,0},{'i',0,0},{'j',0,0},{'k',0,0},{'l',0,0},{'m',0,0},{'n',0,0},{'o',0,0},{'p',0,0},{'q',0,0},{'r',0,0},{'s',0,0},{'t',0,0},{'u',0,0},{'v',0,0},{'w',0,0},{'x',0,0},{'y',0,0},{'z',0,0}};
int sum=0;
main()
{
int n1,n2,i,num,j;
char ch,name[256],letter[1000],str[1000];
FILE *fp=NULL;
GloMenu();
scanf("%d",&n1);
while(n1!=2)
{
switch(n1)
{
case 1:
getchar();
printf("请输入文档名(如E:\\\\abc.txt):\n");
gets(name);
fp=fopen(name,"r");
while(fp==NULL)
{
printf("文档名输入错误!请重新输入:");
gets(name);
fp=fopen(name,"r");
}
num=0;
ch=fgetc(fp);
while(ch!=EOF)
{
letter[num]=ch;
num++;
ch=fgetc(fp);
}
for(i=0;i<num;i++)
{
if(isalpha(letter[i]))
{
sum++;
if(islower(letter[i]))
{
la[(letter[i]-97)%26].freq++;
}
else
{
la[(letter[i]-65)%26].freq++;
}
}
}
for(i=0;i<26;i++)
{
la[i].per=(float)la[i].freq*100/sum;
}
Calculate();
Menu();
scanf("%d",&n2);
while(n2!=6)
{
switch(n2)
{
case 1:SortFUp();
Menu();
scanf("%d",&n2);
break;
case 2:SortFDown();
Menu();
scanf("%d",&n2);
break;
case 3:SortLUp();
Menu();
scanf("%d",&n2);
break;
case 4:SortLDown();
Menu();
scanf("%d",&n2);
break;
case 5:
printf("手动输入字母或字母组合(少于1000个字母):\n");
getchar();
j=0;
scanf("%c",&str[j]);
while(str[j]!='\n')
{
j++;
scanf("%c",&str[j]);
}
printf("输入出现了%d次",find(letter,str));
Menu();
scanf("%d",&n2);
break;
default:printf("输入错误!请重新选择:\n");
Menu();
scanf("%d",&n2);
}
}
if(n2==6)
{
for(i=0;i<26;i++)
{
la[i].freq=0;
}
sum=0;
GloMenu();
scanf("%d",&n1);
break;
}
fclose(fp);
break;

if(n1==2) exit(0);
}
}
}
void GloMenu()
{
printf("\n");
printf("----------------------------------------\n");
printf("请选择:\n");
printf("1 统计英文文档中所有出现的字母及出现次数\n2 退出\n");
printf("----------------------------------------\n");
}
void Menu()
{
printf("\n");
printf("----------------------------------------\n");
printf("请选择:\n");
printf("1 词频升序排列\n2 词频降序排列\n3 字母升序排列\n4 字母降序排列\n5 提供查询功能\n6 返回上一级\n");
printf("----------------------------------------\n");
}
void Calculate()
{
int i;
printf("字母\t出现次数\t出现百分比\n");
for(i=0;i<26;i++)
{
if(la[i].freq!=0) printf("%c(%c)\t %d\t %3.1f%%\n",(la[i].ch-32),la[i].ch,la[i].freq,la[i].per);
}
Paint(la);
}
void SortFUp()
{
int i,j,lastpos;
struct LetterArr lfd[26],temp;
for(i=0;i<26;i++)
{
lfd[i].ch=la[i].ch;
lfd[i].freq=la[i].freq;
lfd[i].per=la[i].per;
}
i=25;
while(i>0)
{
lastpos=0;
for(j=0;j<i;j++)
{
if(lfd[j+1].freq<lfd[j].freq)
{
temp.ch=lfd[j].ch;
temp.freq=lfd[j].freq;
temp.per=lfd[j].per;
lfd[j].ch=lfd[j+1].ch;
lfd[j].freq=lfd[j+1].freq;
lfd[j].per=lfd[j+1].per;
lfd[j+1].ch=temp.ch;
lfd[j+1].freq=temp.freq;
lfd[j+1].per=temp.per;
lastpos=j;
}
}
i=lastpos;
}
printf("按词频升序排列为:\n");
printf("字母\t出现次数\t出现百分比\n");
for(i=0;i<26;i++)
{
if(lfd[i].freq!=0) printf("%c(%c)\t %d\t %3.1f%%\n",(lfd[i].ch-32),lfd[i].ch,lfd[i].freq,lfd[i].per);
}
}
void SortFDown()
{
int i,j,lastpos;
struct LetterArr lfd[26],temp;
for(i=0;i<26;i++)
{
lfd[i].ch=la[i].ch;
lfd[i].freq=la[i].freq;
lfd[i].per=la[i].per;
}
i=25;
while(i>0)
{
lastpos=0;
for(j=0;j<i;j++)
{
if(lfd[j+1].freq>lfd[j].freq)
{
temp.ch=lfd[j].ch;
temp.freq=lfd[j].freq;
temp.per=lfd[j].per;
lfd[j].ch=lfd[j+1].ch;
lfd[j].freq=lfd[j+1].freq;
lfd[j].per=lfd[j+1].per;
lfd[j+1].ch=temp.ch;
lfd[j+1].freq=temp.freq;
lfd[j+1].per=temp.per;
lastpos=j;
}
}
i=lastpos;
}
printf("按词频降序排列为:\n");
printf("字母\t出现次数\t出现百分比\n");
for(i=0;i<26;i++)
{
if(lfd[i].freq!=0) printf("%c(%c)\t %d\t %3.1f%%\n",(lfd[i].ch-32),lfd[i].ch,lfd[i].freq,lfd[i].per);
}
}
void SortLUp()
{
int i;
printf("按字母升序排列为:\n");
printf("字母\t出现次数\t出现百分比\n");
for(i=0;i<26;i++)
{
if(la[i].freq!=0) printf("%c(%c)\t %d\t %3.1f%%\n",(la[i].ch-32),la[i].ch,la[i].freq,la[i].per);
}
}
void SortLDown()
{
int i;
printf("按字母降序排列为:\n");
printf("字母\t出现次数\t出现百分比\n");
for(i=25;i>=0;i--)
{
if(la[i].freq!=0) printf("%c(%c)\t %d\t %3.1f%%\n",(la[i].ch-32),la[i].ch,la[i].freq,la[i].per);
}
}
void Paint(struct LetterArr arr[])
{
int i,j;
for(i=0;i<25;i++)
{
if(la[i].freq!=0)
{
printf("%c|",la[i].ch);
for(j=0;j<la[i].freq;j++)
{
printf("*");
}
printf(" %3.1f%%\n",la[i].per);
}
}
}
int find(char *str1,char *str2)
{
char *s1;
char *s2;
int count=0;
while(*str1!='\0')
{
s1=str1;
s2=str2;
while((*s1==*s2)&&(*s1!='\0')&&(*s2!='\0'))
{
s1++;
s2++;
}
if(*s2=='\0')
count++;
str1++;
}
return count ;
}

我这段代码里的查询功能的字符串里的统计出现的次数怎么实现不了的,总说是0次

最佳答案
  • 五星知识达人网友:由着我着迷
  • 2021-07-31 05:48
建议你把问题提取出来,这样一大堆,没人想看
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯