乙個C 風格的C 程式

2021-12-29 19:45:53 字數 2254 閱讀 3386

寫c#程式就是在設計乙個類!

先看乙個c#程式(表示式求值):

[csharp] 

using system; 

using system.collections.generic; 

using system.linq; 

using system.text; 

namespace consoleapplicationcal 

,             , 

, , 

, ; 

public static char precede(char a, char b) 

switch (b) 

return (precede_matrix[i, j]); 

} public static double operate(double a, char oper, double b) 

} public static bool isoperand(char c) 

static void main(string args) 

string doublestring = str.substring(startindex, length); 

//     operand = atof(&str[i - 1]); //  把從c開頭的數轉化成double 

opnd.push(double.parse(doublestring));   

c = str[i++]; 

} else                            // c is operator or delimiter 

} } 

//  optr棧的棧頂元素和當前讀入的字元均為「#」 

//  即「#」=「#」時整個表示式求值完畢 

console.writeline(opnd.peek()); 

}      

} } 

} 2、下面是用c++改寫的:

[cpp]

#include  

#include  

#include  

using namespace std; 

// 因為只有靜態常量整型資料成員才可以直接(也必須)在類中初始化, 

// 故下面這個全域性陣列定義在類(命名空間)的外面,待解決 

char precede_matrix[7][7] = 

,     , 

, , 

, ; 

namespace consoleapplicationcal   

public: static char precede(char a, char b) 

switch (b) 

return (precede_matrix[i][j]); 

} public: static double operate(double a, char oper, double b) 

} public: static bool isoperand(char c) 

public: static void main(void) 

string doublestring = str.substr(startindex, length); 

//     operand = atof(&str[i - 1]); //  把從c開頭的數轉化成double 

char **endptr = null;   // 沒什麼用,只是為了strtod的傳參 

opnd.push(strtod(doublestring.c_str(), endptr)); 

c = str[i++]; 

} else                            // c is operator or delimiter 

} } 

//  optr棧的棧頂元素和當前讀入的字元均為「#」 

//  即「#」=「#」時整個表示式求值完畢 

cout << opnd.top() << endl; 

} } 

}; }  

int main(int argc, char **ar**) 

可以看到,上面的c++程式主體就是在設計乙個類,而c#將這種特點發揮到了極致,c#程式是從main()開始執行的,完全在類中進行一切。

另外,上面的c++程式開頭的全域性變數初始化之所以不能定義在類裡面,是因為這是在設計乙個類!往往不能在類中直接對資料成員初始化,只有靜態常量整型資料成員才可以直接(也必須)在類中初始化。

最後,發現c#中的建構函式挺妙的,東西多。

乙個C 風格的C 程式

寫c 程式就是在設計乙個類!先看乙個c 程式 表示式求值 using system using system.collections.generic using system.linq using system.text public static char precede char a,char ...

分享乙個Metro風格的程式

前言 環境專案結構 總結最近在學微軟的rx,這個基於非同步和事件的類庫。rx是由observables linq schedulers 組成。它的優勢在於處理i o操作的時候,能夠很好的處理非同步操作。於是出於練習,做了乙個進銷存系統,至於為什麼選進銷存系統。因為前段時間裡的園友都分享了一些進銷存的...

乙個c語言程式

include include include void shuffle int wdeck 4 13 void deal int wdeck 4 13 char wface 13 char wsuit 4 int main char wface 13 int wdeck 4 13 printf 這...