用户输入字符串后,程序不计算函数
Program is not calculating functions after user enters string
我的程序遇到了一个问题。这里的程序应该接受一个指向C字符串的指针作为参数,并计算字符串中包含的单词数以及字符串中的字母数。这两个值都应该传递回主函数,但不使用全局变量。在这个函数之后,我应该编写另一个函数,它接受字母数和单词数,并将每个单词的平均字母数(或平均单词大小)发送回主函数。我写的函数应该完成上述所有功能,并且从计数中排除标点符号和空格。不过,我遇到的问题是,当我以用户身份输入字符串并按enter键时,不会发生计算。我可以一直按回车键,直到没有尽头,但我不明白为什么会发生这种情况。任何见解都将不胜感激,我是指针和C字符串的新手。
这是我的代码:
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
void Count_All(char*, int&, double&, int&); // Function prototype.
double Calc_Average (char*, int, int, double); // Function prototype.
int main()
{
const int size = 500;
char userString[size];
int Word = 0;
int Pun = 0;
double Total_Characters = 0;
double Average = 0.0;
cout << "Please enter a string of 500 or less characters: ";
cin.getline(userString, size);
cout << "n";
Count_All (userString, Word, Total_Characters, Pun);
cout << "Number of words in the string: " << Word << "n";
Average = Calc_Average (userString, Word, Pun, Total_Characters);
cout <<"nAverage number of letters per word: "<< fixed <<
showpoint << setprecision(2) << Average << "n" << endl;
cin.ignore(1);
return 0;
}
void Count_All (char*strptr, int &Word, double &Total_Characters, int &Pun) // Counts all characters and types.
{
int index = 0;
while (*strptr != ' ')
{
if ((isspace(*strptr)) || (ispunct(*strptr)))
{
while ((isspace(*strptr)) || (ispunct(*strptr)))
{
index++;
}
}
if ((isalnum(*strptr)) || (ispunct(*strptr)))
{
Word++;
while ((isalnum(*strptr))||(ispunct(*strptr)))
{
index++;
Total_Characters++; // Counting the total printable characters (including digits and punctuation).
if((ispunct(*strptr)))
{
Pun++; // Counting punctuation.
}
}
}
index++;
}
}
double Calc_Average(char*strptr, int Word, int Pun, double Total_Characters) // Calculates the average number of characters per words.
{
double Average = 0.0;
Total_Characters = Total_Characters - Pun; // Subtracting punctuation from all of the characters in the string (not including spaces).
Average = (Total_Characters / Word);
return Average;
}
您从未推进过strptr
,因此所有的比较都将永远针对相同的char
和Count_All
循环。
您应该将index++
的所有实例替换为strptr++
。这将导致strptr
向下移动字符串并查看每个字符,而不是一遍又一遍地查看同一个字符。完成此操作后,index
未被使用,可以删除。
此外,您的最后一个strptr++
应该在它上面的if
块内,以避免意外地越过字符串的末尾而进入未知。
更好的方法是使用std::string
,并消除使用原始char[]
s时出现的所有这些问题。
Count_All
函数实现中有双无限循环。
我已经修正并简化了你的两个函数。没有巨大的变化对我来说很困难。为什么total
具有double
类型?字符数如何可以是非整数?
void Count_All(const char* strptr, int& word, int& total, int& pun)
{
bool last_isalnum = false;
for (word = total = pun = 0; *strptr != ' '; ++strptr) {
bool cur_isalnum = isalnum(*strptr) != 0;
bool cur_ispunct = ispunct(*strptr) != 0;
if (last_isalnum != cur_isalnum)
++word;
if (cur_ispunct)
++pun;
if (cur_ispunct || cur_isalnum)
++total;
last_isalnum = cur_isalnum;
}
}
double Calc_Average(int word, int pun, int total)
{
return static_cast<double>(total - pun) / word;
}
首先,这里似乎有无限循环
while ((isspace(*strptr)) || (ispunct(*strptr))) (line 42)
这里是
while ((isalnum(*strptr)) || (ispunct(*strptr))) (line 51)
您总是在检查同一位置的内容,而不进行任何指针运算来推进您正在检查的内存位置。
相关文章:
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何计算函数的时间复杂度?
- 有没有办法计算函数内arry的长度而不是作为参数传入?
- 如何计算函数的搜索复杂度
- 使用引用参数计算函数
- 递归计算函数f = n!/(c^n)的值
- 为什么这个C++字符串长度计算函数比另一个更快
- 用递归计算函数f(n)
- C++平均计算函数返回0
- 有没有比生成/累积更快的方法来计算函数结果的平均值
- 如何计算函数的时间
- 由于标识符未知,LLDB 无法计算函数
- 从 C++11 中的列表中选择并计算函数
- 计算后缀表达式.pop() 返回不正确的值来计算函数
- 在计算函数返回值之前或之后是否销毁了局部变量
- 计算函数指针的类型
- 计算函数中的模给出了与直接使用%运算符不同的答案
- 提升:以秒/毫/微米/纳米为单位计算函数的运行时间
- 行列式计算函数的优化
- 如何在c++中计算函数的执行时间和cpu消耗