小编Cha*_*ana的帖子

为什么我无法以长数据类型存储数据?

int power(int first,int second) {
    int counter1 = 0;
    long ret = 1;

    while (counter1 != second){
        ret *= first;
        counter1 += 1;
    }
    return ret;
}


int main(int argc,char **argv) {

    long one = atol(argv[1]);
    long two = atol(argv[2]);
    char word[30];
    long finally;

    printf("What is the operation? 'power','factorial' or 'recfactorial'\n");
    scanf("%20s",word);

    if (strcmp("power",word) == 0){
        finally = power(one,two);
        printf("%ld\n",finally);
        return 0;
    } 

}
Run Code Online (Sandbox Code Playgroud)

这个函数用于执行像计算器一样的"power of power"操作,所以如果我写:./a.out 5 3它将给我5的3的幂并打印出125

问题是,如果数字如下:./a.out 20 10,20到10的幂,我希望看到结果:1.024 x …

c types long-integer

13
推荐指数
2
解决办法
1429
查看次数

警告:C99中隐式声明功能无效?

这是一个头文件

#include <stdio.h>

int m = 18;
int x = 4;

int singles (n) {
    if (n == 1)
         return 0;
    return doubles(n-1);
} 

int doubles (n) {
    if (n == 1)
        return 0;

    return triples(n-1);
}

int triples (n) {
    if (n == 1)
        return m;

    return (singles(n-1) + doubles (n-1) + triples (n-1))*(m-1);
}
Run Code Online (Sandbox Code Playgroud)

这是主文件

#include <stdio.h>
#include "test.h"

int main () {
    printf("%d",singles (x));
}
Run Code Online (Sandbox Code Playgroud)

所以至少对我来说这是非常复杂的.这个想法是在主函数中我将调用单个(x),其中x = 4,因此它更像单个(4),它将调用双精度(3),它将调用三倍(2),它将调用所有将返回0的单个(1),返回0的双倍(1)和将返回m的三倍(1).

所以我得到的错误是

./test.h:13:12: warning: implicit declaration of function 'doubles' is …
Run Code Online (Sandbox Code Playgroud)

c

5
推荐指数
1
解决办法
4万
查看次数

Valgrind — 堆统计数据很奇怪:内存泄漏?

Valgrind 给了我非常高的释放、分配和分配的字节数,并且没有意识到我已经释放了堆中的内存。

#include <stdio.h>
#include <stdlib.h>

int main(){
    return 0;
}

gcc -std=c99 -g -Werror -Wextra -pedantic valgrind.c
valgrind --leak-check=full ./a.out
==44611== Memcheck, a memory error detector
==44611== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==44611== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==44611== Command: ./a.out
==44611== 
==44611== 
==44611== HEAP SUMMARY:
==44611==     in use at exit: 22,223 bytes in 183 blocks
==44611==   total heap usage: 259 allocs, 76 frees, 28,335 bytes allocated
==44611== …
Run Code Online (Sandbox Code Playgroud)

c macos valgrind memory-leaks

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

递归python脚本

found = 0
def new(string):
    global found

    if found > len(string):
        return 0 

    fish = string.find('x',found,len(string))
    found = fish + 1

    return new(string) + 1


text = 'onxonxoinxoinoxn'
final_text = text + 'x'
print new(final_text)
Run Code Online (Sandbox Code Playgroud)

所以我是递归的新手,我知道有一种更简单的方法可以做到这一点,但是有人可以解释如何解决这个问题.这基本上是一个递归函数来查找字母'x'可以找到的总次数变量'text'.

This is my error:
4
7
11
16
18
0
4
7
Traceback (most recent call last):
11
16
  File "/Users/Charana/Documents/Projects/untitled/Main.py", line 18,      
Run Code Online (Sandbox Code Playgroud)

in new(final_text)RuntimeError:超出最大递归深度

所以它的工作原理,但它继续循环.我会提前停止它

python

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

Haskell难以使用变量的类型签名

tuple :: (Integer a,Fractional b) => (a,b,String)
tuple = (18,5.55,"Charana")
Run Code Online (Sandbox Code Playgroud)

所以这给了我错误

‘Integer’ is applied to too many type arguments
In the type signature for ‘tuple’:
tuple :: (Integer a, Fractional b) => (a, b, String)
Run Code Online (Sandbox Code Playgroud)

为什么是这样?

haskell typeclass

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

解析错误(=),对于列表使用模式匹配的函数?

list (x:xs) = "this letter is " ++ [x] ++ " and " ++ function xs
    where  function [] = "Empty"
           function x:[] = "Singlet"
           function x:y:[] = "Double"
           function x:y:xs = "LARGE list!"
Run Code Online (Sandbox Code Playgroud)

知道为什么这个功能不起作用我得到了

输入'='上的解析错误

haskell pattern-matching

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

为什么我的功能的签名不起作用?

function :: (Floating a, RealFrac a, Integral b) => a -> b
function x = floor (sqrt x)
Run Code Online (Sandbox Code Playgroud)

好的,所以如果你看一下这个函数,我指定的a可以是两个RealFracFloatingTypeclasses,然后b是一个Integral.这是因为签名floorsqrt:

floor :: (Integral b, RealFrac a) => a -> b 
sqrt :: Floating a => a -> a
Run Code Online (Sandbox Code Playgroud)

可以看出sqrt只需要花费Floating而且floor只需要花费RealFrac.所以我设置aRealFracFloating.然后我指定那bIntegral因为floor给了我们一个Integral.

所以我的问题是

newfunction :: (Integral a, RealFrac …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass

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

Floating类型类与Double有何不同?

*User> :t sqrt
sqrt :: Floating a => a -> a
Run Code Online (Sandbox Code Playgroud)

我不明白是什么Floating a => a -> a想告诉我的.我的教授告诉我sqrt可以想到sqrt :: Double -> Double.它确实像那样,但Floating a => a -> a意味着什么?

谢谢

haskell types typeclass

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

警卫解析错误

factorial :: Int -> Int 

factorial 0 = 1

factorial n 
   | n < 0 == error "Cant call a nagative number"
   | otherwise = n * factorial (n-1)
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么我收到这个错误?

haskell.hs:77:2: parse error on input ‘|’
Run Code Online (Sandbox Code Playgroud)

haskell

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

使用atoi不能接受char类型 - 任何方式?

以下代码失败,因为atoi()需要a char *而我只传递一个char.我想存储只有第一个数字的值.知道我怎么能这样做吗?

int main () {
  char a[]= "123";
  int b = atoi(a[0]);
  printf("%d",b);
}
Run Code Online (Sandbox Code Playgroud)

这是确切的错误消息:

division.c:9:16: warning: incompatible integer to pointer conversion passing
      'char' to parameter of type 'const char *'; take the address with &
      [-Wint-conversion]
  int b = atoi(a[0]);
           ^~~~
           &
Run Code Online (Sandbox Code Playgroud)

c

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