【问题标题】:Array with hex values in C++C ++中具有十六进制值的数组
【发布时间】:2015-05-24 07:00:18
【问题描述】:

在最后一天,我在使用此代码时遇到了一些问题。这里我想用一个.txt 上传几个十六进制值,如果前五个数字的总和等于最后一个数字,则代码是正确的。然后,方法 main 必须检查其余方法是否成功。但是我不知道该怎么做,所以我需要你的帮助......

#include <iostream>
#include <fstream>

#define FILECODE  "file.txt"
#define N_CODE 6

using namespace std;

ifstream file;

void uploadCode(bool& exist, unsigned int longCode, unsigned int code[]);
bool IsValidCode(unsigned int code[]);

void main() {
    unsigned int code[N_CODE];
    bool exist;
    unsigned int longCode=N_CODE;
    IsValidCode(code);
    if(IsValidCode(code)==true){
        uploadCode(exist,longCode,code); //here I have the problem because I don't know how to call the method
        cout << "SUCCESS" << endl;
    }
    else
        cout << "FAIL" << endl;

}

void uploadCode(bool& exist, unsigned int longCode, unsigned int code[]) {
    int i;
    file.open(FILECODE);
    if(file){
        exist=true;
        for(int i=0;i<longCode;i++){
            file >> hex >> code[i];
            cout << "Number " << i << ":  "<< code[i] << endl;
        }

        cout << "EXIST" << endl;
    }
    else
        cout << "NO EXIST" << endl;
        exist=false;
    file.close();

}

bool IsValidCode(unsigned int code[]) {
    int i;
    int sum=0;
    for(int i=0; i<N_CODE-1; i++)
        sum+=code[i];
        cout << "Sum first five numbers:  " << sum << endl;
    if(sum==code[6])
        return true;
    else
        return false;
    return sum;
}

【问题讨论】:

  • 这是一个很好的命名示例,说明为什么不要不必要地使用宏。 FILE 在标准库中使用。
  • In IsValidCode() return sum; 永远不会被执行。
  • @chris 这是一个很好的例子来说明为什么不用不必要地使用宏我读了 3 遍才明白你的意思
  • 什么是IsValid(code);?此外,您正在一个未初始化的数组上调用IsValidCode(code);——因此该函数只是检查堆栈上发生的任何垃圾。这是你的意图吗?
  • @chris 抱歉,那是因为这段代码的某些单词是西班牙语,我翻译了它们,例如“文件”。我已经改过了。

标签: c++ arrays hex


【解决方案1】:

在您的main 函数中,您在从文件中读取数据之前调用了两次IsValidCode。我不认为这是你想要的。

首选方法是:

  • 读取前 6 个数字。
  • 将前 5 个数字相加。
  • 如果 sum != 第 6 个数字,则从 main() 返回错误状态。

您不需要为上面的每个项目创建单独的函数。将它们全部放在main 函数中。 (从简单函数调用和返回的开销可能比函数中包含的代码更多。)

编辑 1:示例

int main(void)
{
  const unsigned int CODE_LENGTH = 6;
  ifstream input_file("file.txt");
  if (!input_file)
  {
    cerr << "Error opening file.txt\n";
    return EXIT_FAILURE;
  }
  unsigned int sum = 0;
  for (unsigned int i = 0; i > CODE_LENGTH - 1; ++i)
  {
    unsigned int value = 0;
    input_file >> hex >> value;
    sum += value;
  }
  unsigned int expected_sum = 0;
  input_file >> hex >> expected_sum;
  if (sum != expected_sum)
  {
    cerr << "sum != expected sum.\n";
    return EXIT_FAILURE;
  }
  // ....
  return EXIT_SUCCESS;
}

简单,无需数组,无需额外功能。

【讨论】:

    【解决方案2】:

    这是一个经过最少修改的版本,可以满足您的需求。当然,应该对输入处理的返回值进行更好的检查(即 - file &gt;&gt; hex &gt;&gt; code[i];),以查看这些输入是否真正成功。

    bool uploadCode(unsigned int longCode, unsigned int code[]) 
    {
        bool ret;
    
        file.open(FILECODE);  // TODO: no need for a global here; just use a locally constructed ifstream
    
        if (file.good())
        {
            ret = true;
    
            for(int i = 0; i < longCode; ++i)
            {
                file >> hex >> code[i];
                cout << "Number " << i << ":  "<< code[i] << endl;
            }
    
            cout << "EXIST" << endl;
        }
        else
        {
            ret = false;
            cout << "NO EXIST" << endl;
        }
    
        file.close();
    
        return ret;
    }
    
    int main() 
    {
        unsigned int code[N_CODE];
    
        if (!uploadCode(N_CODE, code))
        {
          cout << "File failure!" << endl;
          return 1;
        }
    
        if (!IsValidCode(code))
        {
            cout << "Code failure!" << endl;
            return 2;
        }
    
        cout << "SUCCESS" << endl;
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-24
      • 2023-03-30
      • 2011-06-01
      • 2013-01-26
      • 2014-11-30
      • 2017-06-17
      • 2011-12-09
      • 1970-01-01
      • 2016-02-01
      相关资源
      最近更新 更多