C语言做作弊器
刚刚接触C语言的文件处理,做了个“虫虫大作战”的作弊器。#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//函数声明
char menu();
void main();
int look_news();
double search(char n[],FILE *fp0);
char menu()
{
system("cls");
printf("*************************************************\n");
printf("* 虫虫大作战作弊器V1.0 *\n");
printf("* 主菜单 *\n");
printf("* 1.查看昆虫信息 *\n");
printf("* 2.修改昆虫信息 *\n");
printf("* 3.恢复昆虫信息 *\n");
printf("* 4.查看修改历史 *\n");
printf("* 5.退出系统 *\n");
printf("* 请选择菜单项(1~5) *\n");
printf("*************************************************\n");
return getch();
}
void main()
{
char c,t;
while(1)
{
c=menu();
switch(c)
{
case '1':
look_news();
break;
case '2':
modify_news();
break;
case '3':
break;
case '4':
break;
case '5':
system("cls");
printf("\n\t你想退出作弊器吗 ?(y/n)");
t=getch();
if(t=='y'||t=='Y')exit(0);
break;
}
printf("\n\t请按任意键返回主菜单……");
getch();
}
}
int look_news()
{
int n,number;
char name[20];
char dz1[100]="buginfos\\";
char dz2[100]="bugs\\";
char filename[50];
FILE *fosfp,*namefp,*sxfp;
printf("请输入要查看的昆虫序号:");
scanf("%d",&n);
if((namefp=fopen("虫虫序号.txt","r"))==0)
printf("打开虫虫序号文本错误");
while(!feof(namefp))
{
fscanf(namefp,"%d %s %s\n",&number,name,filename);
if(number==n)
{
break;
}
}
if(number!=n)
{
printf("找不到此序号的昆虫!");
return 0;
}
strcat(dz1,filename);
strcat(dz2,filename);
if((fosfp=fopen(dz1,"r"))==0||(sxfp=fopen(dz2,"r"))==0)
{
printf("打开文件出错");
return 0;
}
printf("\n名字:%s\n",name);
printf("序号:%d\n",number);
printf("价格:%.0f点花蜜\n",search("Price",fosfp));
printf("孵化时间:%.0f秒\n",search("ReloadTime",fosfp));
printf("速度:%.1f\n",search("Speed",sxfp));
printf("生命值:%.0f\n",search("InitialHealth",sxfp));
printf("近战攻击力:%.1f\n",search("MeleeDamage",sxfp));
printf("近战攻击距离:%.1f\n",search("MeleeDistance",sxfp));
printf("攻击速度:%.1f\n",search("AttackSpeed",sxfp));
return 0;
}
int modify_news()
{
int n,number;
char sx[10];
char name[20];
char dz1[100]="buginfos\\";
char dz2[100]="bugs\\";
char filename[50];
FILE *fosfp,*namefp,*sxfp;
printf("请输入要修改的昆虫序号:");
scanf("%d",&n);
if((namefp=fopen("虫虫序号.txt","r"))==0)
printf("打开虫虫序号文本错误");
while(!feof(namefp))
{
fscanf(namefp,"%d %s %s\n",&number,name,filename);
if(number==n)
{
break;
}
}
if(number!=n)
{
printf("找不到此序号的昆虫!");
return 0;
}
strcat(dz1,filename);
strcat(dz2,filename);
if((fosfp=fopen(dz1,"r"))==0||(sxfp=fopen(dz2,"r"))==0)
{
printf("打开文件出错");
return 0;
}
printf("\n名字:%s\n",name);
printf("序号:%d\n",number);
printf("价格:");
scanf("%s",&sx);
modify("Price",sx,dz1);
printf("孵化时间:");
scanf("%s",&sx);
modify("ReloadTime",sx,dz1);
printf("速度:");
scanf("%s",&sx);
modify("Speed",sx,dz2);
printf("生命值:");
scanf("%s",&sx);
modify("InitialHealth",sx,dz2);
printf("近战攻击力:");
scanf("%s",&sx);
modify("MeleeDamage",sx,dz2);
printf("近战攻击距离:");
scanf("%s",&sx);
modify("MeleeDistance",sx,dz2);
printf("攻击速度:");
scanf("%s",&sx);
modify("AttackSpeed",sx,dz2);
return 0;
}
double search(char n[],FILE *fp0)
{
char mc[20],sp[10],str;
char number[30];
while(!feof(fp0))
{
fscanf(fp0,"%s %s %s\n",sp,mc,number);
if(strcmp(mc,n)==0)
{
return atof(number);
}
}
}
int modify(char n[],char x[],char dz[])
{
char content[100][100];
int m,i,fal,len,s;
char p;
char mc[20],sp[10],str;
char number[30];
FILE *fp5;
len=strlen(x);
for(i=0;i<len;i++)
{
if(isdigit(x[i])==0)
fal=0;
}
if(fal==0)
return 0;
s=atoi(x);
m=0;
if((fp5=fopen(dz,"r"))==NULL)
printf("文件打开失败");
while(fgets(content[m],50,fp5)!=NULL)
{
m++;
}
rewind(fp5);
i=0;
while(!feof(fp5))
{
fscanf(fp5,"%s %s %s\n",sp,mc,number);
if(strcmp(mc,n)==0)
{
sprintf(content[i],"%s %s %d\n","sp",n,s);
}
i++;
}
fclose(fp5);
if((fp5=fopen(dz,"w"))==NULL)
printf("文件无法打开");
for(i=0;i<m;i++)
{
fputs(content[i],fp5);
}
fclose(fp5);
return 0;
}
大家帮忙看看还有什么可以改进的。