计算频率 [英] Count the frequency
问题描述
大家好,
有谁知道如何编写一个C程序来计算给定数据文件中某个关键模式的频率?
输入:
?三个按空格分隔的字母数字键
?数据文件包含M×3矩阵的字母数字键。其中M是
行数和3列数。
输出:
?输入键的出现次数
示例:
输入文件db.txt及其内容:
4 7 9
2 1 G
5 3 4
A 3 E
1 A 3
F 5 6
X 0 4
样品运行#1
请输入3个键(由空间):7 A 4
钥匙?7?在数据库中有1次出现
关键?A?在数据库中有2次出现
关键?4?在数据库中有3次出现
样品运行#2
请输入3个键(以空格分隔):ABC
钥匙?A?在数据库中有1次出现
关键?B?在数据库中没有出现
关键?C?在数据库中没有出现
以下是我的提示。但是,我不知道怎么能完成这个程序。
我们将不胜感激。
***************************
#include< stdio.h>
#include< string.h>
int numeric [10];
int alpha [26];
void ReadData(char * input_file)
{
FILE * fptr;
char buffer [128];
char * token;
int index;
fptr = fopen(input_file, " r");
if(fptr!= NULL)
{
while(fgets(buffer,sizeof( buffer),fptr)!= NULL)
{
token = strtok(buffer,"");
while(令牌! = NULL)
{
if(token [0]> =''0''&& token [0]< =' '9'')
{
/ *用数字计算输入数量[] * /
}
else if(token [0]> =''A''&& token [0]< =''Z'')
{
/ *计算alpha []中的输入数量* /
}
/ *获取下一个标记:* /
token = strtok(NULL," " );
}
}
}
}
/ *得到发生指定的密钥* /
int GetKeyCount(字符密钥)
{
int index;
if(key> =''0''&& key< =''9'')
{
?.. / *如果(键> =''A''&& key<返回数字输入键的出现数字[] * /
}
else =''Z'')
{
?.. / *返回alpha输入键中出现的alpha [] * /
}
其他
返回0; / *返回0,如果没有定义键* /
}
void main()
{
char查询[32]; / *存储用户输入的查询字符串* /
char * token;
ReadData(" db.txt"); / *从输入设置密钥发生数据库
文件* /
printf("请输入3个密钥(以空格分隔):) ;
while(获取(查询)!= NULL)
{
token = strtok(查询, ; / *从一个空格分隔的行
逐个获取密钥 - 例如1 2 A * /
while(令牌!= NULL)
{
/ *虽然在string中有令牌* /
/ *打印出来自密钥令牌[0]和函数的结果
GetKeyCount()* /
?? ..
/ *下一个令牌:* /
??。
}
printf(请输入3个键(以空格分隔):);
}
}
Hi all,
Do anyone know how to write a C program to count the frequency of a
certain key pattern in a given datafile?
Input:
? Three alpha-numeric keys separated by space
? A data file contains M by 3 matrix of alpha-numeric keys. Where M is
number of rows and 3 is number of columns.
Output:
? Number of occurrences of the input keys
Example:
Input file db.txt and its content:
4 7 9
2 1 G
5 3 4
A 3 E
1 A 3
F 5 6
X 0 4
Sample run #1
Please enter 3 keys (separated by space):7 A 4
The key ?7 ? has 1 occurrence in the database
The key ?A? has 2 occurrences in the database
The key ?4? has 3 occurrences in the database
Sample run #2
Please enter 3 keys (separated by space): A B C
The key ?A? has 1 occurrence in the database
The key ?B? has zero occurrence in the database
The key ?C? has zero occurrence in the database
The following is the hints that I have. However, I do not know how I can
complete the program. Your help will be appreciated.
***************************
#include <stdio.h>
#include <string.h>
int numeric[10];
int alpha[26];
void ReadData(char* input_file)
{
FILE* fptr;
char buffer[128];
char* token;
int index;
fptr = fopen(input_file, "r");
if (fptr != NULL)
{
while (fgets(buffer, sizeof(buffer), fptr) != NULL)
{
token = strtok(buffer, " ");
while ( token != NULL )
{
if (token[0] >= ''0'' && token[0] <= ''9'')
{
/* calculate number of input in numeric[] */
}
else if (token[0] >= ''A'' && token[0] <= ''Z'')
{
/* calculate number of input in alpha[] */
}
/* Get next token: */
token = strtok( NULL, " " );
}
}
}
}
/* get the occurrence of the specified key */
int GetKeyCount(char key)
{
int index;
if (key >= ''0'' && key <= ''9'')
{
?.. /* return the occurrence of the numeric input key in numeric[] */
}
else if (key >= ''A'' && key <= ''Z'')
{
?.. /* return the occurrence of the alpha input key in alpha[] */
}
else
return 0; /* return 0, if the key is not defined */
}
void main()
{
char enquiry[32]; /* store the enquiry string input by user */
char* token;
ReadData("db.txt"); /* setup the key occurrence database from the input
file */
printf("Please enter 3 keys (separated by space): ");
while (gets(enquiry) != NULL)
{
token = strtok(enquiry, " "); /* get the key one by one from the line
delimited by a space - e.g. "1 2 A" */
while (token != NULL )
{
/* While there are tokens in "string" */
/* print out the result from the key token[0] and the function
GetKeyCount() */
??..
/* Get next token: */
??.
}
printf("Please enter 3 keys (separated by space): ");
}
}
推荐答案
" rccf6178" < RC ****** @ yahoo.com.hk>在消息中写道
news:7a ****************************** @ localhost.ta lkaboutprogramming。 com ...
"rccf6178" <rc******@yahoo.com.hk> wrote in message
news:7a******************************@localhost.ta lkaboutprogramming.com...
大家好,
有谁知道如何编写C程序来计算给定数据文件中某些关键模式的频率?
Hi all,
Do anyone know how to write a C program to count the frequency of a
certain key pattern in a given datafile?
做自己的作业......
Make your own homework...
"蒲公英" <哒******* @ meadow.net>写在
新闻:41 *********************** @ dreader9.news.xs4al l.nl:
"dandelion" <da*******@meadow.net> wrote in
news:41***********************@dreader9.news.xs4al l.nl:
有谁知道如何编写C程序来计算给定数据文件中某些关键模式的频率?
Do anyone know how to write a C program to count the frequency of a
certain key pattern in a given datafile?
>
做自己的作业...
Make your own homework...
^^^^
做
- -
- 马克 - >
-
^^^^
Do
--
- Mark ->
--
"马克A.奥德尔 < OD ******* @ hotmail.com>在消息中写道
news:Xn ******************************** @ 130.133.1 。 4 ...
"Mark A. Odell" <od*******@hotmail.com> wrote in message
news:Xn********************************@130.133.1. 4...
"蒲公英" <哒******* @ meadow.net>在
新闻中写道:41 *********************** @ dreader9.news.xs4al l.nl:
"dandelion" <da*******@meadow.net> wrote in
news:41***********************@dreader9.news.xs4al l.nl:
有谁知道如何编写C程序来计算给定数据文件中某些关键模式的频率?
自己做作业......
Do anyone know how to write a C program to count the frequency of a
certain key pattern in a given datafile?
Make your own homework...
^^^^
做
^^^^
Do
当你的荷兰语/德语/法语/拉丁语和我的一样好时,它会'是时候开始了
纠正我。
-
- 马克 - >
-
When your dutch/german/french/latin is as good as mine, it''s time to start
correcting me.
--
- Mark ->
--
这篇关于计算频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!