Sissejuhatus mustritesse C ++

Mis tahes programmeerimiskeelt õppides peavad õpilased harjutama mitmesuguste mustrite printimist, et mõista ja rakendada loogilist mõtlemist, samuti voo juhtimise teadmisi keelest praktilisel ja lõbusamal viisil. C ++ mustrid on põhiprogrammid, mida kasutatakse mis tahes keele mõistmiseks. Nende programmide rakendamiseks kasutatakse kahte või kolme voolu juhtimissilmust. Tavaliselt kasutatakse mustriprogrammides vähemalt kahte silmust, st üks silmus rea loomiseks ja teine ​​silmus veeru loomiseks. Esimene silmus, mis on välimine silmus, tähistab ridade arvu ja teine ​​silmus on sisemine silmus, mis tähistab veergude arvu. Mustri põhistruktuur C ++ keeles on esitatud järgmiselt:

Päis: # sisaldab

Peamine (): int main ()

(

Muutuvdeklaratsioon: int z = 10;

Kere: cout << z;

Tagasi: tagasi 0;

)

C ++ mustrite näited

C ++ keeles on erinevaid mustreid, näiteks tähekujundus, numbrimustrid ja märgimustrid. Selles osas käsitleme näidete abil erinevate mustrite loomist.

1. Numbrimustrid

Selles jaotises näeme, kuidas printida erinevaid numbrimustreid C-keeles

Näide nr 1 - programmeerige numbripüramiidi printimiseks C ++

Järgmises C ++ programmis saab kasutaja anda soovitud arvu numbripüramiidi mustri printimiseks ridade arvu, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int n, x, y, k;
cout << "Enter the number of rows to show number pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
(
for(y = 1; y <= n; y++)
(
if(y <= x)
cout << y;
else
cout << " ";
)
for(y = n; y >= 1; y--)
(
if(y <= x)
cout << y;
else
cout << " ";
)
cout << "\n";
)
return 0;
)

Väljund:

Näide nr 2 - programmeerige numbriteemandi printimiseks C ++

Järgmises C ++ programmis saab kasutaja anda teemantmustri printimiseks vajalike ridade arvu, nagu ta soovib, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int n, x, y, k;
cout << "Enter the number of rows to show number paatern: ";
cin >> n;
for(x = 1; x <= n; x++)
(
for(y = x; y (
cout << " ";
)
for(k = 1; k < (x*2); k++)
(
cout << k;
)
cout << "\n";
)
for(x = 4; x >= 1; x--)
(
for(y = n; y > x; y--)
(
cout << " ";
)
for(k = 1; k < (x*2); k++)
(
cout << k;
)
cout << "\n";
)
return 0;
)
#include
using namespace std;
int main()
(
int n, x, y, k;
cout << "Enter the number of rows to show number paatern: ";
cin >> n;
for(x = 1; x <= n; x++)
(
for(y = x; y (
cout << " ";
)
for(k = 1; k < (x*2); k++)
(
cout << k;
)
cout << "\n";
)
for(x = 4; x >= 1; x--)
(
for(y = n; y > x; y--)
(
cout << " ";
)
for(k = 1; k < (x*2); k++)
(
cout << k;
)
cout << "\n";
)
return 0;
)

Väljund:

Näide nr 3 - programmeerige numbripüramiidi mustri printimiseks C ++ -s

Järgmises C ++ programmis saab kasutaja anda soovitud arvu numbripüramiidi mustri printimiseks ridade arvu, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int x, s, n, y = 0, cntr = 0, cntr1 = 0;
cout << "Enter the number of rows to show number pattern: ";
cin >> n;
for(x = 1; x <= n; ++x)
(
for(s = 1; s <= nx; ++s)
(
cout << " ";
++cntr;
)
while(y != 2 * x - 1)
(
if (cntr <= n - 1)
(
cout << x + y;
++cntr;
)
else
(
++cntr1;
cout << (x + y - 2 * cntr1);
)
++y;
)
cntr1 = cntr = y = 0;
cout << "\n";
)
return 0;
)

Väljund:

Näide 4 - programmeerige vertikaalse arvu püramiidi printimiseks C ++

Järgmises C ++ programmis saab kasutaja anda vertikaalse arvu püramiidi mustri printimiseks vajalike ridade arvu, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int n, x, y;
cout << "Enter the number of rows to show number pattern: ";
cin >> n;
for(int x = 1; x < n; x++)
(
for(int y = 1; y <= x; y++)
cout << y;
cout << "\n";
)
for(int x = n; x >= 0; x--)
(
for(int y = 1; y <= x; y++)
cout << y;
cout << "\n";
return 0;
)
)

Väljund:

Näide nr 5 - programmeerige numbripüramiidi printimiseks C ++

Järgmises C ++ programmis saab kasutaja anda soovitud arvu numbripüramiidi mustri printimiseks ridade arvu, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int n, x, y;
cout << "Enter the number of rows to show number patterns: ";
cin >> n;
for (int x = n; x >= 0; x--)
(
for (int y = 1; y <= x; y++)
cout << y;
cout << "\n";
)
for(int x = 1; x <= n; x++)
(
for(int y = 1; y <= x; y++)
cout << y;
printf("\n");
)
return 0;
)

Väljund:

2. Tähemustrid

Selles jaotises näeme, kuidas erinevaid tärnimustrid printida.

Näide nr 1 - Programm C ++ -s, et printida tähtteemant

