Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

การพิมพ์ Bash รูปแบบต่างๆใน C ++


บทความนี้มีวัตถุประสงค์เพื่อพิมพ์ bash รูปแบบครึ่งพีระมิดโดยใช้ภาษาการเขียนโปรแกรม C++ ในมุมมองของรูปแบบที่กำหนดที่จะพิมพ์ อัลกอริธึมต่อไปนี้กำลังได้รับการประสานเพื่อให้บรรลุเป้าหมายของเราเป็น;

อัลกอริทึม

Step-1 Set the length of the Bash (Height)
Step-2 Outer loop to handle the number of rows
Step-3 Inner loop to handle columns
Step-4 Print the pattern with the character (@)
Step-5 Set the pointer to a new line after each row (outside the inner loop)
Step-6 Repeat the loop till the Bash Height

ตัวอย่าง

ดังนั้นซอร์สโค้ด C++ ต่อไปนี้จึงถูกแกะสลักออกมาในที่สุดโดยปฏิบัติตามอัลกอริธึมดังกล่าวดังต่อไปนี้

#include <iostream>
using namespace std;
void PrintBash(int n){
   // outer loop to handle number of rows
   for (int i=0; i<n; i++){
      // inner loop to handle number of columns
      for(int j=0; j<=i; j++ ){
         // printing character
         cout << "@ ";
      }
      // ending line after each row
      cout << endl;
   }
}
int main(){
   int Len = 6;
   PrintBash(Len);
   return 0;
}

ผลลัพธ์

หลังจากคอมไพล์โค้ดข้างต้นแล้ว ครึ่งพีระมิดจะถูกพิมพ์ออกมาหน้าตาแบบนี้

@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
@ @ @ @ @ @