源代码如下:

​

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#define M 100
int n=0,x=0,y=0, random , guess , counter=0;
char random1 , guess1 , name0[20] , fh[100]={'`','~','!','@','$','%','^','&','*','(',')','-','_','=','[',']','{','}',':',';','"',',','.','/','?','+','-','|','*','/'};
double pro;
struct game{
      char name[20];
      int b , c;
      double prob;
}a[M+1];
int main ()
{
    system("title 猜数(字符)游戏");
    system("color F0");
    for(int w=0;w<M;w++)
        a[w].b=0;
    printf("\t\t\t\tplease input your name:");
    gets(name0);
    MENU();
    AGAIN();
    READ();
    system("cls");
    int v=1;
    for(int w=0;w<M+1;w++)
    {
        if(a[w].b!=0){
        printf("\t\t\tNO:%d  %10s  %5d  %5d  %3.2lf\n",v,a[w].name,a[w].b,a[w].c,a[w].prob);
        v++;
        }
    }
    WRITE();

}
void WRITE ()
{
    FILE*w;
    w=fopen("e:\\project.txt","wb");
    fwrite(a,sizeof(struct game),M,w);
    fclose(w);
}
void READ ()
{
    FILE*w;
    int p=0;
    w=fopen("e:\\project.txt","rb");
    fread(a,sizeof(struct game),M,w);

    for(int i=0;i<M+1;i++)
    {
        if(strcmp(a[i].name,name0)==0)
        {
            p++;
            if(a[i].prob<pro)
            {
                strcpy(a[i].name,name0);
                a[i].b=x;
                a[i].c=y;
                a[i].prob=pro;
            }
        }
    }
    if(p==0){
       strcpy(a[M].name,name0);
            a[M].b=x;
            a[M].c=y;
            a[M].prob=pro;
    }
    PPX();
    fclose(w);
}
void PPX ()
{
    int  i ,j , k;
    struct game q;
    for(i=0;i<M;i++){
    k=i;
       for(j=i+1;j<M+1;j++){
        if(a[k].prob<a[j].prob)k=j;
       }
       q=a[i];
       a[i]=a[k];
       a[k]=q;
       }
}

void START ()
{

        random=RANDOM1();
        printf("\t\t\ttype a number from 1 to 1000:");
        scanf("%d",&guess);
        counter++;
        while(random!=guess)
        {
            TABLE();
            counter++;
            printf("\t\t\t  type a number from 1 to 1000:");
            scanf("%d",&guess);

        }
        TABLE();
        x=x+counter;
        y=y+1;
        pro=(double)(1.0/(double)counter);
        printf("\n\t\t\tyour winning probability is %.2lfpersent",pro*100);
        system("pause");
    }
void START1 ()
{

        random1=fh[RANDOM2()];
        printf("\t\t\ttype a symble from the keyboard:");
        scanf("%c",&guess1);
        counter++;
        while(random1!=guess1)
        {
            TABLE1();
            counter++;
            printf("\n\t\t\t  type a symble from the keyboard:");
            scanf("%c",&guess1);

        }
        TABLE1();
        x=x+counter;
        y=y+1;
        pro=(double)(1.0/(double)counter);
        printf("\n\t\t\tyour winning probability is %.2lfpersent",pro*100);
        system("pause");
    }


void MENU ()
{
    printf("\t\t\t╔═══════════════════════════════════════╗\n");
    printf("\t\t\t║                                       ║\n");
    printf("\t\t\t║        欢迎来到猜数(字符)游戏         ║\n");
    printf("\t\t\t║                                       ║\n");
    printf("\t\t\t║                                       ║\n");
    printf("\t\t\t║        ┏━━┓        ┏━━┓       ┏━━┓    ║\n");
    printf("\t\t\t║   猜数:┃ 1┃ 猜字符:┃ 2┃  退出:┃ 3┃    ║\n");
    printf("\t\t\t║        ┗━━┛        ┗━━┛       ┗━━┛    ║\n");
    printf("\t\t\t║                                       ║\n");
    printf("\t\t\t║                                       ║\n");
    printf("\t\t\t╚═══════════════════════════════════════╝\n");

    switch(getch()){
        case '1':
                system("cls");
                START();
                break;
        case '2':
                system("cls");
                START1();
                break;
        case '3':
                exit(0);
                break;
        default:
            printf("error");
        }
}
void TABLE ()
{
    system("cls");
    printf("\t\t\t╔═════════════════════════════");
    printf("═══════════════════════════╗\n");
    printf("\t\t\t║                                                        ║\n");
       if(guess > random)
           printf("\t\t\t║ Too big , type a number from 1 to 1000 again           ║\n");
       else if(guess < random)
           printf("\t\t\t║ Too small , type a number from 1 to 1000 again         ║\n");
       else
           printf("\t\t\t║  congratulations,the number is : %5d                 ║\n" ,random);
    printf("\t\t\t║                                                        ║\n");
    printf("\t\t\t║                                                        ║\n");
    printf("\t\t\t║ the number of the times you have guessed is: %5d     ║\n", counter);
    printf("\t\t\t║                                                        ║\n");
    printf("\t\t\t║                                                        ║\n");
    printf("\t\t\t╚══════════════════════════════");
    printf("══════════════════════════╝\n");


}
void TABLE1 ()
{
    system("cls");
    printf("\t\t\t╔═════════════════════════════");
    printf("═══════════════════════════╗\n");
    printf("\t\t\t║                                                        ║\n");
       if(guess1!=random1)
           printf("\t\t\t║ type a symble from the keyboard again                  ║\n");
       else
           printf("\t\t\t║  congratulations,the symble is : %5c                 ║\n" ,random1);
    printf("\t\t\t║                                                        ║\n");
    printf("\t\t\t║                                                        ║\n");
    printf("\t\t\t║ the symble of the times you have guessed is: %5d     ║\n", counter);
    printf("\t\t\t║                                                        ║\n");
    printf("\t\t\t║                                                        ║\n");
    printf("\t\t\t╚══════════════════════════════");
    printf("══════════════════════════╝\n");


}
void AGAIN ()
{
    system("cls");
    printf("\t\t\t╔═══════════════════════════╗\n");
    printf("\t\t\t║                           ║\n");
    printf("\t\t\t║         你成功啦          ║\n");
    printf("\t\t\t║                           ║\n");
    printf("\t\t\t║                           ║\n");
    printf("\t\t\t║        ┏━━┓        ┏━━┓   ║\n");
    printf("\t\t\t║   重来:┃ 1┃   退出:┃ 2┃   ║\n");
    printf("\t\t\t║        ┗━━┛        ┗━━┛   ║\n");
    printf("\t\t\t║                           ║\n");
    printf("\t\t\t║                           ║\n");
    printf("\t\t\t╚═══════════════════════════╝\n");
    switch(getch()){
        case '1':
            system("cls");
            counter=0;
            MENU();
            break;
        case '2':
            printf("\t\t\tRECORD WRITTEN\n\t\t\t%s %d %d %.2lf\n",name0,x,y,pro*100);
            system("pause");
            break;
        default:
            printf("error");
        }
}
int RANDOM1 ()
{
    int                   x;
    srand      (time(NULL));
    x=((unsigned)rand()%1000);
    return x;
}
int RANDOM2 ()
{
    int                   x;
    srand      (time(NULL));
    x=((unsigned)rand()%30);
    return x;
}

[点击并拖拽以移动]
​

程序员宿舍淦酒必备,每人猜完自动排名,排最后的进行惩罚哈哈哈。(脑洞有点大)

菜单页面:

 

创建文件并保存排名,游戏结束自动显示排名

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