GCC和MinGW(C ++)之间奇怪的输出差异

用户名

我正在为一个计算机科学课设计一个项目。我编写了代码,并使用MinGW对其进行了测试,并且工作正常。然后,我将代码复制到大学的Linux服务器上,并在那里进行了测试,因为这是我的教授对作业进行评分的基础。输出非常不同-就像它在输出中间打印出回车符一样。

有趣的是,在使用Cygwin GCC(32位,4.7.2)编译和运行时,该问题也会出现。是否有人对这种情况的发生以及如何解决有任何见解?

代码示例输入文件(将命名为lifepath.txt,并与可执行文件放在同一目录中)。

#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <string>
#include <cstdlib>

using namespace std;

bool younger(string s1, string s2) {
    string y1 = s1.substr(s1.rfind('-')+1);
    string y2 = s2.substr(s2.rfind('-')+1);

    if(y1 < y2) return true;
    else if(y1 > y2) return false;

    string m1 = s1.substr(s1.find('-')+1, s1.rfind('-')-s1.find('-')-1);
    string m2 = s2.substr(s2.find('-')+1, s2.rfind('-')-s2.find('-')-1);

    if(m1 < m2) return true;
    else if(m1 > m2) return false;

    string d1 = s1.substr(0, s1.find('-'));
    string d2 = s2.substr(0, s2.find('-'));

    return d1 < d2;
}

int main() {
    ifstream in("lifepath.txt");
    map<int, int> pathcounts;
    set<string> bdays;
    string oldest, youngest;

    map<string, int> years;

    string line;
    while(getline(in, line)) {
        string monthday = line.substr(0, line.rfind('-'));
        bdays.insert(monthday);
        string num = line;
        while(num.find('-') != string::npos) num.erase(num.find('-'));
        int path = atoi(num.c_str()) % 9 + 1;
        pathcounts[path]++;

        if(youngest == "" || younger(line, youngest)) youngest = line;
        if(oldest == "" || younger(oldest, line)) oldest = line;

        years[line.substr(line.rfind('-')+1)]++;

    }

    for(int i = 1; i <= 9; i++) {
        cout << i << ": " << pathcounts[i] << endl;
    }

    cout << endl;

    cout << "The oldest birthday is " << oldest << endl;
    cout << "The youngest birthday is " << youngest << endl;

    cout << endl;

    cout << "There are " << bdays.size() << " unique birthdays" << endl;

    cout << endl;

    string modeyear = "";
    int modeyearcount = 0;
    for(map<string, int>::iterator it = years.begin(); it != years.end(); it++) {
        if(it->second > modeyearcount) {
            modeyear = it->first;
            modeyearcount = it->second;
        }
        else if(it->second == modeyearcount) modeyear += " " + it->first;
    }

    cout << "The mode of the birthyears is " << modeyear;
    cout << ", appearing " << modeyearcount << " times" << endl;


    return 0;
}

我将发布一个指向输出图像的链接作为答复,因为我没有足够的代表以使一个帖子中有两个以上的链接。

伊利亚·伯索夫(IłyaBursov)

这是因为\r在Windows中,不同的行尾意味着从文件中读取数据并设置Years数组时,只需转到行的开头。您不仅可以使用键,####而且还可以使用键,####\r并且在期间显示最后一个符号cout

因此,您需要更改代码:

years[line.substr(line.rfind('-')+1, 4)]++;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ipython和python之间的输出差异

来自分类Dev

ipython和python之间的输出差异

来自分类Dev

CMS 6.5和7.0之间的Sitecore FieldRenderer输出差异

来自分类Dev

CMS 6.5和7.0之间的Sitecore FieldRenderer输出差异

来自分类Dev

C中while循环和for循环之间的差异导致输出差异

来自分类Dev

C:使用完全相同的输入时,printf和vfprintf之间的输出差异

来自分类Dev

Knitr pdflatex控制台和PDF之间的输出差异

来自分类Dev

KnitR:控制台和PDF(编码)之间的输出差异

来自分类Dev

python中的numpy.sum()和matlab中的sum()之间的输出差异

来自分类Dev

C#char返回方法与对象返回方法之间的输出差异

来自分类Dev

hexdump和hexedit的输出差异

来自分类Dev

Linux和Windows上的输出差异

来自分类Dev

C - Switch 语句 - 输出差异

来自分类Dev

Python-比较2个文件和输出差异

来自分类Dev

在 sas compare 中,只输出差异和新记录

来自分类Dev

使用synced关键字和join()产生的输出差异

来自分类Dev

批处理文件命令以比较最新文件和输出差异

来自分类Dev

从控制台和通过contentscript执行相同代码时的输出差异

来自分类Dev

程序输出差异

来自分类Dev

parseDateTime输出差异

来自分类Dev

代码输出差异

来自分类Dev

输出差异-Java

来自分类Dev

运行脚本时Ruby版本之间的输出差异

来自分类Dev

fsck和df输出之间的差异

来自分类Dev

python模块的终端和bash脚本之间的奇怪行为差异

来自分类Dev

0.6 和 0.5 秒之间的 Powershell 差异感觉很奇怪

来自分类Dev

Python中函数打印和打印函数输出之间的差异

来自分类Dev

Python中变量和打印变量的输出之间的差异

来自分类Dev

int(r / 2)和r // 2输出之间的差异

Related 相关文章

热门标签

归档