generated from justuser-31/code_projects_template
up
This commit is contained in:
parent
ceed37d939
commit
99861f5cb0
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -4,3 +4,6 @@
|
|||||||
[submodule "STEP1_Basics/5_Recursion/c_cross_pack"]
|
[submodule "STEP1_Basics/5_Recursion/c_cross_pack"]
|
||||||
path = STEP1_Basics/5_Recursion/c_cross_pack
|
path = STEP1_Basics/5_Recursion/c_cross_pack
|
||||||
url = ssh://git@git.del.pw:2222/justuser-31/c_cross_pack.git
|
url = ssh://git@git.del.pw:2222/justuser-31/c_cross_pack.git
|
||||||
|
[submodule "STEP1_Basics/6_Files/c_cross_pack"]
|
||||||
|
path = STEP1_Basics/6_Files/c_cross_pack
|
||||||
|
url = https://gitea.del.pw/justuser-31/c_cross_pack.git
|
||||||
|
1
STEP1_Basics/6_Files/.gitignore
vendored
Normal file
1
STEP1_Basics/6_Files/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
file
|
1
STEP1_Basics/6_Files/c_cross_pack
Submodule
1
STEP1_Basics/6_Files/c_cross_pack
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 2f6bba5ce7e8069438a39b4e1a73df1cea899378
|
91
STEP1_Basics/6_Files/main.c
Normal file
91
STEP1_Basics/6_Files/main.c
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include "c_cross_pack/libs/string.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int matrix[2][2];
|
||||||
|
int a, b;
|
||||||
|
|
||||||
|
// Scan
|
||||||
|
printf("Enter a: "); scanf("%d", &a);
|
||||||
|
printf("Enter b: "); scanf("%d", &b);
|
||||||
|
|
||||||
|
matrix[0][0] = a; matrix[0][1] = a + 1;
|
||||||
|
matrix[1][0] = b; matrix[1][1] = b + 1;
|
||||||
|
|
||||||
|
// Format strings
|
||||||
|
string first = strInit(int2Str(&matrix[0][0]));
|
||||||
|
strAdd(&first, " ");
|
||||||
|
strAdd(&first, int2Str(&matrix[0][1]));
|
||||||
|
string second = strInit(int2Str(&matrix[1][0]));
|
||||||
|
strAdd(&second, " ");
|
||||||
|
strAdd(&second, int2Str(&matrix[1][1]));
|
||||||
|
|
||||||
|
// Write data
|
||||||
|
FILE* fptr = fopen("file", "w");
|
||||||
|
fprintf(fptr, first);
|
||||||
|
fprintf(fptr, "\n");
|
||||||
|
fprintf(fptr, second);
|
||||||
|
fprintf(fptr, "\n");
|
||||||
|
fclose(fptr);
|
||||||
|
|
||||||
|
string rawFirst = strInit("");
|
||||||
|
string rawSecond = strInit("");
|
||||||
|
int newMatrix[2][2];
|
||||||
|
|
||||||
|
// Read new values
|
||||||
|
fptr = fopen("file", "r");
|
||||||
|
string buffer = strInit("");
|
||||||
|
strResize(&buffer, 128);
|
||||||
|
bool isFirst = true;
|
||||||
|
while (fgets(buffer, 128, fptr)) {
|
||||||
|
//printf("%s", buffer);
|
||||||
|
if (isFirst) {
|
||||||
|
strAdd(&rawFirst, buffer);
|
||||||
|
isFirst = false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
strAdd(&rawSecond, buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(fptr);
|
||||||
|
|
||||||
|
// Parse first values pairs
|
||||||
|
isFirst = true;
|
||||||
|
char* token = strtok(rawFirst, " ");
|
||||||
|
while (token)
|
||||||
|
{
|
||||||
|
if (isFirst) {
|
||||||
|
newMatrix[0][0] = atoi(token);
|
||||||
|
isFirst = false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
newMatrix[0][1] = atoi(token);
|
||||||
|
}
|
||||||
|
token = strtok(NULL, " ");
|
||||||
|
}
|
||||||
|
// Parse first values pairs
|
||||||
|
isFirst = true;
|
||||||
|
token = strtok(rawSecond, " ");
|
||||||
|
while (token)
|
||||||
|
{
|
||||||
|
if (isFirst) {
|
||||||
|
newMatrix[1][0] = atoi(token);
|
||||||
|
isFirst = false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
newMatrix[1][1] = atoi(token);
|
||||||
|
}
|
||||||
|
token = strtok(NULL, " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nOriginal matrix:\n"
|
||||||
|
"[[%d, %d], [%d, %d]]\n"
|
||||||
|
"Readed matrix:\n"
|
||||||
|
"[[%d, %d], [%d, %d]]\n",
|
||||||
|
matrix[0][0], matrix[0][1], matrix[1][0], matrix[1][1],
|
||||||
|
newMatrix[0][0], newMatrix[0][1], newMatrix[1][0], newMatrix[1][1]);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user