⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 24.c

📁 24点游戏,DOS下运行的,增加了输入验证的强度,可以在程序中对答案进行查询
💻 C
📖 第 1 页 / 共 2 页
字号:
/*核心24点算法模块*/
#include "MAGIC.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <string.h>

void Magic(int a,int b,int c,int d,char *Answer[])
{
    int sgn1,sgn2,sgn3;
    float res1,res2,res;

    int i=1,j;
    int flag=0;
    int AnsCount=0;
    int past;

    int *G0[3],*G1[12];

    int G000[4],G001[4],G002[4];
    int G100[4],G101[4],G102[4],G110[4],G111[4],G112[4],
        G120[4],G121[4],G122[4],G130[4],G131[4],G132[4];

    char form[18]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
    char tform[18],ttform[18];
    char Out[25];

    G000[0]=a;G000[1]=b;G000[2]=c;G000[3]=d;
    G001[0]=a;G001[1]=c;G001[2]=b;G001[3]=d;
    G002[0]=a;G002[1]=d;G002[2]=b;G002[3]=c;

    G0[0]=G000;G0[1]=G001;G0[2]=G002;

    G100[0]=a;G100[1]=b;G100[2]=c;G100[3]=d;
    G101[0]=a;G101[1]=c;G101[2]=b;G101[3]=d;
    G102[0]=a;G102[1]=d;G102[2]=b;G102[3]=c;

    G110[0]=b;G110[1]=a;G110[2]=c;G110[3]=d;
    G111[0]=b;G111[1]=c;G111[2]=a;G111[3]=d;
    G112[0]=b;G112[1]=d;G112[2]=a;G112[3]=c;

    G120[0]=c;G120[1]=a;G120[2]=b;G120[3]=d;
    G121[0]=c;G121[1]=b;G121[2]=a;G121[3]=d;
    G122[0]=c;G122[1]=d;G122[2]=a;G122[3]=b;

    G130[0]=d;G130[1]=a;G130[2]=b;G130[3]=c;
    G131[0]=d;G131[1]=b;G131[2]=a;G131[3]=c;
    G132[0]=d;G132[1]=c;G132[2]=a;G132[3]=b;

    G1[0]=G100;G1[1]=G101;G1[2]=G102;
    G1[3]=G110;G1[4]=G111;G1[5]=G112;
    G1[6]=G120;G1[7]=G121;G1[8]=G122;
    G1[9]=G130;G1[10]=G131;G1[11]=G132;

for(i=0;i<12;i++)
{   for(sgn3=0;sgn3<6;sgn3++)
    {
        for(j=0;j<17;j++)
        {
            form[j]=' ';
        }

        switch(sgn3)
        {
            case 0:
                res1=*(G1[i]+2)+*(G1[i]+3);
                form[6]=ntoc(*(G1[i]+2));
                form[7]='+';
                form[8]=ntoc(*(G1[i]+3));
                break;
            case 1:
                res1=*(G1[i]+2)-*(G1[i]+3);
                form[6]=ntoc(*(G1[i]+2));
                form[7]='-';
                form[8]=ntoc(*(G1[i]+3));
                break;
            case 2:
                res1=*(G1[i]+3)-*(G1[i]+2);
                form[6]=ntoc(*(G1[i]+3));
                form[7]='-';
                form[8]=ntoc(*(G1[i]+2));
                break;
            case 3:
                res1=(*(G1[i]+2))*(*(G1[i]+3));
                form[6]=ntoc(*(G1[i]+2));
                form[7]='*';
                form[8]=ntoc(*(G1[i]+3));
                break;
            case 4:
                if(*(G1[i]+3)==0) break;
                res1=(float)(*(G1[i]+2))/(*(G1[i]+3));
                form[6]=ntoc(*(G1[i]+2));
                form[7]='/';
                form[8]=ntoc(*(G1[i]+3));
                break;
            case 5:
                if(*(G1[i]+2)==0) break;
                res1=(float)(*(G1[i]+3))/(*(G1[i]+2));
                form[6]=ntoc(*(G1[i]+3));
                form[7]='/';
                form[8]=ntoc(*(G1[i]+2));
                break;
        }

        strcpy(tform,form);

        for(sgn2=0;sgn2<6;sgn2++)
        {
            switch(sgn2)
            {
                case 0:
                    res2=*(G1[i]+1)+res1;
                    form[3]=ntoc(*(G1[i]+1));
                    form[4]='+';
                    break;
                case 1:
                    res2=*(G1[i]+1)-res1;
                    form[3]=ntoc(*(G1[i]+1));
                    form[4]='-';
                    if(form[7]=='+')form[7]='-';
                    else if(form[7]=='-')form[7]='+';
                    break;
                case 2:
                    res2=res1-*(G1[i]+1);
                    form[11]=ntoc(*(G1[i]+1));
                    form[10]='-';
                    break;
                case 3:
                    res2=(*(G1[i]+1))*res1;
                    form[3]=ntoc(*(G1[i]+1));
                    form[4]='*';
                    if((form[7]=='+')||(form[7]=='-'))
                    {
                        form[5]='(';
                        form[9]=')';
                    }
                    break;
                case 4:
                    if(res1==0)break;
                    res2=(float)(*(G1[i]+1))/res1;
                    form[3]=ntoc(*(G1[i]+1));
                    form[4]='/';
                    if((form[7]=='+')||(form[7]=='-'))
                    {
                        form[5]='(';
                        form[9]=')';
                    }
                    else if(form[7]=='*')
                    {
                        form[7]='/';
                    }
                    else if(form[7]=='/')
                    {
                        form[7]='*';
                    }
                    break;
                case 5:
                    if(*(G1[i]+1)==0) break;
                    res2=(float)res1/(*(G1[i]+1));
                    form[11]=ntoc(*(G1[i]+1));
                    form[10]='/';
                    if((form[7]=='+')||(form[7]=='-'))
                    {
                        form[5]='(';
                        form[9]=')';
                    }
                    break;
            }

            strcpy(ttform,form);
            for(sgn1=0;sgn1<6;sgn1++)
            {
                switch(sgn1)
                {
                    case 0:
                        res=*(G1[i])+res2;
                        form[0]=ntoc(*(G1[i]));
                        form[1]='+';
                        break;
                    case 1:
                        res=*(G1[i])-res2;
                        form[0]=ntoc(*(G1[i]));
                        form[1]='-';

                        if(form[4]=='+')
                        {
                            form[4]='-';

                            if(form[7]=='+')
                            {
                                form[7]='-';
                            }
                            else if(form[7]=='-')
                            {
                                form[7]='+';
                            }
                        }
                        else if(form[4]=='-')
                        {
                            form[4]='+';

                            if(form[7]=='+')
                            {
                                form[7]='-';
                            }
                            else if(form[7]=='-')
                            {
                                form[7]='+';
                            }
                        }

                        if(form[10]=='+')
                        {
                            form[10]='-';

                            if(form[7]=='+')
                            {
                                form[7]='-';
                            }
                            else if(form[7]=='-')
                            {
                                form[7]='+';
                            }
                        }
                        else if(form[10]=='-')
                        {
                            form[10]='+';

                            if(form[7]=='+')
                            {
                                form[7]='-';
                            }
                            else if(form[7]=='-')
                            {
                                form[7]='+';
                            }
                        }
                        break;
                    case 2:
                        res=res2-*(G1[i]);
                        form[13]='-';
                        form[14]=ntoc(*(G1[i]));
                        break;
                    case 3:
                        res=(*(G1[i]))*res2;
                        form[0]=ntoc(*(G1[i]));
                        form[1]='*';
                        if((form[4]=='+')||(form[4]=='-')||(form[10]=='+')||(form[10]=='-'))
                        {
                            form[2]='(';
                            form[12]=')';
                        }
                        break;
                    case 4:
                        if(res2==0)break;
                        res=(float)(*(G1[i]))/res2;
                        form[0]=ntoc(*(G1[i]));
                        form[1]='/';
                        if((form[4]=='+')||(form[4]=='-')||(form[10]=='+')||(form[10]=='-'))
                        {
                            form[2]='(';
                            form[12]=')';
                        }
                        else if((form[4]=='*')||(form[10]=='*'))
                        {
                            if(form[4]=='*')form[4]='/';
                            else if(form[10]=='*')form[10]='/';

                            if(form[7]=='*')
                            {
                                form[7]='/';
                            }
                            else if(form[7]=='/')
                            {
                                form[7]='*';
                            }

                        }
                        else if((form[4]=='/')||(form[10]=='/'))
                        {
                            if(form[4]=='/')form[4]='*';
                            else if(form[10]=='/')form[10]='*';

                            if(form[7]=='*')
                            {
                                form[7]='/';
                            }
                            else if(form[7]=='/')
                            {
                                form[7]='*';
                            }
                        }
                        break;
                    case 5:
                        if(*(G1[i])==0) break;
                        res=res2/(*(G1[i]));
                        form[13]='/';
                        form[14]=ntoc(*(G1[i]));
                        if((form[4]=='+')||(form[4]=='-')||(form[10]=='+')||(form[10]=='-'))
                        {
                            form[2]='(';
                            form[12]=')';
                        }
                        break;
                }
                if(fabs(res-24)<1e-5)
                {
                    Output(form,Out);

                    for(past=AnsCount-1;past>=0;past--)
                    {
                        if(strcmp(Answer[past],Out)==0)
                        break;
                    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -