链表所使用结构体定义:
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)