题目
根据特定功能设计程序,要求由main.c,Fun1.c-Fun3.c选择其中任意两个, 共三个C语言文件和1个头文件组成,
其中fun1.c, fun2.c和fun3.c都使用了define.h中的声明,C语言文件的功能分别是:
fun1.c: 输出9*9口诀
fun2.c:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
fun3.c:输入某年某月某日,判断这一天是这一年的第几天?
要求:输入fun1,执行1;输入fun2,执行2;输入fun3,执行3。
都执行完,英文显示”恭喜你,已完成测试”,否则,英文输出”测试未完成,请继续努力”。
请为该程序编写相应的makefile文件,用make 命令生成最后的可执行文件main,并运行,确保功能正确。
程序
fun1.c
#include "define.h"
void fun1()
{
int i, j;
for (i = 1; i <= 9; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d*%d=%d ", j, i, i*j);
}
printf("\n");
}
}
int main()
{
int flag = 0;
printf("input flag\n");
scanf("%d",&flag);
if (flag==1)
{
fun1();
printf("finished!\n");
}
else if (flag==2)
{
fun2();
printf("finished!\n");
}
else if (flag==3)
{
fun3();
printf("finished!\n");
}
else
{
printf("unfinished!\n");
}
return 0;
}
fun2.c
#include "define.h"
void fun2()
{
int a, b, c, count = 0;
for (a = 1; a <= 4; a++) {
for (b = 1; b <= 4; b++) {
for (c = 1; c <= 4; c++) {
if (a != b && b != c && a != c) { // 保证三个数字互不相同
count++;
printf("%d%d%d\t", a, b, c);
}
}
}
}
printf("共%d个三位数\n", count);
}
fun3.c
#include "define.h"
void fun3()
{
int year, month, day,sum,leap;
printf("需要判断的时间(年,月,日):");
scanf("%d,%d,%d", &year, &month, &day);
printf("\n");
switch (month){
case 1:sum = 0; break;
case 2:sum = 31; break;
case 3:sum = 59; break;
case 4:sum = 90; break;
case 5:sum = 120; break;
case 6:sum = 151; break;
case 7:sum = 181; break;
case 8:sum = 212; break;
case 9:sum = 243; break;
case 10:sum = 273; break;
case 11:sum = 304; break;
case 12:sum = 334; break;
default:printf("输入日期格式错误\n");
}
sum = sum + day;
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
leap = 1;
}
else{
leap = 0;
}
if (leap == 1 && month > 2) {
sum = sum + 1;
}
printf("这是%d年的第%d天\n", year, sum);
}
define.h
#include <stdio.h>
extern void fun1();
extern void fun2();
extern void fun3();
extern int main();
makefile
CC = gcc
CFLAGS = -Wall -Wextra -std=c11
SRCS = fun1.c fun2.c fun3.c
OBJS = $(SRCS:.c=.o)
DEPS = define.h
TARGET = main
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) $^ -o $@
%.o: %.c $(DEPS)
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJS) $(TARGET)
运行结果
make
1
2
3
其他