小编0x4*_*141的帖子

如何在文件中使用for循环在Python中工作?

我有这段代码

f = open('textfile.txt', 'r')
for line in f:
    print line
Run Code Online (Sandbox Code Playgroud)

我只想说textfile.txt是这样的

1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)

这是如何运作的?它是如何知道文件中的位置的?我知道它是一遍又一遍地打印,但为什么不一遍又一遍地打印整个文件.我不知道f是一个范围.我还假设它知道停在EOF?

python python-2.7

3
推荐指数
1
解决办法
101
查看次数

如何将数组初始化为非数字值

我正在尝试初始化一个数组,以便我可以使用一个简单的if语句来检查是否将值放入数组中.

这可能吗?

这是我的代码

double number[1024] = {non-numeric value}
int i = 0;
while(1){
    if (number[i] != non-numeric value){

    printf ("%f", number[i]);
    i++;
}
    else
        break;

}
Run Code Online (Sandbox Code Playgroud)

c floating-point

2
推荐指数
1
解决办法
2303
查看次数

在宏中使用__FUNCTION__

使用:

#define DEBUG_FOO(foo) fprintf(stderr, "foo is %x, currently in %s\n", foo, __FUNCTION__);
Run Code Online (Sandbox Code Playgroud)

将打印:

foo is 0, currently in (null)
Run Code Online (Sandbox Code Playgroud)

使用时:

#define DEBUG_FOO(foo) fprintf(stderr, "currently in %s, foo is %x\n", __FUNCTION__, foo);
Run Code Online (Sandbox Code Playgroud)

将打印:

currently in foo_bar, foo is 0
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?我希望要么两者兼有,要么两者都将__FUNCTION__作为null.

使用的编译器是linaro arm-linux-gnueabihf-gcc

c gcc c-preprocessor

2
推荐指数
1
解决办法
816
查看次数

C:如果声明不起作用

if (num2 != 0)
    printf("\nInput 3 / Input 2 (int)    %12d", divide);
else if (num2 == 0)
    printf("\nInput 3 / Input 2 (int)    DIV/0");


if (flt4 != 0)
    printf("\nInput 2 / Input 1 (double) %16.3f", divide2);
else if (flt4 == 0)
    printf("\nInput 2 / Input 1 (double)    DIV/0");
Run Code Online (Sandbox Code Playgroud)

当我将num2或flt4设置为0时程序崩溃.否则它完全正常.我正在查看关于if/else语句的阅读,我相信我的格式是正确的,但显然有一个错误.

谢谢.

编辑:

我定义除法的代码是:

void calculate (int num1, int num2, int num3, float flt4,int* sum,int* mult,
            int* mod,int* divide,double* mult2,double* divide2)
{

*sum = num1 + flt4;
*mult = num1 * num3; …
Run Code Online (Sandbox Code Playgroud)

c if-statement divide-by-zero

1
推荐指数
1
解决办法
971
查看次数

传递多维数组

我正在尝试制作2x2阵列,将其初始化为0,并将其传递给函数.

我试过用它来初始化它

int array[2][2] = {0}
Run Code Online (Sandbox Code Playgroud)

但只有前5个值为0.

对我来说更大的问题是将数组传递给函数.

二维数组是指向指针的指针吗?所以我应该能够把它传递给;

function(**array);
Run Code Online (Sandbox Code Playgroud)

它是否正确?

当我尝试在函数中访问此数组时,我可以以格式访问它

void function (int **array){
    array[0][2] = choice
}
Run Code Online (Sandbox Code Playgroud)

因为这不起作用,我假设某处我必须定义数组的大小.

谢谢.

c multidimensional-array

0
推荐指数
1
解决办法
119
查看次数

在C中输入字符时代码循环

这主要是出于好奇,为什么会发生这种情况,因为在我的情况下并不重要.如果我键入一个无效的数字,它会正确地转到重复标签并要求我再次输入一个数字,但如果我输入一个像'f'这样的字符,它将无休止地循环而不会停止.为什么是这样?

这里的数组和所有变量都是int类型.

    repeat: 
    printf("Enter number of available space, you are %c: ", userXO);
    scanf("%d", user);

    switch (*user)
    {
        case 1: if (spaces[0][0] == 49){ spaces[0][0] = userXO;}else goto repeat; break;
        case 2: if (spaces[0][1] == 50){ spaces[0][1] = userXO;}else goto repeat; break;
        case 3: if (spaces[0][2] == 51){ spaces[0][2] = userXO;}else goto repeat; break;
        case 4: if (spaces[1][0] == 52){ spaces[1][0] = userXO;}else goto repeat; break;
        case 5: if (spaces[1][1] == 53){ spaces[1][1] = userXO;}else goto repeat; break;
        case 6: …
Run Code Online (Sandbox Code Playgroud)

c

-3
推荐指数
1
解决办法
115
查看次数