#include <string.h>
int main()
{
char s[4]="abc",sc='d'//比较的字符串和字符
FILE *fp
char filename[100],c
int i=0,n1=0,n2=0
long fpos,len
printf("input filename:\n")
gets(filename)//输入文件名称
if((fp=fopen(filename,"r"))==NULL)//打开文件
{
printf("open %s error!\n",filename)
return 1
}
len=strlen(s)
c=fgetc(fp)
while(!feof(fp))
{
if(c==s[0])//如果第一个字符相等,比较剩下的字符串
{
fpos=ftell(fp)//记住当前文件指针位置
for(i=1i<leni++)
{
if(fgetc(fp)!=s[i])//如果不匹配,跳出循环
{
fseek(fp,fpos,0)//重新设置指针位置
break
}
}
if(i==len)//如果匹配成功,累加数目
n1++
}
if(c==sc)//与字符sc匹配,累加数目
n2++
c=fgetc(fp)
}
printf("\n与字符串%s匹配的有%d个\n",s,n1)//输出匹配个数
printf("与字符%c匹配的有%d个\n",sc,n2)
getchar()
return 0
}
前两天刚写完,还没优化,已运行通过了.晕,一维的好麻烦,这个也是碰巧前两天刚写好的,你看着自己修改下
#include <stdio.h>
typedef struct
{
int line
int row
int num
}Node
int main()
{
/*
int a[9][9]={
{4,0,3,6,0,0,0,0,0},
{0,0,0,0,0,1,0,2,4},
{0,1,0,0,4,0,5,0,0},
{0,0,0,9,0,4,0,6,0},
{3,0,2,0,0,0,4,0,9},
{0,7,4,1,0,3,0,0,0},
{0,0,1,0,9,0,0,4,0},
{2,4,0,3,0,0,0,0,0},
{0,0,0,4,0,8,2,0,7}}
*/
int a[9][9]={
{0,0,0,8,0,0,0,6,0},
{8,7,0,0,0,0,0,0,0},
{2,9,0,0,4,1,0,0,5},
{0,0,5,7,0,0,0,0,9},
{0,2,0,0,0,0,0,1,0},
{9,0,0,0,0,4,3,0,0},
{7,0,0,6,1,0,0,9,8},
{0,0,0,0,0,0,0,5,2},
{0,6,0,0,0,9,0,0,0}}
/*
int a[9][9]={
{0,2,0,0,6,0,0,0,0},
{0,9,0,4,0,5,1,3,0},
{0,0,8,7,0,0,0,0,5},
{6,0,0,3,0,0,4,0,0},
{0,0,0,9,0,6,0,0,0},
{0,0,7,0,0,1,0,0,3},
{4,0,0,0,0,7,3,0,0},
{0,8,5,2,0,4,0,7,0},
{0,0,0,0,9,0,0,1,0}}
*/
/*
int a[9][9]={
{0,0,3,0,2,0,0,0,6},
{0,0,2,0,9,0,0,0,4},
{7,0,0,8,0,0,2,0,3},
{0,8,0,0,7,0,5,0,0},
{0,7,0,1,0,6,0,3,0},
{0,0,0,2,0,0,0,9,0},
{4,0,6,0,0,8,0,0,5},
{6,0,0,0,4,0,3,0,0},
{9,0,0,0,1,0,7,0,0}}
*/
int i,j,n,en,flag,y,k=0,x,qu,p,q
Node b[70]
for(i=0i<9i++)
{
for(j=0j<9j++)
{
if(!a[i][j])
{
b[k].line=i
b[k].row=j
b[k].num=0
k+=1
}
}
}
en=k
/*从b[0]开始试,若b[k].num>9,则k-1,否则k+1*/
for(k=0k<en)
{
++b[k].num
i=b[k].line
j=b[k].row
a[i][j]=b[k].num
n=0
while(n<9&&b[k].num<=9)
{
if(n==i)
{
for(y=0y<9y++)
{
if(y==j)
continue
if(a[n][y]==a[i][j])
flag=1
}
}
else if(n==j)
{
for(y=0y<9y++)
{
if(y==i)
continue
if(a[y][n]==a[i][j])
flag=1
}
}
/*判断同一块中有没有相同值*/
qu=3*(i/3)+j/3
switch(qu)
{
case 0:x=0
y=0
break
case 1:x=0
y=3
break
case 2:x=0
y=6
break
case 3:x=3
y=0
break
case 4:x=3
y=3
break
case 5:x=3
y=6
break
case 6:x=6
y=0
break
case 7:x=6
y=3
break
default :x=6
y=6
break
}
p=x
q=y
for(x<p+3x++)
{
for(y<q+3y++)
{
if(x==i&&y==j)
continue
if(a[x][y]==a[i][j])
{
flag=1
break
}
}
if(flag==1)
break
}
if(flag==1)
{
a[i][j]=++b[k].num
flag=0
n=0
continue
}
n++
}
if(b[k].num>9)
{
a[i][j]=b[k].num=0
k--
if(k<0)
{
printf("error!\r\n")
return -1
}
}
else
k++
}
for(i=0i<9i++)
{
for(j=0j<9j++)
{
printf("%d",a[i][j])
}
printf("\r\n")
}
return 1
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)