Järgmises C ++ programmis saab kasutaja anda tähtede teemantmustri printimiseks soovitud arvu ridu, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int n, s, x, y;
cout << "Enter number of rows to show star pattern: ";
cin >> n;
for(x = 0; x <= n; x++)
(
for(s = n; s > x; s--)
cout << " ";
for(y = 0; y < x; y++)
cout << "* ";
cout << "\n";
)
for(x = 1; x < n; x++)
(
for(s = 0; s < x; s++)
cout << " ";
for(y = n; y > x; y--)
cout << "* ";
cout << "\n";
)
return 0;
)

Väljund:

Näide 2 - programmeerige vertikaalse kõvera printimiseks C ++

Järgmises C ++ programmis saab kasutaja anda vertikaalse kõvera mustri printimiseks soovitud arvu ridu, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int n, x, y;
cout << "Enter number of rows to show star pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
(
for(y = 1; y <= x; y++)
(
cout << "*";
)
cout << "\n";
)
for(x = n; x >= 1; x--)
(
for(y = 1; y <= x; y++)
(
cout << "*";
)
cout << "\n";
)
return 0;
)

Väljund:

Näide 3 - C ++ programmeerimine õõnsa arvu teemandi printimiseks

Järgmises C ++ programmis saab kasutaja anda õõnsa teemantmustri printimiseks soovitud arvu ridu, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
cout << "Enter the number of rows to show the star pattern: ";
int n, x, y, s = 1, k;
cin >> n;
for(x = 0; x <= n; x++)
(
for(y = n; y > x; y--)
(
cout << " ";
)
cout << "*";
if (x > 0)
(
for(k = 1; k <= s; k++)
(
cout << " ";
)
s += 2;
cout << "*";
)
cout << "\n";
)
s -= 4;
for(x = 0; x <= n -1; x++)
(
for(y = 0; y <= x; y++)
(
cout << " ";
)
cout << "*";
for(k = 1; k <= s; k++)
(
cout << " ";
)
s -= 2;
if(x != n -1)
(
cout << "*";
)
//ending line after each row
cout << "\n";
)
return 0;
)

Väljund:

Näide nr 4 - C ++ programmeerimine õõnsa tähe kolmnurga printimiseks

Järgmises C ++ programmis saab kasutaja anda õõnsa tähe kolmnurga mustri printimiseks vajalike ridade arvu, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int n, x, y, s;
cout << "Enter number of rows to show the star pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
(
//for loop to put space in pyramid
for (s = x; s < n; s++)
cout << " ";
//for loop to print star
for(y = 1; y <= (2 * n - 1); y++)
(
if(x == n || y == 1 || y == 2 * x - 1)
cout << "*";
else
cout << " ";
)
cout << "\n";
)
return 0;
)

Väljund:

Näide nr 5 - programm tähe kolmnurga printimiseks programmis C ++

Järgmises C ++ programmis saab kasutaja anda soovitud arvu tähtede kolmnurga mustri printimiseks ridade arvu, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int n, s, x, y;
cout << "Enter number of rows to show star pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
(
for(s = x; s < n; s++)
cout << " ";
//for loop for displaying star
for(y = 1; y <= x; y++)
cout << "* ";
cout << "\n";
)
return 0;
)

Väljund:

3. Tegelaskujud

Selles jaotises näeme, kuidas printida erinevaid märgimustreid.

Näide nr 1 - programmeerige C ++ järjestikuste märkide kolmnurga printimiseks

Järgmises C ++ programmis saab kasutaja anda ridade arvu järjestikuste märkide kolmnurga kolmnurga mustri printimiseks vastavalt soovile, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int n, x, y;
cout << "Enter number of rows to show character pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
(
for(y = 1; y <= x; y++)
(
cout << char('A' + y -1);
)
cout << "\n";
)
return 0;
)

Väljund:

Näide 2 - programmeerige tähemärkide kolmnurga printimiseks C ++

Järgmises C ++ programmis saab kasutaja anda tähemärkide kolmnurga mustri printimiseks soovitud arvu ridu, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int n, x, y;
cout << "Enter number of rows to show character pattern: ";
cin >> n;
for(x = 1; x <= n; x++)
(
for(y = 1; y <= x; y++)
(
cout << char('A'-1 + x);
)
printf("\n");
)
return 0;
)

Väljund:

Näide nr 3 - programmeerige C ++ abil märkide ümberpööratud kolmnurk

Järgmises C ++ programmis saab kasutaja anda tähemärkide ümberpööratud kolmnurga mustri printimiseks soovitud arvu ridu, tulemus kuvatakse ekraanil:

Kood:

#include
using namespace std;
int main()
(
int n, x, y;
cout << "Enter number of rows to show character pattern: ";
cin >> n;
for(x= 1; x <= n; x++)
(
for(y = n; y >= x; y--)
(
cout << char('A'-1 + x);
)
cout << "\n";
)
return 0;
)

Väljund:

Järeldus

Selles artiklis oleme näinud erinevaid numbreid, tähte ja märkide mustreid. Loodetavasti leiate sellest artiklist abi.

Soovitatav artikkel

See on C ++ mustrite juhend. Siin käsitleme C ++ mustrite sissejuhatust ja selle erinevaid mustreid koos näidete ja süntaksiga. Lisateavet leiate ka meie muudest soovitatud artiklitest -

  1. Mustrid Pythonis
  2. Tähemustrid Java-s
  3. Tähemustrid Pythonis
  4. Silmus C ++
  5. Juhend mustrite kohta JavaScriptis
  6. C-tüüpi mustrite 3 parimat tüüpi
  7. Näited Java numbrimustritest

Kategooria: