当前位置:网站首页>C language must memorize code Encyclopedia

C language must memorize code Encyclopedia

2022-07-06 16:04:00 Programming fish 66

For students who have just learned computer programming , Every programming knowledge is very important . The following is a small compilation for you to sort out c Language must memorize code , Hope you enjoy it .

1、/* Output 9*9 formula . common 9 That's ok 9 Column ,i The control line ,j Control the column .*/

#include "stdio.h"

main()

{int i,j,result;

for (i=1;i<10;i++)

{ for(j=1;j<10;j++)

{

result=i*j;

printf("%d*%d=%-3d",i,j,result);/*-3d Indicates left alignment , Occupy 3 position */

}

printf("\n");/* Wrap after each line */

}

}

2、/* Classical questions : There is a pair of rabbits , From the day after birth 3 A couple of rabbits are born every month from , Every month after the third month, a couple of rabbits will be born , If the rabbits don't die , Ask the total number of rabbits per month ?

The law of the rabbit is a series 1,1,2,3,5,8,13,21....*/

main()

{

long f1,f2;

int i;

f1=f2=1;

for(i=1;i<=20;i++)

{ printf("%12ld %12ld",f1,f2);

if(i%2==0) printf("\n");/* Control output , Four in each line */

f1=f1+f2; /* Add up the first two months and assign the value to the third month */

f2=f1+f2; /* Add up the first two months and assign the value to the third month */

}

}

3、/* Judge 101-200 How many primes are there between , And output all prime numbers and the number of prime numbers .

Program analysis : The way to judge prime numbers : Remove with a number 2 To sqrt( The number of ), If it can be divided ,

It means that this number is not a prime number , On the contrary, prime numbers .*/

#include "math.h"

main()

{

int m,i,k,h=0,leap=1;

printf("\n");

for(m=101;m<=200;m++)

{ k=sqrt(m+1);

for(i=2;i<=k;i++)

if(m%i==0)

{leap=0;break;}

if(leap) /* After the end of the internal circulation ,leap Still 1, be m Prime number */

{printf("%-4d",m);h++;

if(h%10==0)

printf("\n");

}

leap=1;

}

printf("\nThe total is %d",h);

}

4、/* If a number is exactly equal to the sum of its factors , This number is called " Complete ". for example 6=1+2+3. Programming

find 1000 All the completions within .*/

main()

{

static int k[10];

int i,j,n,s;

for(j=2;j<1000;j++)

{

n=-1;

s=j;

for(i=1;i<j;i++)

{if((j%i)==0)

{ n++;

s=s-i;

k[n]=i;

}

}

if(s==0)

{printf("%d is a wanshu: ",j);

for(i=0;i<n;i++)

printf("%d,",k[i]);

printf("%d\n",k[n]);

}

}

}

5、/* The function of the following program is to make a 4×4 The array is rotated counterclockwise 90 The output is , The data of the original array is required to be input randomly , New array 4 That's ok 4 Column mode output ,

Please complete the procedure in the blank space .*/

main()

{ int a[4][4],b[4][4],i,j; /*a Store the original array data ,b Store rotated array data */

printf("input 16 numbers: ");

/* Enter a set of data and store it in the array a in , Then rotate and store in b Array */

for(i=0;i<4;i++)

for(j=0;j<4;j++)

{ scanf("%d",&a[i][j]);

b[3-j][i]=a[i][j];

}

printf("array b:\n");

for(i=0;i<4;i++)

{ for(j=0;j<4;j++)

printf("%6d",b[i][j]);

printf("\n");

}

}

6、/* Program to print right angle Yang Hui triangle */

main()

{int i,j,a[6][6];

for(i=0;i<=5;i++)

{a[i][i]=1;a[i][0]=1;}

for(i=2;i<=5;i++)

for(j=1;j<=i-1;j++)

a[i][j]=a[i-1][j]+a[i-1][j-1];

for(i=0;i<=5;i++)

{for(j=0;j<=i;j++)

printf("%4d",a[i][j]);

printf("\n");}

}

7、/* Enter... Through the keyboard 3 Famous student 4 Results of courses ,

Calculate the average score of each student and the average score of each course .

All scores are required to be put into a 4 That's ok 5 In the array of columns , Use a space between the data of the same person , Enter for different people

In the last column and the last row, the average score of each student 、 The average score of each course and the total average score of the class .*/

