QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1610|回复: 0

编程技巧汇萃(下)

[复制链接]
发表于 2003-5-17 00:52:26 | 显示全部楼层 |阅读模式
[code:1]/*显示警告窗口*/
void warn_message(char *s1,char *s2)
{
char buffer[2000];
gettext(30,8,76,16,buffer);
CLSXY(8,32,9,30,6);
CLSXY(4,30,8,30,6);
COLOR(15,4);
gotoxy(35,10);
DISPLAY(s1);
gotoxy(35,12);
DISPLAY(s2);
gotoxy(40,13);
DISPLAY("按任意键继续...");
getch();
puttext(30,8,76,16,buffer);
}

/*得到文件长度*/
long GetFileLength(char *file)
{
FILE *fp;
int i;
if ((fp=fopen(file,"rb"))==NULL)
{
clrscr();
printf("Can't open the file %s .",file);
return 0;
}
i=0;
while(!feof(fp))
{
i++;
fgetc(fp);
}
fclose(fp);
return (i-1);
}

/*窗口滚屏*/
void WINDOW_ROLL(int y1,int x1,int yl,int xl,int direct)
{
union REGS regs;
int x2,y2;
x1--;
y1--;
x2=x1+xl-1;
y2=y1+yl-1;
regs.h.ah=5+direct; /*ah=06h 向上滚动当前页*/
/*ah=07h 向下。。。 */
regs.h.al=1;
regs.h.bh=16*pmbj+pmqj;
regs.h.ch=y1; /*左上角行*/
regs.h.cl=x1; /*列*/
regs.h.dh=y2; /*右下角行*/
regs.h.dl=x2; /*列*/
int86(16,®s,®s);
}

/*扫描键盘*/
int scan_keyboard(int *m)
{
union inkey
{
unsigned char ch[2];
int ii;
}cc;
while(!bioskey(1))
{;}
cc.ii=bioskey(0);
*m=cc.ch[1];
return cc.ch[0];
}

/*插入字符串*/
void INSERT_STRING(char *ds,char *ss,int n)
{
char s[100];
MID(ds,s,n,-1);
ds[n-1]=0;
strcat(ds,ss);
strcat(ds,s);
}
void STR_ADD_CHAR(char *s,char ch)
{
char s1[2];
s1[0]=ch;
s1[1]=0;
strcat(s,s1);
}

/*检测磁盘是否准备就绪*/
int CheckDiskReading(int n)
{
int result;
char buffer[512];
result=biosdisk(4,n,0,0,0,1,buffer);
result=result&0x02;
if (result!=0x02) return 0;
return 1;
}

/*检测磁盘是否写保护*/
int CheckDiskWriteProf(void)
{
int result;
char buffer[512];
result=biosdisk(5,0,0,0,0,1,buffer);
if (result==0x03) return 0;
return 1;
}

/*修改文件的某一行*/
int ModifyFileLine(char *filename,char *s,int n)
{
char data[20][80];
int i;
FILE *fp;
if ((fp=fopen(filename,"r"))==NULL) return 0;
i=0;
while(READ_STR(data[i],fp))
i++;
fclose(fp);
fp=fopen(filename,"w");
strcpy(data[n-1],s);
n=i-1;
for(i=0;i<=n;i++)
WRITE_STR(data[i],fp);
fclose(fp);
return 1;
}

