#include <iostream> #include <stdlib.h> using namespace std; int main() { cout<<"Hello World!"<<endl; system("pause"); return 0; }
/*編譯成test.exe後,假設存放在: D:\cpp files\test 打開命令提示字元(按下win + R,打cmd),輸入: D:\cpp files\test\test.exe 87 */ #include <iostream> using namespace std; int main(int argc,char *argv[]) { int i; cout<<"argc = "<<argc<<endl; for(i=0;i<argc;i++) cout<<argv[i]<<endl; return 0; }
#include <iostream> #include <cstdlib> #include <ctime> #define SIZE 20 using namespace std; int search(int [ ],int,int,int); int main(void) { int i,a[SIZE],from=0,key; srand(time(NULL)); //亂數產生陣列元素值 for(i=0;i<SIZE;i++) a[i] = rand( ) % 10 + 1; cout<<"請輸入欲搜尋的值(1~10)"; cin>>key; while(from < SIZE) { int ans; ans = search(a,from,SIZE,key); if(ans == -1) { if(!from) cout<<key<<" not found\n"; break; } else { cout<<key<<" found at a["<<ans<<"] = "<<a[ans]<<endl; from = ans + 1; } } return 0; } int search(int a[ ],int from,int size,int key) { int i; for(i=from;i<size;i++) if(a[i] == key) return i; return -1; }
#include <iostream> using namespace std; void vset(int,int); void rset(int*,int); int main(void) { int x=0,*p; //宣告變數x及整數指標p p = &x; //取得變數x的位址(將p指向x) vset(x,1); cout<<"x = "<<x; rset(p,1); //將指標當作引數來傳遞進函式 cout<<"x = "<<x; return 0; } void vset(int x,int y) { x = y; } void rset(int *p,int y) { *p = y; //間接存取變數x }
#include <iostream> using namespace std; int Mystrcmp(char *,char *); int main(void) { int result; char word1[ ] = "I like C"; //字串1 char word2[ ] = "This is fun"; //字串2 result = Mystrcmp(word1,word2); if(!result) cout<<"word1 equal word2\n"; else cout<<"word1 does not equal word2\n"; return 0; } int Mystrcmp(char *str1,char *str2) { int i; for(i=0;!(*(str1+i) == '\0' && *(str2+i) == '\0'); i++) if(*(str1+i) != *(str2+i)) return -1; return 0; }
#include <iostream> using namespace std; void Mystrcpy(char *,char *); int main(void) { int result; char word1[ ] = "I like C"; char word2[ ] = "This is fun"; Mystrcpy(word1,word2); cout<<"word2 = "<<word2<<endl; return 0; } void Mystrcpy(char *str1,char *str2) { int i; for(i=0;*(str1+i)!='\0';i++) *(str2+i) = *(str1+i); *(str2 + i) = '\0'; }
#include <iostream> #include <fstream> using namespace std; char* encode(char*); char* decode(char*); int main(void) { char ch,str[80]; cout<<"請輸入字串:"; gets(str); cout<<"您要 1)加密 2)解密 :"; cin>>ch; if(ch == '1') { cout<<"After encode : "; cout<<encode(str); } else if (ch == '2') { cout<<"After decode :"; cout<<decode(str); } else cout<<"Unknown input"; return 0; } char* encode(char *str) { char *r=str; while(*str) { *str = *str + 13; str++; } return r; } char* decode(char *str) { char *r=str; while(*str) { *str = *str - 13; str++; } return r; }