麻烦帮忙看看这段程序,改成C语言~谢谢
按照这段程序的方法,不要改算法什么的,然后改成C~谢谢
- Java code
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(); } }}[解决办法]
- C/C++ code
#include <string.h>#include <stdio.h>#define LINE_CHARS 80#define LINE_SIZE 5int main(int argc, char* argv[]){ FILE* fileIn; FILE* fileOut; char inputLine[LINE_CHARS]; int position; fileIn = fopen("in.txt", "rt"); fileOut = fopen("out.txt", "wt"); if(fileIn == NULL || fileOut == NULL) return -1; fgets(inputLine, LINE_CHARS, fileIn); position = 1; while(!feof(fileIn)) { if(strlen(inputLine) > 0) { if(position > 1) fprintf(fileOut, "\n"); fprintf(fileOut, "\n"); position = 1; } else { if((position + strlen(inputLine) - 1) > LINE_SIZE) { fprintf(fileOut, "\n"); position = 1; } fprintf(fileOut, "%s\n", inputLine); position += strlen(inputLine); if(position <= LINE_SIZE) { fprintf(fileOut, " "); position++; } } fgets(inputLine, LINE_CHARS, fileIn); } fclose(fileIn); fclose(fileOut); return 0; }
[解决办法]
这里if(strlen(inputLine) > 0)看错了,不好意思,把这句改为if(strlen(inputLine) == 0)就行了。