c基础

First Post:

Last Update:

Word Count:
807

Read Time:
4 min

C language

for循环

1
2
3
4
5
6
7
#include <stdio.h>
int main()
{
for(double value=36;value>0;value/=2)
printf("%3d",value);
return 0;
}

打开文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
int main(void)
{
char line[10];
FILE* source_file=fopen("test_01.c","r");
char* end;
if(!source_file)
{
printf("Opne fali!");
return 0;
}
while(1)
{
end=fgets(line,sizeof(line),source_file);
if(end)
{
printf("%s",line);
}
else break;
}

return 0;
}

斐波那契数列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<stdio.h>
int main(void){ //实现斐波那契数列---2019.7.20
int arr_length=0;
int i_1=1,i_2=1,i_3=2;
int i=0;
printf("Input a array length\n");
scanf("%d",&arr_length);
if(arr_length==3)
printf("%d %d %d\n",i_1,i_2,i_3);
if(arr_length==2)
printf("%d %d\n",i_1,i_2);
if(arr_length==1)
printf("%d\n",i_1);
if(arr_length>=4)
{
printf("%d %d %d",i_1,i_2,i_3);
while(i<(arr_length-3))
{
i_1=i_2;
i_2=i_3;
i_3=i_1+i_2;
printf(" %d",i_3);
i++;
}
printf("\n");
}
if(arr_length<=0)
printf("There is a error!\n");
return 0;
}

金字塔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>	//print Jin TA
void Print(int line,char sign){
for(int i_1=line;i_1>0;i_1--)
{
for(int i_2=0;i_2<(i_1-1);i_2++)
printf(" ");
for(int i_3=0;i_3<(2*(line-i_1+1)-1);i_3++)
printf("%c",sign);
printf("\n");
}
}
int main(){
Print(25,'$');
return 0;
}

汉罗塔攻略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>    //--------实现hannuota攻略程序
int hannuota(int n,char A,char B,char C)
{
if(n==1){
printf("Move %c to %c\n",A,C);
return 0;
}
hannuota(n-1,A,C,B);
printf("Move %c to %c\n",A,C);
hannuota(n-1,B,A,C);

}
int main(void){
int num=0;
printf("输入塔的个数\n");
scanf("%d",&num);
hannuota(num,'A','B','C');
return 0;
}

递归累加

递归函数来实现1+2+3+4+….+100

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>//使用递归函数来实现1+2+3+4+....+100
int Plus(int num){
if(num==1){
return ;
}
Plus(num-1);
}
int main(void){
printf("Please input a number to calculate\n");
scnaf("%d",&num);
Plus(num);
return 0;
}

桶排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
char* Order(char* arr,int length) //桶排序
{
int max=0;
int i=0;
printf("%d",length);
while(i<length)
{
if(*(arr+i)>max)
{
max=*(arr+i);
}
i++;
}
printf("%d\n",max);
}
int main()
{
char arr[10]={1,34,32,34,11,3,35,67,89,22};
Order(arr,sizeof(int));
return 0;
}

宏定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
void Define_Undef()
{
#define A "Hello"
printf(A);
#undef A
#define A " World\n"
printf(A);
}
void Define_Compare()
{
#define CMP(a,b) (a>b?a:b)
printf("MAX: %d",CMP(1,2));

}
void If_Endif_Elif()
{
#define TURE 1
#define FALSE 0
#define MAX(num1,num2) (num1>num2?TURE:FALSE)
#define NUM 1
#if NUM
printf("Hello!\n");
#endif
#undef NUM
#define NUM1 1
#define NUM2 2
#if MAX(NUM1,NUM2)
printf("%d > %d \n",NUM1,NUM2);
#elif !(MAX(NUM1,NUM2))
printf("%d < %d\n",NUM1,NUM2);
#endif
}
void Ifdef_Else_Endif()
{
// #define IF
#ifdef IF
printf("IF has defined\n");
#else
printf("IF has not defined\n");
#endif
}
void main()
{
//Define_Undef();
//Define_Compare();
//If_Endif_Elif();
Ifdef_Else_Endif();
}

位域

bitfiled

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
typedef struct bs {
int a:2;
int b:1;
int c:12;
unsigned d:4

}bs;
struct bit_2 {
unsigned a:2;
unsigned :2; //It can't use
unsigned :0; //NULL

unsigned b:4;
unsigned c:4;

};
int main(void) {
bs bit;
printf("sizeof(%d) \n",sizeof(struct bs));
bit.a = 4;
bit.b = 1;
bit.c = 34235;
printf("a: %d b: %d c: %d\n", bit.a, bit.b, bit.c);

return 0;
}
打赏点小钱
支付宝 | Alipay
微信 | WeChat