我的程序在我的计算机上运行,但在CodeEval上不起作用

My program works on my computer, but not on CodeEval

本文关键字:我的 但在 CodeEval 不起作用 运行 计算机 程序      更新时间:2023-10-16

我现在正在研究CodeEval上的一个简单挑战。您需要逐行从文件中获取输入,每行包含由管道分隔的十六进制数字和二进制数字。目标是对左侧的所有十六进制数求和,对右侧的二进制数求和,并测试哪个总和更大。如果右侧(二进制边)大于或等于十六进制边,则打印"True",如果不是,则打印"False"。示例行是"5e 7d 59 |1101100 10010101 1100111",输出将为真,因为右侧大于左侧。我的代码在我的计算机上打印了正确的输出,但在 CodeEval 上,没有结果输出,我的分数为零。未列出任何错误。有没有我看不到的问题?

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <math.h>
using namespace std;
fstream myFile;
string line;
vector<string> panNums;
int sum1, sum2;
int toDec(string s);
int main(int argc, char *argv[])
{
  //open the file
  // get numbers by line
  myFile.open(argv[1]);
  while(getline(myFile, line))
    {
      //cout << line << endl;
        istringstream mystream(line);
        string nums;
        // read in each number into string nums one by one
        // then add that number to the vector that was created
        while(mystream)
          {
            mystream >> nums;
            panNums.push_back(nums);            
          }
        bool afterSep = false;
        sum1 = 0;
        sum2 = 0;
        for(int i = 0; i < panNums.size() - 1; i++)
            {
                stringstream stream;
                if(panNums.at(i) == "|")
                 {
                    sum1 = sum2;
                    sum2 = 0;
                    afterSep = true;
                    i++;
                 }  
                // if true, do stuff
                if(afterSep)
                 {
                    // deals with the binary side
                    sum2 += toDec(panNums.at(i));
                 }
                // if false, do other stuff
                else
                 {
                    // deals with the hexidecimal side
                    istringstream f(panNums.at(i));
                    int temp;
                    // reading hex number into int(AKA converting to int)
                    f >> hex >> temp;
                    sum2 += temp;
                 }  
            }
                // cout << "sum1 " << sum1 << endl;
                // cout << "sum2 " << sum2 << endl;
                if(sum2 >= sum1)
                 {
                    cout << "True" << endl;
                 }
                else
                 {
                    cout << "False" << endl;
                 }
        // clear the current vector in order to exclusively have the next line of text stored
        panNums.clear();
    }
}
int toDec(string s)
{
    int num = 0;
    int i = s.size() - 1;
    // starts at index 0
    // which is really the 2^6 or however big the binary number is
    for(int a = 0; a < s.size(); a++)
      {
        if(s.substr(i, 1) == "1")
         {
            num += pow(2, a);
         }
        i--;
      }
    // cout << num << endl;
    return num;
}

两台计算机是相同的操作系统吗?如果是这样,它们应该没问题,但否则您需要在两个系统上编译,这意味着每个系统都有一个可执行文件。无论如何,当我在 mac 上编写东西并想在 mac 和 Linux 上运行它时,这在这里都有效。

相关文章: