69,371
社区成员
发帖
与我相关
我的任务
分享
#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;
}
if((fpin=fopen("input.txt","r"))==NULL) //改成"c:\\input.txt"
{
printf("error open file\n");
return;
}
fpout=fopen("a.txt","w"); //"c:\\a.txt"
#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;
}
string 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);
}