前言:一般一个代码比较大需要多个人一起完成,在一个文件写比较耽误时间和麻烦。因此我们可以采用多文件的形式完成项目。
一.以多文件的形式怎样书写
1.一般情况下,函数的声明与类型的声明放在头文件(.h)中,函数的实现放在(.c)文件中。
比如判断一年是不是闰年,我在VS2022创建了三个文件,test.h头文件 test.c调用函数的文件 release.c书写主函数的文件
2.扫雷游戏以多文件形式的代码
//game.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define row ROW+2
#define col COL+2
#define fire 10
//棋盘的外观1.初始化棋盘
void Initboard(char chess[row][col], int ROWs, int COLs, char set);
void printboard(char chess[row][col], int ROWs, int COLs);
void setfire(char chess[row][col], int ROWs, int COLs);
void findfire(char show1[row][col], char show2[row][col], int ROWs, int COLs);
int countfire(char show1[row][col], int x, int y);
//调用函数的代码test.c
#include "game.h"
void Initboard(char chess[row][col], int ROWs, int COLs, char set)
{
for (int i = 1;i <= ROWs;i++)
{
for (int j = 1;j <= COLs;j++)
chess[i][j] = set;
}
}//初始化棋盘
void printboard(char chess[row][col], int ROWs, int COLs)
{
int i = 0;
for (i = 0;i <= ROWs;i++)
printf("%d ", i);//%2d可控制列宽
printf("\n");
for (int k = 1;k <= ROWs;k++)
{
printf("%d ", k);
for (int j = 1;j <= COL;j++)
{
printf("%c ", chess[k][j]);
}
printf("\n");
}
}//打印棋盘
void setfire(char show1[row][col], int ROWs, int COLs)
{
int count = fire;
while (count)
{
int x = rand() % ROWs + 1;
int y = rand() % COLs + 1;
if (show1[x][y] == '0')
{
show1[x][y] = '1';
count--;
}
}
}//放雷
//排查周围的雷
int countfire(char show1[row][col], int x, int y)
{
int count=0;
for ( int i = 0;i <= 2;i++)
{
for (int j = 0;j <= 2;j++)
{
if (show1[x + i - 1][y + j - 1] == '1')
count++;
}
}
return count;
}
void findfire(char show1[row][col],char show2[row][col], int ROWs, int COLs)//排雷
{
int x = 0;
int y = 0;
int l = 0;
while (l <ROWs * COLs - fire)
{
printf("请输入要排查的坐标:");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= ROWs && y >= 1 && y <= COLs)
{
if (show1[x][y] == '1')
{
printf("what a pity,game over\n");
printboard( show1,ROWs,COLs);
break;
}
else
{
l++;
int k = countfire(show1, x, y);
show2[x][y] = k + '0';
printboard(show2, ROWs, COLs);
}
}
else
printf("请输入合法坐标");
}
if (l == ROWs * COLs - fire)
printf("successful find fires");
}
//主函数代码game.c
#include "game.h"
void menu()
{
printf("******************\n");
printf("***** 1. play *****\n");
printf("***** 0. notplay *****\n");
printf("*******************\n");
}
void game()
{
char show1[row][col], show2[row][col];//定义两个字符型的函数,show1为埋雷的函数,show2为玩家游戏页面
Initboard(show2,ROW,COL,'*');
Initboard(show1,ROW,COL,'0');
printboard( show2,ROW,COL);
setfire(show1, ROW,COL);
findfire(show1, show2, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf("请选择 :");
scanf("%d", &input);
switch (input)
{
case(1):
game();
break;
case(0):
printf("退出游戏\n");
break;
default:
printf("选择错误请重新选择\n");
break;
}
} while (input);
return 0;
}
二.static和extern
1.static的基本意思和用途
(1)static是静态的的意思,是C语言中的关键字。
(2)用途:
a.修饰局部变量:保留原先的值,延长生命周期
如果不用static修饰则b=3,static保留sum被a调用的值,使不会被销毁
b.修饰全局变量
c.修饰函数
b和c的应用目的:使其只能在本文件中使用,不能用在其他文件使用,比如下面代码中如果在test3-9中使用static修饰liuliu则不会被调用。同理函数
//test3-9.c
static int liuliu=100;
//test.c
#include <stdio.h>
extern int liuliu;
int main()
{
printf("%d",liuliu};
return 0;
}
小结:⼀个全局变量被static修饰,使得这个全局变量只能在本源⽂件内使⽤,不能在其他源⽂件内使⽤。 本质原因是全局变量默认是具有外部链接属性的,在外部的⽂件中想使⽤,只要适当的声明就可以使 用;但是全局变量被 static 修饰之后,外部链接属性就变成了内部链接属性,只能在⾃⼰所在的源 ⽂件内部使⽤了,其他源⽂件,即使声明了,也是⽆法正常使⽤的。
使⽤建议:如果⼀个全局变量,只想在所在的源⽂件内部使⽤,不想被其他⽂件发现,就可以使⽤static修饰
2.extern基本概念与用途
1.extern本意v驱逐和n.大学实习生,外勤修女adj.外来的对外的。是C语言的关键字之一
2。主要用途:⽤来声明外部符号,如果⼀个全局的符号在A⽂件中定义的,在B⽂件中想使⽤,就可以使用extern进行声明,然后使用。
如此图