Require Administration Permission in VS 2013

C++ Project

  1. Right click the Project, click Properties
  2. Navigate to Linker
  3. Navigate to Manifest File
  4. Edit the UAC Execution Level to requireAdministrator

C# project:

Edit the properties\app.manifest, modify the permission node as the one in the specifications there.

Debug with UAC:

Start the VisualStudio with Administrator permission, and you can debug the projects need UAC.

NetStream.Read in C#

调用NetStream.Read(buffer, offset, len)有一个坑,那就是它一次读取并不一定会真正读完你指定的len长度,所以最好这么写:

    for (int alreadyRead = 0; alreadyRead < len; )
        alreadyRead += stream.Read(buffer, alreadyRead, len - alreadyRead);

为此浪费了不少时间·

Access to ‘class’ Tag Attribute in HTML using HtmlDocumentElement in C#

<p class="aClass">some content</p>

像上面这一段html代码,在用HtmlDocumentElement进行处理的时候,用直观的

element.getAttribute("class");

是得不到class属性的值”aClass”的,原因是IE用className代替了class。

所以要想得到class属性,需要这么做:

element.getAttribute("className");

参考:http://zhidao.baidu.com/question/208025778.html

LeetCode题目:Merge k Sorted Lists

Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.



分析
设总长度为n。
最简单的办法就是扫一遍k个链表的开头,哪个最小就把它移除,加入到结果链表中。这样时间复杂度是kn
第二个办法就是进行二分,将k个链表分为两个一组,组内进行merge。形成一个新的链表集合,大小为(k + 1)/2。继续两个一组merge,这样下去一共会进行logk次merge,最后merge成为一个链表。总的时间复杂度是n
logk

其中两个list合并的代码同LeetCode题目:Merge Two Sorted Lists



代码:72ms过大集合

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        ListNode *proot = NULL;
        ListNode **pplast = &proot;
        while(l1 || l2){
            if(l1) {
                ListNode *minNode;
                if(!l2 || l1->val <= l2->val){
                    //l1 is min
                    minNode = l1;
                    l1 = l1->next;
                } else if(l2) {
                    //l2 is min
                    minNode = l2;
                    l2 = l2->next;
                }
                //proceed root
                *pplast = minNode;
                pplast = &(minNode->next);
            } else {
                //l1 is empty
                *pplast = l2;
                break;
            }
        }
        return proot;
    }
    ListNode *mergeKLists(vector &lists) {
        if(lists.size() == 0)
            return NULL;
        int curSize = lists.size();
        while(curSize > 1) {
            int halfSize = (1 + curSize) / 2;
            //merge i,i + halfSize
            for(int i = 0 ; i < halfSize && i + halfSize < curSize; ++i) {
                ListNode *first = lists[i];
                ListNode *second = lists[i + halfSize];
                ListNode *result = mergeTwoLists(first,second);
                lists[i] = result;
            }
            //set curSize to halfsize
            curSize = halfSize;
        }
        return lists[0];
    }
};

POJ1002 Phone Number Duplication

上周三做了一道简单的poj题目,但是虽然简单却有比较大的收获,主要有四点:
1.gets()比cin()快
2.cin()不自动吃掉’\n’,需要的话,用getchar()吃掉
3.printf(“%.3d”,anInt),不足3位在前面补0
4.时间问题,抓主要矛盾(线性搜索改为二叉树搜索),new操作都是次要的,链表比数组慢也是次要的

下面是代码:

<

pre class=”brush:cpp;auto-links:false”>
// main.cpp
// POJ1002-PhoneNumberDuplication
// Created by Qiu Xiangyu on 12-7-10.
// gets()比cin()快
// cin()不自动吃掉’\n’,需要的话,用getchar()吃掉
// printf(“%.3d”,anInt),不足3位在前面补0
// 时间问题,抓主要矛盾(线性搜索改为二叉树搜索),new操作都是次要的,链表比数组慢也是次要的

include

//#include
using namespace std;

