一个汇总所有知识点的C语言程序


```c
/*
程序功能:
1、测试字符串的读写
2、测试文件相关操作函数
3、动态分配内存


*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<string.h>
#include <malloc.h>

//常量定义
#define N 20

//子函数声明
void stringTest();
void stringTest2();
void stringTest3();
double jiecheng(int x);
void fileWrite();
void fileRead();
void Cheng();
void printA();
void variable();
void output();
void input();
void calc1();
void point();
void pointUse();
void dou();
void array();
void arrayUse();
void stringUse();

void Struct();
void Enum();

void type();
int f();
void memory();


//枚举
enum week{SUN=10,MON=20,TUE,WED,THU,FRI,SAT};

//结构体声明,定义结构体
struct stu
{
	int num ;
	char sex ;
	char name[N] ;
};

//自定义数据类型
typedef int INT;
typedef int *POINT;//定义指向int型的指针类型

//主函数
void main()
{
	 
	//stringTest2();//调用子函数2
	//stringTest();//调用子函数1
	//stringTest3();//调用子函数3
	//printf("%d\n", '\0');
	//fileWrite();
	//fileRead();
	//Cheng();
	//printA();
	//variable();
	//output();
	//input();
	//calc1();
	//point();
	//pointUse();
	//dou();
	/*int x;
	printf("%d\n", (x = 30)>10);*/
	//array();
	//arrayUse();
	//stringUse();
	//Struct();
	//Enum();
	//type();
	//void (*pf)() = &type;//定义函数指针,指向函数type,其返回值是void
	//pf();//调用指向的函数
	//int(*p1)() = &f;
	//printf("%d\n",p1());
	memory();
}
//函数21:分配内存
void memory()
{
	/*
	p=(int *)malloc(4);//分配长度为4字节的存储空间
	p=(int *)calloc(N,4);//分配N 个长度为4字节的存储空间
	p=(int *)relloc(p1,4);//对原来的存储区,再开辟4个字节的内存
	返回内存的首地址
	ch=fgetc();//读文件字符函数
	fputc('A',fp);//文件写字符函数
	fget(str,11,fp);//读字符串函数,str是字符数组名
	fputs("abcd",fp);//文件写字符串函数


	*/
	int num = 0;
	char ch;
	//double a[162];
	FILE *fp;
	double t=10;
	char str[100] = "C:\\Users\\Administrator\\Desktop\\Others\\Ren\\data2.txt";
	fp = fopen(str, "r");
	if (fp == NULL)
	{
		printf("Open the file error!\n");
		exit(0);
	}
	while ( (ch=fgetc(fp) )!=EOF)  //若没有读到数据末尾 
	{
		fscanf(fp, "%lf", &t);
		num++;
	}//空读一次数据
	fclose(fp);
	printf("num=%d\n", num);//确定文件中的数据个数


	fp = fopen(str, "r");//空读一次后,文件指针已经指到末尾,因此需要再次打开文件
	int j = 0;
	double *p=NULL;
	p=(double *) calloc(num, sizeof(double));//分配相应内存
	/*
	请注意,此处读走第一个字符!!
	因此必须使用do-while语句。而不是while循环
	*/
	do
	{
		fscanf(fp, "%lf", p+j);//将数据存到内存中sizeof(double)*
		j++;
	} while ((ch = fgetc(fp)) != EOF);  //若没有读到数据末尾 
	

	printf("---------------\n");
	/*fscanf(fp, "%lf", &t);
	printf("%lf\n", t);*/

	for (int i = 0; i < num; i++)
	{
		printf("%lf\n", p[i]);//输出数组元素sizeof(double)*
	}
	
	fclose(fp);//关闭文件
	printf("%lf\n", p[0]);//测试第一个数据
	free(p);//释放内存

}
//子函数20:函数指针的使用
int f()
{
	return 20;
}
//子函数19:typedef自定义数据类型
void type()
{
	INT x = 30;
	POINT p = &x;
	printf("%d,%d\n", x, *p);
}
//子函数18:枚举类型
void Enum()
{
	/*
	定义枚举元素时,要列举出所有可用的值。
	枚举类型被定义之后,可以当成符号常量使用。
	枚举元素是一个符号常量,它在内存中以整型数据的方式保存。
	可以为枚举元素指定不同的整数值。
	*/
	enum week x =FRI;//定义枚举变量
	printf("%d,%d,%d\n", sizeof(SUN), SUN, MON);
	printf("%d\n", x);
}
//子函数17:结构体
void Struct()
{
	struct stu st[2],sx;
	st[0].num = 1001;
	st[0].sex = 'M';
	strcpy(st[0].name, "Chenglin Li");//只能用字符串拷贝函数,为字符串数组赋初值

	st[1].num = 1002;
	st[1].sex = 'F';
	strcpy(st[1].name, "Yawen Zhang");

	/*sx.name[N] = "My Fu";*/
	
	strcpy(sx.name, "Yawen Zhang");
	printf("%s\n", sx.name);
	for (int i = 0; i < 2; i++)
	{
		printf("%5d,%s,%-3c\n", st[i].num, st[i].name, st[i].sex);
	}
	printf("%s\n", st[0].name);
	puts(st[0].name);
}
//子函数16:字符串函数
void stringUse()
{
	char str1[30] = "Chenglin ", str2[] = "Chenglin Li";
	char *p1 = str1, *p2 = str2;
	puts(p1);
	puts(p2);
	printf("-----------------\n");
	strcat(p1,p2);
	//strcat(str1, str2);

	puts(p1);
	printf("%d\n", strcmp(p2, p1));
}

//子函数15:数组的使用
void arrayUse()
{
	/*
	指针数组用来处理多维数组
	1 2 3
	4 5 6
	*/
	//int a[2][3] = {1,2,3,4,5,6};
	//int *p[2];//定义指针数组
	//p[0] = &a[0];
	//p[1] = &a[1];
	//printf("%3d%3d\n", *p[0], *p[1]);
	//printf("%3d%3d\n", *(p[0]+1), *(p[1]+1));
	//char *pc[] = {"Chenglin","Yawen"};
	//puts(pc[0]);
	//puts(pc[1]);

	////int b[3] = { 10, 11, 12 };
	////int(*p2)[3]=b;//定义数组指针,指向一个包含3个元素的一位数组
	//int b[3][4] = { { 1, 2, 3, 4 }, { 11, 12, 13, 14 }, { 21, 22, 23, 24 } };
	//int(*p2)[4]; //定义一个数组指针,指向4个元素的一维数组
	//p2= b;
	//for (int i = 0; i < 12; i++)
	//	printf("%3d", (*p2)[i]);//遍历所有元素
	//putchar(10);

	//int (*p)[4];//存放一个4个元素的数组地址
	////int *p = a;
	//p = a;//指向数组 
	//printf("%d\n", sizeof(a));
	//for (int i = 0; i < 4; i++)
	//	printf("%3d\n", p[i]);
	//putchar(10); 


}
//子函数14:数组的使用
void array()
{
	int a[N];
	int *p = a;//指向数组的指针
	for (int i = 0; i < N; i++)
	{
		a[i] = i;

	}
	printf("%d\n", a);//数组首地址,元素a[0]的地址
	printf("%d\n", &a);//整个数组的地址
	printf("%d\n", a + 1);
	printf("%d\n", *(a + 1));
	printf("%d\n", &a+1);//
}
//子函数13:逗号表达式
void dou()
{
	/*
	逗号表达式的要领:

	1.从左到右逐个计算;
	2.逗号表达式作为一个整体,它的值为最后一个表达式的值;
	3. 逗号表达式的优先级别在所有运算符中最低。
	*/
	/*int x = 0;
	x = 10, 12, 100;
	printf("%d\n", x);
	printf("%d\n", x < 5 ? 100:200);
	*/
	char c1, c2;
	c1 = 65;
	putchar(c1);
	putchar(10);//换行
	c2 = '3';
	putchar(c2);
	putchar(10);//换行


}

//子函数12:指针使用
void pointUse()
{
	/*
	指针类型的变量占用4个字节。
	*/
	int a;
	int *p2 = &a;//指针指向变量a
	//int *p1 = p;//新建一个整型指针变量,使它和指针p相等
	//char c;
	//char *pc = &c;//指针指向变量c
	//printf("sizeof(int)=%d\n", sizeof(int));
	//printf("%d,%d\n",p,p+1);
	//printf("%d,%d\n", p, p - 2);
	//printf("%d,%d\n", pc, pc+1);
	//printf("%d,%d\n", pc, pc - 2);
	//printf("%d\n", p !=0);
	//printf("%d\n", p1);
	//printf("%d\n", p1 + 5);
	char *p = "Chenglin Li";//11个字符长度
	printf("%d,", sizeof(p));
	printf("%d,", strlen("Chenglin Li"));
	printf("%d,", sizeof("Chenglin Li"));
	printf("%d,", sizeof(p2));//
	putchar(10);
	double *px = 0;
	printf("%d\n", sizeof(p));
	printf("%3d\n%3d\n",p,p++);
	//int a[10];
	//int *pc = a;
	//printf("%3d\n%d\n", pc, pc++);//


}
//子函数11:指针运算
void point()
{
	/*int a = 0;
	int *p = &a;
	printf("a=%d\n", a);
	*p = 5;
	printf("a=%d\n", a);
*/
	char *p = "Chenglin Li";
	printf("%s", p);//利用指针地址输出字符串
	putchar(10);//换行
	puts(p);
	//putchar(10);//换行
	for (int i = 0; p[i] != 0;i++)
		putchar(*(p + i));
	putchar(10);//换行

}
//子函数10:算术运算
void calc1()
{
	int a = 10, b = 4, c = 8;
	int c1 = 0,c2 = 0;
	c1 = a / 3 % 2;
	printf("c1=%d\n", c1);
	float d = 1.23;
	printf("d=%d\n",(int)d);//强制类型转换
}
//子函数9:基本输入
void input()
{
	char c;
	puts("Please input a char:");
	c = getchar();
	putchar(c);
	putchar(10);


}

//子函数8:基本输出
void output()
{
	float x = 1234.5678;
	for (int i = 'a'; i <= 'z'; i++)
	{
		putchar(i-32);
		putchar(i);
		putchar(0x20);//空格
	}
	putchar(10);//换行符
	//float x = 1234.5678;
	printf("x=%f\n", x);
	printf("x=%.2E\n", x);
}

//子函数7:变量
void variable()
{
	int a = 5;
	int *p = &a;
	printf("a=%d\n", a);
	printf("&a=%d\n", &a);
	printf("p=%d\n", p);
	printf("*p=%d\n", *p);

}
//子函数6:打印A
void printA()
{
	int x = 10;
	for (int i = 1; i <= x; i++)//5行
	{
		for (int j = x-i; j >= 1; j--)//空格
		{
			printf(" ");
		}
		for (int k = 1; k <= 2 * i - 1; k++)
		{
			printf("A");//打印A
		}
		printf("\n");//换行
	}
}
//子函数5:打印第一个语言程序
void Cheng()
{
	printf("Welcome to the C world!\n");
	printf("欢迎进入C语言的世界!\n");
}

//子函数1:测试字符串的读写
void stringTest()
{
	/*控制台读取一段字符串,以空格或者tab键或者回车键结尾
	使用函数printf()和scanf()函数
	使用字符数组接收字符串,字符串结束标志'\0'
	puts()函数输出字符串,以'\0'作为结束标志.puts()函数自动换行
	*/
	char s[N];
	//char ch;
	int i = 0,j;
	printf("Please input a string: ");//控制台提示输出一段字符串
	while ((s[i++] = getchar()) != '\n');
	//{
	//	s[i++] = getchar();//获取字符,回车键结束
	//}

	//while ((ch= getchar()) != '\n')
	//{
	//	s[i++] = getchar();//获取字符,回车键结束
	//}


	//getchar();//舍去回车键
	//getchar();//舍去回车键
	s[i] = '\0';//添加字符串结束标志
	printf("i=%d\n", i);	                                                                                                                                                                                    
	for (j = 0; j < i; j++)
	{
		printf("%c", s[j]);//printf()函数格式化输出字符串
		//putchar(s[i]);//putchar()函数输出字符串

	}
	printf("\n");//输出一个换行

	puts(s);//puts()函数输出字符串
	putchar('\n');//输出一个换行

}


//子函数2:测试字符串的读写
void stringTest2()
{
	/*
	gets()函数录入字符串,以回车键结束
	puts()函数输出字符串,以'\0'作为结束标志
	*/
	char s[N];
	//char ch;
	int i = 0;
	printf("Please input a string: ");//控制台提示输出一段字符串
	gets(s);//输入字符串
	puts(s);//输出字符串
}


//子函数3:测试字符串的读写
void stringTest3()
{
	/*
	scanf()函数录入字符串,以回车键 ,空格键结束
	printf()函数输出字符串,以'\0'作为结束标志
	*/
	char s[N];
	//char ch;
	//int i = 0, j;
	printf("Please input a string: ");//控制台提示输出一段字符串
	scanf("%s", s);//输入字符串并换行
	printf("%s\n",s);//输出字符串
}

//子函数4:文件的创建与写入
void fileWrite()
{
	/*
	写方式打开文件,只能向文件中写入,
	若不存在,则以指定的文件名新建,若已经存在,则将文件删除,重建一个新文件
	*/
	char str [N]= "g:\\fileOne.txt";//声明一个文件地址
	FILE *fp;//定义文件指针
	//fp = fopen("g:\\fileOneNum.txt", "w");//
	fp = fopen(str, "r");//
	if (fp == NULL)
	{
		printf("Open Error!\n");
		exit(0);//程序终止
	}
	for (int i = 1; i <= 20; i++)
	{
		//fprintf(fp, "%d! = %.0lf\n", i,jiecheng(i));
		fprintf(fp,"%d\n",i);
	}
	fclose(fp);//关闭文件
	printf("Program running over!\n");

}

//建立一个阶乘的子函数
double jiecheng(int x)
{
	if (x <=0)
		return 0;
	else if (x == 1)
		return 1;
	else
		return x*jiecheng(x - 1);

}

//子函数5:文件的读取
void fileRead()
{
	/*
	读方式打开文件,只能读取文件,
	该文件必须事先存在,且只能从文件中读取,读取一个数据后,指针自动后移。
	*/
	char str[N] = "g:\\fileOneNum.txt";//声明一个文件地址
	int num[N];
	char c[] = "r";
	FILE *fp;//定义文件指针
	//两个反斜杠,第一个表示转义字符,第二个表示根目录
	//fp = fopen("g:\\fileOneNum.txt", "r");
	fp = fopen(str, c);//
	if (fp == NULL)
	{
		printf("Open Error!\n");
		exit(0);//程序终止
	}
	for (int i = 0; i < 20; i++)
	{
		//fprintf(fp, "%d! = %.0lf\n", i,jiecheng(i));
		fscanf(fp, "%d", &num[i]);//读取数字到数组中
	}
	fclose(fp);//关闭文件
	printf("Program running over!\n");
	for (int i = 0; i < 20; i++)
	{
		printf("%d\n", num[i]);//输出读取的数字
	}
	

}










  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值