我一直在阅读"C程序设计语言"一书中学习C,我偶然发现了算术s[i] - '0'
,他们说它给出了存储在s [i]中的字符的数值.我不太明白它,怎么能通过减法给出价值?注意这在atoi函数中使用,该函数将一串数字转换为其数字等效项.谢谢
我明白使用时如下:
int getX() const {...}
Run Code Online (Sandbox Code Playgroud)
表示此函数不会修改其正文中使用的任何变量.但是我没有取消错误的是在2个地方使用const,如下所示:
const int* getX () const {...}
Run Code Online (Sandbox Code Playgroud)
将const
关键字放在之前的用途是什么int*
?
我正在用C++编写一个练习,并且我编写了一个在我看来有效的代码,但我不确定,因为我还是初学者,有人可以检查它并告诉我它们是否是相同与否.
解决方案代码:
Point *PointArray::get( const int pos) {
return pos >= 0 && pos < size ? points + pos : NULL;
}
Run Code Online (Sandbox Code Playgroud)
我的代码是:
Point* PointArray::get (const int position) {
return &(data[position]);
}
Run Code Online (Sandbox Code Playgroud)
我意识到在我的代码中我必须检查条件以检查是否返回空指针,但除此之外,我的版本是否与 points + pos
points
&data
是Point类的数组,我命名为我的数据,名为它的解决方案.
编辑:
我添加条件后的代码:
Point* PointArray::get (const int position) {
return ((position >= 0 && position < size) ? &(data[position]) : NULL);
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个节点类,我想创建一个内部节点迭代器类,到目前为止我写的:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Node<E> {
E data;
Node<E> next;
int current = 0;
public Node(E data, Node<E> next){
this.data = data;
this.next = next;
}
public void setNext(Node<E> next){
this.next = next;
}
private class NodeIterator implements Iterator {
/*@Override
public boolean hasNext() {
Node<E> node = this;
for(int i=1; i<current; i++){
node = node.next;
}
if(node.next==null){
current = 0;
return false;
}
current++;
return true;
}*/
@Override
public boolean hasNext() {
// code here
} …
Run Code Online (Sandbox Code Playgroud) 我为 Intel 8080 编写了一个模拟器,我想检查我实现的指令是否正确。是否有测试套件或方法来测试每条指令是否正确?
我的模拟器是用 C 编写的。
我正在尝试在Standard ML中创建一个带有2个参数并返回bool的函数,并且2个参数可以是任何类型,在其编写的规范中,'a * 'a -> bool
但每当我尝试它时,它会自动生成2个参数.如何使该函数采用'a'类型的2个参数.
以下是我一直试图实现的功能:
fun lessThan (a, b) =
if a < b then true
else false;
Run Code Online (Sandbox Code Playgroud)
但在写完上述功能之后,我得到的是:
val lessThan = fn : int * int -> bool
Run Code Online (Sandbox Code Playgroud)
我想要的是这个:
val lessThan = fn : 'a * 'a -> bool
Run Code Online (Sandbox Code Playgroud)
我能做些什么才能让它发挥作用?
我想对整数对的数组列表进行排序。到目前为止,我已经能够根据第一个元素对它们进行排序,但是我得到了类似 (1,2)、(1,-2) 的结果。我还想根据第二个元素对它们进行排序,这样我就可以获得正确的排序数组列表,但我似乎无法让它工作。
第一个元素排序的代码是:
private class FirstElmComparator implements Comparator<Pair> {
public int compare(Pair pr1, Pair pr2) {
return pr1.compareFirstElms(pr2);
}
}
Run Code Online (Sandbox Code Playgroud)
和 compareFirstElms 函数如下:
protected int compareFirstElms (Pair p) {
return (new Integer (this.p1)).compareTo(new Integer (p.p1));
}
Run Code Online (Sandbox Code Playgroud)
我可以考虑使第二个元素比较器如下:
private class SecondElmComparator implements Comparator<Pair> {
public int compare(Pair pr1, Pair pr2) {
return pr1.compareSecondElms(pr2);
}
}
protected int compareSecondElms (Pair p) {
return (new Integer (this.p2)).compareTo(new Integer (p.p2));
}
Run Code Online (Sandbox Code Playgroud)
注意:p1 和 p2 是一对中的第一个和第二个元素。
但我认为它会覆盖第一个元素的排序顺序,还是我错了?任何人都可以帮我解决这个问题。
我想我的Linux机器上安装的Cilk,并在他们的目录中,他们告诉我,写的:./configure
然后make
再make install
以我的机器上安装编译器,但是当我键入make
它提供了很多信息,然后在最后它给了我以下错误:
Compilation Failed: 1 error, 0 warnings
Error: failed running cilk2c
make[2]: *** [fib.o] Error 1
make[2]: Leaving directory `/home/khaled/Desktop/cilk-5.4.6/examples'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/khaled/Desktop/cilk-5.4.6'
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)
我从这里下载了cilk目录:[http://supertech.csail.mit.edu/cilk/][1]
有没有办法让它发挥作用?另外,有没有办法在Windows上安装cilk?
注意:我不知道这是否重要,但我在VirtualBox上运行Ubuntu 11.10.
编辑:
我得到了打字后得到的所有照片make
,但我无法得到整件事,因为我认为没有足够的空间,无论如何这里是从我收到的陈述的顶部到结尾:
mv -f .deps/libcilk_p_la-cilk.Tpo .deps/libcilk_p_la-cilk.Plo
/bin/bash ../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I.. -DCILK_TIMING=1 -DCILK_STATS=1 -DCILK_CRITICAL_PATH=1 -pthread -g -O2 -MT libcilk_p_la-sched.lo -MD -MP -MF .deps/libcilk_p_la-sched.Tpo -c -o libcilk_p_la-sched.lo `test -f …
Run Code Online (Sandbox Code Playgroud) 我正在写一些代码,当我试图测试我的代码到现在为止,我得到一个错误.
这是我的代码:
#include <stdio.h>
enum { add = 0, addu, sub, subu } mips_opcode;
typedef enum mips_opcode mips_opcode_t;
typedef unsigned char byte; // 8-bit int
struct mips {
char *name;
byte opcode;
};
typedef struct mips mips_t;
void init (mips_t *out, char *name_tmp, mips_opcode_t opcode_tmp) {
out->name = name_tmp;
out->opcode = (byte)opcode_tmp;
}
int main (void) {
pritnf("no error i assume\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并且命令行中的错误是:
main.c:14:55: error: parameter 3 ('opcode_tmp') has incomplete type
Run Code Online (Sandbox Code Playgroud)
我不能使用枚举作为参数或我在这里做错了什么?
我正在尝试为我的代码编写正则表达式,正则表达式用于验证电话号码格式,前两位数字应为01,04,05,06,07,08,09,后跟短划线( - )和仅后跟6位数字.
我使用了以下正则表达式:0[1456789]{1}-[0-9]{6}
.我使用以下站点来确保我的正则表达式正常工作:RegExr和我正在测试它如下01-123456
但是,当我运行我的代码时,我的函数返回就好像数字无效,但事实并非如此.
这是我的代码:
function validHomePhone () {
global $home_phone;
$home_phone_regex = "0[1456789]{1}-[0-9]{6}";
return preg_match($home_phone_regex, $home_phone);
}
Run Code Online (Sandbox Code Playgroud)
为什么我会得到这样的结果?
c ×2
c++ ×2
function ×2
java ×2
arrays ×1
ascii ×1
assembly ×1
atoi ×1
char ×1
cilk ×1
comparator ×1
compare ×1
const ×1
emulation ×1
enums ×1
expression ×1
instructions ×1
integer ×1
intel-8080 ×1
interface ×1
iterator ×1
linux ×1
ml ×1
nodes ×1
parameters ×1
php ×1
point ×1
pointers ×1
preg-match ×1
regex ×1
sml ×1
smlnj ×1
test-suite ×1
types ×1
validation ×1