2014-05-22 22 views
0

我的代碼是:與C格式「%3d」等效的C++是什麼?

#include<iostream> 
using namespace std; 
int main() 
{ 
    int count=0; 
    int total=0; 

    while(count<=10) 
    { 
     total=total+count; 
     cout<<"count"<<"="<<count/*<<','*/<<'\t'<<'\t'<<"total"<<"="<<total<<endl; 
     count++; 
    } 
} 
+1

你想做什麼?你期望的輸出是什麼? –

+4

使用I/O操縱器(例如['setw()'](http://en.cppreference.com/w/cpp/io/manip))。 –

+0

當計數= 9總= 45 – shazi

回答

3

所有的C標準庫的設施仍然可用在C++中。你可以寫你的程序是這樣的:

#include <cstdio> 
using std::printf; 

int main() 
{ 
    int count = 0; 
    int total = 0; 

    while (count<=10) 
    { 
    total = total + count; 
    printf("count=%3d, total=%3d\n", count, total); 
    count++; 
    } 
} 

這是我個人的看法是,stdio.h輸出接口幾乎總是更容易使用和產生比iostream輸出接口,特別是對數字的格式化輸出更可讀的代碼(如在這種情況下),所以我會毫不猶豫地這樣做。 iostream的主要優勢在於它可以擴展到格式對象通過operator<<重載,但是像這樣的程序並不需要。

注意,無論是stdio.h也不iostream輸入接口符合目標,由於惡劣的,標準的編纂蝽象定義數字輸入溢出觸發未定義行爲(我不是做這件事!)

+2

(不是我的倒票,但是......)雖然在一個層次上是正確的,但它並不是很有幫助,因爲OP明確要求使用C++''格式。 –

+2

@JonathanLeffler這是一個公平的批評,我很高興有這個答案失去了聲譽遊戲的答案,實際上使用'iostream's,但我認爲這是有價值的文件,你不*有*使用'如果你不想的話,在C++中使用iostream。 – zwol

+0

@Zack這不是你想要使用iostream;這就是說,如果他們能夠避免使用'printf',那麼他們正確的思想中就沒有人會使用它。 –

3

使用iostream格式,你需要包括iomanip頭,並使用setwsetfill,像這樣:

#include <iostream> 
#include <iomanip> 

int main() { 
    using namespace std; 

    int count=0; 
    int total=0; 

    while(count<=10) 
    { 
     total=total+count; 
     cout<<"count"<<"="<<count<<'\t'<<'\t'<<"total"<<"="<<setfill(' ')<<setw(3)<<total<<endl; 
     count++; 
    } 
} 
+0

什麼是'setfill'?據我可以告訴''是默認填充,所以它什麼都不做。 – nwp

+0

'setfill'定義瞭如果輸入「字符串長度」小於'setw'指定的值,將使用哪個字符。使用'setfill'來表示_explicitly_是一種很好的做法,你希望那個字符作爲填充符,因爲它是依賴於編譯器的,它改變了流的狀態,而不僅僅是下一個輸入('setw') - 這意味着如果你有一個像'setfill('0')'以前的調用,你需要_reset_它。 –

+0

好吧,這是有道理的。 – nwp

1

您可以使用setwleftiomanip達到您想要的效果。

#include <iostream> 
#include <iomanip> 

int main() 
{ 
    int count=0; 
    int total=0; 

    while(count<=10) 
    { 
     total=total+count; 
     std::cout << "count = " << std::setw(15) << std::right << count << "total = " << total << std::endl; 
     count++; 
    } 
} 

setw設置與(在這種情況下15)的下一個「COUT印象」和left只設置aligment向左的。

注:作爲建議由@Zack你可以在年底,而不是<< endl<< '\n'。由於<< endl與編寫<< '\n' << flush完全相同,因此在這種情況下不需要刷新。

+0

代之以建議'<<'\ n''。 << << endl'與<< <<'\ n'<< flush'完全相同,在這種情況下不需要刷新。 – zwol

+0

爲什麼你用'setw(15)'代替'setw(3)'?爲什麼「離開」?爲什麼在全局使用namespace std; 這條線應該是類似於 'cout <<「count =」<< setw(3)<< count <<「\ ttotal =」<< setw(3)<< total <<'\ n'; '使其與'%3d'相同。 – nwp

+0

@nwp我發佈的代碼是關於如何實現某些格式的示例。你可以建議'setw(3)',如果你想在這裏確實沒有任何關聯性。關於'left'好吧,你沒有嘗試過代碼嗎?我不這麼認爲。 –

0

在C++中,IO格式化的執行方式與C中的方式相同(因爲所有的C功能都在C++中),或者在頭中顯示std::setwstd::setprecission和其他C++ manipulators

因此,無論這將是罰款:

#include <cstdio> 

int main() 
{ 
    int count=0; 
    int total=0; 

    while(count <= 10) 
    { 
     total += count; 
     printf("count = %3d, total = %3d\n", count++, total); 
    } 
} 

#include <iostream> 
#include <iomanip> 

int main() 
{ 
    int count=0; 
    int total=0; 

    while(count <= 10) 
    { 
     total += count; 
     std::cout << "count = " << std::setw(15) << 
         count++ << "total = " << total << std::endl; 
    } 
    return 0; 
} 

你也可以做的imbue自定義格式自定義方面適用於locale(即延長的std :: numpunct)。

http://ideone.com/nHfTL6

+0

老兄,你已經複製我的答案!你的代碼甚至不使用你建議的函數('setprecission')。 –

+0

@RaydelMiranda你的意思是的內容? – 4pie0

相關問題