Skip to content

Commit fbdee2e

Browse files
committed
add read from file
1 parent e43667c commit fbdee2e

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2 4 3 5 1 7 6 9 8

longestAscendingSubsequence/longestAscendingSubsequence.h

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
#define MAXLEN 100
22

33
#include <stdio.h>
4+
#include <string.h>
5+
#include <stdlib.h>
46

57
typedef int SubsequencesMatrix[2][MAXLEN];
68
typedef int IntegerSequence[MAXLEN];
79

10+
typedef struct InputSequence
11+
{
12+
int sequence[MAXLEN];
13+
int length;
14+
};
15+
16+
InputSequence read_from_file()
17+
{
18+
FILE *file = fopen("inputSequence.txt", "r");
19+
20+
InputSequence data;
21+
22+
// if (file == NULL)
23+
// {
24+
// perror("Failed to open file");
25+
// return;
26+
// }
27+
28+
int index = -1;
29+
char line[256];
30+
const char delimiter[] = " ";
31+
32+
while (fgets(line, sizeof(line), file))
33+
{
34+
char *token = strtok(line, delimiter);
35+
36+
while (token != NULL) {
37+
data.sequence[++index] = atoi(token);
38+
token = strtok(NULL, delimiter); // Get the next token
39+
}
40+
}
41+
42+
data.length = index;
43+
44+
fclose(file);
45+
46+
return data;
47+
}
48+
849
void print_maximums_matrix(IntegerSequence sequence, SubsequencesMatrix matrix, int len)
950
{
1051

@@ -45,13 +86,15 @@ void determine_max_length_and_display_sequence(IntegerSequence input, Subsequenc
4586
printf("Result: \n");
4687
int cursor = maxIndex;
4788
int stackTop = -1;
48-
while (cursor >= 0) {
89+
while (cursor >= 0)
90+
{
4991
resultStack[++stackTop] = input[cursor];
5092
cursor = matrix[1][cursor];
5193
}
5294

53-
//read stack
54-
for (;stackTop > 0;) {
95+
// read stack
96+
for (; stackTop > 0;)
97+
{
5598
printf("%d, ", resultStack[stackTop--]);
5699
}
57100

longestAscendingSubsequence/main

4.55 KB
Binary file not shown.

longestAscendingSubsequence/main.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
#include "longestAscendingSubsequence.h"
2+
#include <stdio.h>
23

34
int main(int argc, char const *argv[])
45
{
5-
int sequence[MAXLEN] = {2, 4, 3, 5, 1, 7, 6, 9, 8};
6-
int length = 0;
6+
InputSequence sequence_from_file = read_from_file();
7+
8+
// if (*sequence_from_file) {
9+
// printf("File read error \n");
10+
// return -1;
11+
// }
12+
713
SubsequencesMatrix subsequences;
814

9-
for (int i = 0; sequence[i] != '\0'; i++)
10-
{
11-
length++;
12-
// printf("element %d = %d \n", i, sequence[i]);
1315

16+
for (int i = 0; sequence_from_file.sequence[i] != '\0'; i++)
17+
{
1418
if (i == 0)
1519
{
1620
subsequences[0][i] = 1; // subsequence max length
@@ -22,7 +26,7 @@ int main(int argc, char const *argv[])
2226

2327
for (int j = 0; j < i; j++)
2428
{
25-
if ((sequence[j] < sequence[i]) && (subsequences[0][j] >= maxLen))
29+
if ((sequence_from_file.sequence[j] < sequence_from_file.sequence[i]) && (subsequences[0][j] >= maxLen))
2630
{
2731
maxLen = subsequences[0][j] + 1;
2832
maxIndex = j;
@@ -34,8 +38,8 @@ int main(int argc, char const *argv[])
3438
}
3539
}
3640

37-
print_maximums_matrix(sequence, subsequences, length);
38-
determine_max_length_and_display_sequence(sequence, subsequences, length);
41+
print_maximums_matrix(sequence_from_file.sequence, subsequences, sequence_from_file.length);
42+
determine_max_length_and_display_sequence(sequence_from_file.sequence, subsequences, sequence_from_file.length);
3943

4044
return 0;
4145
}

0 commit comments

Comments
 (0)