#include <stdio.h> #include <vector> #include <algorithm> //random_shuffle #include <time.h> int gen(int i) {return rand()%i; } int main() { srand(time(0)); std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); random_shuffle(v.begin(), v.end(), gen); for (unsigned int x=0; x<v.size(); x++) printf("%d\n", v[x]); return 0; }
bloggerads
2013年10月31日 星期四
C++: vector Shuffle(洗牌) 函數
C++提供隨機打亂vector的函數, random_shuffle()。若想要每次第一次都是不同的亂法,可以引入一個function pointer, 而這個function限定一個輸入和一個輸出引數, type要和iterator一樣, 且輸出值不能為負數或大於輸入引數值
2013年10月30日 星期三
C++的Container: Vector使用範例
一般C語言如果想要動態增加陣列大小,則必須要呼叫 realloc()。C++提供了vector更靈活的使用陣列,請參考如下的範例:
輸出:
#include <stdio.h> #include <vector> void print(char x, std::vector<int> &vec) // Call by Reference { printf("%c.", x); for (int i=0; i<vec.size(); i++) printf(" %d", vec[i]); puts(""); } int main() { std::vector<int> vec;// vector<int> vec(10, 0); //初始size為10個0 std::vector<int>::iterator end; vec.push_back(10); print('a', vec); vec.push_back(20); print('b', vec); vec.insert(vec.begin() + 1, 30); // 將30插入陣列中第1個位置 //vec.insert(vec.begin() + 1, 2, 30); // 將兩個30插入陣列中第1個位置 print('c', vec); //auto end=vec.end()-1; // -std=c++11 vec.erase(end=vec.end()-1); // 移除最後一個元素 print('d', vec); vec.pop_back(); // 移除最後一個元素 print('f', vec); return 0; }
輸出:
a. 10
b. 10 20
c. 10 30 20
d. 10 30
f. 10
b. 10 20
c. 10 30 20
d. 10 30
f. 10
2013年10月28日 星期一
C++的Container: List使用範例
#include<list> #include<iostream> #include<stdio.h> using namespace std; #define OFFSET_ITERATOR(iterator, offset) {\ iterator = obj1.begin(); \ for (int i=0; i<offset; i++) \ iterator++;\ } //將intertaor移動到offset的節點上 void show_list(); list<int> obj1;//建一個空的list對像 list<int>::iterator j; list<int>::iterator k; int main(void) { for(int i=0;i<10;i++) obj1.push_front(i); show_list(); // Remove first node (pop front) obj1.pop_front(); printf(">obj1.pop_front();\n"); show_list(); // Remove node OFFSET_ITERATOR(j, 4); obj1.erase(j); printf(">obj1.erase(4); //delete node: j-1, i.e. 4\n"); show_list(); // Insert node OFFSET_ITERATOR(j, 4); obj1.insert(j, 55); printf(">obj1.insert(4, 55);\n"); show_list(); // Remove node from j to k-1, where (j, k)=(2, 4) OFFSET_ITERATOR(j, 2); OFFSET_ITERATOR(k, 4); obj1.erase(j, k); printf(">obj1.erase(2, 3); //delete node: j~k-1, i.e. 2~3\n"); show_list(); system("pause"); return 0; } void show_list() { for (j=obj1.begin(); j!=obj1.end(); j++) cout<< *j <<" "; printf("\n"); }
2013年9月23日 星期一
C++ class的應用(一)
目前想到的應用有兩種,寫成class程式會更漂亮:
+ class CLog
+ {
+ public:
+ CLog()
+ {
+ if ( !(fp= fopen("my_log.txt", "w") )
+ )
+ { printf("Warning! Unable to create file\n");}
+ }
+ ~CLog()
+ {
+ fclose(fp);
+ }
+ bool write(const char *p)
+ {
+ return ( fprintf(fp, "%s", p) ? 1: 0 );
+ }
+ bool write(char *p)
+ {
+ return ( fprintf(fp, "%s", p) ? 1: 0 );
+ }
+
+ private:
+ FILE *fp;
+
+ };
- TUI: 在DOS底下切換文字視窗
- 檔案讀寫: (如下)
+ class CLog
+ {
+ public:
+ CLog()
+ {
+ if ( !(fp= fopen("my_log.txt", "w") )
+ )
+ { printf("Warning! Unable to create file\n");}
+ }
+ ~CLog()
+ {
+ fclose(fp);
+ }
+ bool write(const char *p)
+ {
+ return ( fprintf(fp, "%s", p) ? 1: 0 );
+ }
+ bool write(char *p)
+ {
+ return ( fprintf(fp, "%s", p) ? 1: 0 );
+ }
+
+ private:
+ FILE *fp;
+
+ };
2013年9月19日 星期四
C++ : string
Example 1
+ //
+ // Find all key world position in a string
+ //
+
+ #include <stdio.h>
+ #include <iostream> // cout
+ #include <string> // string
+
+ int main ()
+ {
+ std::string str ("xx23xx67xxabcdxx");
+ std::string key ("xx");
+
+ for (int pos = str.find(key) ; pos >= 0; )
+ {
+ printf( "xx at: %d\n", pos);
+ pos = str.find(key, pos+1);
+ }
+
+ return 0;
+ }
___________OUTPUT____________
xx at: 0
xx at: 4
xx at: 8
xx at: 14
Example 2
+ //
+ // string -> char array
+ // char array -> string
+ //
+
+ #include <stdio.h>
+ #include <string>
+ #include <string.h> //strncpy
+
+ int main () {
+ char c1[]="123";
+ std::string cpp(c1); // char array -> string
+ printf("%s\n", c1);
+
+ char c2[10];
+ for(int x=0; x<sizeof(c2); c2[x]=0, x++); // Reset c2
+ strncpy(c2, cpp.c_str(), sizeof(c2)-1); // string -> char array
+
+ printf("%s\n", c2);
+ return 0;
+ }
___________OUTPUT____________
123
123
Example 3
+ #include <stdio.h>
+ #include <string>
+ #include <sstream>
+ // #include <iostream> cout
+ //
+ // Find all key world position in a string
+ //
+
+ #include <stdio.h>
+ #include <iostream> // cout
+ #include <string> // string
+
+ int main ()
+ {
+ std::string str ("xx23xx67xxabcdxx");
+ std::string key ("xx");
+
+ for (int pos = str.find(key) ; pos >= 0; )
+ {
+ printf( "xx at: %d\n", pos);
+ pos = str.find(key, pos+1);
+ }
+
+ return 0;
+ }
___________OUTPUT____________
xx at: 0
xx at: 4
xx at: 8
xx at: 14
Example 2
+ //
+ // string -> char array
+ // char array -> string
+ //
+
+ #include <stdio.h>
+ #include <string>
+ #include <string.h> //strncpy
+
+ int main () {
+ char c1[]="123";
+ std::string cpp(c1); // char array -> string
+ printf("%s\n", c1);
+
+ char c2[10];
+ for(int x=0; x<sizeof(c2); c2[x]=0, x++); // Reset c2
+ strncpy(c2, cpp.c_str(), sizeof(c2)-1); // string -> char array
+
+ printf("%s\n", c2);
+ return 0;
+ }
___________OUTPUT____________
123
123
Example 3
+ #include <stdio.h>
+ #include <string>
+ #include <sstream>
+ // #include <iostream> cout
+
+ int main()
+ {
+ std::stringstream ss;
+ std::string str;
+ //
+ // int -> string
+ //
+ int i= 20;
+ // ss.clear();
+ // str = std::st_string(i);
+ ss << i;
+ ss >> str;
+ std::cout << "int -> string " << str << std::endl;
+ printf("----------------------------\n");
+ //
+ // string -> int
+ //
+ int ival;
+ ss.clear();
+ ss << str;
+ ss >> ival;
+ std::cout << "string -> int " << ival << std::endl;
+
+ return 0;
+ }
+ int main()
+ {
+ std::stringstream ss;
+ std::string str;
+ //
+ // int -> string
+ //
+ int i= 20;
+ // ss.clear();
+ // str = std::st_string(i);
+ ss << i;
+ ss >> str;
+ std::cout << "int -> string " << str << std::endl;
+ printf("----------------------------\n");
+ //
+ // string -> int
+ //
+ int ival;
+ ss.clear();
+ ss << str;
+ ss >> ival;
+ std::cout << "string -> int " << ival << std::endl;
+
+ return 0;
+ }
2013年6月14日 星期五
Intel (x86 Ivy+Panther Point) Power Sequence (開機訊號時序)
這篇介紹的是按下電腦開機按鈕到Bios接手之前的訊號時序邏輯, 以Desktop Motherboard為範例
// 開啟電源後,從SIO(super IO)報到南僑
1. PWRBTN# assert (to SIO)
2. SIO assert SB_PWRBTN# to PCH
// PCH和SIO的handshake
3. PCH assert SUS_WARN# to SIO
4. SIO assert SUS_ACK# to PCH
5. PCH de-assertSLP_LAN# 去叫邏輯電路打開 3.3v_ME
6 PCH de-assertSLP_A# 去叫邏輯電路打開 1.05v_ME
7. PCH de-assert SLP_S4# 信號去開啟dual power rail (這個power rail是由standby power轉來的)
8. PCH de-assert SLP_S3# 信號去開啟ATX power的PSON#
9. ATX power 回 ATX_PWROK給PCH
10. PCH發給CPU, DRAM_pwrok 和 CPU_pwrOK,代表VCORE ready
11. PCH de-assert PLTRST# (有些設計是PCIRST#), 接著就開始Reset一些周邊裝置
done.
// 開啟電源後,從SIO(super IO)報到南僑
1. PWRBTN# assert (to SIO)
2. SIO assert SB_PWRBTN# to PCH
// PCH和SIO的handshake
3. PCH assert SUS_WARN# to SIO
4. SIO assert SUS_ACK# to PCH
5. PCH de-assertSLP_LAN# 去叫邏輯電路打開 3.3v_ME
6 PCH de-assertSLP_A# 去叫邏輯電路打開 1.05v_ME
7. PCH de-assert SLP_S4# 信號去開啟dual power rail (這個power rail是由standby power轉來的)
8. PCH de-assert SLP_S3# 信號去開啟ATX power的PSON#
9. ATX power 回 ATX_PWROK給PCH
10. PCH發給CPU, DRAM_pwrok 和 CPU_pwrOK,代表VCORE ready
11. PCH de-assert PLTRST# (有些設計是PCIRST#), 接著就開始Reset一些周邊裝置
done.
2013年6月6日 星期四
C# : Show Version
string FileLocate = @"\\Address\Filename.exe"; if (File.Exists(FileLocate) == true) { var versionInfo = FileVersionInfo.GetVersionInfo(FileLocate); string version = versionInfo.ProductVersion; // "1.0.0" as default string MessageBoxTitle = "Version"; string MessageBoxContent = version.ToString() + "\n"; DialogResult dialogResult = MessageBox.Show(MessageBoxContent, MessageBoxTitle, MessageBoxButtons.OK); }
訂閱:
文章 (Atom)
