提交 a99549a1 编写于 作者: B Ben

Split anonymous structs into two examples

上级 eba76a12
/* Compile with: /* Compile with:
LDLIBS="-lm -ldl -lreadline" CFLAGS="-g -Wall -std=gnu11 -O3" make dynamic LDLIBS="-lm -ldl -lreadline" CFLAGS="-g -Wall -std=gnu11 -O3" make dynamic
*/ */
#define _GNU_SOURCE //cause stdio.h to include asprintf
#include <dlfcn.h> #include <dlfcn.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -12,12 +11,12 @@ void get_a_function(){ ...@@ -12,12 +11,12 @@ void get_a_function(){
fprintf(f, "#include <math.h>\n" fprintf(f, "#include <math.h>\n"
"double fn(double in){\n"); "double fn(double in){\n");
char *a_line = NULL; char *a_line = NULL;
char *header = ">>double fn(double in){\n>> "; char *prompt = ">>double fn(double in){\n>> ";
do { do {
free(a_line); free(a_line);
a_line = readline(header); a_line = readline(prompt);
fprintf(f, "%s\n", a_line); fprintf(f, "%s\n", a_line);
header = ">> "; prompt = ">> ";
} while (strcmp(a_line, "}")); } while (strcmp(a_line, "}"));
fclose(f); fclose(f);
} }
......
...@@ -12,7 +12,7 @@ double one_dist(gsl_vector *v1, void *v2){ ...@@ -12,7 +12,7 @@ double one_dist(gsl_vector *v1, void *v2){
long double distance(apop_data *data, apop_model *model){ long double distance(apop_data *data, apop_model *model){
gsl_vector *target = model->parameters->vector; gsl_vector *target = model->parameters->vector;
return -apop_map_sum(data, .fn_vp=one_dist, .param=target, .part='r'); return -apop_map_sum(data, .fn_vp=one_dist, .param=target);
} }
apop_model *min_distance= &(apop_model){ apop_model *min_distance= &(apop_model){
...@@ -25,9 +25,8 @@ int main(){ ...@@ -25,9 +25,8 @@ int main(){
2.9, 8.6, 2.9, 8.6,
-1.3, 3.7, -1.3, 3.7,
2.9, 1.1); 2.9, 1.1);
Apop_model_add_group(min_distance, apop_mle, .method= APOP_SIMPLEX_NM, Apop_model_add_group(min_distance, apop_mle, .method= "NM simplex",
.tolerance=1e-5); .tolerance=1e-5);
Apop_model_add_group(min_distance, apop_parts_wanted); apop_model *est = apop_estimate(locations, min_distance);
apop_model *est=apop_estimate(locations, min_distance);
apop_model_show(est); apop_model_show(est);
} }
/* Compile with:
make LDLIBS='-lm' CFLAGS="-g -Wall -std=gnu11" seamlessthree
*/
#include <stdio.h>
#include <math.h>
typedef struct point {
double x, y;
} point;
typedef struct {
union {
struct {
double x, y;
};
point p2;
};
double z;
} threepoint;
double length (point p){
return sqrt(p.x*p.x + p.y*p.y);
}
double threelength (threepoint p){
return sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
}
int main(){
threepoint p = {.x=3, .y=0, .z=4};
printf("p is %g units from the origin\n", threelength(p));
double xylength = length(p.p2);
printf("Its projection onto the XY plane is %g units from the origin\n", xylength);
}
/* Compile with: /* Compile with:
make LDLIBS='-lm' CFLAGS="-g -Wall -std=gnu11 --ms-extensions" seamlesstwo make LDLIBS='-lm' CFLAGS="-g -Wall -std=gnu11 -fms-extensions" seamlesstwo
*/ */
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
......
...@@ -9,7 +9,6 @@ void matrix_cross_base(double *list1, double *list2){ ...@@ -9,7 +9,6 @@ void matrix_cross_base(double *list1, double *list2){
int count1 = 0, count2 = 0; int count1 = 0, count2 = 0;
while (!isnan(list1[count1])) count1++; while (!isnan(list1[count1])) count1++;
while (!isnan(list2[count2])) count2++; while (!isnan(list2[count2])) count2++;
if (!count1 || !count2) {printf("missing data."); return;}
for (int i=0; i<count1; i++){ for (int i=0; i<count1; i++){
for (int j=0; j<count2; j++) for (int j=0; j<count2; j++)
......
#include <stdio.h> #include <stdio.h>
int main(){ int main(){
printf("3./5=%g\n", 3./5); printf("13./5=%g\n", 13./5);
printf("3/5=%i\n", 3/5); printf("13/5=%i\n", 13/5);
printf("3%%5=%i\n", 3%5); printf("13%%5=%i\n", 13%5);
} }
...@@ -6,7 +6,6 @@ void loops(int max){ ...@@ -6,7 +6,6 @@ void loops(int max){
printf("Hello #%i\n", i); printf("Hello #%i\n", i);
i++; i++;
} while (i < max); //Note the semicolon. } while (i < max); //Note the semicolon.
} }
int main(){ int main(){
......
...@@ -7,6 +7,6 @@ int main(){ ...@@ -7,6 +7,6 @@ int main(){
char string[len]; char string[len];
intlist[7] = 7; intlist[7] = 7;
snprintf(string, 20, "Item seven is %i.", intlist[7]); snprintf(string, len, "Item seven is %i.", intlist[7]);
printf("string says: <<%s>>\n", string); printf("string says: <<%s>>\n", string);
} }
...@@ -6,7 +6,7 @@ void double_in(int *in){ ...@@ -6,7 +6,7 @@ void double_in(int *in){
} }
int main(){ int main(){
int *x= malloc(sizeof(int)); int x[1];
*x= 10; *x= 10;
double_in(x); double_in(x);
printf("x now points to %i.\n", *x); printf("x now points to %i.\n", *x);
......
...@@ -40,6 +40,7 @@ int main(int argc, char **argv){ ...@@ -40,6 +40,7 @@ int main(int argc, char **argv){
char *ucs = localstring_to_utf8(string_from_file(argv[1])); char *ucs = localstring_to_utf8(string_from_file(argv[1]));
Stopif(!ucs, return 1, "Exiting."); Stopif(!ucs, return 1, "Exiting.");
FILE *out = fopen("uout.html", "w"); FILE *out = fopen("uout.html", "w");
Stopif(!out, return 1, "Couldn't open uout.html for writing."); Stopif(!out, return 1, "Couldn't open uout.html for writing.");
fprintf(out, "<head><meta http-equiv=\"Content-Type\" " fprintf(out, "<head><meta http-equiv=\"Content-Type\" "
...@@ -48,6 +49,7 @@ int main(int argc, char **argv){ ...@@ -48,6 +49,7 @@ int main(int argc, char **argv){
fprintf(out, "Its Unicode encoding required %zu bytes.<br>", strlen(ucs)); fprintf(out, "Its Unicode encoding required %zu bytes.<br>", strlen(ucs));
fprintf(out, "Here it is, with each space-delimited element on a line " fprintf(out, "Here it is, with each space-delimited element on a line "
"(with commentary on the first character):<br>"); "(with commentary on the first character):<br>");
ok_array *spaced = ok_array_new(ucs, " \n"); ok_array *spaced = ok_array_new(ucs, " \n");
for (int i=0; i< spaced->length; i++, (spaced->elements)++){ for (int i=0; i< spaced->length; i++, (spaced->elements)++){
fprintf(out, "%s", *spaced->elements); fprintf(out, "%s", *spaced->elements);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册