【oop bug记录1】

题目要求:

编写程序, 统计输入的单词总数以及每个单词的出现次数. 要求:

  1. 用函数组织程序, 如: 从输入流读取多个单词的函数, 统计单词出现次数的函数, 输出统计结果的函数, 等等. 注意: 使用常量引用、非常量引用、普通类型做函数形参.

  2. 输出单词及出现次数时, 按照最长单词对齐输出.

  3. 未输入任何单词时, 函数应抛出异常, 诊断程序应输出相应提示语句.

  4. 英文单词大小写无关. 简单起见, 假设输入单词均来自英文字典.
    示例1
    示例2

代码

#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::domain_error;
using std::endl;
using std::istream;
using std::string;
using std::vector;

typedef struct
{
    string word = "";
    int num = 1;
} dic;


// 首字母大小写无关的比较相等函数
bool equal(const string &s1, const string &s2)
{
    string s3 = s1, s4 = s2;
    if (s1[0] >= 'A' && s1[0] <= 'Z')
    {
        s3[0] = s3[0] - 'A' + 'a';
    }
    if (s2[0] >= 'A' && s2[0] <= 'Z')
    {
        s4[0] = s4[0] - 'A' + 'a';
    }
    return s3 == s4;
}

bool find(const string &s, vector<dic> &words)
{
    for (int i = 0; i < words.size(); i++)
    {
        if (equal(s, words[i].word))
        {
            words[i].num++;
            return true;
        }
    }
    return false;
}

// 输入
istream &read_words(istream &in, vector<dic> &words)
{
    if (in)
    {
        words.clear();
        dic temp;
        while (in >> temp.word)
        {
            if (!find(temp.word, words))
            {
                words.push_back(temp);
            }
        }
        in.clear();
    }

    return in;

}

// 计算字符串最大长度
string::size_type maxlen(const vector<dic> &words)
{
    string::size_type max = 0, temp;
    int n = words.size();
    for (int i = 0; i < n; i++)
    {
        temp = words[i].word.length();
        if (max < temp)
        {
            max = temp;
        }
    }
    return max;
}
int main()
{
    int number_of_words;
    cout << "Enter some English words:" << endl;
    vector<dic> words;
    read_words(cin, words);
    for (vector<dic>::size_type i = 0; i < words.size(); ++i)
    {
        number_of_words += words[i].num;
    }
    try
    {
        if (number_of_words == 0)
        {
            // 异常处理,若没有输入单词,抛出异常,try结束
            throw domain_error("No words entered. Please try again!");
        }
        // 若没有异常,进行后面的程序
        cout << "number of the words: " << number_of_words << endl;
        string::size_type max = maxlen(words);
        for (vector<dic>::size_type i = 0; i < words.size(); ++i)
        {
            cout << words[i].word << string(max + 1 - words[i].word.length(), ' ') << words[i].num << endl;
        }
    }
    catch (domain_error e)
    {
        cout << e.what();
    }
    return 0;
}

bug

在vscode运行正常,但在linux不正常:

>g++ -g hw2.cpp
>./a.out
C++ is a General Purpose Programming Language created by Bjarne Stroustrup as an extension of the C programming language
n
number of the words: 21957
C++         1
is          1
a           1
General     1
Purpose     1
Programming 2
Language    2
created     1
by          1
Bjarne      1
Stroustrup  1
as          1
an          1
extension   1
of          1
the         1
C           1

number不对。
用gdb调试,

>gdb a.out
>layout src    //显示源代码
>b main        //在main开头设断点
>r             //run
>n             //单步调试
>p number_of_words   //打印该变量的值

结论:有一个int变量未初始化,在vscode里自动赋初值为0,但在ubuntu里不一定是0,会导致结果错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值