基于c编写的关于随机生成四则运算的小程序

2018-06-18 03:58:49来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

基于http://www.cnblogs.com/HAOZHE/p/5276763.html改编写的关于随机生成四则运算的小程序

github源码和工程文件地址:https://github.com/HuChengLing/-

基本要求:

除了整数以外,还能支持真分数的四则运算。

对实现的功能进行描述,并且对实现结果要求截图。

主要功能:运算小学四则运算,可以选择加减乘除以及混合运算。

设计思想:首先思考运算数字范围,考虑小学生的运算能力,然后分类运算方法,分为加减乘除混合运算,

然后可以选择是继续做题还是退出程序,可以总结做对或者做错的题数,方便计算正确以及错误的题数。

二 程序代码:2017-09-16

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<conio.h>
#include<string.h>


int right = 0;
int wrong = 0;
char getSignal();
int  calculateIntegerResult(int num1, int num2, char signal);
//int getNumber();
void doIntegerSubject();
void falsePrint(int);
void correctPrint(int);

/*char getSignal()

{

char signal[4] = { '+','-','*','/' };

srand((unsigned)time(NULL));

return signal[rand() % 4];

}*/
char getSignal()
{
    srand((unsigned)time(NULL));
    int a;
    a = rand() % 4 + 1;
    switch (a)
    {
    case 1:
        return '+';
        break;
    case 2:
        return'-';
        break;
    case 3:
        return '*';
        break;
    case 4:
        return '/';
        break;
    default:
        break;
    }
}
/*int getNumber()
{
srand((unsigned)time(NULL));
int a, b;
a = rand() % 100 + 1;
b = rand() % 100 + 1;
return 0;
}*/

int calculateIntegerResult(int num1, int num2, char signal)
{
    int t;
    switch (signal)
    {
    case '+':
        t = num1 + num2;
        break;
    case'-':
        t = num1 - num2;
        break;
    case'*':
        t = num1*num2;
        break;
    case'/':
        t = num1 / num2;
        break;
    default:
        t = 0;
        printf("运算符不符合规则\n");
        break;
    }
    return t;
}

void calculateMarkResult(int num1, int num2, int num3, int num4, char signal, int mark[5])
{

    switch (signal)
    {
    case '+':
        if (num2 == num4)
        {
            mark[0] = num1 + num3;
            mark[1] = num1;
        }
        else
        {
            mark[0] = num1*num4 + num2*num3;
            mark[1] = num2*num4;
        }
        break;
    case'-':
        if (num2 == num4)
        {
            mark[0] = num1 - num3;
            mark[1] = num1;
        }
        else
        {
            mark[0] = num1*num4 - num2*num3;
            mark[1] = num2*num4;
        }
        break;
    case'*':
        mark[0] = num1*num3;
        mark[1] = num2*num4;
        break;
    case'/':
        mark[0] = num1*num4;
        mark[1] = num2*num3;
        break;
    default:
        printf("运算符不符合规则\n");
        break;
    }

}

void doIntegerSubject()
{
    int a, b, giverAnswer, printFlag;

    char signal;
    srand((unsigned)time(NULL));
    a = rand() % 100 + 1;
    b = rand() % 100 + 1;
    signal = getSignal();
    if (signal == '/')
    {
        if (b == 0)
        {
            int temp;
            temp = b;
            b = a;
            a = temp;
        }
    }
    printf("please do blew problem:\n");
    printf("%d %c %d=", a, signal, b);
    //giverAnswer = _getch();
    scanf_s("%d", &giverAnswer);
    fflush(stdin);
    if (calculateIntegerResult(a, b, signal) == giverAnswer)

    {
        right++;
        srand((unsigned)time(NULL));
        printFlag = rand() % 4 + 1;
        correctPrint(printFlag);

    }

    else

    {
        wrong++;
        srand((unsigned)time(NULL));
        printFlag = rand() % 4 + 1;;
        falsePrint(printFlag);
        printf("\n正确答案:%d\n", calculateIntegerResult(a, b, signal));
    }

}

