initial commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.idea
|
||||
58
mem.c
Normal file
58
mem.c
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <stdio.h> // p r i n t f ( . . )
|
||||
#include <stdlib.h> // EXIT_SUCCESS
|
||||
#include <signal.h> // s i g n a l ( . . )
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
struct vector {
|
||||
double *val;
|
||||
int size;
|
||||
};
|
||||
|
||||
struct vector * new ( unsigned int n ){
|
||||
struct vector* v = malloc(sizeof(struct vector));
|
||||
v->size = n;
|
||||
v->val = calloc(n, sizeof(double));
|
||||
return v;
|
||||
}
|
||||
|
||||
void printVec(struct vector* v){
|
||||
printf("[");
|
||||
for (int i = 0; i< v->size; i++){
|
||||
printf("%.2f,",v->val[i]);
|
||||
}
|
||||
printf("]");
|
||||
}
|
||||
|
||||
struct vector * add ( struct vector * v , struct vector * w ){
|
||||
if (v->size != w->size) return NULL;
|
||||
|
||||
struct vector *y = new(v->size);
|
||||
for (int i = 0; i< v->size; i++){
|
||||
y->val[i] = v->val[i] + w->val[i];
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
struct vector * smul ( double s , struct vector * v ){
|
||||
struct vector *y = new(v->size);
|
||||
for (int i = 0; i< v->size; i++){
|
||||
y->val[i] = v->val[i] * s;
|
||||
}
|
||||
return y;
|
||||
|
||||
}
|
||||
|
||||
int main ( int argc , char * argv []) {
|
||||
struct vector *v = new(3);
|
||||
v->val[0] = 23.0;
|
||||
v->val[1] = 5.0;
|
||||
|
||||
struct vector *w = new(3);
|
||||
w->val[1] = 2.0;
|
||||
w->val[2] = 54.0;
|
||||
|
||||
|
||||
printVec(smul(3.0,add(v,w)));
|
||||
return EXIT_SUCCESS ;
|
||||
}
|
||||
38
sig.c
Normal file
38
sig.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h> // p r i n t f ( . . )
|
||||
#include <stdlib.h> // EXIT_SUCCESS
|
||||
#include <signal.h> // s i g n a l ( . . )
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
int signb = 1;
|
||||
|
||||
void handler ( int signum ) {
|
||||
if ( signum == SIGINT ){
|
||||
if (signb == 1 )
|
||||
printf ( "Just give me a moment.\n" );
|
||||
else if (signb == 2)
|
||||
printf("I said I need a moment !\n");
|
||||
else {
|
||||
printf("Fine. I’m out of here.\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
signb +=1;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int string_length ( char string []){
|
||||
unsigned int i = 0;
|
||||
while (string[i++] != '\0');
|
||||
return i-1;
|
||||
|
||||
}
|
||||
|
||||
int main ( int argc , char * argv []) {
|
||||
signal ( SIGINT , handler );
|
||||
|
||||
while (1) {
|
||||
pause();
|
||||
//printf("Waiting loop resumed .\n");
|
||||
}
|
||||
return EXIT_SUCCESS ;
|
||||
}
|
||||
Reference in New Issue
Block a user