2012-02-25 283 views
4

我需要編寫一個程序,它需要從文件中獲取2個整數。然後它必須從這兩個數字中構建一個金字塔。它看起來像這樣:C++金字塔的數字

enter image description here

我寫的代碼和它的作品,我想,我打賭不能想辦法如何使它看起來像一個金字塔。

這裏是它的外觀,當我做到這一點:

enter image description here

這是我的代碼:

#include <fstream> 
using namespace std; 

    int main(){ 

     ifstream inFile("Duomenys.txt"); 
     ofstream outFile("Rezultatai.txt"); 
     int N,M,smth,suma=0; 

     inFile >> N >> M; 
     smth=N; 

     while(N<=M){ 
      for(int i=smth;i<=N;i++){ 
       outFile<<i<<" "; 
       suma+=i; 
       if(i==N){ 
        for(int i=N-1;i>=smth;i--){ 
         outFile<<i<<" "; 
         suma+=i; 
        } 
       } 
      } 
      outFile<<endl; 
      N++; 
     } 
     outFile<<endl<<"Skaiciu suma: "<<suma; 

     inFile.close(); 
     outFile.close(); 
     return 0; 
    } 

所以我的問題是,如何使我的回答會是金字塔形狀像例子?

+9

計算最後一行的寬度,然後使用該信息將每一行居中。 – 2012-02-25 11:50:39

+0

我想知道如何做到這一點。<。< – RnD 2012-02-25 11:54:03

+0

你可能也想看看'width'參數。 – 2012-02-25 11:56:19

回答

0
#include <fstream> 
#include <iostream> 
#include <iomanip> 
#include <assert.h> 

using namespace std; 

template <class T> 
int numDigits(T number) 
{ 
    int digits = 0; 
    while (number) { 
     number /= 10; 
     digits++; 
    } 
    return digits; 
} 

int main() 
{ 
    ifstream inFile("Duomenys.txt"); 
    ofstream outFile("Rezultatai.txt"); 
    int N,M,smth,suma=0; 

    inFile >> N >> M; 
    smth=N; 

    // assuming positive numbers 
    assert(N>=0 && M>=0); 
    // this will be the size of each printed number 
    int nd = numDigits<int>(M)+1; 

    while(N<=M){ 
     for(int i=N;i<=M;i++) 
      outFile << setw(nd) << " "; 
     for(int i=smth;i<=N;i++){ 
      outFile << setw(nd) << i; 
      suma+=i; 
      if(i==N){ 
       for(int i=N-1;i>=smth;i--){ 
        outFile << setw(nd) << i; 
        suma+=i; 
       } 
      } 
     } 
     outFile<<endl; 
     N++; 
    } 
    outFile<<endl<<"Skaiciu suma: "<<suma; 

    inFile.close(); 
    outFile.close(); 
    return 0; 
} 
+0

這是工作,如果所有的數字打印在一個獨特的字符串 – 2012-02-25 12:36:28

0
inFile >> N >> M; 
     smth=N; 

     while(N<=M){ 
      for(int position=0;position<(M-N);position++){ // doesn't work if M<N obviously 
       for(int digit=(smth+position);digit;digit=digit/10){ 
        outFile<<" "; 
       } 
       outFile<<" "; // this is to complement the spacer for each digit in your code 
      } 
      for(int i=smth;i<=N;i++){ 
       outFile<<i<<" "; 
       ... 
+0

這絕對是這次大聲笑 – Beeblbrox 2012-02-25 13:06:19

0

首先你必須計算你的第二個數字的位數。它是如此容易。然後,您可以使用以下公式計算金字塔深度:(第二個數字 - 第一個數字)+1。之後,您可以確定在最後一行中,您將擁有的最大數字數((第二個數字 - 第一個數字)* 2 + 1)*數字計數=金字塔頭部的x。所以你應該在(x,y)=(金字塔頭的x,...)處打印金字塔的頭部。

0

要確定最後一行的長度,可以將輸出寫入std::stringstream並獲取長度爲myStrStream.str().size()(然後將字符串流的內容打印到std::cout或outFile),或者您可以分別計算最後一行的所有項目的長度,然後求和,然後求和,包括空格。我認爲第一種方法更簡單。

最簡單的方法可能是回溯。

0

這也取決於每個數字的數字。 假設每個數字都有兩位數字,就足以爲每次迭代添加一定數量的空間。 這個數字在你的情況是5的第一次迭代過程中,並最終爲零:

#include <fstream> 
using namespace std; 

int main(){ 

    ifstream inFile("Duomenys.txt"); 
    ofstream outFile("Rezultatai.txt"); 
    int N,M,smth,suma=0; 

    inFile >> N >> M; 
    smth=N; 

    while(N<=M){ 
     for(int i=smth;i<=N;i++){ 
      outFile<<i<<" "; 
      suma+=i; 
      if(i==N){ 
       for(int i=N;i<M;i++) 
        cout << " "; 
       for(int i=N-1;i>=smth;i--){ 
        outFile<<i<<" "; 
        suma+=i; 
       } 
      } 
     } 
     outFile<<endl; 
     N++; 
    } 
    outFile<<endl<<"Skaiciu suma: "<<suma; 

    inFile.close(); 
    outFile.close(); 
    return 0; 
} 

另一種方法是使用了iomanip的運輸及工務局局長,但這種情況下,你必須寫所有數字的字符串並打印全每次都有字符串。

0
#include<fstream> 
using namespace std; 
ofstream outFile("output"); 
void printSpace(int a){ 
    string spaces(a,' '); 
    outFile<<spaces; 
} 
int main(){ 
    int N=1,M=11,smth=4,suma=0; 
    int l=2*(M-N); 
    while(N<=M){ 
     printSpace(l); 
     for(int i=smth;i<=N;i++){ 
      outFile<<i<<" "; 
      suma+=i; 
      if(i==N){ 
       for(int i=N-1;i>=smth;i--){ 
        outFile<<i<<" "; 
        suma+=i; 
       } 
      } 
     } 
     l-=2; 
     outFile<<endl; 
     N++; 
    } 
    outFile<<endl<<"Skaiciu suma: "<<suma; 

    outFile.close(); 
    return 0; 
}