程序要实现 通过上下键能翻行 左右键翻页 程序不知哪里错了不能实现这个功能 请帮忙修改一下 多谢!
#include
#include
#include
#include
#include
#define MAXLINE 100 // 最大支持行数
fpos_t posbefore[MAXLINE]; // 保存行开始位置
char tmp[100]; // 输出缓冲区
void DisplayMenu(); // 显示菜单
int ChooseMenu(); // 接收用户对菜单的选择
void DisplayFile(FILE* Pointer); // 显示文件
void DisplayScreen(FILE* Pointer, int Current); // 显示一屏幕的信息
int main()
{
FILE* lpFile = NULL;
int Choose;
DisplayMenu();
while (Choose = ChooseMenu())
{
switch (Choose)
{
case 1:
// 选择了 1.打开文件<chapter.dat>
lpFile = fopen("chapter.dat", "r");
if (!lpFile)
{
printf("\n\t打开文件出错!\n");
exit(0);
}
else
{
printf("\n\t文件已经打开!\n\n");
system("pause");
}
break;
case 2:
if (lpFile)
{
DisplayFile(lpFile);
fclose(lpFile);
lpFile = NULL;
}
else
{
printf("\n\t文件未打开,请先打开文件!\n\n");
system("pause");
}
// 选择了 2.显示文件
break;
default:
break;
}
system("cls");
DisplayMenu();
}
if (lpFile) // 不为NULL时关闭文件
{
fclose(lpFile);
}
return 0;
}
void DisplayMenu() // 此函数用于显示菜单
{
printf("\n\n\t\t英文文章的显示\n");
printf("\n\t作者:芮铭泽\n");
printf("\n\t1.打开文件\n");
printf("\n\t2.显示文件\n");
printf("\n\t3.退出程序\n");
printf("\n\t翻行使用<↑,↓>翻页使用<←,→>\n");
printf("\n");
return ;
}
int ChooseMenu() // 此函数用于接收用户对菜单的选择,返回整型值1,2,0,分别表示选择第1,2,3项菜单
{
char tmp[10];
while (true)
{
printf("\n\t请选择您要进行的操作:[ ]\b\b");
scanf("%9s", tmp);
switch (tmp[0])
{
case '1':
// 选择了 1.打开文件
return 1;
case '2':
// 选择了 2.显示文件
return 2;
case '3':
// 选择了 3.退出程序
return 0;
default:
printf("\n\t没有此选项!\n");
break;
}
}
return 0;
}
void DisplayFile(FILE* Pointer) // 显示文件
{
fpos_t pos; // 记录文件读取位置
int i = MAXLINE; // 显示行计数
int j = 1;
int Max = 0;
int Current = 0;
int InputChar;
fgetpos(Pointer, &pos);
posbefore[0] = pos;
system("cls");
while (i--)
{
if (!fgets(tmp, 100, Pointer)) // 是否已到文件尾?
{
break;
}
fgetpos(Pointer, &posbefore[j++]); // 存储每一行开始
}
Max = j - 1;
while (true)
{
DisplayScreen(Pointer, Current);
Current = 0;
while (true)
{
InputChar = getch();
InputChar &= 0xff;
if (InputChar == 'q' || InputChar == 'Q')
{
break;
}
if (InputChar != 0xe0)
{
continue;
}
InputChar = getch();
// printf("%02x",InputChar);
switch (InputChar) // ↑:0xe048 ↓:0xe050 ←:0xe04b →:0xe04d
{
case 0xe048:
// ↑
// printf("\n\t↑\n");
Current --;
if (Current < 0)
{
Current = 0;
}
DisplayScreen(Pointer, Current);
break;
case 0xe050:
// ↓
// printf("\n\t↓\n");
Current ++;
if (Current > Max)
{
Current = Max;
}
DisplayScreen(Pointer, Current);
break;
case 0xe04b:
// ←
// printf("\n\t←\n");
Current -= 22;
if (Current < 0)
{
Current = 0;
}
DisplayScreen(Pointer, Current);
break;
case 0xe04d:
// →
// printf("\n\t→\n");
Current += 22;
if (Current > Max)
{
Current = Max;
}
DisplayScreen(Pointer, Current);
break;
}
}
break;
}
return ;
}
void DisplayScreen(FILE* Pointer, int Current) // 显示一屏幕的信息
{
int i;
int len;
int line;
int Table;
system("cls");
fsetpos(Pointer, &posbefore[Current]); // 设置文件读取位置
printf("\n\t按 <q> 返回主菜单\n");
printf("1 5 10 15 20 25 30 35 40 45 50 55 60 65 70\n");
for (i = 0; i < 22; i++)
{
if (!fgets(tmp, 100, Pointer)) // 是否已到文件尾?
{
printf("\n\t已到文件尾!\n", tmp);
break;
}
len = strlen(tmp);
if(tmp[len-1] == '\n')
{
tmp[len-1] = '\0';
}
for (line = 0,Table = 0;line < len; line++) // 扫描缓冲区中的 '\t' 键,一个 '\t' 最大可占8个空格宽度
{
if (tmp[line] == '\t')
{
if(!((line+Table*7)%8))
{
Table ++;
}
else
{
len += 8-line%8;
}
}
}
len = len + Table*7;
len %= 80;
len = 80 - len - 10;
if (len < 0) // 用空格填充,准备输出行号
{
printf("%s", tmp);
printf("\n");
for (len = 0; len < 7; len++)
{
printf(" ");
}
printf("第%d行\n",++Current);
}
else
{
for (; len > 0; len--)
{
strcat(tmp," ");
}
sprintf(tmp,"%s第%d行\n",tmp,++Current);
printf("%s",tmp);
}
len = strlen(tmp);
line = len / 80;
if (line) // 当文件中一行在屏幕上不能只显示一行时,减少显示的行数
{
i += line;
}
}
}
该回答引用ChatGPT-3.5
这里有几个问题需要修复:
stdio.h
、stdlib.h
、conio.h
和string.h
。请在程序开头添加以下代码行:#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
DisplayScreen
中的行号计算有误。当前行号应该是Current + i + 1
,而不仅仅是++Current
。请修改以下两行代码:printf("%s第%d行\n", tmp, Current + i + 1);
DisplayFile
函数中的循环控制条件有误。将while (true)
替换为while (Choose != 3)
,以便在选择退出程序时正确退出循环。while (Choose != 3)
main
函数中,将while (Choose = ChooseMenu())
更正为while ((Choose = ChooseMenu()) != 3)
,以确保当选择退出程序时正确退出循环。while ((Choose = ChooseMenu()) != 3)
修复完上述问题后,你的程序应该能够实现通过上下键翻行和左右键翻页的功能。