/*成批拷贝文件*/
int CopyFile(char *sfile,char *dfile,int f2d,int barlong,int height,int x,int y)
{
int Copyfile(char *sf,char *df);
int MakeNdir(char *Dir);
char filename[200][13],d[40],s[40],s1[40];
struct ffblk ffblk;
int done,i,j,l,len;
i=0;
done=findfirst(sfile,&ffblk,0);
if (!done) strcpy(filename[i],ffblk.ff_name);
while(!done)
{
done=findnext(&ffblk);
if (!done)
{
i++;
strcpy(filename[i],ffblk.ff_name);
}
}
if (f2d)
{
Copyfile(sfile,dfile);
return 1;
}
strcpy(s,sfile);
l=strlen(sfile);
for(j=l-1;j>=0;j--)
if (s[j]=='\\')
{
s[j+1]=0;
break;
}

/*拷贝一个文件*/
int Copyfile(char *sf,char *df)
{
FILE *in,*out;
char ch;
in=0;
out=0;
if ((in=fopen(sf,"rb"))==NULL)
exit(0);
if ((out=fopen(df,"wb"))==NULL)
exit(0);
while(!feof(in))
{
ch=fgetc(in);
if (ferror(in)) return 0;
fputc(ch,out);
if (ferror(out)) return 0;
}
fclose(in);
fclose(out);
return 1;
}

/*建立目录*/
int MakeNdir(char *Dir)
{
int i,l,j;
char s[10][40];
j=0;
l=strlen(Dir);
for(i=0;i
if (Dir[i]=='\\')
{
LEFT(s[j],Dir,i);
j++;
}
strcpy(s[j],Dir);
for(i=0;i<=j;i++)
if (access(s[i],0)) mkdir(s[i]);
return 1;
}

/*得到目录*/
int GetDir(char *dirF,char dataK[][14])
{
struct ffblk ffblk;
int done;
int i;
i=0;
done=findfirst(dirF,&ffblk,FA_DIREC);
while(!done)
{
if (ffblk.ff_attrib==16||ffblk.ff_attrib==17)
{
strcpy(dataK[i],ffblk.ff_name);
strcat(dataK[i],"\\");
i++;
}
done=findnext(&ffblk);
}
return i;
}

/*得到文件名*/
int GetFile(char *dirF,char dataK[][14])
{
struct ffblk ffblk;
int done;
int i;
i=0;
done=findfirst(dirF,&ffblk,0);
while(!done)
{
strcpy(dataK[i],ffblk.ff_name);
done=findnext(&ffblk);
i++;
}
return i;
}

/*任意两个正整数相加(<80位)*/
/*参数说明:numA,numB分别为加数和被加数,result存放相加后的结果*/
void add(char *numA,char *numB,char *result)
{
int i,j,a,b,c,jw,Alen,Blen; /*定义变量*/
char num[81];
char numC[81]; /*定义新的字串,长度与numA相同*/
char ch;
char s[1];
num[0]=0;
numC[0]=0;
if (strlen(numA)>=80) return;
if (strlen(numB)>=80) return;
for(i=0;i
if (!isdigit(numA[i])) return; /*如果非法返回结果为空*/
for(i=0;i
if (!isdigit(numB[i])) return;
if (strlen(numA)
{
strcpy(num,numA);
strcpy(numA,numB);
strcpy(numB,num);
num[0]=0;
}
Alen=strlen(numA);
Blen=strlen(numB);
strcpy(numC,numB);
for(i=0;i
numC[Alen-i-1]=numC[Blen-i-1];
for(i=0;i
numC[i]='0';
jw=0; /*进位*/
for(i=0;i
{
s[0]=numA[Alen-i-1];
s[1]=0;
a=atoi(s);
s[0]=numC[Alen-i-1];
s[1]=0;
b=atoi(s);
c=a+b+jw;
if (c>=10)
{
jw=1;
c=c-10;
}
else jw=0;
itoa(c,s,10);
num[i]=s[0];
}
if (jw==1) {num[i]='1';num[i+1]=0;}
else num[i]=0;
j=strlen(num);
for(i=0;i
{
ch=num[i];
num[i]=num[j-i-1]; /*将num反转*/
num[j-i-1]=ch;
}
strcpy(result,num);
return; /*返回相加结果*/
}

/*任意两个正整数相乘*/
/*参数说明:numA,numB 分别为乘数和被乘数,resultm存放积*/
void mult(char *numA,char *numB,char *resultm)
{
int i,j,k,l,m,a,b,c,jw,Alen,Blen,f; /*定义变量*/
char resulta[82];
char num[161];
char ch;
char s[1];
num[0]=0;
f=0;
if (strlen(numA)>=80) return;
if (strlen(numB)>=80) return;
for(i=0;i
if (!isdigit(numA[i])) return; /*如果非法返回结果为空*/
for(i=0;i
if (!isdigit(numB[i])) return;
if (strlen(numA)
{
strcpy(num,numA);
strcpy(numA,numB);
strcpy(numB,num);
num[0]=0;
}
Alen=strlen(numA);
Blen=strlen(numB);
strcpy(resultm,"0");
for(i=0;i
{
s[0]=numB[Blen-i-1];
s[1]=0;
c=atoi(s);
strcpy(resulta,numA);
if (c==0) strcpy(resulta,"0");
for(j=0;j
l=strlen(resulta);
for(k=0;k
{
resulta[l+k]='0';
resulta[l+k+1]=0;
}
add(resultm,resulta,resultm);
}
}

摘自 : 鹏程C语言教学网站

[/code:1]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-15 22:53 , Processed in 0.062230 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

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