struct node{
long phoneNumber;
int repeatCount;
node leftChild;
node *rightChild;
node *parant;
};
inline int translateCode(char c)
{
if (c >= ‘0’ && c <= ‘9’) {
return c – ‘0’;
}
if (c >= ‘A’ && c <= ‘O’) {
int num = c – ‘A’;
return num / 3 + 2;
}
if (c == ‘P’ || c == ‘R’ || c == ‘S’) {
return 7;
}
if (c >= ‘T’ && c <= ‘V’) {
return 8;
}
if (c >= ‘W’ && c <= ‘Y’) {
return 9;
}
return -1;
}
inline node *findNodeWithNumber(node
startNode, long number)
{
if (startNode == NULL) {
return NULL;
}
if (startNode->phoneNumber == number) {
return startNode;
} else if (startNode->phoneNumber < number) {
if (startNode->rightChild == NULL) {
return startNode;
}
return findNodeWithNumber(startNode->rightChild, number);
} else {
if (startNode->leftChild == NULL) {
return startNode;
}
return findNodeWithNumber(startNode->leftChild, number);
}
}
bool haveDuplication = false;
inline void output(node *root)
{
if (root == NULL) {
return;
}
output(root->leftChild);

if (root->repeatCount > 1) {
    haveDuplication = true;
    long phoNum = root->phoneNumber;
    int first = phoNum / 10000;
    int last = phoNum % 10000;
    printf("%.3d-%.4d %d\n",first,last,root->repeatCount);
}

output(root->rightChild);

}
int main(int argc, const char * argv[])
{
node *head = NULL;
int totalNumber;
cin>>totalNumber;
getchar();
// cout<<“total:”<<totalNumber<<endl;
for (int iNumber = 0; iNumber < totalNumber; iNumber ++) {
char inputbuffer[500];
char *str = inputbuffer;
gets(str);
// cout<<aNumber<<endl;
// cout<<str;
long phoneNumber = 0;
int strLen = strlen(str);// aNumber.length();
// if (strLen == 0) {
// iNumber–;
// continue;
// }
for (int ichar = 0; ichar < strLen; ichar ++) {
char c = str[ichar];
if (c == ‘-‘) {
continue;
}
int num = translateCode(c);
if (num == -1) {
continue;
}
phoneNumber = 10 * phoneNumber + num;
}
// cout<<phoneNumber<<endl;
node *phoneNode = findNodeWithNumber(head, phoneNumber);
if (phoneNode) {
if (phoneNode->phoneNumber == phoneNumber) {
phoneNode->repeatCount ++;
} else if (phoneNode->phoneNumber < phoneNumber) {
node *parant = phoneNode;
node *child = new node();
child->phoneNumber = phoneNumber;
child->repeatCount = 1;
child->parant = parant;
parant->rightChild = child;
} else {
node *parant = phoneNode;
node *child = new node();
child->phoneNumber = phoneNumber;
child->repeatCount = 1;
child->parant = parant;
parant->leftChild = child;
}
} else {
phoneNode = new node();
phoneNode->phoneNumber = phoneNumber;
phoneNode->repeatCount = 1;
head = phoneNode;
}
}
// cout<<“done”<<endl;
output(head);
if (!haveDuplication) {
cout<<“No duplicates.”<<endl;
}
return 0;
}

通过javascript控制flash

昨天研究一个用C#写的自动化应用,针对一个网页的按钮进行点击操作,这一步可以用javascript很自然的解决(见:利用JavaScript从UIWebView获取、修改、提交网页内数据的方法)。

怎么在C#中利用webbrowser运行javascript可以参见:C#中让WebBrowser运行Javascript脚本

然后页面居然弹出来一个flash,第二步的操作时针对这个flash的。为了解决这个问题,只好对flash进行反编译(用到了一个叫做“硕思闪客精灵”的软件),才看到里面的代码。好在于网上有很多关于javascript和flash的交互方法。可以通过javascript来调用flash中的方法。在查看了flash的源代码之后,发现需要调用其中一个函数(叫做function吧)。

实现方法就是:
第一步,拿到flash对象
好在于在页面内部我直接找到了这个函数,要针对不同的浏览器用不同的方式获取,代码如下:

  1. function getSWF(a){
  2.   if(navigator.appName.indexOf(\”Microsoft\”)!=-1){
  3.     return window[a];
  4.   } else {
  5.     if(window.navigator.userAgent.indexOf(\”Firefox\”)!=-1){
  6.       return document[a];
  7.     } else {
  8.       if(window.navigator.userAgent.indexOf(\”Chrome\”)!=-1){
  9.         return document[a];
  10.       } else {
  11.         if(window.navigator.userAgent.indexOf(\”Safari\”)!=-1){
  12.           return document.getElementById(a);
  13.         } else {
  14.           if(window.navigator.userAgent.indexOf(\”Opera\”)!=-1){
  15.             return document[a];
  16.           }
  17.         }
  18.       }
  19.     }
  20.   }
  21. }
  1. flash=getSWF(‘flashName’);

第二步,调用flash中的方法

  1. flash.function();

另外一些可用的方法如下:
播放:Play()
停止: StopPlay()
停止并回到首帧: Rewind()
下一帧: TGetProperty(nameOfTargetMovieClip, propertyIndex) and GotoFrame(frameNum)
方大或缩小: Zoom(relative percentage)
发送数据: SetVariable(variableName, variableValue)
读取数据: GetVariable(variableName)

C#中让WebBrowser运行Javascript脚本

C#中可以让Webbrowser运行Javascript脚本来实现各种自动化操作,比如点击网页上的按钮,输入用户名密码等等。代码也很简单:

>>>>>>>>>>>>>>>>>>>>方法一:
只需要先获取HtmlElement,然后对获取的element进行操作即可,比如:

  1.             HtmlElement btn = webBrowser1.Document.GetElementById(“aBtn”);
  2.             if (btn != null)
  3.                 btn.InvokeMember(“click”);

>>>>>>>>>>>>>>>>>>>>方法二:
给页面注入javascript代码:

  1. HtmlElement ele = webBrowser1.Document.CreateElement(“script”);
  2. ele.SetAttribute(“type”, “text/javascript”);
  3. ele.SetAttribute(“text”, “someJavaScript”);
  4. webBrowser1.Document.Body.AppendChild(ele);

最后一句话执行之后会自动执行javascript

>>>>>>>>>>>>>>>>>>>>方法三:
运行页面中已经有的脚本:

  1. webBrowser1.Document.InvokeScript(“someInPageJavaFunction”, null);

>>>>>>>>>>>>>>>>>>>>另外:
另外webbrowser中加载网页,如果遇到脚本错误的话,默认会弹出对话框,这有时候比较烦人,不过可以关闭之:

  1. webbrowser.ScriptErrorsSuppressed = true;