我有一个任务,需要从包含坐标(x 和 y)的文本文件中读取信息,并且需要计算可以根据这些坐标创建多少条平行线。我不完全确定该怎么做。我尝试在 friend 的帮助下编写自己的代码。
程序打开文本文件,但卡在: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;
}
对于这个任务,我应该使用“结构、指针和动态内存”。
文本文件(Task5.txt)内容:
x: 10, y: 15
x: 8, y: -5
x: 85, y: 156
x: 46, y: 67
最佳答案
从文件中读取点的示例。
像这样
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x;
int y;
} Point;
Point *getPointsFromFile(FILE *fp, size_t *n){
int x, y;
Point *p = NULL;
size_t size = 0;
while(2 == fscanf(fp, " x : %d , y : %d", &x, &y)){
Point *temp = realloc(p, ++size * sizeof(*p));
if(!temp){
perror("realloc at getPointsFromFile");
free(p);
exit(EXIT_FAILURE);
}
p = temp;
p[size-1] = (Point){ .x = x, .y = y};
}
//rewind(fp);
*n = size;
return p;
}
int main(void){
FILE *fp = fopen("Task5.txt", "r");
size_t n = 0;
Point *points = getPointsFromFile(fp, &n);
fclose(fp);
for(size_t i = 0; i < n; ++i)
printf("(%d, %d)\n", points[i].x, points[i].y);
free(points);
return 0;
}
关于c - 编写代码来查找坐标之间的平行线(使用 C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41161855/