怎么修改这个代码?

lijianbin9 2010-03-27 06:09:27
input.txt文件是这样的 abcdef //读取这段字母 进行全排列并且 生成到 c:\a.txt

8 //这是第二行,读取 这个数字,进行斐波那契相加,结果生成到c:\b.txt

这是 两个代码,共用一个 input.txt文件。。

要求 从指c:\input.txt文件中读取第一行的字母,进行 全排列,结果生成到c:\a.txt
读取数字8 进行斐波那契数列相加(1-8);结果生成到c:\b.txt


全排列代码如下
#include <stdio.h>
#include <string.h>
void swap(char *str1,char *str2)
{
char temp;
temp=*str1;
*str1=*str2;
*str2=temp;
}

void permStr(char *str,int i)
{
if(i==strlen(str)-1)
printf("%s\n",str);
else
{
for(int j=i;j<strlen(str);j++)
{
swap(&str[i],&str[j]);

permStr(str,i+1);
swap(&str[i],&str[j]);
}
}
}

void main()
{
char str[]={"abh"};//这里应该怎么修改
permStr(str,0);


}



斐波那契数列相加代码
#include <stdio.h>
int f(int n)
{
int fib=0;
if(n==0 || n==1)
fib = 1;
else
fib = f(n-1)+f(n-2);
return fib;
}
int main()
{
int n, i, fib=0;
scanf("%d", &n);
for(i=0; i<n; i++)
{
fib = f(i);
printf("Fibonacci(%d) is %d\n", i, fib);
}
return 0;
}





题意比较复杂,,要求比较多,,,
...全文
327 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
lijianbin9 2010-03-29
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 huanmie_09 的回复:]
楼主用我的程序还有问题?
按我15楼的把文件名改下,将input.txt放在c盘根目录,ok。
[/Quote]
要是 我的input.txt 中 第二行的 数字用在别的 程序中怎么 修改呢?
lijianbin9 2010-03-29
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 huanmie_09 的回复:]
楼主用我的程序还有问题?
按我15楼的把文件名改下,将input.txt放在c盘根目录,ok。
[/Quote]谢谢哦,,刚弄好,,
原来是 input文件的命名问题,,,建立个 inout文本文档 就默认为 input.txt了,,

幼稚的错误啊,,,
huanmie_09 2010-03-29
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 lijianbin9 的回复:]
引用 19 楼 huanmie_09 的回复:
楼主用我的程序还有问题?
按我15楼的把文件名改下,将input.txt放在c盘根目录,ok。

要是 我的input.txt 中 第二行的 数字用在别的 程序中怎么 修改呢?
[/Quote]
看不到你的图片。
用在别的程序里你把readFile函数和trim函数拷过去,然后调用就行了.
flag = readFile(fileName, 2, sBuffer); /*读第二格非空行*/
if(!flag) { /*flag=0表示读取成功*/
n = atoi(sBuffer); /*将读取的内容转换成整数*/
}
guofzhao 2010-03-29
  • 打赏
  • 举报
回复
-----------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void swap(char *str1,char *str2)
{
char temp;
temp=*str1;
*str1=*str2;
*str2=temp;
}

void permStr(char *str,int i, FILE *fout)
{
int j = 0;

if(i == (int)strlen(str) - 1)
fprintf(fout, "%s\n", str);
else
{
for(j=i; j<(int)strlen(str); j++)
{
swap(&str[i],&str[j]);

permStr(str,i+1, fout);
swap(&str[i],&str[j]);
}
}
}

int main()
{
char str[10];
FILE *fin, *fout;
char ch;
int i = 0;

if ((fin = fopen("input.txt", "r")) < 0) {
printf("open error");
exit(1);
}

if ((fout = fopen("a.txt", "w")) < 0) {
printf("open error");
exit(1);
}

while ( (ch = fgetc(fin)) != '\n' )
str[i++] = ch;
str[i] = '\0';

permStr(str,0, fout);

fclose(fin);
fclose(fout);
return 0;
}
--------------------------------------------------
lijianbin9 2010-03-29
  • 打赏
  • 举报
回复
huanmie_09 2010-03-28
  • 打赏
  • 举报
回复
楼主用我的程序还有问题?
按我15楼的把文件名改下,将input.txt放在c盘根目录,ok。
cattycat 2010-03-28
  • 打赏
  • 举报
