C语言学习记录

学习目标:

  • 一周掌握 C 入门知识

学习内容:

提示:这里可以添加要学的内容

例如:

  1. 搭建 C开发环境
  2. 掌握 C基本语法
  3. 掌握条件语句
  4. 掌握循环语句

学习时间:

  • 周一至周五晚上 7 点—晚上9点
  • 周六上午 9 点-上午 11 点
  • 周日下午 3 点-下午 6 点

学习产出:

提示:这里统计学习计划的总量

例如:

  • C编程作业
  • C相关论文

学习进度:

学习尚硅谷C语言B站课程:

22-12-11

74th(19:30):

while练习题,字符数组引入,头文件:#include <string.h>

75th(9:30),76th(14:30):

do...while简介,练习提,do...while于while区别

22-12-13

77th(24:30),78th(7:30):

多重循环控制实例

编程技巧:由简单到复杂,由死到活,增加或修改功能

23-2-13

C语言中随机函数的基础知识及应用

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void testRandomFunc()
{
	//srand(100);//srand用来设置范围,设置为100,意味着随机数的最小值为100
	srand((unsigned int)time(NULL));//时间戳,srand设置随机函数种子 
	//srand(time(NULL));也可以
	int result = rand();
	printf("%d\n", result);
}
void testRandom()
{
	//1.范围问题
	//1.1直接取余 rand()%x [0,x-1]
	int data = rand() % 10;//[0,9]
	printf("%d\n", data);

	//1.2不从零开始
	data = (rand() % 10) + 1;//[1,10]
	printf("%d\n", data);

	//1.3能被整除的数字
	data = rand() % 5 * 10;//能被10整除
	printf("%d\n", data);

	//2.概率问题
	//数学问题:数字的生成个数去描述
	int index = 0;

	while (1)
	{
		printf("第%d次 ",++index);
		int result = rand() % 100;//[0,99]
		if (result == 0&&index>=100)//次数大于一百次,类似保底机制
		{
			printf("REWARD\n");
			break;
		}
		else if (result <= 10)
		{
			printf("SCHOLARSHIP\n");
		}
		else {
			printf("patince\n");
		}

	}
}


int main()
{
	srand((unsigned int)time(NULL));
	testRandomFunc();
	testRandom();
}

文件的操作

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
int main()

{
	FILE* fp = NULL;
	//打开一个文件
	fp=fopen("D:/test.txt","r+");
	//fprintf(fp, "hello,world\n");
	//fputs("hello,chongqing\n", fp);
	
	//方法2 读取整个文件
	//说明,循环读取fp所指向的文件内容,如果读到NULL就结束
	char buff[1024];
	while (fgets(buff,1024,fp) != NULL)
	{
		printf("%s",buff);
	}
	fclose(fp);
}

文件的输入:

字符读写文件

#include<stdio.h>
#include<string.h>
int main()
{
	//文件写操作
	FILE* fp = fopen("./test001.txt", "w+");
	char array[] = "helloworld";
	for (int i = 0; i < strlen(array); i++)
	{
		fputc(array[i], fp);
	}
	fclose(fp);
	//文件读操作
	FILE* read = fopen("./test001.txt", "r+");
	int userkey = 0;
	char temp[1024] = { "" };
	int i = 0;
	while ((userkey = getc(read))!= EOF)
	{
		temp[i++] = userkey;
	}
	puts(temp);
	fclose(read);
}

字符串读写文件

#include<stdio.h>
#include<string.h>
void SAVEfile(const char*buffer,const char*fileNAME)
{
	FILE* fp = fopen(fileNAME, "w+");
	fputs(buffer,fp);
	fclose(fp);
}

void READfile(const char* fileNAME)
{
	FILE* fp = fopen(fileNAME, "r+");
	if (fp == NULL) return;
	char temp[1024] = { "" };
	fgets(temp, 1024, fp);
	puts(temp);
	fclose(fp);
}

int main()
{
	SAVEfile(" imissyou", "./string.txt");
	READfile("./string.txt");

}

格式化读写文件

#include<stdio.h>
#include<string.h>
void testFUN() 
{
	fprintf(stdout, "%s", "HEllo,world\n");
	fprintf(stdout, "%s", "enter a num");
	int num;
	int result = fscanf(stdin, "%d", &num);
	printf("%d", num);
}
struct MM
{
	char name[20];
	int age;
	int num;
};

//void saveFile(struct MM array[], int arrayNum, const char* fileName)
//{
//	FILE* fp = fopen(fileName, "w");
//	for (int i = 0; i < arrayNum; i++)
//	{
//		fprintf(fp, "%s\t%d\t%d\n", array[i].name, array[i].age,
//			array[i].num);
//	}
//	fclose(fp);
//}

void SAVEfile(struct MM array[], int arrayNum, const char* filename)
{
	FILE* fp = fopen(filename, "w+");
	for (int i = 0; i < arrayNum; i++)
	{
		fprintf(fp, "%s\t%d\t%d\n",array[i].name, array[i].age, array[i].num);
	}
	fclose(fp);
}

void READfile(const char* filename)
{
	FILE* fp =fopen (filename,"r+");
	struct MM temp;
	while ((fscanf(fp, "%s\t%d\t%d", &temp.name, &temp.age, &temp.num)) != EOF)
	{
		printf("%s\t%d\t%d\n", temp.name, temp.age, temp.num);
	}
	fclose(fp);
}


//获取文件行数
//void getFileLine(const char*filename)
//{
//	int count = 0;
//	char buffer[1024] = "";
//	FILE* fp = (filename, "r+");
//	while (1)
//	{
//		fgets(buffer, 1024, fp) != NULL;
//		if (feof(fp)) break;
//		count++;
//	}
//
//	printf("the line is %d", count);
//	fclose(fp);
//}

int getFileLine(const char* fileName)
{
	int count = 0;
	FILE* fp = fopen(fileName, "r");
	if (fp == NULL)
	{
		printf("文件打开失败!\n");
		return;
	}
	char buffer[1024] = "";
	//while (fgets(buffer, 1024, fp)!=NULL)
	//{
	// count++;
	//}
	while (1)
	{
		fgets(buffer, 1024, fp);
		if (feof(fp))
			break;
		count++;
	}
	fclose(fp);
	return count;
}
int main()
{
	//testFUN();
	struct MM array [3] = {"ali",18,1001,"aa",19,1002,"haha",20,1003};
	SAVEfile(array, 3, "./mm.txt");
	//READfile("./mm.txt");
	int result=getFileLine("./mm.txt");
	printf("%d", result);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值