if(argc > 3) { access_type = tolower(argv[3][0]); if (argv[3][1] == '*') items_count = strtoul(argv[3]+2, 0, 0);详细分析
时间: 2024-06-09 11:06:56 浏览: 1
这段代码主要是用来解析命令行参数的。假设程序运行时有三个参数,那么 `argc` 的值应该为 4。第三个参数为一个字符,用来指定访问类型,可以是大写或小写字母。这里使用了 `tolower()` 函数将字符转换为小写字母。如果第三个参数的第二个字符是星号 `*`,那么剩下的字符串将被解析为无符号长整型数,表示要访问的元素数量。这里使用了 `strtoul()` 函数将字符串转换成无符号长整型数。最后,将解析结果保存在 `access_type` 和 `items_count` 变量中供后续使用。注意,这段代码没有做参数个数或格式的错误检查,因此在使用时需要保证参数的正确性。
相关问题
#pragma GCC optimize ("O3") #pragma pack (16)//所有的存储都是以16个字节为单位的 #include <stdio.h> #include <stdlib.h> #include <string.h> #define tolower(c) (c>='A'&&c<='Z')?c-'A'+'a':c #define DATA 5200000 #define SIZE 1000005 int trie[4200000][26]; typedef struct node { int cnt; int logo; struct node *child[26]; } Node; Node *root; char str[35000000]; typedef struct word { char wor[85]; int cnt; } Word; Word w[300000]; struct node *creat() { Node *Root = (Node *)malloc(sizeof(Node)); Root->logo = 0; Root->cnt = 0; for (int i = 0; i < 26; i++) { Root->child[i] = NULL; } return Root; } void insert(Node *root, char *word, int flag) { struct node *leaf = root; for (int i = 0; word[i] != '\0'; i++) { int index = word[i] - 'a'; if (!leaf->child[index]) { leaf->child[index] = creat(); } leaf = leaf->child[index]; } if (leaf->logo != -1) leaf->logo = flag; leaf->cnt++; } int count = 0; void dfs(Node *leaf, char *word, int level) { if (leaf->logo == 1) { word[level] = '\0'; strcpy(w[count++].wor, word); w[count - 1].cnt = leaf->cnt; } for (int i = 0; i < 26; i++) { if (leaf->child[i]) { word[level] = i + 'a'; dfs(leaf->child[i], word, level + 1); } } } int cmp(const void *p1, const void *p2) { Word *v1, *v2; v1 = (Word *)p1; v2 = (Word *)p2; if (v1->cnt != v2->cnt) return v2->cnt - v1->cnt; else return strcmp(v1->wor, v2->wor); } int main(int argc, char *argv[]) { char s[1024]; int temp; int n, m;//读入n,m; //n = atoi(argv[1]); //m = atoi(argv[2]); scanf("%d%d", &n, &m); //读入stopwords中的元素,并令末序数组值为0,即该单词不计入 root = creat(); FILE *stopwords = fopen("stopwords.txt", "r"); while (fscanf(stopwords, "%s", s) != EOF) { insert(root, s, -1); } int cnt; FILE *article = fopen("article.txt", "r"); cnt = fread(str, sizeof(char), 35000000, article); char word[85]; int w_cnt = 0; for (int i = 0; i < cnt; i++) { char c = tolower(str[i]); if (c >= 'a' && c <= 'z') { word[w_cnt++] = c; } else { word[w_cnt] = '\0'; p = 0; w_cnt = 0; if (strlen(word) > 0) { insert(root, word, 1); } }//对article中的所有单词进行计数 } dfs(root, word, 0); qsort(w, count, sizeof(w[0]), cmp); printf("%s", w[0].cnt); return 0; }
这段代码是一个基于Trie树的单词统计程序,在给定的文章中统计出现频率最高的单词。程序从文件中读入stopwords(不计入统计的常用单词)和article(待统计的文章),使用Trie树统计文章中出现的单词频率,并按照出现频率从高到低输出结果。
具体实现中,程序使用了一个结构体Node表示Trie树中的节点,其中包含了该节点出现次数cnt和单词编号logo。同时,程序定义了一个结构体Word表示单词及其出现次数。程序中的insert函数用于向Trie树中插入单词,dfs函数用于遍历Trie树并统计单词出现次数,cmp函数用于按照出现频率从高到低排序输出结果。
注意,程序中的一些细节需要注意,比如将所有字母转换为小写字母,以及对于长度为0的单词不进行计数等。
写一函数将字符串转成全小写。要求不能使用库函数。 //函数原型(声明)参考:void tolower(char s[]); //提示:字符串以'\0'字符作为结束标志。 void tolower(char s[]); int _tmain(int argc, _TCHAR* argv[]) { char s[80]; printf("Please input a string:"); scanf("%s",s); tolower(s); printf("%s",s); return 0; } void tolower(char s[]) { char *p=s; while( (1) ){ if (*p>='A'&&*p<='Z') (2) ; (3) ; } }
下面是一个将字符串转成全小写的函数实现:
```c
void tolower(char s[]) {
char *p = s;
while (*p != '\0') {
if (*p >= 'A' && *p <= 'Z') {
*p = *p + 32; // 将大写字母转成小写字母
}
p++;
}
}
```
在这个函数中,我们使用了指针 `p` 遍历字符串 `s` 中的每一个字符。对于每一个大写字母,我们将其 ASCII 码值加上 32,即将其转成对应的小写字母。最后,字符串 `s` 中的所有大写字母都变成了小写字母。