私は座標(xとy)があるテキストファイルから情報を読み取る必要があり、平行線の数を数える必要があるこれらの座標から作成することができます。私はそれをどうやって行うのか完全にはわからない。私は友人の助けを借りて自分のコードを書こうとしました。座標間の平行線を見つけるためのコードを書く(Cを使う)
プログラムはテキストファイルを開きますが、f = getLines(file);
の部分にはまってしまいます。そのため、完全なコードを提供しています。 。 。この作業のために
(code)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define FILE_PATH "D:\\Task5\\Task5.txt"
//============================== STRUCTURES
typedef struct {
char* lines;
int size;
} FileLines;
typedef struct {
int x;
int y;
} Point;
typedef struct {
Point p1;
Point p2;
int slope;
} Line;
//============================== PROTOTYPES
FILE* getFile();
FileLines getLines(FILE* file);
Point *getPointsFromFileLines(FileLines file);
Point getPointFromString(char* string);
Line *getAllLines(Point* coords,int size);
int countParralelLines(Line* lines, int size);
//============================== FUNCTIONS
FILE* getFile() { //
FILE* file;
file = fopen(FILE_PATH,"r");
if (file == NULL)
{
printf("Failed to open file \n");
exit(EXIT_FAILURE);
}
else
printf("File opened for reading \n");
return file;
}
FileLines getLines(FILE *file) {
char c;
FileLines f = {0,0};
int fileSize = sizeof(f.lines)/sizeof(f.lines[0]);
char *line = 0;
int size = sizeof(line)/sizeof(line[0]);
while(!feof(file)) {
while(c != '\n'){
c = getc(file);
line = realloc(line, ++size);
line[size-1] = c;
}
f.lines = realloc(f.lines, ++fileSize);
f.lines[fileSize-1] = *line;
}
return f;
}
Point *getPointsFromFileLines(FileLines file) {
Point* p = 0;
int i;
int size = (sizeof(p)/sizeof(p[0]));
for(i = 0; i < file.size; i++) {
p = realloc(p, (size*(i+1)));
getPointFromString((char *)file.lines[i]);
}
return p;
}
Point getPointFromString(char* string) {
Point p = {0,0};
while(*string != '\n') { //
if(*string == 'x') {
do {
if(isdigit(*string))
break;
string++;
} while(*string); //
p.x = (int) strtol(string, &string, 10);
}
if(*string == 'y') { //
do {
if(isdigit(*string))
break;
string++;
} while(*string); //
p.y = (int) strtol(string, &string, 10);
}
}
return p;
}
Line *getAllLines(Point* coords,int size) {
int number_of_lines = factorial(size);
Line l[number_of_lines];
int i, j;
for(i = 0; i < number_of_lines; i++) {
for(j = i; j < number_of_lines; j++) {
l[j].p1 = coords[i];
l[j].p2 = coords[j+1];
}
}
return l;
}
int factorial(unsigned int i) {
if(i <= 1)
return 1;
return (i + factorial(i-1));
}
int countParralelLines(Line* lines,int size) {
int count;
int i, j;
for(i = 0; i < size; i++) {
for(j = 0; j < size; i++) {
if(i==j) continue;
if(lines[i].slope == lines[j].slope)
count++;
}
}
return count;
}
//============================== MAIN PART
int main()
{
FileLines f = {0,0};
FILE *file;
file = getFile();
printf("1 \n");
f = getLines(file);
printf("2 \n");
Point *coords = 0;
coords = getPointsFromFileLines(f);
printf("3 \n");
int size = sizeof(coords)/sizeof(coords[0]);
int possible_lines = factorial(size);
Line lines[possible_lines];
memcpy(lines, getAllLines(coords, size), (sizeof(lines)*sizeof(lines[0])));
printf("4 \n");
int count = countParralelLines(lines, possible_lines);
printf("count: %i", count);
return 0;
}
Iは、 "構造体、ポインタと動的メモリ" を使用することになっています。
テキストファイル(Task5.txt)内容:ファイルからの読み取りポイントの
x: 10, y: 15
x: 8, y: -5
x: 85, y: 156
x: 46, y: 67
よう
はStackOverflowのへようこそ:これをお読みください:[どのように依頼する](http://stackoverflow.com/help/how-to-ask)。 –
* "うまくいかないようです" *申し訳ありませんが、それは十分な問題記述ではありません。単にコードをリンクするだけではなく、[mcve]の提供方法をお読みください。 – user694733
1) 'sizeof(f.lines)/ sizeof(f.lines [0])'と 'sizeof(line)/ sizeof(line [0])'などは間違っています。 – BLUEPIXY