回复
我给你改的那个程序要求input.txt在你的程序文件夹下,或者你改一下路径。
if((fpin=fopen("input.txt","r"))==NULL) //改成"c:\\input.txt"
{
printf("error open file\n");
return;
}
fpout=fopen("a.txt","w"); //"c:\\a.txt"

把input.txt放到c盘根目录下。
另外,看了下12楼的写法,writefile函数频繁调用的时候频繁打开文件非常不好,性能比较差。
lijianbin9 2010-03-28
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 cattycat 的回复:]
我给你改的那个程序要求input.txt在你的程序文件夹下,或者你改一下路径。

C/C++ code
if((fpin=fopen("input.txt","r"))==NULL) //改成"c:\\input.txt"
{
printf("error open file\n");
return;
}
fpout=fopen……
[/Quote] 为什么还是不行啊,,我已经把input。txt 放到 c根目录下了,,,名字 也没有错,,
东大坡居士 2010-03-28
  • 打赏
  • 举报
回复
斐波那切数列,用递归求值~~~~~~~~~~~~~~~~~
huanmie_09 2010-03-27
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 lijianbin9 的回复:]
引用 5 楼 cattycat 的回复:
楼上的,区别不大。

char str[]={"abh"};//这里应该怎么修改

char str[]="abh";

这两个是一样的。下面是根据你的把全排列的改到输出到文件中。至于斐波那契数列的你自己可以参考写。


C/C++ code
#include <stdio.h>
#include <string.h>
void ……
[/Quote]
把文件名改一下啊。
我是从c:\a.txt下读的文件。
在main函数中,把这里改下:
char fileName[40] = "c:\\a.txt";
char fileName2[40] = "c:\\b.txt";
char fileName3[40] = "c:\\c.txt";
改成:
char fileName[40] = "c:\\input.txt";
char fileName2[40] = "c:\\a.txt";
char fileName3[40] = "c:\\b.txt";

lijianbin9 2010-03-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 cattycat 的回复:]
楼上的,区别不大。

char str[]={"abh"};//这里应该怎么修改

char str[]="abh";

这两个是一样的。下面是根据你的把全排列的改到输出到文件中。至于斐波那契数列的你自己可以参考写。


C/C++ code
#include <stdio.h>
#include <string.h>
void swap(char *str1,char *……
[/Quote]

怎么 显示error open file 啊,。。。我 已经见了 input.txt文件 了啊
某某9 2010-03-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhoutanliang 的回复:]

C/C++ code

string str("abh");
[/Quote]这是c++ 标准库的,楼主写的是c代码!
huanmie_09 2010-03-27
  • 打赏
  • 举报
回复
楼主参考一下,用追加模式写的文件。

#include<stdio.h>
#include<string.h>
#define LINEMAXCHARS 256

void swap(char *str1,char *str2);
void permStr(char *str,int i, char *fileName);
int readFile(char *fileName, int line, char *sBuffer); /*从文件中读取第line个非空行的数据*/
char * trim(char *sp); /*去掉字符串sp首尾的空格字符*/
int writeFile(char *fileName, char *sBuffer); /*向文件中追加写入sBuffer*/
int fab(int n);

int main()
{
char sBuffer[LINEMAXCHARS];
char s[LINEMAXCHARS];

char fileName[40] = "c:\\a.txt";
char fileName2[40] = "c:\\b.txt";
char fileName3[40] = "c:\\c.txt";

int flag = 1;
int i, fib, n;

flag = readFile(fileName, 1, sBuffer); /*读第一个非空行*/

if(!flag) {
sBuffer[strlen(sBuffer)-1] = '\0';
permStr(sBuffer, 0, fileName2);
printf("success!\n");
}
else {
printf("failed!\n");
}

flag = readFile(fileName, 2, sBuffer); /*读第二格非空行*/
if(!flag) {
n = atoi(sBuffer);
for(i = 0; i < n; i++) {
fib = fab(i);
printf("Fibonacci(%d) is %d\n", i, fib);
sprintf(s, "Fibonacci(%d) is %d\n", i, fib);
writeFile(fileName3, s);
}
printf("success!\n");
}
else {
printf("failed!\n");
}
return 0;
}

int fab(int n)
{
int fib=0;
if(n==0 || n==1)
fib = 1;
else
fib = fab(n-1)+fab(n-2);
return fib;
}


void swap(char *str1,char *str2)
{
char temp;
temp = *str1;
*str1 = *str2;
*str2 = temp;
}


void permStr(char *str, int i, char *fileName)
{
int j;
if(i == strlen(str)-1) {
printf("%s\n",str);
writeFile(fileName, str);
writeFile(fileName, "\n");
}
else {
for(j=i;j<strlen(str);j++) {
swap(&str[i],&str[j]);
permStr(str,i+1, fileName);
swap(&str[i],&str[j]);
}
}
}

/*去掉字符串前后的空格*/
char * trim(char *sp)
{
char sDest[LINEMAXCHARS];
char *pStr;
int i = 0;

if (sp == NULL) {
return NULL;
}

pStr = sp;
while (*pStr == ' ' || *pStr == '\t') {
pStr++;
}
strcpy(sDest, pStr);

i = strlen(sDest) - 1;
while((i>=0)&&(sDest[i] == ' ' || sDest[i] == '\t' || sDest[i] == '\r' ) ){
sDest[i] = '\0';
i-- ;
}

strcpy(sp, sDest);

return sp;
}

int readFile(char *fileName, int line, char *sBuffer)
{
FILE *fd, *fd1;
char s[LINEMAXCHARS];
char copy_file[50];
int n = 1;

if((fd = fopen(fileName,"r")) == NULL) {
printf("Can't open file %s\n",fileName);
return 1;
}


while(fgets(sBuffer, LINEMAXCHARS, fd)) {
strcpy(s, sBuffer);
trim(s);
/* 如果为空行, 跳过 */
if (s[0] == '\n' ) {
continue;
}
if(n == line) {
break;
}
n++;
}
fclose(fd);

return 0;
}

int writeFile(char *fileName, char *sBuffer)
{
FILE *fd;

if((fd = fopen(fileName,"a")) == NULL) {
printf("Can't open file %s\n",fileName);
return 1;
}

fputs(sBuffer, fd);

fclose(fd);
return 0;
}
se7venhigh 2010-03-27
  • 打赏
  • 举报
回复
斐波那契的程序可以借鉴5L的 只不过是中间涉及到一个字符串跟整型的转换~
用到stdlib.h中的两个函数atoi和itoa,LZ可以google下~
flyyyri 2010-03-27
  • 打赏
  • 举报
回复
顶一个先
lijianbin9 2010-03-27
  • 打赏
  • 举报
回复
大家没有看 题目要求啊,,
添加注释的那个地方 怎么按照题目要求修改啊 ?,
lijianbin9 2010-03-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhoutanliang 的回复:]
C/C++ code

char str[]={"abh"};//这里应该怎么修改
//这样也可以的
char str[]={‘a’,'b','h'};//这里应该怎么修改
[/Quote]我的 意思是 这里怎么 按照题目要求修改,,,怎么读取 input的 字母 怎么输出 到a.txt中,,
AlanBruce 2010-03-27
  • 打赏
  • 举报
回复
标准库的
AlanBruce 2010-03-27
  • 打赏
  • 举报
回复

string str("abh");
cattycat 2010-03-27
  • 打赏
  • 举报
回复
楼上的,区别不大。

char str[]={"abh"};//这里应该怎么修改

char str[]="abh";

这两个是一样的。下面是根据你的把全排列的改到输出到文件中。至于斐波那契数列的你自己可以参考写。

#include <stdio.h>
#include <string.h>
void swap(char *str1,char *str2)
{
char temp;
temp=*str1;
*str1=*str2;
*str2=temp;
}

void permStr(FILE*fp,char *str,int i)
{
if(i==strlen(str)-1)
fprintf(fp,"%s\n",str);
else
{
for(int j=i;j<strlen(str);j++)
{
swap(&str[i],&str[j]);

permStr(fp,str,i+1);
swap(&str[i],&str[j]);
}
}
}

void main()
{
char str[10];
FILE* fpin,*fpout;
if((fpin=fopen("input.txt","r"))==NULL)
{
printf("error open file\n");
return;
}
fpout=fopen("a.txt","w");
fgets(str,10,fpin); //这里input.txt只有一行abcdef
permStr(fpout,str,0);

fclose(fpin);
fclose(fpout);

}
加载更多回复(4)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