#include <stdio.h>

#include <stdlib.h>

main()

{ float a[4][5],sum1,sum2;

int i,j;

for(i=0;i<3;i++)

for(j=0;j<4;j++)

scanf("%f",&a[i][j]);

for(i=0;i<3;i++)

{ sum1=0;

for(j=0;j<4;j++)

sum1+=a[i][j];

a[i][4]=sum1/4;

}

for(j=0;j<5;j++)

{ sum2=0;

for(i=0;i<3;i++)

sum2+=a[i][j];

a[3][j]=sum2/3;

}

for(i=0;i<4;i++)

{ for(j=0;j<5;j++)

printf("%6.2f",a[i][j]);

printf("\n");

}

}

8、/* Perfect the program , Realize the input string output in reverse order ,

Such as the input windows Output swodniw.*/

#include <string.h>

main()

{ char c[200],c1;

int i,j,k;

printf("Enter a string: ");

scanf("%s",c);

k=strlen(c);

for (i=0,j=k-1;i<k/2;i++,j--)

{ c1=c[i];c[i]=c[j];c[j]=c1; }

printf("%s\n",c);

}

Pointer to the method :

void invert(char *s)

{int i,j,k;

char t;

k=strlen(s);

for(i=0,j=k-1;i<k/2;i++,j--)

{ t=*(s+i); *(s+i)=*(s+j); *(s+j)=t; }

}

main()

{ FILE *fp;

char str[200],*p,i,j;

if((fp=fopen("p9_2.out","w"))==NULL)

{ printf("cannot open the file\n");

exit(0);

}

printf("input str:\n");

gets(str);

printf("\n%s",str);

fprintf(fp,"%s",str);

invert(str);

printf("\n%s",str);

fprintf(fp,"\n%s",str);

fclose(fp);

}

9、/* The function of the following program is from the character array s Delete and store in c The characters in .*/

#include <stdio.h>

main()

{ char s[80],c;

int j,k;

printf("\nEnter a string: ");

gets(s);

printf("\nEnter a character: ");

c=getchar( );

for(j=k=0;s[j]!= '\0';j++)

if(s[j]!=c)

s[k++]=s[j];

s[k]= '\0';

printf("\n%s",s);

}

10、/* Write a void sort(int *x,int n) The implementation will x Array n Data from large to small

Sort .n And array elements in the main function . The results are displayed on the screen and output to a file p9_1.out in */

#include<stdio.h>

void sort(int *x,int n)

{

int i,j,k,t;

for(i=0;i<n-1;i++)

{

k=i;

for(j=i+1;j<n;j++)

if(x[j]>x[k]) k=j;

if(k!=i)

{

t=x[i];

x[i]=x[k];

x[k]=t;

}

}

}

void main()

{FILE *fp;

int *p,i,a[10];

fp=fopen("p9_1.out","w");

p=a;

printf("Input 10 numbers:");

for(i=0;i<10;i++)

scanf("%d",p++);

p=a;

sort(p,10);

for(;p<a+10;p++)

{ printf("%d ",*p);

fprintf(fp,"%d ",*p); }

system("pause");

fclose(fp);

}

1、scanf(“a=%d,b=%d”,&a,&b) The exam is super important ! Remember to input data at the terminal in the format of the first part . The core of the exam is : As like as two peas . On the black screen, enter a=12,b=34 Only then can 12 and 34 Give to a and b . Not a little different .

2、scanf(“%d,%d”,x,y); This way of writing is absolutely wrong ,scanf The second part of the must be the address !scanf(“%d,%d”,&x,&y); Pay attention to the writing so that you can !

3、 Pay special attention to the pointer in scanf For example : int x=2;int *p=&x;scanf(“%d”,x); error scanf(“%d”,p); correct scanf(“%d”,&p); error scanf(“%d”,*p) error .

4、 Specify the length of the input ( The key points of the exam ) The input terminal :1234567scanf(“-M%d”,&x,&y,&z);x by 12,y by 3456,z by 7 The input terminal :1 234567 because 1 and 2 There's a space in the middle , So only 1 Give me a seat xscanf(“-M%d”,&x,&y,&z);x by 1,y by 2345,z by 67.

5、 Characters and integers are close relatives :int x=97;printf(“%d”,x); The result is 97printf(“%c”,x); The result is a.

原网站

版权声明
本文为[Programming fish 66]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919348753.html