C++ 欧拉项目"13 C"和"x2B ;+;Can';我不明白为什么结果是错误的

C++ 欧拉项目"13 C"和"x2B ;+;Can';我不明白为什么结果是错误的,c++,C++,这是,这是我要做的 从txt文件获取表达式 将所有数字中最右边的数字相加 保存总和中最右边的数字 丢弃总和中最右边的数字并重复 这里有一些代码` #include <iostream> #include <fstream> using namespace std; int main() { unsigned int sum=0; ifstream myfile; myfile.open("expression.txt"); // file

这是,这是我要做的

  • 从txt文件获取表达式

  • 将所有数字中最右边的数字相加

  • 保存总和中最右边的数字

  • 丢弃总和中最右边的数字并重复

这里有一些代码`

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    unsigned int sum=0;
  ifstream myfile;
  myfile.open("expression.txt");      // file where the numbers are kept
  char * currentvar; // pointer to the character being read
  currentvar = new char;
  int required [10]; //array to store the results of each iteration
  for(int i=49, m=0;i>=40;i--,m++)  // outer loop to move from the rightmost digits to the left
  {

      int index;          //variable to set what we are reading from file
      index = i;
      for (int k =0;k<100;k++) // inner loop to move from the
      {

          myfile.seekg(index,ios::beg);
          myfile.read(currentvar,1);
          sum += ((*currentvar)-'0') ;
          index += 52;          // set to read next line
      }
      required [m]= sum - ((sum/10)*10);  // save the rightmost digit in sum
        sum = sum/10; // shift the sum to the right
  }
  cout<<endl;
  for(int j=9;j>=0;j--)
  {
      cout<<required[j];
  }

  delete currentvar;


    return 0;
}

由于@Rup comment,我发现了这个问题,我从一开始就误解了这个问题,我应该计算总和的最左边的数字,但我计算的是最右边的数字。 这是修改后的代码

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    unsigned int sum=0;
  ifstream myfile;
  myfile.open("expression.txt");      // file where the numbers are kept
  char * currentvar; // pointer to the character being read
  currentvar = new char;
  int required [50]; //array to store the results of each iteration
  for(int i=49, m=0;i>=0;i--,m++)  // outer loop to move from the rightmost digits to the left
  {


      int index;          //variable to set what we are reading from file
      index = i;
      for (int k =0;k<100;k++) // inner loop to move from the
      {

          myfile.seekg(index,ios::beg);
          myfile.read(currentvar,1);
          sum += ((*currentvar)-'0') ;
          index += 52;          // set to read next line
      }
      required [m]= sum - ((sum/10)*10);  // save the rightmost digit in sum
        sum = sum/10; // shift the sum to the right
  }

   cout<<sum; // print the left most digits of the sum
  for(int j=49;j>=0;j--)
  {
      cout<<required[j]; // print the rest of the sum 
  }


  delete currentvar;


    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
无符号整数和=0;
ifstreammyfile;
myfile.open(“expression.txt”);//保存数字的文件
char*currentvar;//指向正在读取的字符的指针
currentvar=新字符;
int required[50];//用于存储每次迭代结果的数组
for(int i=49,m=0;i>=0;i--,m++)//外循环从最右边的数字向左移动
{
int index;//设置从文件中读取的内容的变量
指数=i;

对于(int k=0;k我发现这个问题多亏了@Rup comment,我从一开始就误解了这个问题,我应该计算和的最左边的数字,但我计算的是最右边的数字。 这是修改后的代码

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    unsigned int sum=0;
  ifstream myfile;
  myfile.open("expression.txt");      // file where the numbers are kept
  char * currentvar; // pointer to the character being read
  currentvar = new char;
  int required [50]; //array to store the results of each iteration
  for(int i=49, m=0;i>=0;i--,m++)  // outer loop to move from the rightmost digits to the left
  {


      int index;          //variable to set what we are reading from file
      index = i;
      for (int k =0;k<100;k++) // inner loop to move from the
      {

          myfile.seekg(index,ios::beg);
          myfile.read(currentvar,1);
          sum += ((*currentvar)-'0') ;
          index += 52;          // set to read next line
      }
      required [m]= sum - ((sum/10)*10);  // save the rightmost digit in sum
        sum = sum/10; // shift the sum to the right
  }

   cout<<sum; // print the left most digits of the sum
  for(int j=49;j>=0;j--)
  {
      cout<<required[j]; // print the rest of the sum 
  }


  delete currentvar;


    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
无符号整数和=0;
ifstreammyfile;
myfile.open(“expression.txt”);//保存数字的文件
char*currentvar;//指向正在读取的字符的指针
currentvar=新字符;
int required[50];//用于存储每次迭代结果的数组
for(int i=49,m=0;i>=0;i--,m++)//外循环从最右边的数字向左移动
{
int index;//设置从文件中读取的内容的变量
指数=i;

对于(int k=0;kYou可以在内部循环中打印
*currentvar
sum
,以查看何时出错。小提示:您可以将每个数字的前14位(超过10位用于处理进位,但不超过16位不达到最大值)存储为
,然后添加它们。我不确定所需的[m]是多少bit works:看起来你一直在增加m,超过9,这是数组的限制?我也会再次检查你的seek每次都获取了正确的数字,尽管它看起来会提供源文件有CRLF行结尾。-Rup我不认为它的增量超过9,我通过调试代码进行检查,它只存储到9但是答案是不正确的,我也检查了哪些数字是手动添加的,它也是正确的,我的算法一定有问题预期的输出是什么,实际的输出是什么?你是对的,对不起,我没有注意到我被限制在>=40。这意味着你只看最右边的十位数字,而不是第一位数字ten?您可以在内部循环中打印
*currentvar
sum
以查看何时出错。小提示:您可以将每个数字的前14位(超过10位以处理进位,但不超过16位以不达到最大值)存储为
long
,并将其相加。我不确定所需的[m]是多少bit works:看起来你一直在增加m,超过9,这是数组的限制?我也会再次检查你的seek每次都获取了正确的数字,尽管它看起来会提供源文件有CRLF行结尾。-Rup我不认为它的增量超过9,我通过调试代码进行检查,它只存储到9但是答案是不正确的,我也检查了哪些数字是手动添加的,它也是正确的,我的算法一定有问题预期的输出是什么,实际的输出是什么?你是对的,对不起,我没有注意到我被限制在>=40。这意味着你只看最右边的十位数字,而不是第一位数字对于将来,只需使用
char currentvar;
,而不是
currentvar=new char;
。当您需要指向它的指针时,请执行:
myfile.read(¤tvar,1)
这个
&
给你一个没有凌乱的new和delete的指针。
这个
&
给你一个没有凌乱的new和delete的指针,而不是
currentvar=new char;
只需使用
char currentvar;
就可以了。当你需要一个指向它的指针时,可以这样做:
myfile.read(¤tvar,1);
这个
给你一个没有凌乱的new和delete的指针。