哈囉大家好,我是不知道為什麼開始都不發輕鬆文章的過路君子
最近找到了不錯的遊戲,找時間來寫如何呢?
以下的程式會盡可能#include最少的標頭檔來完成所需的效果。
當然某些小功能可以透過更多的標頭檔來更簡單的完成,一切都依照個人喜好,小編僅提供一種想法,畢竟簡單和隨心所欲才是寫程式最大的樂趣嘛!
限定小數輸出位數
#include<iostream>
using namespace std;
int main()
{
double a = 3.1415926;
cout << a << '\n';
cout.precision(3);
cout << a;
return 0;
}
輸出
3.14159
3.14
使游標從某處開始
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout << "Type a word: ______\b\b\b\b\b\b";
cin >> s;
cout << s;
return 0;
}
輸出
Type a word: Hoo___
Hoo
只有在標頭檔(.h)的內容還未包含到程式中時
才會包含這些內容
#ifndef HEAD-NAME #define HEAD-NAME //Header file contents go here #endif
判斷char內文字型態(C也可用)
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
char str[5] = "o 3!";
for(int b=0;11>b;b++)
{
if(isalpha(str[b])) cout << "type:alpha " << str[b] << '\n'; //是否為英文字母
if(isdigit(str[b])) cout << "type:digit " << str[b] << '\n'; //是否為數字
if(isspace(str[b])) cout << "type:space " << str[b] << '\n'; //是否為空白
if(ispunct(str[b])) cout << "type:punct " << str[b] << '\n'; //是否為標點符號
}
return 0;
}
輸出
type:alpha o
type:space
type:digit 3
type:punct !
定義常數
//C風格的定義方式 #define UNI 6 //C++風格的定義方式 const int UNI = 6; //C++定義常數的優點: //一、是允許型態檢查 //二、可以對陣列或是結構使用
同位組合(union)
#include<iostream>
using namespace std;
union SomeValue
{
int hello;
int world;
int uiuxp;
};
int main()
{
SomeValue merge;
merge.hello = 60;
cout << merge.world << ' ' << merge.uiuxp << '\n';
merge.uiuxp = 174;
cout << merge.hello << ' ' << merge.world << '\n';
return 0;
}
輸出
60 60
174 174
列舉(不得為數字開頭)
#include<iostream>
using namespace std;
enum Value
{
apple,
Ak47,
o6719,
dfj234nfmd,
};
int main()
{
Value our;
our = dfj234nfmd;
switch(our)
{
case apple:
case o6719:
case Ak47:
cout << "yummy!";
break;
case dfj234nfmd:
cout << "What is this?!";
break;
}
return 0;
}
輸出
重新定義變數名稱
#include<iostream>
#include<string>
using namespace std;
int main()
{
typedef string ux;
ux ui("user");
cout << ui;
return 0;
}
輸出
串接區域class
#include<iostream>
using namespace std;
int main()
{
class vov
{
public:
int *pa;
struct vov *next;
}begin, began, begun;
begin.next = &began;
began.next = &begun;
begun.pa = new int;
*begun.pa = 6185;
cout << *begin.next->next->pa;
delete begun.pa;
return 0;
}
輸出
讀取命令列指令
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
for(int a=1;argc>a;a++) cout << argv[a] << '\n';
return 0;
}
輸入
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
for(int a=1;argc>a;a++) cout << argv[a] << '\n';
return 0;
}
輸出
something
about
this.
