我已经在我的代码(无法正常工作)上浇注了一段时间。这是针对Project Euler问题的,其中找到一个非常大的和,然后要求打印出该和的前十个数字。 (可以在这里找到问题:https://projecteuler.net/problem=13

我运行了几个“测试”,在这些测试中添加了打印命令,以查看代码中各个点的各种值。运行代码时,我得到了从符号到十位数的任何数字,应该是一位数字。

无论如何。我的问题是:这是类型转换问题还是我缺少的方法存在其他明显问题?我一直在研究类型转换,试图找到解决方法,但无济于事。

感谢您的任何帮助!

代码如下:

// this is a program to find a very large sum of many very large numbers

#include <stdio.h>
#include <math.h>

int main()
{
//declare all ints needed
int i;
int j;
int d; // digit, need to add 48
int placesum; // sum of addition in _'s place (1's, 10's, 10000's)
int place; // final place value
int c = 0, tens = 1, otherc; // counters for start finder
int a = 0; // another counter

//declare all arrays
char numarray[101][51]; //array of strings containing all 100 numbers
char sum[100];

printf("please save data to largesumdata.txt\n\n   press enter when ready");
getchar();

// THE PROBLEM- I don't know how to get my data into my program // FIXED

// using fscanf()
    FILE *pf; // declare a pointer to the file
pf = fopen("largesumdata.txt", "r"); // trys to open file // "r" means read only
if(pf == NULL)
    printf("Unable to open file, sorry Jar\n");
else
{
    for(j = 0; j < 100; j++)
        fscanf(pf, "%s\n", &numarray[j]); // fscanf(pointer, data type, location)
}
//TESTING
//printf("You have reached point A\n");//POINT A WAS REACHED
//TESTING

//TESTING
//printf("Check1, %c\n", numarray[45][23]);
//TESTING

//TESTING
//printf("%c\n", numarray[90][22]);//Can successfully call characters from array
//TESTING

// (Brute force attempt) //I NEVER MESS WITH numarray WHY IS IT CHANGING
for(i = 49; i >= 0; i--)
{
    //printf("%d\n", d);
    for(j = 0; j < 100; j++)
    {

        d = (int)numarray[j][i] - 'o';
        //printf("%d\n", d);
        //holdup// d -= 48; // ASCII conversion // could also write "d = d-48"
        //printf("%d\n", d);
        placesum += d; // could also write "placesum = placesum + d"
        //printf("%d\n", placesum);
    }

    place = placesum % 10;
    placesum = placesum / 10; // takes "10's place" digit for next column

    // now need to put 'int place' into 'char sum'
    sum[i+5] = (char)place+'0'; // ASCII conversion // "+5" for extra space //HERE not properly stored in sum


}

//TESTING
//printf("Check2, %c\n", numarray[45][23]);
//TESTING

//TESTING
//printf("You have reached point B\n");//POINT B WAS REACHED
//TESTING

// find out where sum starts

for(c=0; c<10; c++)
    if(sum[c] != '0')
        break;

//TESTING
//printf("You have reached point C\n"); //POINT C WAS REACHED

//TESTING

otherc = 4-c;

printf("The first 10 digits of the sum of all those f***ing numbers is....\n");
printf("%d-%d-%d-%d-%d-%d-%d-%d-%d-%d", sum[otherc, otherc+1, otherc+2, otherc+3, otherc+4, otherc+5, otherc+6, otherc+7, otherc+8, otherc+9]);

//%c-%c-%c-%c-%c-%c-%c-%c-%c-%c //copy and paste purposes
//%d-%d-%d-%d-%d-%d-%d-%d-%d-%d // ^^^^^

getchar();
return 0;

}


附言如果我过多的笔记令人困惑,我深表歉意

最佳答案

您使用错误的格式在C中打印数组。

sum[otherc, otherc+1, otherc+2, otherc+3, otherc+4, otherc+5, otherc+6, otherc+7, otherc+8, otherc+9]->这实际上衰减为sum[otherc+9],因为C将,视为运算符。

要在每个数组索引处打印值,您应该像这样使用它:sum[otherc], sum[otherc+1], sum[otherc+2],..

要了解有关C的,(逗号)运算符的更多信息,可以begin here

如上所述,在您的printf中,第一个格式说明符%dsum[otherc + 9],因为sum[otherc,...,otherc+9]实际上是一个数字,并且是数组otherc + 9的第sum个索引。您没有为其他格式说明符提供要打印的任何内容,因此会产生垃圾。

09-25 20:58