公告版位
星落的瞬間!放棄的後悔是永遠!

目前分類:各種C++(CMD.EXE中) (43)

瀏覽方式: 標題列表 簡短摘要

假設 CloudSet 是一個類別
然後你有一個 CloudSet* 叫 c1

然在有個函數長這樣
void CreateCloud(Ogre::SceneManager* SceneMgr, CloudSet * cloud)

裡面呼叫了
cloud = new CloudSet(SceneMgr,"cloud1");

可是在外面的c1卻不會改變,除非你的函數是

讓地獄深紅的天亮 發表在 痞客邦 留言(2) 人氣()

不錯的演算法網站
http://www.csie.ntnu.edu.tw/~u91029/index.html

程式語言比較 C的優點是超快的/C++的優點是超快的物件導向
缺點大家都知道就不多說了。
http://www.approximity.com/ruby/Comparison_rb_st_m_java.html

目前覺得c++是目前最強的靜態語言了,而且可以與其它的動態(腳本)語言快速溝通。
除了c/c++外,目前的世界排名前幾名都是動態語言了。


讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

#include <vector>
#include <iostream>

int main()
{
    using namespace std;
    vector<int> numAry;
    vector<int>::iterator pos;
    numAry.push_back(1);
    for (int i=0;i<10000;i++)

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

#include <vector>
#include <iostream>

    using namespace std;
    class A
    {
    public:
        A(){cout << "A is Construct" << endl;}
        ~A(){cout << "A is Destruct" << endl;}
    };

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

恩,最近對sourceforge.net失望了,不如參考novus大大的來改,
在psdn看到也有結構清楚的,但廢碼太多,不如novus大大來的簡潔有力!

 

// design by novus 2009 // some right reserved // design by 天亮damody 2009 // 隨便抄吧! #include <iostream> #include <string> #include <sstream> #include <cstdlib> #include <cmath> using namespace std; double calculate(string s); double Eval3(istream& iss); double Eval2(istream& iss); double Eval1(istream& iss); double Eval0(istream& iss); string redefine(string str,const char * a,const char * b); int main() { string input = "(x^2+3*y^2)*e(-x^2-y^2)"; string tmp; char strx[100],stry[100]; /*cin >> input;*/ //輸入公式 //印出一個面的值,準備來畫3D了 for (double x = -2;x < 2;x +=0.1) { for (double y = 0;y < 1;y +=0.1) { sprintf(strx,"(%lf)",x); sprintf(stry,"(%lf)",y); tmp = redefine(input,"x",strx); tmp = redefine(tmp,"y",stry); printf("%2.2lf ",calculate(tmp)); } cout << endl; } //一般的運算 //直到按q才離開 for (cin >> input;input != "q";cin >> input) { cout << calculate(input) << endl; } system("pause"); } //前置字串處理器 double calculate(string s) { istringstream iss; s = redefine(s,"sin","s"); s = redefine(s,"cos","c"); s = redefine(s,"tan","t"); s = redefine(s,"exp","e"); s = redefine(s,"e^","e"); //cout << s << endl; iss.str(s); return Eval0(iss); } //前置字串處理函數 string redefine(string str,const char *a,const char *b) { for (;string::npos != str.find(a);) { str.replace(int(str.find(a)),strlen(a),b,strlen(b)); } return str; } //優先權愈高愈先算 //優先權3的運算 double Eval3(istream& iss) { double Eval0(istream& iss); double res=0; if (iss.peek() == '(' && iss.get()) { res = Eval0(iss); iss.peek() == ')' && iss.get(); } else if (iss.peek() == 's' && iss.get()) res = sin(Eval2(iss)/180*4*atan(1)); else if (iss.peek() == 'c' && iss.get()) res = cos(Eval2(iss)/180*4*atan(1)); else if (iss.peek() == 't' && iss.get()) res = tan(Eval2(iss)/180*4*atan(1)); else if (iss.peek() == 'e' && iss.get()) res = exp(Eval2(iss)); else { iss >> res; } return res; } //優先權2的運算 double Eval2(istream& iss) { double res = Eval3(iss); if (iss.peek() == '^' && iss.get()) res = pow(res,Eval2(iss)); return res; } //優先權1的運算 double Eval1(istream& iss) { double res = Eval2(iss); while (iss.peek() == '*' || iss.peek() == '/') (iss.get() == '*')? (res*=Eval2(iss)): (res/=Eval2(iss)); return res; } //優先權0的運算 double Eval0(istream& iss) { double res = Eval1(iss); while (iss.peek() == '+' || iss.peek() == '-') res +=(iss.get() == '+')? Eval1(iss): -Eval1(iss); return res; }

 


讓地獄深紅的天亮 發表在 痞客邦 留言(2) 人氣()

第一題 (magic.c) #include <stdio.h> #include <stdlib.h> int question1() { int matA[10][10] = {0}; int mat3[3][3] = { {7,11,6}, { 7, 8,9}, {10, 5,9} }; int i,j; printf("What this matrix's magic number?\n"); for (i = 0;i < 3;i++) { for (j = 0;j < 3;++j) { matA[i][j]=mat3[i][j]; printf("%3d",matA[i][j]); } printf("\n"); } printf("This matrix's magic is %d.\n",magic(matA,3)); system("pause"); return 0; } int magic(int A[][10], int n) { int sum=0,i,j,tmp; for (i = 0;i < n;++i) { sum+=A[0][i]; } for (i = 0;i < n;++i) { tmp = 0; for (j = 0;j < n;++j) { tmp += A[i][j]; } if (tmp != sum) return 0; tmp = 0; for (j = 0;j < n;++j) { tmp += A[j][i]; } if (tmp != sum) return 0; } tmp=0; for (i = 0;i < n;++i) { tmp += A[i][i]; } if (tmp != sum) return 0; tmp=0; for (i = 0;i < n;++i) { tmp += A[n-i-1][i]; } if (tmp != sum) return 0; return sum; } 第二題 (MinMaxAvg.c) #include <stdio.h> #include <stdlib.h> void MinMaxAvg(int values[], int n, int* min, int* max, double* avg); int question2() { int v[] = {0,11,22,33,44,55,66,77,88,99}; int min,max,i; double avg; MinMaxAvg(v,10,&min,&max,&avg); printf("What is the min,max and avg in this Array?\n"); for (i = 0;i < 10;++i) { printf("%3d",v[i]); } printf("\nmin= %d , max= %d , avg = %lf\n",min,max,avg); system("pause"); return 0; } void MinMaxAvg(int values[], int n, int* min, int* max, double* avg) { int i,sum = 0,_min = values[0],_max = values[0]; double _avg; for (i = 0;i < n;++i) { _min = (_min > values[i])?values[i]:_min; _max = (_max < values[i])?values[i]:_max; sum += values[i]; } _avg = (double)sum / n; *min = _min; *max = _max; *avg = _avg; } 第三題 (StringCompare.c) #include <stdio.h> #include <stdlib.h> #include <string.h> int question3() { char string1[100] = {0}; char string2[100] = {0}; printf("Enter string1:"); scanf("%s",string1); printf("Enter string2:"); scanf("%s",string2); printf("%s ",string1); switch (strcmp(string1,string2)) { case 1: printf(">"); break; case 0: printf("="); break; case -1: printf("<"); break; } printf(" %s\n",string2); system("pause"); return 0; } 第四題 (Convert.c) #include <stdio.h> #include <stdlib.h> #include <string.h> char* NumToStr(long n); char* Convert(const long n); int question4() { long input; printf("Enter any 1-9 digit number: "); scanf("%ld",&input); printf("%s\n",Convert(input)); system("pause"); return 0; } char* Convert(const long n) { char tmp[200] = ""; if (n>=0) { if (n <= 20) { return NumToStr(n); } else if (n < 100) { if (n % 10 == 0) { return NumToStr(n); } else { strcat(tmp,NumToStr(n/10*10)); strcat(tmp,"-"); strcat(tmp,NumToStr(n%10)); return tmp; } } else if (n < 1000) { if (n % 100 == 0) { strcat(tmp,NumToStr(n/100)); strcat(tmp," "); strcat(tmp,NumToStr(100)); return tmp; } else { strcat(tmp,NumToStr(n/100)); strcat(tmp," "); strcat(tmp,NumToStr(100)); strcat(tmp," "); strcat(tmp,Convert(n%100)); return tmp; } } else if (n < 1000000) { if (n % 1000 == 0) { strcat(tmp,Convert(n/1000)); strcat(tmp," "); strcat(tmp,NumToStr(1000)); return tmp; } else { strcat(tmp,Convert(n/1000)); strcat(tmp," "); strcat(tmp,NumToStr(1000)); strcat(tmp," "); strcat(tmp,Convert(n%1000)); return tmp; } } else if (n >= 1000000) { if (n % 1000000 == 0) { strcat(tmp,Convert(n/1000000)); strcat(tmp," "); strcat(tmp,NumToStr(1000000)); return tmp; } else { strcat(tmp,Convert(n/1000000)); strcat(tmp," "); strcat(tmp,NumToStr(1000000)); strcat(tmp," "); strcat(tmp,Convert(n%1000000)); return tmp; } } } else { return "I don't know"; } } char* NumToStr(const long n) { switch (n) { case 0: return "zero"; break; case 1: return "one"; break; case 2: return "two"; break; case 3: return "three"; break; case 4: return "four"; break; case 5: return "five"; break; case 6: return "six"; break; case 7: return "seven"; break; case 8: return "eight"; break; case 9: return "nine"; break; case 10: return "ten"; break; case 11: return "eleven"; break; case 12: return "twelve"; break; case 13: return "thirteen"; break; case 14: return "forteen"; break; case 15: return "fifteen"; break; case 16: return "sixteen"; break; case 17: return "seventeen"; break; case 18: return "eighteen"; break; case 19: return "nineteen"; break; case 20: return "twenty"; break; case 30: return "thirty"; break; case 40: return "forty"; break; case 50: return "fifty"; break; case 60: return "sixty"; break; case 70: return "seventy"; break; case 80: return "eighty"; break; case 90: return "ninety"; break; case 100: return "hundred"; break; case 1000: return "thousand"; break; case 1000000: return "million"; break; } }

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()



題目:設計稱為car的結構,記錄有關汽車的下列資訊:其製造商存在字元陣鬥的字串中,以及製造的年份,為一整數。寫一程式,先詢問使用者有多少汽車要列進目錄,然後用new配置此數量之car結構的動態陣列。再來,提示使用者輸入每一輛車的製造商(可能由一個以上的單字組成)和製造年份。注意,它要交互讀入字串和數字。最後,顯示每一輛汽車的資訊。程式的幸後結果有如下述:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

改進兩點,
一.用根號縮小判斷範圍
二.用+2取代+1減少要判斷的數
/*
  Name: 質因數判斷加強版,感謝某網友提供想法
  Date: 19/06/08 09:59
*/
#include <iostream>
#include <cmath>
int main()

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

因為常常忘記,但這功能很實用所以記下來~~

很簡單,就是在原型後面加上"= VALUE" VALUE是你定的常數,
限制是預設引數只能從右到左設定,以下源碼實現了陣列預設引數。

EX:

///////////////////////////////////////////////////////////

// args.cpp

// Generated by Code::Blocks svn build 5456

// Created on: 2009/3/31

// Original author: 天亮damody

// contry: ROC

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

因為常常忘記,但這功能很實用所以記下來~~

很簡單,就是在原型後面加上"= VALUE" VALUE是你定的常數,
限制是預設引數只能從右到左設定,以下源碼實現了陣

EX:

///////////////////////////////////////////////////////////

// args.cpp

// Generated by Code::Blocks svn build 5456

// Created on: 2009/3/31

// Original author: 天亮damody

// contry: ROC

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

因為常常忘記,但這功能很實用所以記下來~~

很簡單,就是在原型後面加上"= VALUE" VALUE是你定的常數,
限制是預設引數只能從右到左設定,以下源碼實現了陣列預設引數。

EX:

/////////////////////////////////////////////////////////// // args.cpp // Generated by Code::Blocks svn build 5456 // Created on: 2009/3/31 // Original author: 天亮damody // contry: ROC // capability: How to use default argument // /////////////////////////////////////////////////////////// #include <iostream> #include <stdlib.h> #include <string> using namespace std; string hobbys[] = {"sleep","eat"}; int I_need_girlfriend(const string name = "sleep beauty", unsigned int age = 18, string hobby[] = hobbys, unsigned int length = 1); int main() { // use default argument string hobby[] = {"sport","read"}; I_need_girlfriend("perfect girl", 20, hobby, 2); // use default argument string normal_hobby = "string"; I_need_girlfriend("normal girl", 20,&normal_hobby); // not use default argument I_need_girlfriend(); system("pause"); } int I_need_girlfriend(const string name, unsigned int age, string hobby[], unsigned int length) { cout << name << endl << "age = " << age << endl << "hobby = "; for (int i = 0;i < length;i++) { cout << hobby[i] << ", "; } cout << "\b\b.\n\n"; }

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

// C RunTime 標頭檔

#include <stdlib.h>

#include <iostream>

#include <time.h>

#include <string.h>

 

//撲克牌結構

typedef struct {

    char suit[10];

    int face;

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

// C RunTime 標頭檔 #include <stdlib.h> #include <iostream> #include <time.h> #include <string.h> //撲克牌結構 typedef struct { char suit[10]; int face; }pockcard; //玩家結構 typedef struct { char name[20]; int value; int win; pockcard card[2]; }player; //副程式 //交換牌 void swap(pockcard pock[52],int a, int b); //拿一付新牌 void TakeNew(int iDeck[][13],pockcard pock[52]); //洗牌 void wash(pockcard pock[52]); //亮出所有牌 void show(int iDeck[][13], const char *ccFace[],pockcard pock[52]); //亮每個人的牌 void showEachone(player *Pman,int iPeoplenum, const char *ccFace[]); //發牌 int shffle(player *Pman, int iPeoplenum,pockcard *pock,int now); //算分 int countgrade(player *Pman, int iPeoplenum); //主程式 int main() { using namespace std; const char *face[13]={"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; int iPeoplenum,t,now = 0; int pockpie[4][13] = {0}; char ch = '\0'; pockcard pock[52] = {0}; cout << "How many people want to play?:> "; cin >> iPeoplenum; //得到人數 player *PM = new player[iPeoplenum]; //初始化每個人的資料 for (int i = 0;i < iPeoplenum;i++) { cout << "Player" << i + 1 << "'s name:>"; cin >> PM[i].name; PM[i].value = 0; PM[i].win = 0; } srand(time(0)); //拿一付新牌 TakeNew(pockpie, pock); //洗牌 wash(pock); //亮出所有牌 show(pockpie, face, pock); printf("\n\n"); for (;ch != 'n';) { //發牌 now = shffle(PM, iPeoplenum, pock ,now); cout << "=============================\n"; //亮每個人的牌 showEachone(PM, iPeoplenum, face); t = countgrade(PM, iPeoplenum); //輸出這回合的成績 cout << PM[t].name << " win!\n"; for (int i = 0;i < iPeoplenum;i++) cout << PM[i].name << " has " << PM[i].value << " value. "; cout << "\n"; for (int i = 0;i < iPeoplenum;i++) printf("%5s : %3d\n",PM[i].name,PM[i].win); //看牌用完了沒?來繼續 if (!(now + iPeoplenum*2 < 53)) { now = 0; //洗牌 wash(pock); //亮牌 show(pockpie, face, pock); printf("\n\n"); } for (int i = 0;i < iPeoplenum;i++) PM[i].value = 0; cout << "Play again?(y/n):"; cin >> ch; } system("PAUSE"); return 0; } /拿一付新牌 void TakeNew(int iDeck[][13],pockcard pock[52]) { int row, column, card = 0; //照順序做出張牌 for (row = 0;row < 4;row++) { for (column = 0;column < 13;column++) { ++card; switch (row) { case 0: strcpy(pock[row*13+column].suit,"Spades"); pock[row*13+column].face = column; break; case 1: strcpy(pock[row*13+column].suit,"Hearts"); pock[row*13+column].face = column; break; case 2: strcpy(pock[row*13+column].suit,"Diamonds"); pock[row*13+column].face = column; break; case 3: strcpy(pock[row*13+column].suit,"Clubs"); pock[row*13+column].face = column; break; } iDeck[row][column] = card; } } } //洗牌 oid wash(pockcard *pock) { int r1, r2; //取兩個數字交換做次 for (int times= 0; times < 100; times++) { r1 = rand() % 52; r2 = rand() % 52; swap(pock,r1,r2); } } //亮出所有牌 void show(int iDeck[][13], const char *ccFace[],pockcard pock[52]) { int card, g = 0; //從結構中取牌,把張牌依序亮出 for (card = 0; card < 52; card++) { ++g; printf("%2d : %2s of %s\n",g, ccFace[pock[card].face],pock[card].suit); } } /發牌 int shffle(player Pman[], int iPeoplenum,pockcard *pock,int now) { int j = 0; for (int i = now;i < now + iPeoplenum * 2;i++) { Pman[j].card[0] = pock[i]; ++i; Pman[j].card[1] = pock[i]; ++j; } return now + iPeoplenum * 2; } //亮每個人的牌 void showEachone(player *Pman,int iPeoplenum, const char *ccFace[]) { int g = 0; ; for (int i = 0;i < iPeoplenum;i++) { std::cout << Pman[i].name << "'s card :" << "\n"; printf("1: %2s of %s ",ccFace[Pman[i].card[0].face],Pman[i].card[0].suit); printf("2: %2s of %s\n",ccFace[Pman[i].card[1].face],Pman[i].card[1].suit); } } //算分 int countgrade(player *Pman, int iPeoplenum) { int max,index = 0,i; for (i = 0;i < iPeoplenum;i++) { if (0 == Pman[i].card[0].face) Pman[i].value += 14; else Pman[i].value += (Pman[i].card[0].face+1); if (0 == Pman[i].card[1].face) Pman[i].value += 14; else Pman[i].value += (Pman[i].card[1].face+1); } max = Pman[0].value; for (i = 0;i < iPeoplenum;i++) if (max < Pman[i].value) { max = Pman[i].value; index = i; } Pman[index].win++; return index; } //交換牌 void swap(pockcard pock[52],int a, int b) { pockcard t = pock[a]; pock[a] = pock[b]; pock[b] = t; }

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

int scanf(const char * __format, ...); /* printf, scanf definitions */ int printf(const char * __format, ...); int main(void) { int m; char c; printf("Enter some input> "); scanf("%d", &m); scanf("%c",&c); printf("The input entered is %d and %c.\n", m, c); return (0); }

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

只有二選一時,可以用(statement1 == statement2)來取true or false,
如果是1 、 2二選一,可以用--value來實作。 


讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

為什麼不會浪費記憶體呢?
因為他是用new 一個char[1]陣列,要改變時就砍掉舊資料,
再用new來的空間存新資料。 

因為網誌問題所以重複了。


讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

為什麼不會浪費記憶體?
因為他用了new來實作,宣告時不指定其值,會先產生一個char [1]的陣列,
然後,改變資料時,先砍掉舊資料,再把新資料用new找到的記憶體儲存,
 


讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

http://damody.pixnet.net/blog/post/25602994

這篇比較詳細。


讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

改進兩點,
一.用根號縮小判斷範圍
二.用+2取代+1減少要判斷的數

/* Name: 質因數判斷加強版,感謝某網友提供想法 Date: 19/06/08 09:59 */ #include <iostream> #include <cmath> int main() { using namespace std; double Num2; unsigned long Num,bh,x; char a; bool temp; again: cout << "請輸入一個正整數:__________\b\b\b\b\b\b\b\b\b\b"; cin >> Num; Num2 = sqrt(Num); temp = true; if (Num == 2) goto exit; if (Num % 2 == 0 or Num <2){ temp = false; goto exit; } for (unsigned long i = 3;i <= Num2;i = i + 2){ if (Num % i == 0){ temp = false; goto exit; } } exit: if (temp == false) cout << "\n不是質數。\n"; else cout << "\n是質數。\n"; cout << "是否繼續?(y/n)"; cin.get(); a = getchar(); system("cls"); if (a == 'y') goto again; else //system("pause"); return 0; }

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()



題目:設計稱為car的結構,記錄有關汽車的下列資訊:其製造商存在字元陣鬥的字串中,以及製造的年份,為一整數。寫一程式,先詢問使用者有多少汽車要列進目錄,然後用new配置此數量之car結構的動態陣列。再來,提示使用者輸入每一輛車的製造商(可能由一個以上的單字組成)和製造年份。注意,它要交互讀入字串和數字。最後,顯示每一輛汽車的資訊。程式的幸後結果有如下述:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()

«12 3