鱼C论坛

 找回密码
 立即注册
查看: 1411|回复: 1

文件读写为什么显示不全(代码如下,才做一半,单词的话是自己的文件夹里面创建txt )

[复制链接]
发表于 2021-11-20 15:59:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h> //标准输入输出函数库
#include <time.h> //用于获得随机数
#include <windows.h> //控制dos界面
#include <stdlib.h> //即standard library标志库头文件,里面定义了一些宏和通用工具函数
#include <string.h>

#define CNO 4 //选项数量
#define swap(type,x,y) do {type t = x;x = y;y = t; }while(0)

void endgame(); //结束练习函数

void File_in(); //存储最高分文件
void File_out(); //读取文件最高分
void choose(); //分支选择函数
void Lost(); //结果界面
void rule(); //计分规则函数
int read(); //读取单词文件函数



int color(int c); //设置颜色函数
void gotoxy(int x,int y); //设置光标函数
void start(); //开始界面函数







/*
void run(); //判断选择题结果函数

int make(int c[],int n);//生成选项并返回正确的下标

void print(const int c[],int sw); //显示选项
*/



int score = 0; //分数
int HighScore = 0; //最高分数
int QNO; //单词数量
char **cptr; //指向中文单词的指针数组
char **eptr; //指向英语单词的指针数组



/*文字颜色函数*/
int color (int c)
{
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);//更改文字颜色
        return 0;
}

