#include <stdio.h>
#define LED 13
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
int i;
char command[5];
for (i = 0; i < 4; i++) {
command[i] = Serial.read();
}
command[4] = '\0';
Serial.println(command);
if (strcmp(command, "AAAA") == 0) {
digitalWrite(LED, HIGH);
Serial.println("LED13 is ON");
} else if (strcmp(command, "BBBB") == 0) {
digitalWrite(LED, LOW);
Serial.println("LED13 is OFF");
}
}
Run Code Online (Sandbox Code Playgroud)
我试图用Arduino的Serial读取一个4个字符长的字符串,当它是AAAA时打开一个LED,当它是BBBB时关闭串口.
但是,当我输入"AAAA"时,它会显示"AAAÿ"并且沿途有很多"ÿ".
我认为我正在正确地阅读所有内容,但它运作得不好,对我做错了什么的想法?
double get_random(double min, double max) {
/* Returns a random double between min and max */
return min * ((double) rand() / (double) RAND_MAX) - max;
}
Run Code Online (Sandbox Code Playgroud)
这是我在最小和最大之间产生随机双打的功能.但是,当我打电话时get_random(-1.0, 1.0);,我得到-2.0和-1.0之间的值.
知道我做错了什么以及如何解决它?
#include <iostream>
#include <string>
#include <vector>
/*
Using STL's string class because the problem does not refer any
limits regarding the number of characters per line.
*/
using namespace std;
int main()
{
string line;
vector<string> lines;
while (getline(cin, line))
{
lines.push_back(line);
}
unsigned int i, u;
unsigned int opening = 1; // 2 if last was opening, 1 if it was closing
for (i = 0; i < (int) lines.size(); i++)
{
for (u = 0; u < (int) …Run Code Online (Sandbox Code Playgroud) 我正在开发一个JavaScript库(这里是存储库),它包含目录中的源代码和目录中的lib/一些演示/示例demos/.一些演示以及库的整个源代码必须通过Webpack,因为它们必须通过Babel(因为我使用的是ECMAScript 6).
但是,我不确定我是否应该只为演示和源代码提供webpack配置文件,因为这样我认为我不能使我的库输出成为单个JavaScript文件.因为库本身是由几个文件组成的,所以它有各种入口点(Sprite,SpriteList和Game).然后将它们放在单个输出文件上,我希望它们可以合并.我知道我可以使用Webpack和Gulp来实现这一目标,但我不确定这是否正确.
简而言之,我正在寻找的是组织我的构建配置的最佳选择(一个webpack文件vs两个webpack文件,第二个在demos/目录中)以及webpack vs webpack + gulp.
理想情况下,我只是调整我的配置文件,并且只能使用Webpack为我的库输出生成单个文件,但我不认为这是可能的.
配置文件如下:
import path from 'path';
export default {
entry: {
Sprite: "./lib/Sprite.js",
SpriteList: "./lib/SpriteList.js",
Game: "./lib/Game.js",
HelicopterDemo: "./demos/helicopter_game/PlayState.js",
CircleExample: "./demos/circle_example/PlayState.js"
},
output: {
path: __dirname,
filename: "./build/[name].js",
library: ["Pentagine", "[name]"],
libraryTarget: "umd"
},
module: {
loaders: [
{
test: /.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015']
}
}
]
},
resolve: {
root: [
path.resolve('./lib/')
]
},
externals: { …Run Code Online (Sandbox Code Playgroud) 我在React中有这个事件监听器:
document.removeEventListener("mouseup", this.handleDocumentClick);
Run Code Online (Sandbox Code Playgroud)
根据Flow的源代码,这是此方法的定义之一:
removeEventListener(type: MouseEventTypes, listener: MouseEventListener, optionsOrUseCapture?: EventListenerOptionsOrUseCapture): void;
Run Code Online (Sandbox Code Playgroud)
看来听众的类型必须是MouseEventListener:
type MouseEventHandler = (event: MouseEvent) => mixed
type MouseEventListener = {handleEvent: MouseEventHandler} | MouseEventHandler
Run Code Online (Sandbox Code Playgroud)
所以看来我可以这样输入:
handleDocumentClick = (evt: MouseEvent) => {
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,没有Flow错误。现在,我想检查事件的目标是否在我存储在React Ref中的另一个DOM节点内:
$menu: React.ElementRef<typeof Menu>;
handleDocumentClick = (evt: MouseEvent) => {
if (this.$menu !== null) {
const menu = ReactDOM.findDOMNode(this.$menu);
if (menu) {
console.log(menu.contains(evt.currentTarget));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个错误:
54: console.log(menu.contains(evt.currentTarget));
^^^^^^^^^^^^^^^^^ EventTarget. This type is incompatible with the expected param …Run Code Online (Sandbox Code Playgroud) 我用JavaScript编写了这个正则表达式:
node_modules\/(?!react\-hot).*-loader.*
Run Code Online (Sandbox Code Playgroud)
这个主意是匹配每个字符串,例如node_modules/babel-loader或node_modules/css-loader除外node_modules/react-hot-loader。
(这里的上下文是我正在编写Flowtype配置文件)。
我正在尝试在OCaml中测试相同的RegEx,但未成功:
# let regexp = Str.regexp "node_modules/(?!react-hot).*-loader.*";;
val regexp : Str.regexp = <abstr>
# Str.string_match regexp "node_modules/babel-loader" 0 ;;
- : bool = false
# Str.string_match regexp "node_modules/react-hot-loader" 0 ;;
- : bool = false
Run Code Online (Sandbox Code Playgroud)
我不明白为什么该babel-loader字符串不匹配RegEx,因为正则表达式在RegEx 101中适用于JavaScript RegExes。
我了解OCaml正则表达式不遵循与JavaScript正则表达式相同的标准。但是,在这里的官方文档中阅读了有关OCaml正则表达式的信息后,我找不到任何可能导致这种行为差异的内容。
我有一个事件触发Metor.call():
Meteor.call("runCode", myCode, function(err, response) {
Session.set('code', response);
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
但是我runCode在服务器内部的函数Metheor.methods也有一个回调函数,我无法找到一种方法让它response在上面的代码中返回一些内容.
runCode: function(myCode) {
var command = 'pwd';
child = exec(command, function(error, stdout, stderr) {
console.log(stdout.toString());
console.log(stderr.toString());
// I Want to return stdout.toString()
// returning here causes undefined because runCode doesn't actually return
});
// I can't really return here because I don't have yet the valuer of stdout.toString();
}
Run Code Online (Sandbox Code Playgroud)
我想办法有exec回调一些回报,因为runCode没有setInterval哪个会的工作,但在我看来哈克的方式.
我有一个简单的JavaScript项目,它使用Babel将ECMAScript 6转换为ES5,然后需要Browserify来利用ES6的模块.
就这样,我想出来Gruntfile.js编译它:
module.exports = function(grunt) {
"use strict";
grunt.loadNpmTasks('grunt-babel');
grunt.loadNpmTasks('grunt-browserify');
grunt.initConfig({
"babel": {
options: {
sourceMap: true
},
dist: {
files: {
"lib/pentagine.js": "lib/pentagine_babel.js",
"demos/helicopter_game/PlayState.js": "demos/helicopter_game/PlayState_babel.js"
}
}
},
"browserify": {
dist: {
files: {
"lib/pentagine.js": "lib/pentagine_babel.js",
"demos/helicopter_game/PlayState.js": "demos/helicopter_game/PlayState_babel.js"
}
}
}
});
grunt.registerTask("default", ["babel", "browserify"]);
};
Run Code Online (Sandbox Code Playgroud)
grunt运行得很好没有任何错误.但是,我收到以下错误:
Uncaught SyntaxError: Unexpected reserved word在export
Uncaught SyntaxError: Unexpected reserved word上import
基本上我在主文件中做的是以下内容:
export class Game {
...
}
Run Code Online (Sandbox Code Playgroud)
然后导入它像:
import {Sprite, Game} from "lib/pentagine";
Run Code Online (Sandbox Code Playgroud)
我正在根据ECMAScript …
我看了看lower_bound,并upper_bound在C ++ STL的<set>。但是,我找不到一种方法来获得最接近(从下面)到集合中另一个值的值。有没有一种简单的方法来获得它,或者我是否必须迭代该集合?
例如,假设我的集合包含以下整数3 4 7 9,然后closest(6) = 4和closest(4) = 4。
我收到以下错误:
Uncaught TypeError: ((jQuery.event.special[handleObj.origType] || (intermediate value)).handle || handleObj.handler).apply is not a function
Run Code Online (Sandbox Code Playgroud)
这是我的代码,它介于错误发生beforeSend的success调用和调用之间:
$('#main-container').on('click', '#sign-in-btn', function (event) {
var username = $('#username-textbox').val();
var password = $('#password-textbox').val();
$.ajax({
url: '/login',
method: 'POST',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));
},
success: function (data) {
}
});
event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
我也尝试进入一个$(document).ready(function () { ... }电话,我得到完全相同的错误.