C语言模块化编程的代码示例

老赵最近写了一个小代码,是关于C语言程序化编程的,挺有趣的,和大家分享一下啦。

一、程序概述

程序的主要功能是随机产生10道数学加减乘除题,用户输入答案,答错或者答对随机给出一句称赞或者鼓励。答对8道题及以上输出分数,否则重新答题。

二、程序结构

程序主要分为两大部分:.c代码文件和.h头文件,如下图所示:
在这里插入图片描述

三、 具体代码

1、主函数

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "Question.h"

int main()
{
    int i,Q;
    int score = 0; //统计答题分数
    for(i=0;i<10;i++)  //循环产生10道题
    {
        Q = Question();
        if(Q)
            score++;
    }
    if(score < 8) //得分少于8分重做
    {
        printf("your score is poor,please try again!\n");
        for(i=0;i<10;i++)  //循环产生10道题
        {
        Q = Question();
        if(Q)
            score++;
        }
    }
    else //得分大于8分,显示分数。
        printf("your score is:%d\n",score);
    return 0;
}

2、随机数函数

包含随机数函数的头文件,头文件里面只需要写函数声明和宏定义常量。

//这是Random.c的代码,用“#include "Random.h"”调用头文件的内容
#include <stdio.h>
#include <stdlib.h>
#include "Random.h"

//产生1-10的随机数
int Random(void)
{
    int number;
    srand(time(NULL));
    number = (rand()%(MAX_NUMBER -MIN_NUMBER+1)) + MIN_NUMBER;
    return number;
}
//这是Random.h头文件的内容
#ifndef RANDOM_H_INCLUDED
#define RANDOM_H_INCLUDED
#define MAX_NUMBER 10 //宏常量
#define MIN_NUMBER 1  //宏常量

int Random(void);//函数声明
#endif // RANDOM_H_INCLUDED

3、产生算法题函数

//这是Question.c的代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "Random.h"
#include "IsError.h"
#include "IsRight.h"
#include "Question.h"


int Question(void)
{
    int a,b,i;
    int reply = 0; //学生给出的答案
    int answer=0; //算式的正确答案
    a = Random();
    sleep(1); //让两个随机数生成时间隔1秒,不然短时间内生成的随机数一样
    b = Random();
    i = (rand()%(MAX_NUMBER -MIN_NUMBER+1)) + MIN_NUMBER;//随机产生加减乘除
    switch(i)
    {
        case 1:
                answer = a+b;
                printf("%d+%d=?\n",a,b);
                scanf("%d",&reply);
                break;
        case 2:
                answer = a-b;
                printf("%d-%d=?\n",a,b);
                scanf("%d",&reply);
                break;
        case 3:
                answer = a*b;
                printf("%d*%d=?\n",a,b);
                scanf("%d",&reply);
                break;
        case 4:
                answer = a/b;
                printf("%d/%d=?\n",a,b);
                scanf("%d",&reply);
                break;
    }
    if(reply == answer)
    {
        IsRight(); //回答正确调用正确评价
        return 1;
    }
    else
    {
        IsError(); //回答错误调用错误评价
        return 0;
    }
}
//这是Question.h的代码
#ifndef QUESTION_H_INCLUDED
#define QUESTION_H_INCLUDED
#define MAX_NUMBER 4
#define MIN_NUMBER 1

int Question(void);

#endif // QUESTION_H_INCLUDED

4、答错反馈函数

//这是IsError.c的代码
#include <stdio.h>
#include "IsError.h"

void IsError()    //回答错误随机给出下面一句评价
{
    int i;
    i = (rand()%(MAX_NUMBER -MIN_NUMBER+1)) + MIN_NUMBER;
    switch(i)
    {
        case 1:
            printf("No. Please try again!\n");
            break;
        case 2:
            printf("Wrong.  Try once more!\n");
            break;
        case 3:
            printf("Don’t give up!\n");
            break;
        case 4:
            printf("Not correct. Keep trying!\n");
            break;
    }
}
//这是IsError.h的代码
#ifndef ISERROR_H_INCLUDED
#define ISERROR_H_INCLUDED
#define MAX_NUMBER 4
#define MIN_NUMBER 1

void IsError();
#endif // ISERROR_H_INCLUDED

5、答对反馈函数

//这是IsRight.c的代码
#include <stdio.h>
#include "IsRight.h"

void IsRight()    //回答正确随机给出下面一句评价
{
    int i;
    i = (rand()%(MAX_NUMBER -MIN_NUMBER+1)) + MIN_NUMBER;
    switch(i)
    {
        case 1:
            printf("Very good!\n");
            break;
        case 2:
            printf("Excellent!\n");
            break;
        case 3:
            printf("Nice work!\n");
            break;
        case 4:
            printf("Keep up the good work!\n");
            break;
    }
}
//这是IsRight.h的代码
#ifndef ISRIGHT_H_INCLUDED
#define ISRIGHT_H_INCLUDED
#define MAX_NUMBER 4
#define MIN_NUMBER 1

void IsRight();
#endif // ISRIGHT_H_INCLUDED

四、运行结果

在这里插入图片描述

五、为什么要模块化编程?

模块化编程的思想是:根据功能将工程划分为不同模块。主函数只调用函数,而不定义函数。在各模块文件中定义功能函数,并将要用到的函数利用同名头文件申明外部函数供其他文件调用。

使用模块化编程可以使代码更有序,可以便捷地编写大型项目。模块化将不需要的细节尽可能对外部隐藏,实现函数的封装。便于重复利用代码,实现某一功能代码可以在多处调用,可以在别的项目使用。便于分工合作,团队共同开发一个项目,可以将程序分为多个模块,每个人只完成一个模块的内容。

以上就是本周老赵分享的全部内容,如果你感觉对你有用,欢迎收藏和点赞哦。如果你有什么地方不懂,记得评论区找老赵。咱们大家一起快快乐乐学编程,奥利给!

  • 8
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小赵同学-

非常感谢你!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值