/*设置光标位置*/
void gotoxy(int x,int y)
{
        COORD c;
        c.X = x;
        c.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
  }  
  
  /*开始界面*/
  void start()
  {
          int n;
          int i,j = 1;
          gotoxy(20,2);
          color(14);
          printf("英 语 单 词 巩 固 练 习");
        color(10);
        for(i = 6;i <= 12;i++)                        //输入上下边框
        {
                for(j = 7;j <=54;j++)                //输出左右边框☆
                {
                        gotoxy(j,i);
                        if(i == 6 || i ==12)
                        {
                                printf("*");
                        }
                        else if(j == 7 || j == 54)
                        {
                                printf("☆");
                        }
                }
         }
         color(13);
         gotoxy(15,8);
         printf("1.开始练习");
         gotoxy(35,8);
         printf("2.积分规则");
         gotoxy(15,10);
         printf("3.退出练习");
         gotoxy(22,13);
         color(12);
         printf("请选择[1 2 3]:[ ]\b\b");//\b为退格,使得光标处于[]中间
         color(15);
         scanf("%d",&n);                                 //输入选项
         switch(n)
         {
                 case 1:
                         system("cls");
                         run();                                        //添加新的代码
                         break;
                 case 2:
                         rule();                                        //积分规则函数
                         break;
                 case 3:
                         exit(0);                                //退出游戏
                         break;
                 default:                                        //输入非1~3的选项
                         color(12);
                         gotoxy(40,28);
                        printf("请输入1~3的数!");
                        getch();                                //输入任意键
                        system("cls");                        //清屏
                        start();
          }
         
  }
  /*积分规则*/
  void rule()
  {
          int i,j = 1;
          system("cls");
          color(12);
          gotoxy(44,3);
          printf("积分规则");
          color(5);
          for(i = 6;i <= 22;i++)                //输出上下边框
          {
                  for(j = 20;j <=75;j++)        //输出左右边框
                  {
                          gotoxy(j,i);
                          if(i ==6 || i == 22)printf("*");
                          else if(j == 20 || j ==75)printf("*");
                   }
          }
         color(13);
         gotoxy(30,8);
         printf("规则1:从4个选项里选择对应的词义");
         color(10);
         gotoxy(30,11);
         printf("规则2:回答正确加10分");
         color(14);
         gotoxy(30,14);
         printf("规则3:回答错误减掉10分");
         color(11);
         gotoxy(30,17);
         printf("规则4:Esc退出练习");
         getch();                                //按任意键返回主界面
         system("cls");
         start();
   }
   
   /*读取单词*/
   int read()
   {
           int i;
           FILE *fp;                                //定义文件指针
           char filename[30];
           color(12);
           printf("请输入单词的存储文件名:\n");
           color(15);
           scanf("%s",filename);
           if((fp = fopen(filename,"r")) == NULL)
                   return 1;
           fscanf(fp, "%d" ,&QNO);        //读取单词的数量
        if((cptr = calloc(QNO,sizeof(char *))) == NULL) return 1;
        if((eptr = calloc(QNO,sizeof(char *))) == NULL) return 1;
       
        for(i = 0;i < QNO ;i++){
                char etemp[1024];
                char ctemp[1024];
                fscanf(fp, "%s%s" ,etemp,ctemp);
                if((eptr[i] = malloc(strlen(etemp) + 1)) == NULL) return 1;
                if((cptr[i] = malloc(strlen(ctemp) + 1)) == NULL) return 1;
                strcpy(eptr[i], etemp);
                strcpy(cptr[i], ctemp);
       
                }
                fclose(fp);
                return 0;
        }
        /*存储最高分文件*/
        void File_in()
        {
                FILE *fp;
                fp = fopen("save.txt","w+");        //以读写的方式建立一个名为save.txt的文件
                fprintf(fp, "%d",score);                //把分数写进文件
                fclose(fp);                                         //关闭文件
         }
         /*在文件中读取最高分*/
         void File_out()
         {
                 FILE *fp;
                 fp = fopen("save.txt","a+");        //打开文件save.txt
                 fscanf(fp, "%d",&HighScore);        //把文件中的最高分读出来
                 fclose(fp);                                                //关闭文件
          }  
          
          /*显示选项*/
           void pritf(const int c[],int sw)
           {
                   int i;
                   color(14);
                   for(i = 0; i < CNO;i++)
                           printf("(%d) %s  ", i, sw ? cptr[c[i]] : eptr[c[i]]);
                   printf(": ");
                }
          
          /*生成选项并返回正确的下标*/
          int make(int c[],int n)
          {
                  int i,j,x;
                 
                  c[0] = n;                                        //在开头元素中存入正确的答案
                 
                for(i = 1;i < CNO;i++) {
                        do{                                                //生成不重复的随机数
                                x = rand() % QNO;
                                for(j = 0;j < i;j++)
                                if(c[j] == x)                //已经生成了相同的随机数
                                        break;
                        }while(i != j);
                        c[i] = x;
                }
                j = rand() % CNO;
                if(j != 0)
                        swap(int,c[0],c[j]);        //移动正确答案
                        return j;
           }
                  
                /*判断选择结果*/
                int run()
                {
                        int i;
                        int nq,pq;                                //题目编号和上一次的题目编号
                        int na;                                        //正确答案的编号
                        int sw;                                        //题目语言(0:中文/1:英文)
                        int retry;                                //重新挑战吗?
                        int cand[CNO];                        //选项的编号
                       
                        if(read() == 1)
                        {
                                printf("\a单词文件读取失败。\n");
                                return 1;
                         }
                         srand(time(NULL));                //设定随机数的种子       
                                
                         pq = QNO;                                //上一次的题目编号(不存在的编号)
                                
                         do{
                                 int no;
                                
                                 do{                                        //决定用于出题的单词的编号
                                         nq = rand() % QNO;
                                 }while(nq==pq);        //不连续出同一个单词
                                 
                                 na = make(cand,nq);//生成选项
                                 sw = rand() % 2;
                                 color(11);
                                 printf("哪一个是%s?\n",sw ? eptr[nq] : cptr[nq]);
                                 
                                 do{
                                         printf(cand, sw);//显示选项
                                        scanf("%d",&no);
                                        if(no != na)
                                        {
                                                color(15);
                                                puts("对不起,您的回答错误,减掉10分");
                                                score -= 10;
                                         }
                                 }while(no != na);
                                 color(13);
                                 puts("您的回答正确,恭喜加10分");
                                 score += 10;
                                 pq = nq;
                                 color(10);
                                 printf("再来一次?0-否/1-是:");
                                 scanf("%d",&retry);                  
                         }while(retry == 1);
                         
                         for(i = 0;i < QNO;i++)
                         {
                                 free(eptr[i]);
                                 free(cptr[i]);
                         }
                         free(cptr);
                         free(eptr);
                         File_out();
                        // endgame();
                         exit(0);
                         return 0;
                 }
  
  int main()
  {
          start();
          run();
          return 0;
  }
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-11-20 16:01:01 | 显示全部楼层
为什么我发不了图片TT
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 01:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表