C语言函数程序设计小学生加法题-10以内随机加法并自动判断对错输出得分

该程序实现了一个简单的加法测试,随机生成10组1到10的加法题目,允许用户三次尝试给出答案。根据回答的正确性和尝试次数计算得分,并输出错误次数和总分。程序包含输入验证和反馈打印功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

#include <stdio.h>
#include  <stdlib.h>
#include <time.h>
#define TRUE 1
#define FALSE 0

int  AddTest(int a, int b);
void Print(int flag, int chance);
int GiveScore(int a,int b);

int main()
{
    int a,b,totalScore = 0,errorCount = 0,gs;
    srand(1);
    int i;
    for(i=1;i<=10;i++)
    {
        a=rand()%10+1;
        b=rand()%10+1;
        printf("%d:%d+%d=",i,a,b);
        gs=GiveScore(a,b);
        totalScore=totalScore+gs;
        if(gs==0)
        {
            errorCount++;
        }
    }
    printf("score = %d, error numbers = %d\n", totalScore, errorCount);
    return 0;
}


int  AddTest(int a, int b)
{
    int s;
    scanf("%d",&s);
    if(s==a+b)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}


void Print(int flag, int chance)
{
	if (flag == TRUE)
		printf("\nRight!\n");
	else if (chance < 3)
		printf("\nNot correct. Try again!\n");
	else
		printf("\nNot correct. You have tried three times! Over!\n");
}


int GiveScore(int a,int b)
{
    int chance,score=0,flag;
    for(chance=1;chance<=3;chance++)
    {

        flag=AddTest(a,b);
        Print(flag,chance);
        if(flag)
        {
            score=10-chance+1;
            break;

        }
        else
        {
            if(chance<3)
            {
                printf("%d+%d=",a,b);
            }
        }
    }
    return score;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Color20

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值