请帮忙用C语言写一个程序!急!!
发布网友
发布时间:2022-05-27 10:57
我来回答
共5个回答
热心网友
时间:2023-10-17 22:23
晚上回去看看吧
=========================
给,已经在VC6.0上编译运行确认:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 100 //暂定文件最大长度为100个char型
char * replace(char *str, char *str1, char *str2, char *strretu)
{
char *str_temp = str;
char *str1_temp = str1;
char *str2_temp = str2;
char *strretu_temp = NULL;
unsigned long char_size = (unsigned long)sizeof(char);
/*计算字符长度*/
unsigned long i,rep_acc = 0;
unsigned long str_len = strlen(str);
unsigned long str1_len = strlen(str1);
unsigned long str2_len = strlen(str2);
/*首先找出Str里有几个地方要被替换的*/
for(; str_temp - str < str_len; )
{
for(i = 0; i < str1_len; i++)
{
if(*str_temp++ != *str1_temp++)
{
str1_temp = str1;
break;
}
else
{
if(i == str1_len - 1)
{
str1_temp = str1;
rep_acc++;
}
}
}
}
str1_temp = str1;
str_temp = str;
/*分配所需内存*/
strretu = strretu_temp = (char *)malloc(((str_len - str1_len * rep_acc) + str2_len * rep_acc + 1) * char_size);
/*替换*/
for(; str_temp - str < str_len; )
{
for(i = 0; i < str1_len; i++)
{
*strretu_temp++ = *str_temp;
if(*str_temp++ != *str1_temp++)
{
str1_temp = str1;
break;
}
else
{
if(i == str1_len - 1)
{
strretu_temp = strretu_temp - str1_len;
for(i = 0; i < str2_len; i++)
{
*strretu_temp++ = *str2_temp++;
}
str2_temp = str2;
str1_temp = str1;
}
}
}
}
*strretu_temp = '\0';
str1_temp = str1;
str_temp = str;
str2_temp = str2;
return(strretu);
}
int main(void)
{
char str[N] = {NULL};
char str1[N] = {NULL};
char str2[N] = {NULL};
char *strretu = NULL;
char ch;
int i=0;
char fliename[20]={NULL};
FILE *p;
printf("\n请输入文件名: \n");
gets(fliename);
if((p=fopen(fliename,"r"))==NULL)
{
printf("\n打开文件失败\n");
getch();
return 0;
}
while((ch=fgetc(p))!=EOF)
{
str[i++]=ch;
}
fclose(p);
printf("\n请输入待查找字符串: \n");
gets(str1);
printf("\n请输入替换字符串: \n");
gets(str2);
strretu = replace(str, str1, str2, strretu);
p=fopen(fliename,"w+");
fputs(strretu, p);
fclose(p);
free(strretu);
printf("替换完成!\n任意键关闭!\n");
getch();
return 0;
}
运行结果:
请输入文件名:
1234.txt
请输入待查找字符串:
1*1
请输入替换字符串:
45 45
替换完成!
任意键关闭!
注意:输入的文件名要带后缀
热心网友
时间:2023-10-17 22:24
去图书馆借本书,这类似的程序多了去了,小改就OK了。重新写一个太麻烦了。。
热心网友
时间:2023-10-17 22:25
main()
{
print("");
………………
;
}
^_^
热心网友
时间:2023-10-17 22:26
好像要用一个QB的系统吧 我们正在学
热心网友
时间:2023-10-17 22:26
先留个名,我试试看
差不多可以了,要写回原文件还真不厚道- -,否则可以简单很多很多很多的- -~
已知BUG:每次替换后文件末尾会增加一个回车,懒得改了- -
强烈要求加到最高250分- -bb...写那么多真累- -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 80
struct buf
{
char c;
struct buf *next;
};
void main()
{
FILE *fp;
char t,file_name[MAXLEN],find_s[MAXLEN],replace_s[MAXLEN];
int flag=0,i,count;
struct buf *head,*node;
while(flag==0)
{
printf("请输入源文件名:\n");
i=0;
while((t=getchar())!='\n')
{
file_name[i]=t;
i++;
}
file_name[i]='\0';
if((fp=fopen(file_name,"r"))==NULL)
{
printf("打开文件失败\n");
continue;
}
flag=1;
}
flag=0;
while(flag==0)
{
printf("\n请输入待查找字符串\n");
i=0;
while((t=getchar())!='\n')
{
find_s[i]=t;
i++;
}
find_s[i]='\0';
if(i==0)
{
printf("未输入任何字符");
continue;
}
flag=1;
}
flag=0;
while(flag==0)
{
printf("\n请输入待替换字符串\n");
i=0;
while((t=getchar())!='\n')
{
replace_s[i]=t;
i++;
}
replace_s[i]='\0';
if(i==0)
{
printf("未输入任何字符");
continue;
}
flag=1;
}
head=node=(struct buf *)malloc(sizeof(struct buf));
flag=1;
while(feof(fp)==0)
{
count=0;
fread(&t,1,1,fp);
while(t==find_s[count])
{ //开始匹配
count++;
if(feof(fp)==1)
{
if(count==(int)strlen(find_s))
{ //到文件末尾且恰好匹配时
count=strlen(replace_s);
i=0;
while(i<count)
{
node->c=replace_s[i];
node->next=(struct buf *)malloc(sizeof(struct buf));
node=node->next;
i++;
}
}
else
{ //到文件未但不匹配时
i=0;
while(i<count)
{
node->c=find_s[i];
node->next=(struct buf *)malloc(sizeof(struct buf));
node=node->next;
i++;
}
flag=0;
}
break;
}
fread(&t,1,1,fp);
}
if(flag==0)
break;
if(count==(int)strlen(find_s))
{ //匹配成功
count=strlen(replace_s);
i=0;
while(i<count)
{
node->c=replace_s[i];
node->next=(struct buf *)malloc(sizeof(struct buf));
node=node->next;
i++;
}
fseek(fp,-1,SEEK_CUR);
}
else
{ //匹配失败
if(count==0)
{ //即本次while循环一个字符也不匹配
node->c=t;
node->next=(struct buf *)malloc(sizeof(struct buf));
node=node->next;
}
else
{ //即本次while循环匹配了部分字符
fseek(fp,0-count,SEEK_CUR);
node->c=find_s[0];
node->next=(struct buf *)malloc(sizeof(struct buf));
node=node->next;
}
}
}
node->next=NULL;
node->c='\0'; //注意这里最后一个节点的 node->c 是无效的
//重新写入文件
fclose(fp);
if((fp=fopen(file_name,"w"))==NULL)
{
printf("写入文件失败\n");
exit(1);
}
node=head;
while(node->next!=NULL)
{
fwrite(&node->c,1,1,fp);
node=node->next;
}
fclose(fp);
//释放空间
node=head;
while(head!=NULL)
{
head=head->next;
free(node);
node=head;
}
printf("\n操作完成\n");
}