🔍 登录 注册
分类:
C/C++开发node扩展之优化累加求和

C/C++开发node扩展之优化累加求和

累加求和即从最小数开始一直加到最大数,例如:1累加到100,即1+2+3......+100;普通算法for(int i = min, i <= max; i++) { sum += i;}下面是以扩展的形式开发的优化算法。#include <nan.h> void change(long long *num1, long long *num2){ *num1 = *num2 + *num1; *num2 = *num1 - *num2; *num1 = *num1 - *num2; } long long add(long long min, long long max){ if (min > max) { change(&min, &max); } long long sum = 0; long long addTimes = 0; if ((min + max) % 2 == 0) { addTimes

大 大苹果 2017/6/6 👍 0 ⭐ 0 💬 0
累加求和算法

累加求和算法

例子:求1累加100。普通算法int sun = 0;for (int i = 1; i<100 i++){ sum = sum + i;}优化后的算法int sum =(1 + 99) * 50 + 50;如果1累加到99.int sun = 0;for (int i = 1; i<99 i++){ sum = sum + i;}优化后的算法int sum =(1 + 98) * 50;优化算法与通用算法对比#include<stdio.h>void change(int *num1, int *num2){ *num1 = *num2 + *num1; *num2 = *num1 - *num2; *num1 = *num1 - *num2;}int add(int min, int max){ if (min > max) { change(&min, &max); } int sum = 0; int addTimes = 0; if ((min + max) % 2 == 0) { addTimes = (max - min) / 2; return a

大 大苹果 2017/6/5 👍 0 ⭐ 0 💬 0
nodejs异常处理

nodejs异常处理

在nodejs开发中,异常处理十分麻烦。一下代码捕捉不到错误function async_error() { setTimeout(function(){ throw new Error("Error"); },10)}function run() { try { async_error(); } catch (err) { console.log(err); }}run();在回调函数内捕获错误,我们不得不在异步函数内写错误处理语句。function async_error() { setTimeout(function(){ try{ throw new Error("Error"); }catch(e){ console.log(e); } },10)}async_error();如果函数嵌套过多,就会变成这样foo('a', function (a) { if (a.error) { throw new

大 大苹果 2017/6/5 👍 0 ⭐ 0 💬 0
koa-static组件的使用

koa-static组件的使用

koa-static组件的使用const path = require('path');app.use(require('koa-static')(path.join(__dirname, './static'), { maxAge: 365 * 24 * 60 * 60,extensions:true}));npm install koa-static --save

大 大苹果 2017/6/5 👍 0 ⭐ 0 💬 0
Apache之.htaccess文件配置允许跨域

Apache之.htaccess文件配置允许跨域

设置允许跨域头Header set Access-Control-Allow-Origin 允许的请求地址设置域名访问<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / #-判断规则,若有多个域名则写多条.(除了以下域名,其他均不允许访问) rewritecond %{http_host} !^tesy.com$ [nc] #rewritecond %{http_host} !^testa1.pw$ [nc] #rewritecond %{http_host} !^testa2.pw$ [nc] #-禁止规则 rewriterule ^.* - [F,L]</IfModule>

大 大苹果 2017/6/5 👍 0 ⭐ 0 💬 0
nginx配置允许跨域访问

nginx配置允许跨域访问

nginx配置允许跨域访问location ~ .*\.(php|php5)?${ add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Headers' 'X-Requested-With, Content-Type, Authorization'; add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS'; if ($request_method = 'OPTIONS') {return 204;} #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf;}

大 大苹果 2017/6/5 👍 0 ⭐ 0 💬 0
C/C++开发求素数的Nodejs插件

C/C++开发求素数的Nodejs插件

使用C语言开发去素数的nodejs插件,c语言代码如下#include <node.h>#include <stdlib.h>namespace Prime {using v8::FunctionCallbackInfo;using v8::Isolate;using v8::Local;using v8::Object;using v8::String;using v8::Value;using v8::Exception;using v8::Number;// void Method(const FunctionCallbackInfo<Value>& args) {// Isolate* isolate = args.GetIsolate();// args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));// }void Prime(const FunctionCallbackInfo<Value>& args){ Isolate* isolate = args.GetIsolate(); int i,

大 大苹果 2017/6/4 👍 0 ⭐ 0 💬 0
linux node的编译安装

linux node的编译安装

linux下编译安装node:yum install gccyum install gcc-c++yum install openssl*yum install -y bzip2*yum -y install python./configure --prefix=/usr/local/nodemake && make installln -s /usr/local/node/bin/* /usr/local/bincd /usr/local/node/lib/node_modules/npm./configuremake && make install2、修改profile文件: #vi /etc/profile 在里面加入: export PATH="$PATH:/opt/au1200_rm/build_tools/bin" 让环境变量立即生效需要执行如下命令: #source /etc/profile ln -s /usr/bin/npm /usr/local/node/bin/npm

大 大苹果 2017/6/3 👍 0 ⭐ 0 💬 0
async与await

async与await

async与await的用法function test(){ return new Promise((resolve, reject) => { mysql.query(sql, (error, result, fields) => { if (error) { reject(error); } else { resolve([result, fields]); } }) });}async function exceTest() { return await test();}var data = await exceTest();async的使用条件:必须是promise对象

大 大苹果 2017/6/3 👍 0 ⭐ 0 💬 0
java文件流操作

java文件流操作

java文件流操作,源码如下:package code; import java.io.File; import java.io.IOException; public class IOStream { public static void main(String[] args)  { File f = new File("F:\\test.txt");         //取得文件路径 System.out.println("文件路径为:" + f.getAbsolutePath()); //得到文件的大小 System.out.println("文件的大小为:" + f.length()); System.out.println("文件可读不:" + f.canRead()); File f1=new File("F:\\test1.txt"); if(!f1.exists()) { //可以创建 try { f1.createNewFile(); } catch (IOException e) { // TOD

大 大苹果 2017/6/3 👍 0 ⭐ 0 💬 0
java连接sql server数据库

java连接sql server数据库

java连接sql server数据库package code; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class DbAdapter { Connection con; Statement sm; ResultSet rs; PreparedStatement ps=null; public static void main(String[] args)  { DbAdapter s = new DbAdapter(); String sql = "update users set username='1' where username='1'"; s.LinkSql(sql); } public boolean LinkSql(String sql) { try { Cl

大 大苹果 2017/6/3 👍 0 ⭐ 0 💬 0
java求水仙花数

java求水仙花数

水仙花数是指一个 n 位正整数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153)。什么是水仙花数水仙花数只是自幂数的一种,严格来说3位数的3次幂数才称为水仙花数。附:其他位数的自幂数名字一位自幂数:独身数两位自幂数:没有三位自幂数:水仙花数四位自幂数:四叶玫瑰数五位自幂数:五角星数六位自幂数:六合数七位自幂数:北斗七星数八位自幂数:八仙数九位自幂数:九九重阳数十位自幂数:十全十美数package code; /**  *   * @author Smart  *  */ public class Narcissistic { public static void main(String[] args)  { int i,j,k,m; for(m = 100; m < 1000; m++) { i = m%10; j = (( m-i ) % 100 ) / 10; k = ( m - j * 10 - i ) / 100; if( (k * k * k + j * j * j + i 

大 大苹果 2017/6/3 👍 0 ⭐ 0 💬 0