initial commit

parents
#include <stdio.h>
int
main(){
int a, b, c;
printf("Enter 2 nubers one by one:\n");
scanf("%d", &a);
scanf("%d", &b);
c = a+b;
printf("%d\n", c);
return 0;}
#include <stdio.h>
#include <math.h>
int
main(){
double x,y;
scanf("%lf %lf", &x, &y);
double rez = ( 1+sin(x+y)*sin(x+y) )/
( 2 + fabs(x - (2*x*x)/(1+fabs(sin(x+y)))));
printf("%lf\n", rez);
return 0;}
#include <stdio.h>
#include <math.h>
int
main(){
double a, b, c, x, rez;
printf("Enter a b c x\n");
scanf("%lf %lf %lf %lf", &a, &b, &c, &x);
rez = -1*(x-a)/cbrtf(x*x + a*a)
-1*(4*pow((x*x + b*b), 3/4))/
(2+a+b+pow((x-c), 2/3));
printf("%lf\n", rez);
return 0;}
#include <stdio.h>
#include <math.h>
#define PI 3.14159
int
main(){
double x, y;
double mR = 240310000, eR = 149900000,
mT = 687, eT = 365.24;
double mW = 2*PI/mT, eW = 2*PI/eT;
for(unsigned long t= 1; t <= pow(10, 4); t+= 1000 ){
x = mR * cos(mW*t) - eR * cos(eW*t);
y = mR * sin(mW*t) - eR * sin(eW*t);
printf("x:%lf\ty:%lf\n", x, y);
}
return 0;}
#include <stdio.h>
#include <math.h>
int
main(){
double a, b, c, s = 0, h;
printf("Enter from to and amount of steps:\n");
scanf("%lf %lf %lf", &a, &b, &c);
h = (b-a)/c;
for(double x = a+h; x <= b-h; x += h){
s += exp(x+2);
}
s = h * ( (exp(a+2) + exp(b+2))/2 + s);
printf("result:%lf\n", s);
return 0;}
#include <stdio.h>
int
main(){
int m;
scanf("%d", &m);
unsigned int p[128] = {1,1,1};
for(int n = 3; n <= m; n++){
p[n] = p[n-2] + p[n-3];
}
for(int n = 0; n <= m; n++){
printf("%d, ", p[n]);
}
putchar('\n');
return 0;}
#include <stdio.h>
int
main(){
int n, a, b, c;
while (1){
scanf("%d", &n);
c = n%10;
b = (n%100 - c)/10;
a = (n - b - c)/100;
if (a+b+c<=10)
break;
}
return 0;}
#include <stdio.h>
int
main() {
int n, X[128], Y[128];
printf("Enter the size of the vector: \n");
scanf("%d", &n);
printf("Enter the elements of the vector X:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &X[i]);
}
for (int i = 0; i < n; i++) {
Y[i] = X[i] * X[i];
}
for (int i = 0; i < n; i++) {
printf("%d ", Y[i]);
}
putchar('\n');
return 0;}
#include <stdio.h>
int
main(){
int len, temp;
scanf("%d", &len);
len = len - 1;
int arr[128] = {0};
for(int i = 0; i <= len ; i++)
scanf("%d", &arr[i]);
for(int i = 0; i <= len/2; i++){
temp = arr[i];
arr[i] = arr[len-i];
arr[len-i] = temp;
}
for(int i = 0; i <= len; i++)
printf("%d ", arr[i]);
putchar('\n');
return 0;}
#include <stdio.h>
int
main(){
int temp, a[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
for(int i = 0; i <= 2; i++){
for(int j = 0; j <= 2; j++){
if(i <= j){
temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
} for(int i = 0; i <= 2; i++){
for(int j = 0; j <= 2; j++){
printf("%d ", a[i][j]);
}
putchar('\n');
}
return 0;}
#include <stdio.h>
int
main(){
float a[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
float temp = 0;
for(int i = 0; i <= 2; i++){
for(int j = 0; j <= 2; j++){
temp += a[i][j];
}
a[i][0] = temp/3;
temp = 0;
} for(int i = 0; i <= 2; i++){
for(int j = 0; j <= 2; j++){
printf("%.3f ", a[i][j]);
}
putchar('\n');
}
return 0;}
#include <stdio.h>
int
main(){
int len, temp;
scanf("%d", &len);
len = len - 1;
int arr[128] = {0};
for(int i = 0; i <= len ; i++)
scanf("%d", &arr[i]);
for(int i = 0; i <= len; i++){
for (int j = i; j > 0; j-- ){
if (arr[j-1] > arr[j]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
for(int n = 0; n <= len; n++)
printf("%d ", arr[n]);
putchar('\n');
return 0;}
File added
#include <stdio.h>
#include <stdlib.h>
int
main()
{
double ***pointer = NULL;
*( *( *( pointer = (double ***) malloc(sizeof(double **))) = (double **)malloc(sizeof(double *))) = (double *)malloc(sizeof(double)));
***pointer = 2.0;
printf("%lf\n", ***pointer);
free(**pointer);
free(*pointer);
free(pointer);
return 0;
}
#include <stdio.h>
int
main()
{
int a = 10, b = 15, c;
int *pa =&a, *pb=&b;
c = *pa + *pb;
printf("%d\n", c);
return 0;
}
#include <stdio.h>
int
main()
{
int a,b;
scanf("%d %d", &a, &b);
int *pa=&a, *pb=&b;
if(*pa > *pb)
printf("max:%d\n", *pa);
else
printf("max:%d\n", *pb);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int
main(){
int lenght;
printf("Enter lenght of the array:");
scanf("%d", &lenght);
double *array = NULL;
array = (double *)malloc(lenght*sizeof(double));
double *iter = array, *end = &array[lenght];
for(iter, end; iter < end; iter++)
scanf("%lf", iter);
for(iter = array; iter < end; iter++)
printf("%lf\n", *iter);
free(array);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int
main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int *iter;
for(iter = &a[9]; iter >= &a; iter--)
printf("%d\n", *iter);
return 0;
}
#include <stdio.h>
int
main()
{
int a = 1234567890;
char * pa = &a;
for(int i = 0; i < 4; i++, pa++)
printf("%c", *pa);
putchar('\n');
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int
main()
{
int lenght, width;
scanf("%d %d", &lenght, &width);
char **string = (char **)malloc(sizeof(char *) * width);
if(string){
printf("Allocated string!\n");
}else{
printf("Malloc failed!\n");
return 1;
}
for(int i = 0; i < width; i++){
string[i] = (char *)malloc(sizeof(char) * lenght);
if(!string){
printf("Malloc %d failed!\n", i);
return 1;
}
}
for(int i = 0; i < width; i++)
free(string[i]);
free(string);
return 0;
}
File added
File added
#include <stdio.h>
int
SomeFunc(int a, int b)
{
printf("%d\n",a+b );
return a+b;
}
int
main()
{
struct SomeStruct{
int (*func)(int, int);
};
struct SomeStruct MyObj = {
&SomeFunc
};
MyObj.func(1, 2);
MyObj.func(10, 12);
MyObj.func(0x0a, 0x12);
return 0;
}
#include <stdio.h>
#include <math.h>
#include <string.h>
typedef struct{
double x;
double y;
double z;
char name[20];
} Vector;
Vector VecProduct(Vector, Vector);
Vector VecMult(Vector, Vector);
double VecLenght(Vector);
void VecPrint(Vector);
int
main()
{
Vector a = {
12, 1, -0.9, "Alpha"
};
Vector b = {
-2, 0.10, 18, "Beta"
};
Vector d = VecProduct(a, b);
VecPrint(d);
Vector e = VecMult(d, b);
VecPrint(e);
return 0;
}
Vector VecProduct(Vector a, Vector b)
{
Vector c;
c.x = a.x*b.x;
c.y = a.y*b.y;
c.z = a.z*b.z;
strcpy(c.name, "product");
return c;
}
Vector VecMult(Vector a, Vector b)
{
Vector c;
strcpy(c.name, "mult");
c.x = a.y*b.z - a.z*b.y;
c.y = a.z*b.x - a.x*b.z;
c.z = a.x*b.y - a.y*b.x;
return c;
}
double VecLenght(Vector c){
return sqrt(c.x*c.x + c.y*c.y + c.z*c.z);
}
void VecPrint(Vector a){
printf("Name:%s\n\tLen = %lf\n\tx = %lf\n\ty = %lf\n\tz = %lf\n", a.name, VecLenght(a), a.x, a.y, a.z);
}
#include <stdio.h>
#include <math.h>
struct{
double a;
double i;
// c = a + i * b;
}typedef compl;
compl Exp(compl, int);
unsigned factorial(int);
void print_complex(compl z);
int
main()
{
compl z = {4, 2};
compl y = Exp(z, 13);
print_complex(y);
return 0;
}
unsigned
factorial(int a)
{
unsigned c = 1;
for (int i = 1; i <= a; i++)
c *= i;
return c;
}
compl
Exp(compl z, int n)
{
compl x = {1, 1};
for (int i = 1; i <= n; i++)
{
x.a += 1.0/factorial(i)*pow(z.a, i);
x.i += 1.0/factorial(i)*pow(z.i, i);
}
return x;
}
void
print_complex(compl z) {
printf("%f + %fi\n", z.a, z.i);
}
#include <stdio.h>
struct{
char name[32];
unsigned year : 12;
unsigned month : 4;
unsigned day : 5;
}typedef DATE;
void print_date(DATE);
int
main()
{
DATE bday = {
"Birthday", 2005, 4, 28
};
print_date(bday);
printf("%zu\n", sizeof(bday));
return 0;
}
void print_date(DATE a)
{
printf("Event:%s\n\t%d-%d-%d\n",a.name, a.day, a.month, a.year);
}
#include <stdio.h>
#include <stdlib.h>
struct dlist_elem{
int feild;
struct dlist_elem *prev;
struct dlist_elem *next;
};
void backward_print_dlist(int count, struct dlist_elem c);
void print_dlist(int count, struct dlist_elem c);
int
main()
{
struct dlist_elem a1 = {10, NULL, NULL};
struct dlist_elem a2 = {11, &a1, NULL};
a1.next = &a2;
struct dlist_elem a3 = {12, &a2, NULL};
a2.next = &a3;
struct dlist_elem a4 = {13, &a3, NULL};
a3.next = &a4;
struct dlist_elem a5 = {14, &a4, NULL};
a4.next = &a5;
struct dlist_elem a6 = {15, &a5, NULL};
a5.next = &a6;
backward_print_dlist(5, a5);
print_dlist(5, a1);
}
void
print_dlist(int count, struct dlist_elem c)
{
putchar('\n');
struct dlist_elem *iter = &c;
for(int i = 1; i <= count; i++)
{
printf("%d)Elem: %p\n\tField: %d\n\tNext: %p\n\tPrev: %p\n", i,iter, iter->feild, iter->next, iter->prev);
iter = iter->next;
}
}
void
backward_print_dlist(int count, struct dlist_elem c)
{
putchar('\n');
struct dlist_elem *iter = &c;
for(int i = count; i > 0; i--)
{
printf("%d)Elem: %p\n\tField: %d\n\tNext: %p\n\tPrev: %p\n", i,iter, iter->feild, iter->next, iter->prev);
iter = iter->prev;
}
}
#include <stdio.h>
union MyUnion{
int num;
float fnum;
double dnum;
};
void
print_all_members(union MyUnion u)
{
printf("int: %d\nfloat: %f\ndouble: %lf\n\n", u.num, u.fnum, u.dnum);
}
int
main()
{
union MyUnion u;
union MyUnion *pu = &u;
pu->num = 12;
print_all_members(u);
pu->fnum = 1.2;
print_all_members(u);
pu->dnum = 0.12;
print_all_members(u);
return 0;
}
#include <stdio.h>
union MyUnion{
unsigned long uli;
char c;
};
int
main()
{
union MyUnion u;
u.uli = 1234567890;
for(size_t i = 0; i < sizeof(unsigned long); i++)
{
putchar(u.c);
}
putchar('\n');
return 0;
}
#include <stdio.h>
enum Days {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
int
main()
{
for(enum Days i = 0; i < 7; i++)
printf("Day number: %d\n",i );
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define LEN 10
typedef enum {
INT_TYPE,
FLOAT_TYPE,
POINTER_TYPE
} DataType;
typedef union {
int i;
float f;
void *p;
} Data;
struct MyStruct{
Data data;
DataType type;
};
void
printData(struct MyStruct *array, int size) {
for (int i = 0; i < size; i++) {
switch (array[i].type) {
case INT_TYPE:
printf("%d)int: %d\n", i, array[i].data.i);
break;
case FLOAT_TYPE:
printf("%d)float: %f\n", i, array[i].data.f);
break;
case POINTER_TYPE:
printf("%d)ptr: %p\n", i, array[i].data.p);
break;
}
}
}
int
main()
{
struct MyStruct *array = NULL;
array = (struct MyStruct* )malloc(LEN * sizeof(struct MyStruct));
int n = 12;
for(struct MyStruct *iter = array; iter < array+LEN; iter++, n++)
{
if (n % 3 == INT_TYPE){
iter->type = INT_TYPE;
iter->data.i = n;
}else if (n % 3 == FLOAT_TYPE){
iter->type = FLOAT_TYPE;
iter->data.f = n/3.0;
}else if (n % 3 == POINTER_TYPE){
iter->type = POINTER_TYPE;
iter->data.p = iter;
}
}
printData(array, LEN);
return 0;
}
File added
#include <stdio.h>
unsigned loop_fact(int n);
unsigned recusion_fact(int n);
int
main()
{
int a = 10;
printf("recursion: %u\n",recusion_fact(a));
printf("loop: %u\n",loop_fact(a));
return 0;
}
unsigned
recusion_fact(int n)
{
unsigned fact = 1;
if (n == 1)
return fact;
else
return n*recusion_fact(n-1);
}
unsigned
loop_fact(int n)
{
unsigned fact = 1;
for (int i = 1; i <= n; i++)
fact*=i;
return fact;
}
#include <stdio.h>
#include <stdlib.h>
#define LEN 12
void swap_even_odd(int *, int);
int
main()
{
int *ptr = NULL;
ptr = (int *)(malloc(LEN*sizeof(int)));
for(int i = 0; i<LEN; i++)
ptr[i] = i;
for(int i = 0; i<LEN; i++)
printf("%d ", ptr[i]);
putchar('\n');
swap_even_odd(ptr, LEN);
for(int i = 0; i<LEN; i++)
printf("%d ", ptr[i]);
putchar('\n');
free(ptr);
return 0;
}
void
swap_even_odd(int *array, int len)
{
int temp;
for(int i = 1; i<len; i+=2)
{
temp = array[i-1];
array[i-1] = array[i];
array[i] = temp;
}
}
#include <stdio.h>
#include <stdlib.h>
double **matrix_alloc(unsigned, unsigned);
void matrix_free(double **, unsigned, unsigned);
void matrix_print(double **, unsigned, unsigned);
void matrix_fill(double **, unsigned, unsigned);
#define LEN 3
#define WID 3
int
main()
{
double **matrix = matrix_alloc(LEN, WID);
matrix_fill(matrix, LEN, WID);
matrix_print(matrix, LEN, WID);
matrix_free(matrix, LEN, WID);
return 0;
}
void matrix_fill(double **matrix, unsigned len, unsigned wid)
{
for (int i = 0; i < len; i++){
for (int j = 0; j < wid; j++){
if (i == j)
matrix[i][j] = 1;
else
matrix[i][j] = 0;
}
}
}
double **matrix_alloc(unsigned len, unsigned wid)
{
double **matrix = NULL;
matrix = (double **)(malloc(wid*sizeof(double *)));
for(int i = 0; i < wid; i++){
matrix[i] = malloc(sizeof(double));
}
return matrix;
}
void matrix_free(double ** matrix, unsigned len, unsigned wid)
{
for(int i = 0; i < wid; i++)
free(matrix[i]);
free(matrix);
}
void
matrix_print(double **matrix, unsigned len, unsigned wid)
{
for (int i = 0; i < len; i++){
for (int j = 0; j < wid; j++){
printf("%lf ", matrix[i][j]);
}
putchar('\n');
}
}
#include <stdio.h>
void
vecProduct(double *a, double *b, double *rez)
{
rez[0] = a[1] * b[2] - a[2] * b[1];
rez[1] = a[2] * b[0] - a[0] * b[2];
rez[2] = a[0] * b[1] - a[1] * b[0];
}
int
main()
{
double a[3] = {10, 11, 12};
double b[3] = {13, 14, 15};
double c[3];
vecProduct(a, b, c);
printf("Product of a and b = (%lf, %lf, %lf)\n", c[0], c[1], c[2]);
return 0;
}
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <locale.h>
#define MY_SIZE 15
int
main()
{
setlocale(LC_ALL,"en_US.iso88591");
char my_string[MY_SIZE];
fscanf(stdin, "%s", my_string);
int len;
for(len = 0; my_string[len] != '\0'; len++)
continue;
char *iter;
for(len = 0, iter = &my_string; *iter != '\0'; iter++, len++)
continue;
size_t len2 = strlen(my_string);
char my_string2[MY_SIZE] = "ABCDEFG";
strcpy(my_string2, my_string);
char my_string3[MY_SIZE] = "abcd";
char my_string4[MY_SIZE*2] = "efgn";
strcat(my_string4,my_string3);
printf("string1:%s\n\tlen: %d\n\tsize_t len2: %zu\nstring2:%s\nstring3:%s\n",my_string, len, len2, my_string2, my_string3);
int rez = strcmp("ab", "ba");
char my_string5[MY_SIZE] = "QwErTyUiOpAsDf";
for(len = 0; len < MY_SIZE; len++)
my_string5[len] = tolower(my_string5[len]);
printf("tolower: %s\n", my_string5);
for(len = 0; len < MY_SIZE; len++)
my_string5[len] = toupper(my_string5[len]);
printf("toupper: %s\n", my_string5);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int
main()
{
char string1[10] = "1.23456";
char string2[10] = "1234567";
double double1 = atof(string1);
int decimal2 = atoi(string2);
printf("%lf\n%d\n", double1, decimal2);
return 0;
}
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 25
int
main()
{
char string[SIZE];
fgets(string, SIZE, stdin );
for(int i = 0; i < strlen(string); i++)
{
printf("%c)[%d]", string[i], i);
if(isascii(string[i]))
printf("is ascii ");
else{
printf("is non-ascii ");
continue;
}
if(isprint(string[i])){
printf("printable ");
}else
printf("non-printable");
if(isblank(string[i]))
printf("blank");
else if(isdigit(string[i]))
printf("digit");
else if(islower(string[i]))
printf("lower case letter");
else if(isupper(string[i]))
printf("upper case letter");
printf(" character № %d in ascii table", string[i]);
putchar('\n');
}
return 0;
}
File added
File added
CC=gcc
CFLAGS=-g -Wall -I.
BUILD_DIR=build
SRC_DIR=src
all: matrix_operations.o matrix_alloc.o main.o
$(CC) -o main.elf $(BUILD_DIR)/*.o $(CFLAGS)
main.o:
$(CC) -o $(BUILD_DIR)/main.o $(SRC_DIR)/main.c $(CFLAGS) -c
matrix_operations.o:
$(CC) -o $(BUILD_DIR)/matrix_operations.o $(SRC_DIR)/matrix_operations.c -c
matrix_alloc.o:
$(CC) -o $(BUILD_DIR)/matrix_alloc.o $(SRC_DIR)/matrix_alloc.c -c
clean:
rm $(BUILD_DIR)/*.o
rm main.elf
#include <stdio.h>
#include "matrix.h"
#include "matrix_operations.h"
#include "matrix_alloc.h"
int
main()
{
Matrix A = matrix_alloc(3, 3);
Matrix B = matrix_alloc(3, 3);
for(int i = 0, k = 1; i < 3; i++){
for(int j = 0; j < 3; j++, k++){
A.data[i][j] = k;
B.data[i][j] = k;
}
}
printf("Matrix 1\n");
matrix_print(A);
printf("Matrix 2\n");
matrix_print(B);
Matrix C = matrix_mult(A, B);
printf("Multiplicatoin result:\n");
matrix_print(C);
Matrix D = matrix_add(B, C);
printf("Addition result:\n");
matrix_print(D);
matrix_free(A);
matrix_free(B);
matrix_free(C);
matrix_free(D);
return 0;
}
#ifndef MATRIX_H
#define MATRIX_H
typedef struct {
int rows;
int cols;
double** data;
} Matrix;
#endif
#include "matrix_alloc.h"
#include <stdlib.h>
#include "matrix.h"
void matrix_free(Matrix A)
{
for(int i = 0; i < A.rows; i++)
free(A.data[i]);
free(A.data);
}
Matrix matrix_alloc(unsigned rows, unsigned colums)
{
Matrix A = {rows, colums, NULL};
A.data = (double **)(malloc(sizeof(double*)*rows));
for(int i = 0; i < rows; i++)
A.data[i] = calloc(colums, sizeof(double));
return A;
}
double *array_alloc(unsigned len){
double *array = NULL;
array = calloc(len, sizeof(double));
return array;
}
#ifndef MATRIX_ALLOC
#define MATRIX_ALLOC
#include "matrix.h"
Matrix matrix_alloc(unsigned rows, unsigned colums);
void matrix_free(Matrix);
double *array_alloc(unsigned len);
#endif
#include "matrix_operations.h"
#include "matrix.h"
#include "matrix_alloc.h"
#include "stdio.h"
Matrix matrix_mult(Matrix A, Matrix B)
{
Matrix C = matrix_alloc(A.rows, B.cols);
for(int i = 0; i < A.rows; i++){
for(int j = 0; j < B.cols; j++){
for(int k = 0; k < A.cols; k++){
C.data[i][j] += A.data[i][k]*B.data[k][j];
}
}
}
return C;
}
Matrix matrix_add(Matrix A, Matrix B)
{
Matrix C = matrix_alloc(A.rows, B.cols);
for(int i = 0; i < A.rows; i++){
for(int j = 0; j < B.cols; j++){
C.data[i][j] += A.data[i][j]+B.data[i][j];
}
}
return C;
}
void matrix_print(Matrix A)
{
for(int i = 0; i < A.rows; i++){
for(int j = 0; j < A.cols; j++){
printf("%lf ", A.data[i][j]);
}
putchar('\n');
}
}
Matrix matrix_number_product(Matrix A, double num)
{
Matrix C = matrix_alloc(A.rows, A.cols);
for(int i = 0; i < C.rows; i++){
for(int j = 0; j < C.cols; j++)
C.data[i][j] = C.data[i][j]*num;
}
return C;
}
#ifndef MATRIX_OPERATAOINS
#define MATRIX_OPERATAOINS
#include "matrix.h"
Matrix matrix_mult(Matrix A, Matrix B);
Matrix matrix_add(Matrix A, Matrix B);
void matrix_print(Matrix A);
Matrix matrix_number_product(Matrix A, double num);
#endif
BUILD_DIR=build
SRC_DIR=src
CC=gcc
CFALGS=-Wall -g
all: main1.elf main2.elf
main1.elf: libstudents.so
$(CC) $(CFALGS) -lstudents -L ./$(BUILD_DIR)/ -o main1.elf $(SRC_DIR)/main1.c
echo export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$PWD"
main2.elf: libstudents.so
$(CC) $(CFALGS) -lstudents -L ./$(BUILD_DIR)/ -o main2.elf $(SRC_DIR)/main2.c
echo export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$PWD"
libstudents.so:
$(CC) $(CFALGS) -shared -fpic -o $(BUILD_DIR)/libstudents.so $(SRC_DIR)/students.c
clean:
rm $(BUILD_DIR)/*o
rm main*.elf
#include <stdlib.h>
#include "students.h"
int
main()
{
int n;
printf("Enter the amount of students:");
scanf("%d", &n);
getchar();
struct Student *students = allocate_array(n);
fill_array(n, students);
FILE *file = NULL;
file = fopen("students.cvs", "a");
write_array(file, n, students);
fclose(file);
free(students);
puts("Students to read:");
scanf("%d",&n);
getchar();
if(n <= 0)
exit(0);
file = fopen("students.cvs","r");
if(!file){
puts("Could not open file!");
exit(0);
}
struct Student *students2 = read_file_to_array(file, n, students2);
fclose(file);
puts("From file:");
print_array(students2, n);
free(students2);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "students.h"
int
main()
{
int n;
printf("Enter the amount of students:");
scanf("%d", &n);
getchar();
struct Student *students = allocate_array(n);
fill_array(n, students);
FILE *file = NULL;
file = fopen("students.bin", "wb");
bin_write_array(file, n, students);
fclose(file);
free(students);
puts("Students to read:");
scanf("%d",&n);
getchar();
if(n <= 0)
exit(0);
file = fopen("students.bin","rb");
if(!file){
puts("Could not open file!");
exit(0);
}
struct Student *students2 = allocate_array(n);
bin_read_file_to_array(file, n, students2);
fclose(file);
puts("From file:");
print_array(students2, n);
free(students2);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct Student{
unsigned id;
char name[50];
char faculty[50];
float rating;
};
struct Student *
allocate_array(int n){
struct Student *students = NULL;
students = malloc(sizeof(struct Student)*n);
if(!students)
exit(1);
return students;
}
void
fill_array(int n, struct Student *students)
{
for(int i = 0; i < n; i++){
printf("\nEnter student's № %d info:\n", i);
printf("\tid:");
scanf("%u", &students[i].id);
getchar();
printf("\tname:");
fgets(students[i].name, sizeof(students[i].name), stdin);
students[i].name[strcspn(students[i].name, "\n")] = 0;
printf("\tfaculty:");
fgets(students[i].faculty, sizeof(students[i].faculty), stdin);
students[i].faculty[strcspn(students[i].faculty, "\n")] = 0;
printf("\trating:");
scanf("%f", &students[i].rating);
getchar();
}
}
void
write_array(FILE *file, int n, struct Student *students)
{
for(int i = 0; i < n; i++){
fprintf(file, "%u,%s,%s,%f\n", students[i].id, students[i].name, students[i].faculty, students[i].rating);
}
}
void
bin_write_array(FILE *file, int n, struct Student *students)
{
fwrite(&students[0], sizeof(struct Student), n, file);
}
void
bin_read_file_to_array(FILE *file, int n)
{
struct Student *students = allocate_array(n);
fread(students, sizeof(struct Student), n, file);
}
struct Student *
read_file_to_array( FILE *file, int n)
{
char *buf = NULL;
buf = malloc(255);
struct Student *students = allocate_array(n);
if(!buf)
exit(1);
char *tmp;
int i = 0;
while(fgets(buf, sizeof(struct Student), file) != NULL && i != n){
if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == '\n'))
buf[strlen (buf) - 1] = '\0';
tmp = strtok(buf, ",");
students[i].id = atoi(tmp);
tmp = strtok(NULL, ",");
strcpy(students[i].name, tmp);
tmp = strtok(NULL, ",");
strcpy(students[i].faculty, tmp);
tmp = strtok(NULL, ",");
students[i].rating = atof(tmp);
i++;
}
free(buf);
return students;
}
void
print_array(int n, struct Student *students)
{
for(int i = 0; i < n; i++){
printf("Student №%d\n\tid:%u\n\tname:%s\n\tfaculty:%s\n\trating:%f\n", i, students[i].id, students[i].name, students[i].faculty, students[i].rating);
}
}
#ifndef STUDENTS_H
#define STUDENTS_H
#include <stdio.h>
struct Student *allocate_array(int n);
void fill_array(int n, struct Student *students);
void print_array(struct Student *students, int n);
void write_array(FILE *file, int n, struct Student *students);
void read_file_to_array(FILE *file, int n);
void bin_write_array(FILE *file,int n, struct Student *students);
void bin_read_file_to_array(FILE *file, int n);
#endif
1,nig,gger,0.000000
2,qwerty,ytrewq,1.000000
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment