求会C语言和JAVA语言的朋友,帮忙将这段JAVA程序改成C程序 30
importjava.io.*;publicclassPretty{publicstaticfinalintLINE_SIZE=5;publicstaticvoidmai...
import java.io.*;
public class Pretty
{
public static final int LINE_SIZE = 5;
public static void main(String[] parms)
{
BufferedReader fileIn;
PrintWriter fileOut;
String inputLine;
int position;
try
{
fileIn = new BufferedReader(new FileReader("in.txt"));
fileOut = new PrintWriter(new FileWriter("out.txt"));
inputLine = fileIn.readLine();
position = 1;
while (inputLine != null)
{
if (inputLine.equals(""))
{
if (position > 1)
{
fileOut.println();
}
fileOut.println();
position = 1;
}
else
{
if ((position+inputLine.length()-1) > LINE_SIZE)
{
fileOut.println();
position = 1;
}
fileOut.print(inputLine);
position += inputLine.length();
if (position <= LINE_SIZE)
{ // add a blank after the current word
fileOut.print(" ");
position++;
}
}
inputLine = fileIn.readLine();
}
fileIn.close();
fileOut.close();
}
catch (IOException ioe)
{
System.out.println(ioe.getMessage());
ioe.printStackTrace();
}
}
}
最好是按照JAVA写出来的方法来写,也就是说,这段java程序用的算法是什么,C也用什么样的算法,谢谢
c语言,不是c++,也不是c# 展开
public class Pretty
{
public static final int LINE_SIZE = 5;
public static void main(String[] parms)
{
BufferedReader fileIn;
PrintWriter fileOut;
String inputLine;
int position;
try
{
fileIn = new BufferedReader(new FileReader("in.txt"));
fileOut = new PrintWriter(new FileWriter("out.txt"));
inputLine = fileIn.readLine();
position = 1;
while (inputLine != null)
{
if (inputLine.equals(""))
{
if (position > 1)
{
fileOut.println();
}
fileOut.println();
position = 1;
}
else
{
if ((position+inputLine.length()-1) > LINE_SIZE)
{
fileOut.println();
position = 1;
}
fileOut.print(inputLine);
position += inputLine.length();
if (position <= LINE_SIZE)
{ // add a blank after the current word
fileOut.print(" ");
position++;
}
}
inputLine = fileIn.readLine();
}
fileIn.close();
fileOut.close();
}
catch (IOException ioe)
{
System.out.println(ioe.getMessage());
ioe.printStackTrace();
}
}
}
最好是按照JAVA写出来的方法来写,也就是说,这段java程序用的算法是什么,C也用什么样的算法,谢谢
c语言,不是c++,也不是c# 展开
3个回答
2012-10-08
展开全部
#include "stdio.h"
#include "string.h"
#define LINE_SIZE 5
#define MAX_LINE_SIZE 50 /* 可自己修改 */
int main(int args, char *argv[])
{
FILE *fileIn = NULL;
FILE *fileOut = NULL;
int position = 1;
char inputLine[MAX_LINE_SIZE + 1];
char outputLine[MAX_LINE_SIZE + 2]; /* 增加一个空格,后面增加的 */
/* 打开文件in.txt */
if ((fileIn = fopen("in.txt", "rb")) == NULL)
{
printf("Cannot open file in.txt.\n");
return -1;
}
/* 打开文件out.txt */
if ((fileOut = fopen("out.txt", "wb")) == NULL)
{
printf("Cannot open file in.txt.\n");
return -1;
}
/* 一行行读取并写入文件out.txt中 */
while ((fgets(inputLine, MAX_LINE_SIZE, fileIn)) != NULL)
{
memset(outputLine, 0, MAX_LINE_SIZE + 2);
strncpy(outputLine, inputLine, strlen(inputLine) - 2); /* 不保留换行符 */
if (strlen(outputLine) == 0) /* 空行 */
{
if (position > 1)
{
fputs("\r\n", fileOut);
}
fputs("\r\n", fileOut);
position = 1;
}
else
{
if ((position + strlen(outputLine) - 1) > LINE_SIZE)
{
fputs("\r\n", fileOut);
position = 1;
}
fputs(outputLine, fileOut);
printf("%s\n", outputLine);
position += strlen(outputLine);
if (position <= LINE_SIZE) {
// add a blank after the current word
fputs(" ", fileOut);
position++;
}
}
}
fclose(fileIn);
fileIn = NULL;
fclose(fileOut);
fileOut = NULL;
return 0;
}
#include "string.h"
#define LINE_SIZE 5
#define MAX_LINE_SIZE 50 /* 可自己修改 */
int main(int args, char *argv[])
{
FILE *fileIn = NULL;
FILE *fileOut = NULL;
int position = 1;
char inputLine[MAX_LINE_SIZE + 1];
char outputLine[MAX_LINE_SIZE + 2]; /* 增加一个空格,后面增加的 */
/* 打开文件in.txt */
if ((fileIn = fopen("in.txt", "rb")) == NULL)
{
printf("Cannot open file in.txt.\n");
return -1;
}
/* 打开文件out.txt */
if ((fileOut = fopen("out.txt", "wb")) == NULL)
{
printf("Cannot open file in.txt.\n");
return -1;
}
/* 一行行读取并写入文件out.txt中 */
while ((fgets(inputLine, MAX_LINE_SIZE, fileIn)) != NULL)
{
memset(outputLine, 0, MAX_LINE_SIZE + 2);
strncpy(outputLine, inputLine, strlen(inputLine) - 2); /* 不保留换行符 */
if (strlen(outputLine) == 0) /* 空行 */
{
if (position > 1)
{
fputs("\r\n", fileOut);
}
fputs("\r\n", fileOut);
position = 1;
}
else
{
if ((position + strlen(outputLine) - 1) > LINE_SIZE)
{
fputs("\r\n", fileOut);
position = 1;
}
fputs(outputLine, fileOut);
printf("%s\n", outputLine);
position += strlen(outputLine);
if (position <= LINE_SIZE) {
// add a blank after the current word
fputs(" ", fileOut);
position++;
}
}
}
fclose(fileIn);
fileIn = NULL;
fclose(fileOut);
fileOut = NULL;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询