R
rccf6178
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): ");
}
}
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): ");
}
}