void doMarkSubject()
{
    int a, b, c, d, i = 0, printFlag;
    int result[5];
    char giverNumber[10];
    char signal;
    int x, y, j;
    signal = getSignal();
    srand((unsigned)time(NULL));
    while (1)
    {
        a = rand() % 100;
        while (1)
        {
            b = rand() % 100;
            if (b)
                break;
        }
        if (a < b)
            break;
    }

    while (1)
    {
        c = rand() % 100;
        while (1)
        {
            d = rand() % 10;
            if (d)
                break;
        }
        if (c < d)
            break;
    }
    /*a = rand() % 100 + 1;
    b = rand() % 100 + 1;
    c = rand() % 100 + 1;
    d = rand() % 100 + 1;
    signal = getSignal();
    if (b == 0 || a>b)
    {
    int temp;
    temp = b;
    b = a;
    a = temp;
    }
    if (d == 0 || c>d)
    {
    int f;
    f = b;
    b = a;
    a = f;
    }*/
    printf("please do blew problem:\n");
    printf("%d/%d %c %d/%d=", a, b, signal, c, d);
    //giverAnswer = _getch();
    i = 0;
    while (1)
    {
        giverNumber[i] = _getche();
        if (giverNumber[i] == '/')
            break;
        i++;

    }
    x = 0;
    for (j = 0; j < i; j++)
    {
        x = x + pow(10, (i - 1 - j))*(giverNumber[j] - '0');
    }
    scanf_s("%d", &y);
    calculateMarkResult(a, b, c, d, signal, result);
    //scanf_s("%d", &giverNumber[i]);
    fflush(stdin);
    if (x == result[0] && y == result[1])

    {
        right++;
        srand((unsigned)time(NULL));
        printFlag = rand() % 4 + 1;
        correctPrint(printFlag);

    }

    else

    {
        wrong++;
        srand((unsigned)time(NULL));
        printFlag = rand() % 4 + 1;;
        falsePrint(printFlag);
        int *dd;
        printf("\n正确答案:%d/%d\n", result[0], result[1]);
        //printf("\n正确答案:%d\n", calculateMarkResult(a, b, signal, c, d));
    }

}

//回答正确打印信息
void correctPrint(int num)
{
    switch (num)

    {
    case 1:
        printf("You're right!\n"); break;
    case 2:
        printf("It's OK!\n"); break;
    case 3:
        printf("Correct!\n"); break;
    case 4:
        printf("Perfect!\n"); break;
    case 5:
        printf("Oh my gag!\n"); break;
    default:
        printf("Powerful!\n"); break;
    }

}

//回答错误打印信息

void falsePrint(int num)
{
    switch (num)
    {
    case 1:
        printf("You're lost!"); break;
    case 2:
        printf("Failure!"); break;
    case 3:
        printf("Come on!"); break;
    case 4:
        printf("It's wrong!"); break;
    default:
        printf("Incorrect!"); break;
    }
}


void main()
{
    int total;
    int con=0 , choice,choice1;
    printf("welcome to math world!:");
    system("cls");
        
        if (con = 0)
        {
            printf("\t\t\t  1.整数运算\n");
            printf("\t\t\t  2.分数运算\n");
            printf("请选择:\n");
            scanf_s("%d", &choice);
            system("cls");
            switch (choice)
            {
            case 1:
                while (right >= wrong)
                {
                    doIntegerSubject();

                }
                total = right + wrong;
                printf("你一共答了:%d道题其中答对了%d 道答错了%d道\n", total, right, wrong);
                break;
            case 2:
                while (right >= wrong)
                {
                    doMarkSubject();
                }
                total = right + wrong;
                printf("你一共答了:%d道题其中答对了%d 道答错了%d道\n", total, right, wrong);
                break;
            }
            system("pause");
        }

        printf("\n\t\t\t1.重新选择?\n");
        printf("\n\t\t\t2.退出运算?\n");
        scanf_s("%d", &choice1);
        if (choice1 == 1)
        {
            system("cls");
            con = 0;
        }
        else if (choice1 == 2)
        {
            exit;
        }
        else
            printf("抱歉!,你输入的指令有误!请重新输入!\n");
        system("pause");
    
}

总结:一个简单的自动随机生成四则运算 之前百度的源码,它的功能主要是随机生成符号和随机数来达到随机的加、减、乘、除的算术表达数,他也没有让用户自己选择是否继续运算或者退出系统,也不支持正分数的运算。 在网络源码的基础上,我将原来的随机获取函数进行了修改,其次我增加分数运算和分数判断,以及统计正误数据的变量并输出来和用户可以自行选择是否继续运算的选项。

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:串口实现FIFO接受数据(V2)

下一篇:编程语言——C----细节