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 …
这是一个头文件
#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) 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) 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:超出最大递归深度
所以它的工作原理,但它继续循环.我会提前停止它
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)
为什么是这样?
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)
知道为什么这个功能不起作用我得到了
输入'='上的解析错误
function :: (Floating a, RealFrac a, Integral b) => a -> b
function x = floor (sqrt x)
Run Code Online (Sandbox Code Playgroud)
好的,所以如果你看一下这个函数,我指定的a可以是两个RealFrac和FloatingTypeclasses,然后b是一个Integral.这是因为签名floor和sqrt:
floor :: (Integral b, RealFrac a) => a -> b
sqrt :: Floating a => a -> a
Run Code Online (Sandbox Code Playgroud)
可以看出sqrt只需要花费Floating而且floor只需要花费RealFrac.所以我设置a为RealFrac和Floating.然后我指定那b是Integral因为floor给了我们一个Integral.
所以我的问题是
newfunction :: (Integral a, RealFrac …Run Code Online (Sandbox Code Playgroud) *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意味着什么?
谢谢
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) 以下代码失败,因为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) haskell ×5
c ×4
typeclass ×3
types ×2
long-integer ×1
macos ×1
memory-leaks ×1
python ×1
valgrind ×1