- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是C语言的新手,我无法编写一个C程序,该程序通过命令行参数获取可变数量的文件,并按(ASCII)字母顺序对单词进行排序,仅显示唯一的单词,但包含频率。我设法通过用户输入按字母顺序对单词进行了排序,但是我不知道如何正确编写代码以接受文件输入,而且我也不知道如何只用频率将每个唯一单词打印一次。
这是到目前为止我得到的,它使用标准输入而不是文件,并且缺少频率计数:
#include <stdio.h>
#include <string.h>
int main(void) {
char a[2048][2048];
int i = 0,
j = 0,
k = 0,
n;
while(i < 2048 && fgets(a[i], 2048, stdin) != NULL)
{
n = strlen(a[i]);
if(n > 0 && a[i][n-1] == '\n')
a[i][n -1] = '\0';
i++;
}
for(j = 0; j < i; j++)
{
char max[2048];
strcpy (max,a[j]);
for(k = j + 1; k < i; k++)
{
if(strcmp(a[k], max) < 0)
{
char temp[2048];
strcpy(temp, a[k]);
strcpy(a[k], max);
strcpy(max, temp);
}
}
strcpy(a[j],max);
}
for( j = 0; j < i; j++){
printf("%s\n", a[j]);
}
return 0;
}
最佳答案
为了将文件中的单词读入仅包含唯一单词的数组,同时跟踪每次看到单词出现的次数,可以通过两种方法来完成。一种简单而直接的方法是保留2个单独的数组。第一个是2D字符数组,其大小足以容纳预期的单词数,第二个是数字数组(unsigned int
或size_t
),该数组包含每个单词在与存储该单词相同的索引处看到的次数字符数组。
从文件中读取单词时,唯一的挑战是确定一个单词之前是否曾被看到过,如果没有,则将新单词以给定的seen
添加到index
字符数组中,然后在该索引处更新频率数组freq
以反映这个词已经被看到1
时间(例如freq[index]++;
)。
如果在检查seen
中的单词列表时,发现当前单词已经出现在索引X
中,那么您跳过将单词添加到seen
中,而只是更新freq[X]++;
。
下面是一个简短的示例。尝试一下,如果您有任何疑问,请告诉我:
#include <stdio.h>
#include <string.h>
#define MAXW 100
#define MAXC 32
int main (int argc, char **argv) {
/* initialize variables & open file or stdin for reading */
char seen[MAXW][MAXC] = {{ 0 }};
char word[MAXC] = {0};
size_t freq[MAXW] = {0};
size_t i, idx = 0;
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) {
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
/* seen 1st word into 'seen' array, update index 'idx' */
if (fscanf (fp, " %32[^ ,.\t\n]%*c", word) == 1) {
strcpy (seen[idx], word);
freq[idx]++;
idx++;
}
else {
fprintf (stderr, "error: file read error.\n");
return 1;
}
/* read each word in file */
while (fscanf (fp, " %32[^ ,.\t\n]%*c", word) == 1) {
/* check against all words in seen */
for (i = 0; i < idx; i++) {
/* if word already in 'seen', update 'freq' count */
if (strcmp (seen[i], word) == 0) {
freq[i]++;
goto skipdup; /* skip adding word to 'seen' */
}
} /* add word to 'seen', update freq & 'idx' */
strcpy (seen[idx], word);
freq[idx]++;
idx++;
skipdup:
if (idx == MAXW) { /* check 'idx' against MAXW */
fprintf (stderr, "warning: MAXW words exceeded.\n");
break;
}
}
if (fp != stdin) fclose (fp);
printf ("\nthe occurrence of words are:\n\n");
for (i = 0; i < idx; i++)
printf (" %-28s : %zu\n", seen[i], freq[i]);
return 0;
}
gcc -Wall -Wextra -O3 -o bin/file_words_occur file_words_occur.c
$ cat dat/words.txt
the quick brown fox jumps over the lazy dog. the fox jumps over the dog to avoid the squirrel.
$ ./bin/file_words_occur <dat/words.txt
the occurrence of words are:
the : 8
quick : 1
brown : 1
fox : 2
jumps : 2
over : 2
lazy : 1
dog : 2
to : 1
avoid : 1
squirrel : 2
was : 1
in : 1
path : 1
of : 1
captain : 1
jack : 1
sparrow : 1
a : 1
pirate : 1
so : 1
brave : 1
on : 1
seven : 1
seas : 1
28
chars long(反分裂主义)。它需要为nol终止字符提供空间,以用于总共
29
字符。选择
MAXC
的
32
应该可以容纳所有普通单词。
stdin
读取的代码功能,就可以使用现有代码来处理多个文件。您需要做的只是
cat file1 file2 file3 | ./prog_name
。更新代码以将多个文件作为参数来处理也不难。 (您可以只用
for (j = 1, j < argc, j++)
包装现有主体,然后打开/关闭提供的每个文件名。(还需要对
fp
声明进行其他一些细微调整)
for
循环中,所以在这种情况下我们可以不用任何功能了,但是学习的地方在哪里?)
seen
数组和
freq
数组之间的关系,因此在排序之后,使用正确的单词出现的次数正确。您不能独立地对数组进行排序并保持这种关系。但是,如果我们将单词和该单词的出现位置都放入一个结构中,则可以按该单词对结构数组进行排序,并且正确的出现次数仍与正确的单词相关联。例如像下面这样的东西会起作用:
typedef struct {
char seen[MAXC];
size_t freq;
} wfstruct;
wfstruct
只是单词频率结构的半描述性名称,可以是任何对您有意义的名称)
wfstruct words[MAXW];
qsort
是你的朋友。
qsort
将对所有内容进行排序,只要您可以传递
qsort
(1)数组,(2)排序多少个元素,(3)元素的大小,以及(4)使用
const void
指针的比较函数它将比较的元素。这总是给新的C程序员一个合适的选择,因为您必须弄清楚(a)如何将指针数组作为指针传递,以及(b)如何处理从指针中获取所需数据在功能上进行比较。
qsort
的比较函数的声明为:
int compare (const void *a, const void *b);
seen
数组的每个元素中的单词
wfstruct
对结构数组进行排序。您知道
seen
将是一个简单的字符串,因此您可以使用
strcmp
进行排序。
seen
(和
const void *a
)中提取
*b
字符串,以便可以将其提供给
strcmp
?”在这里,您知道
const void *a
必须代表要排序的基本元素
struct wfstruct
。因此,您知道
const void *a
是
wfstruct
的指针。由于它将是一个指针,因此您必须使用
->
运算符来取消引用该结构的
seen
成员。 (例如
seen
成员的访问权限为
mystruct->seen
。
struct wfstruct
函数中声明
compare
类型的指针,并将
a
的大小写类型为
(wfstruct *)
。例:
wfstruct *ap = (wfstruct *)a;
pointer to struct wfstruct
(或者简单地说是
pointer to wfstruct
,因为我们在其声明中包括了
typedef
的
wfstruct
)。您对
b
执行相同的操作,现在可以将
ap->seen
和
bp->seen
传递给
strcmp
并对结构数组进行排序:
int compare (const void *a, const void *b)
{
wfstruct *ap = (wfstruct *)a;
wfstruct *bp = (wfstruct *)b;
return (strcmp (ap->seen, bp->seen));
}
qsort
的调用无非是:
/* sort words alphabetically */
qsort (words, idx, sizeof *words, compare);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXW 100
#define MAXC 32
typedef struct {
char seen[MAXC];
size_t freq;
} wfstruct;
int get_word_freq (wfstruct *words, size_t *idx, FILE *fp);
int compare (const void *a, const void *b);
int main (int argc, char **argv) {
/* initialize variables & open file or stdin for seening */
wfstruct words[MAXW] = {{{ 0 }, 0}};
size_t i, idx = 0;
FILE *fp = NULL;
if (argc < 2) { /* read from stdin */
get_word_freq (words, &idx, stdin);
}
else {
/* read each file given on command line */
for (i = 1; i < (size_t)argc; i++)
{ /* open file for reading */
if (!(fp = fopen (argv[i], "r"))) {
fprintf (stderr, "error: file open failed '%s'.\n",
argv[i]);
continue;
}
/* check 'idx' against MAXW */
if (idx == MAXW) break;
get_word_freq (words, &idx, fp);
}
}
/* sort words alphabetically */
qsort (words, idx, sizeof *words, compare);
printf ("\nthe occurrence of words are:\n\n");
for (i = 0; i < idx; i++)
printf (" %-28s : %zu\n", words[i].seen, words[i].freq);
return 0;
}
int get_word_freq (wfstruct *words, size_t *idx, FILE *fp)
{
char word[MAXC] = {0};
size_t i;
/* read 1st word into array, update index 'idx' */
if (*idx == 0) {
if (fscanf (fp, " %32[^ ,.\t\n]%*c", word) == 1) {
strcpy (words[*idx].seen, word);
words[*idx].freq++;
(*idx)++;
}
else {
fprintf (stderr, "error: file read error.\n");
return 1;
}
}
/* read each word in file */
while (fscanf (fp, " %32[^ ,.\t\n]%*c", word) == 1) {
/* check against all words in struct */
for (i = 0; i < *idx; i++) {
/* if word already 'seen', update 'words[i]. freq' count */
if (strcmp (words[i].seen, word) == 0) {
words[i].freq++;
goto skipdup; /* skip adding word to 'words[i].seen' */
}
} /* add to 'words[*idx].seen', update words[*idx].freq & '*idx' */
strcpy (words[*idx].seen, word);
words[*idx].freq++;
(*idx)++;
skipdup:
if (*idx == MAXW) { /* check 'idx' against MAXW */
fprintf (stderr, "warning: MAXW words exceeded.\n");
break;
}
}
fclose (fp);
return 0;
}
/* qsort compare funciton */
int compare (const void *a, const void *b)
{
wfstruct *ap = (wfstruct *)a;
wfstruct *bp = (wfstruct *)b;
return (strcmp (ap->seen, bp->seen));
}
$ ./bin/file_words_occur_multi dat/words.txt dat/words.txt
the occurrence of words are:
a : 2
avoid : 2
brave : 2
brown : 2
captain : 2
dog : 4
fox : 4
in : 2
jack : 2
jumps : 4
lazy : 2
of : 2
on : 2
over : 4
path : 2
pirate : 2
quick : 2
seas : 2
seven : 2
so : 2
sparrow : 2
squirrel : 4
the : 16
to : 2
was : 2
idx
)
main
中,或者(2)传递指向索引的指针并直接更新其值在功能上。上面的示例传递了一个指针。由于解引用和正确使用指针值所需的其他语法对于C语言新手来说可能是个挑战,因此这里有一个示例,将
idx
作为简单变量传递,并在
main
中跟踪总数。
idx
作为常规变量传递并在函数中使用变量的副本,或者是否将
idx
作为指针传递并在值直接在函数中)
get_word_freq
的简单更改,然后是
main
所需的更改(注意:选择
size_t
作为类型而不是
int
,因为数组索引永远不能为负):
size_t get_word_freq (wfstruct *words, size_t idx, FILE *fp)
{
char word[MAXC] = {0};
size_t i;
/* read 1st word into array, update index 'idx' */
if (idx == 0) {
if (fscanf (fp, " %32[^ ,.\t\n]%*c", word) == 1) {
strcpy (words[idx].seen, word);
words[idx].freq++;
idx++;
}
else {
fprintf (stderr, "error: file read error.\n");
return idx;
}
}
/* read each word in file */
while (fscanf (fp, " %32[^ ,.\t\n]%*c", word) == 1) {
/* check against all words in struct */
for (i = 0; i < idx; i++) {
/* if word already 'seen', update 'words[i]. freq' count */
if (strcmp (words[i].seen, word) == 0) {
words[i].freq++;
goto skipdup; /* skip adding word to 'words[i].seen' */
}
} /* add to 'words[*idx].seen', update words[*idx].freq & '*idx' */
strcpy (words[idx].seen, word);
words[idx].freq++;
idx++;
skipdup:
if (idx == MAXW) { /* check 'idx' against MAXW */
fprintf (stderr, "warning: MAXW words exceeded.\n");
break;
}
}
fclose (fp);
return idx;
}
main
中所需的更改:
...
if (argc < 2) { /* read from stdin */
idx = get_word_freq (words, idx, stdin);
}
else {
/* read each file given on command line */
for (i = 1; i < (size_t)argc; i++)
{ /* open file for reading */
...
/* check 'idx' against MAXW */
if ((idx = get_word_freq (words, idx, fp)) == MAXW)
break;
}
}
...
关于c - C:如何按频率编号和字母顺序从可变数量的文件中对单词进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34060602/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!