C-programmeerimise mustrid - Mustrite tüübid koos näidetega

Lang L: none (table-of-contents):

Anonim

C-programmeerimise mustrite sissejuhatus

C programmeerimise mustrid, C on protseduuriline, üldotstarbeline programmeerimiskeel. See loodi esmakordselt aastatel 1969–1973 Dennis Ritchie poolt. C-keele peamised omadused on madal juurdepääs mälule, lihtne märksõnade komplekt ja lihtne rakendamine. On palju keeli, nagu PHP, Java, Javascript jne., Järgib mõningal määral C funktsioone või süntaksi.

C-keele põhistruktuur on esitatud järgmiselt

Päis # sisaldab
Peamine (): int main ()
(
Muutuja deklaratsioon: int x = 12;
Kere: printf (“% d”, x);
Tagasi: tagasi 0;
)

C-programmeerimise mustrite näited

C-keeles on erinevaid mustreid, nagu tähemuster, numbrimallid ja märgimustrid. Selles osas käsitleme abinäidete abil C-keeles erinevate mustrite loomist.

1. Numbrimustrid

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

1. näide: programmeerige C-arv numbripüramiidi mustri printimiseks

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

#include
#include
int main()
(
int n, x, y, k;
printf("Enter the number of rows to show number pattern: ");
scanf("%d", &n);
for(x =1; x <= n; x++)
(
for(y =1; y <= n; y++)
(
if(y <= x)
printf("%d", y);
else
printf(" ");
)
for(y = n; y >= 1;y--)
(
if(y <= x)
printf("%d", y);
else
printf(" ");
)
printf("\n");
)
return 0;
)

Väljund:

Näide2: programmeerige C-kood numbriteemandi mustri printimiseks

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

