- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我应该“修复”给我的代码,以使其在文件中显示正确数量的可见字符(也包括空格)。正确的数字应该是 977。我以前从未处理过文件,我不明白我需要做什么才能显示正确的数字。
* Driver Menu System for Homework
* Andrew Potter - Mar 5, 2019 <-- Please put your name/date here
*/
#include <stdio.h>//header file for input/output -
#include <stdlib.h>
#include <ctype.h>
// since you will place all your assigned functions (programs) in this file, you do not need to include stdio.h again!
int menu(void); //prototype definition section
void hello(void);
void countall(void);
int main(void)
{
int selection = menu();
while(selection != 99) {
switch(selection) {
case 1:
hello();
break;
case 2:
countall();
break;
case 3:
break;
case 4:
break;
default:
printf("Please enter a valid selection.\n");
}
selection = menu();
}
return 0;
}
int menu(void) {
int choice;
printf("***************************\n");
printf(" 1. Hello \n");
printf(" 2. Countall\n");
printf(" 3. \n");
printf(" 4. \n");
printf("99. Exit\n");
printf("Please select number and press enter:\n");
printf("***************************\n");
scanf("%d", &choice);
getchar();
return choice;
}
void hello(void) {
printf("Hello, World!!!\n");
}
//*****Andrew 5/1/19*****
#define SLEN 81 /* from reverse.c */
/* original header: int count(argc, *argv[]) */
void countall(void)
{
int ch; // place to store each character as read
FILE *fp; // "file pointer"
long unsigned count = 0;
char file[SLEN]; /* from reverse.c */
/*Checks whether a file name was included when run from the command prompt
* The argument count includes the program file name. A count of 2 indicates
* that an additional parameter was passed
if (argc != 2)
{
printf("Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
* The following uses the second parameter as the file name
* and attempts to open the file
if ((fp = fopen(argv[1], "r")) == NULL)
{
printf("Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
} */
/*************************************
Code from reverse.c included to make the program work from within our IDE
*************************************/
puts("Enter the name of the file to be processed:");
scanf("%s", file);
if ((fp = fopen(file,"rb")) == NULL) /* read mode */
{
printf("count program can't open %s\n", file);
exit(EXIT_FAILURE);
}
/* EOF reached when C realizes it tried to reach beyond the end of the file! */
/* This is good design - see page 573 */
while ((ch = getc(fp)) != EOF)
{
if (isprint(ch)) {
count++;
}
else if (isprint(ch)) {
count++;
}
putc(ch,stdout); // same as putchar(ch);
count++;
}
fclose(fp);
printf("\nFile %s has %lu characters\n", file, count);
}
我预计我会使用 isprint 和 isspace 的组合得到正确数量的可见字符,但我通常得到 2086。赋值说明是:“Word 识别 977 个字符,包括空格。您当前的 countall() 认为有 1043 个。对您的代码进行必要的更正以仅计算可见字符和空格!(提示:查看教科书中的 567。) "在我编辑任何代码之前,计数是 1043,现在我得到 2020。我需要 977。
最佳答案
isprint()
返回一个 bool 结果 - 如果字符不是“可打印的”则为零,如果是则为非零。因此 isprint(ch) != '\n'
没有意义。你在问题中的完整表达更没有意义,但我会在最后继续讨论。
isprint()
本身为所有可打印字符返回 true(非零),因此您不需要其他测试。此外,您在每个条件 block 中无条件地递增 count
,因此您对每个字符进行计数,并且对某些字符进行两次计数。
你只需要:
if( isprint(ch) )
{
count++;
}
putc( ch, stdout ) ;
虽然您的代码显然是一个不完整的片段,但不清楚您在哪里或如何阅读 ch
。您需要一个 getc()
或其中的等效项。
while( (ch = getc(fp)) != EOF )
{
if( isprint(ch) )
{
count++;
}
putc( ch, stdout ) ;
}
目前尚不清楚您是否需要计算所有空格(包括空格、制表符和换行符)或如您所述仅计算“空格”。如果是这样,请明确 isprint()
将匹配空格,但不匹配控制字符换行符或制表符。 isspace()
匹配所有这些,但不应单独计入 isprint()
,因为“空格”在空白和可打印集中。如果要计算换行符和制表符(不太可能;“垂直制表符”),则:
while( (ch = getc(fp)) != EOF )
{
if( isprint(ch) || isspace(ch) )
{
count++;
}
putc( ch, stdout ) ;
}
您似乎误解了 C 的另一个方面是 bool 表达式的工作原理。要测试单个变量的多个值,您必须编写:
if( var == x || var == y || var == z )
你写了:
if( var == x || y || z )
当你大声朗读它时,这在英语(或其他自然语言)中可能有意义,但在 C 中它意味着:
if( var == (x || y || z ) )
评估 (x || y || z )
为 true
或 false
并将其与 var
进行比较>.
可能值得考虑现有解决方案的语义,以说明为什么它实际上可以编译,但会产生错误的结果。
首先,
isprint(ch) != '\n' || '\t' || '\0'
等同于 isprint(ch) != true
,原因如前所述。因此,您为所有 不可可打印的字符递增计数器。
然后在这里:
isspace(ch) == NULL
NULL
是表示无效指针的宏,isspace()
不返回指针。但是 NULL
将隐式转换为零(或 false)。因此,在这里您为 所有 不是空格的可打印字符递增计数器。
最后,您无条件地计算这里的每个字符:
putc(ch,stdout); // same as putchar(ch);
count++;
所以你的结果将是:
number-of-non-printing-characters +
number-of-printing-characters - number-of-spaces +
total-number-of-characters
这是我认为的 (2 x file-length) - number-of-spaces
最后请注意,如果您以“二进制”模式打开具有 CR+LF 行尾的文本文件(Windows 上文本文件的常规方式),isspace()
将为每个新的字符计数两个字符-线。请务必以“文本”模式打开(无论平台如何)。
关于c - “如何让我的程序从文件中读取可见字符和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55996853/
我有这个代码来查找这个模式:201409250200131738007947036000 - 1,在文本内 final String patternStr = "(\\d{
我正在尝试使用正则表达式清除一些用户输入,以删除 [ 和 ] 并删除任何大于 1 个空格的空格。但我似乎无法实现我想要的效果。这是我第一次使用正则表达式,所以我对如何写出来有点困惑。 (preg_re
我正在尝试构建这个简单的正则表达式来匹配 Java 中的单词+空格,但我在尝试解决它时感到困惑。该网站上有很多类似的示例,但答案大多给出了正则表达式本身,而没有解释它是如何构造的。 我正在寻找的是形成
好吧,我已经阅读了很多建议如何消除多余空间的帖子,但无论出于何种原因,我似乎无法将这些建议应用到我的系统中,所以我在这里寻求您的帮助。 这些是我代码的最后几行: for line in rli
所以我正在我的测试存储上学习网页抓取,但我不确定如何正确地从“sizes”数组中删除空的新行。 const $ = cheerio.load(body) $('div.lis
这个问题已经有答案了: How to prevent invalid characters from being typed into input fields (8 个回答) 已关闭 9 年前。 是
有人知道如何让扫描仪忽略空间吗?我想输入名字和第二个名字,但扫描仪不让我输入,我想保存全名 String name; System.out.print("Enter name: "); name =
这个问题在这里已经有了答案: Make Vim show ALL white spaces as a character (23 个回答) 关闭 8 年前。 VIM(使用 Solarized Dar
我想使用 StreamTokenizer 从 java 文件中提取名称。我已将空格设置为逗号 inputTokenizer.whitespaceChars(',', ','); 但是,
我正在使用此代码逐行读取 txt 文件。 // Open the file that is the first command line parameter FileInputStream fstre
我似乎无法弄清楚我需要的正则表达式。这就是我想要实现的目标: {ANY CHAR} + @javax.persistence.Column(name = "{ANY 30 CHARS}") + {AN
我正在运行 StyleCop(顺便说一句,如果你想提供高质量的代码,我完全推荐它)... 我有这条线 [System.Xml.Serialization.XmlRootAttribute(Namesp
我刚刚更新到 PhpStorm 2016,我突然注意到,每次我按 Ctrl + S 保存文件时,它都会删除我在测试这段代码后按下以继续编写的空格/制表符。 请帮忙,这对我来说很烦人,因为我在每一行代码
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我已经看过几十个关于这个主题的问题和答案,但我仍然无法解决我的问题。 我在我的代码中使用了一个外部 ffmpeg 转换器,我将文件路径作为参数传递,如下所示: OutputPackage oo = c
谁能详细解释一下它们是什么以及它们之间的区别。提前致谢。 最佳答案 转义序列是代表其他内容的字符序列。例如(“\n” = 新行,“\?” = 问号等)。有关更详细的列表,请检查:https://en.
我无法从我的 javascript 文本中删除换行符。这是我正在处理的数据示例: 0: "Christian Pulisic" 1: "↵" 2: "From Wikipedia, the free
我有一个问题 - 我似乎无法从字符串的开头/结尾删除新行/空格。我在正则表达式的开头和结尾使用 \s ,甚至在获取字符串后使用 .trim() ,但无济于事。 public void extractI
我是 php 的新手,我正在尝试将一系列变量添加到 html 超链接中。但是,任何返回空格的变量都会弄乱超链接。 Grants Test
我是一名优秀的程序员,十分优秀!