C语言不错的代码片段

链表所使用结构体定义:
typedef struct student
{
	int data;
};


typedef struct node
{
	struct student data;
	struct node *next;
}Node,*Link;



文件读写:
	FILE *fp;
	if ((fp = fopen("D:\student", "rb+")) == NULL) {
		printf("error!"); exit(0);
	}


	while (!feof(fp))
	{
		if ((p = (Node*)malloc(sizeof(Node))) == NULL) {
			printf("error!"); exit(0);
		}
		if (fread(p, sizeof(Node), 1, fp) == 1) {
			p->next = NULL;
			r->next = p;
			r = p;
			count++;
		}
	}
	fcolse(fp);


	if ((fp = fopen("D:\\student", "wb")) == NULL) {
		printf("wrong"); getchar(); return;
	}

	for (p = l->next; p; p->next)
		if (fwrite(p, sizeof(Node), 1, fp) == 1)
			count++;
		else
			break;




格式化输入:
void stringinput(char *t, int lens, char *notice)
{
	char n[255];
	do {
		printf(notice);
		scanf("%s", n);
		if (strlen(n) > lens)
			printf("wrong!");
	} while (strlen(n) > lens);
	strcpy(t, n);
}
调用:char searchinput[20];
stringinput(searchinput, 10, "请输入学号:");



int numberinput(char *notice)
{
	int t = 0;
	do {
		printf(notice);
		scanf("%d", &t);
		if (t > 100 || t < 0)
			printf("wrong");
	} while (t > 100 || t < 0);
	return t;
}

调用:numberinput("请输入数值:");



//gotoxy在TC中是在system.h库文件里的一个函
void gotoxy(int x,int y)//gotoxy在TC中是在system.h库文件里的一个函
{
	COORD c;
	c.X=x-1;c.Y=y-1;
	SetConsoleCursorPosition (GetStdHandle	(STD_OUTPUT_HANDLE),c);
}



随机数
srand((unsigned)time(NULL));
rand()%MOD;


//用一指定字节填充内存,初始化
memst(map,0,sizeof(map));
//多维数组的理解和应用:
	char p[15][15][4];//"  \0"占四个字符 //前面【15】【15】作为定位,真正存储在后面的【4】
	for(i=0;i<15;i++)
		for(j=0;j<15;j++){
			if(qipan[i][j]==SPA) strcpy(p[i][j],"  \0");
			if(qipan[i][j]==MAN) strcpy(p[i][j],"●\0");
			if(qipan[i][j]==COM) strcpy(p[i][j],"◎\0");
//把多维数组理解为树状图:
 /* 数组a中储存己方和对方共32种棋型的值  己方0对方1    活0冲1空活2空冲3    子数0-3(0表示1个子,3表示4个子) */
int a[2][4][4]={40,400,3000,10000,6,10,1000,10000,20,120,200,0,6,10,300,0,30,300,2500,5000,2,8,500,9000,26,140,0,0,4,20,500,0};//对32种双向棋型赋权值(权值设计越好,AI越聪明,人工智能越好)






#pragma warning(disable: 4996)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值