#include
#include
int main()
(
int n, x, y, k;
printf("Enter the number of rows to show number paatern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
for(y = x; y (
printf(" ");
)
for(k = 1; k < (x*2); k++)
(
printf("%d", k);
)
printf("\n");
)
for(x = 4; x >= 1; x--)
(
for(y = n; y > x; y--)
(
printf(" ");
)
for(k = 1; k < (x*2); k++)
(
printf("%d", k);
)
printf("\n");
)
return 0;
)
#include
#include
int main()
(
int n, x, y, k;
printf("Enter the number of rows to show number paatern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
for(y = x; y (
printf(" ");
)
for(k = 1; k < (x*2); k++)
(
printf("%d", k);
)
printf("\n");
)
for(x = 4; x >= 1; x--)
(
for(y = n; y > x; y--)
(
printf(" ");
)
for(k = 1; k < (x*2); k++)
(
printf("%d", k);
)
printf("\n");
)
return 0;
)

Väljund:

3. näide: programmeerige C-arv numbripüramiidi mustri printimiseks

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

#include
#include
int main()
(
int x, s, n, y = 0, cntr = 0, cntr1 = 0;
printf("Enter the number of rows to show number pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; ++x)
(
for(s = 1; s <= nx; ++s)
(
printf(" ");
++cntr;
)
while(y != 2 * x - 1)
(
if (cntr <= n - 1)
(
printf("%d ", x + y);
++cntr;
)
else
(
++cntr1;
printf("%d ", (x + y - 2 * cntr1));
)
++y;
)
cntr1 = cntr = y = 0;
printf("\n");
)
return 0;
)

Väljund:

4. näide: programmeerige C-s vertikaalse arvu püramiidi muster

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

#include
#include
int main()
(
int n, x, y;
printf("Enter the number of rows to show number pattern: ");
scanf("%d", &n);
for(int x = 1; x < n; x++)
(
for(int y = 1; y <= x; y++)
printf("%d", y);
printf("\n");
)
for(int x = n; x >= 0; x--)
(
for(int y = 1; y <= x; y++)
printf("%d", y);
printf("\n");
)
return 0;
)

Väljund:

Näide 5: programmeerige C-arv numbripüramiidi mustri printimiseks

Järgmises C-programmis saab kasutaja anda numbripüramiidi mustri printimiseks soovitud arvu ridu, tulemus kuvatakse ekraanil

#include
#include
int main()
(
int n, x, y;
printf("Enter the number of rows to show number patterns: ");
scanf("%d", &n);
for (int x = n; x >= 0; x--)
(
for (int y = 1; y <= x; y++)
printf("%d", y);
printf("\n");
)
for(int x = 1; x <= n; x++)
(
for(int y = 1; y <= x; y++)
printf("%d", y);
printf("\n");
)
return 0;
)

Väljund:

2. Tähemustrid

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

1. näide: programmeerige tähtteemantmustri printimiseks C-keeles

Järgmises C-programmis saab kasutaja anda tähtteemandi mustri printimiseks soovitud arvu ridu, tulemus kuvatakse ekraanil

#include
#include
int main()
(
int n, s, x, y;
printf("Enter number of rows to show star pattern: ");
scanf("%d", &n);
for(x = 0; x <= n; x++)
(
for(s = n; s > x; s--)
printf(" ");
for(y = 0; y < x; y++)
printf("* ");
printf("\n");
)
for(x = 1; x < n; x++)
(
for(s = 0; s < x; s++)
printf(" ");
for(y = n; y > x; y--)
printf("* ");
printf("\n");
)
return 0;
)

Väljund:

2. näide: programmeerige vertikaalse kõvera mustri printimiseks C-režiimis

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

#include
#include
int main()
(
int n, x, y;
printf("Enter number of rows to show star pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
for(y = 1; y <= x; y++)
(
printf("*");
)
printf("\n");
)
for(x = n; x >= 1; x--)
(
for(y = 1; y <= x; y++)
(
printf( "*");
)
// ending line after each row
printf("\n");
)
return 0;
)

Väljund:

3. näide: programmeerige sümbol C õõnsa teemandi mustri printimiseks

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

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

Väljund:

4. näide: programmeerige C-tüüpi õõnsa tähe kolmnurga mustri printimiseks

Järgmises C-programmis saab kasutaja anda soovitud arvu ridu õõnsa tähe kolmnurga mustri printimiseks, tulemus kuvatakse ekraanil

#include
#include
int main()
(
int n, x, y, s;
printf("Enter number of rows to show the star pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
//for loop to put space in pyramid
for (s = x; s < n; s++)
printf(" "); //for loop to print star
for(y = 1; y <= (2 * n - 1); y++)
(
if(x == n || y == 1 || y == 2 * x - 1)
printf("*");
else
printf(" ");
)
//ending line after each row
printf("\n");
)
return 0;
)

Väljund:

5. näide: programmeerige tähe kolmnurga mustri printimiseks C-keeles

Järgmises C-programmis saab kasutaja anda tähtede kolmnurga mustri printimiseks soovitud arvu ridu, tulemus kuvatakse ekraanil

#include
#include
int main()
(
int n, s, x, y;
printf("Enter number of rows to show star pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
//for loop to put space
for(s = x; s < n; s++)
printf(" ");
//for loop for displaying star
for(y = 1; y <= x; y++)
printf("* ");
// ending line after each row
printf("\n");
)
return 0;
)

Väljund:

3. Tegelaskujud

Selles jaotises näeme, kuidas printida erinevaid tähemärke C-keeles

1. näide: programmeerige C-s järjestikuste märkide kolmnurga mustri printimiseks

Järgmises C-programmis saab kasutaja anda järjestikuste märkide kolmnurga kolmnurga mustri printimiseks vajalike ridade arvu, tulemus kuvatakse ekraanil

#include
#include
int main()
(
int n, x, y;
printf("Enter number of rows to show character pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
for(y = 1; y <= x; y++)
(
printf("%c", 'A' + y -1);
)
printf("\n");
)
return 0;
)

Väljund:

2. näide: programmeerige tähemärkide kolmnurgamustri printimiseks C-keeles

Järgmises C-programmis saab kasutaja anda tähemärkide kolmnurga kolmnurga mustri printimiseks vajalike ridade arvu, tulemus kuvatakse ekraanil

#include
#include
int main()
(
int n, x, y;
printf("Enter number of rows to show character pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
for(y = 1; y <= x; y++)
(
printf("%c", 'A'-1 + x);
)
printf("\n");
)
return 0;
)

Väljund:

3. näide: programmeerige tähemärkide ümberpööratud kolmnurga mustri printimiseks C-keeles

Järgmises C-programmis saab kasutaja anda tähemärkide ümberpööratud kolmnurga mustri printimiseks vajalike ridade arvu, tulemus kuvatakse ekraanil

#include
#include
int main()
(
int n, x, y;
printf("Enter number of rows to show character pattern: ");
scanf("%d", &n);
for(x= 1; x <= n; x++)
(
for(y = n; y >= x; y--)
(
printf("%c", 'A'-1 + x);
)
printf("\n");
)
return 0;
)

Väljund:

Soovitatav artikkel

See on C-programmeerimise mustrite juhend. Siin käsitleme erinevaid numbreid, tähti ja tegelaskujusid koos näidetega. Lisateavet leiate ka meie muudest soovitatud artiklitest -

  1. Agile programmeerimine
  2. Algoritm programmeerimisel
  3. Objektorienteeritud programmeerimine Java-s
  4. Tähtede mustrite tutvustus Java-s
  5. C-mustrid
  6. C Programmeerimismaatriksi korrutamine