Difference between revisions of "Statistical Algorithms Importer: Linux-compiled Project FAQ"
From Gcube Wiki
(Created page with "{| align="right" ||__TOC__ |} F.A.Q. of Statistical Algorithms Importer (SAI), here are common mistakes we have found in Linux-compiled Project. == How to use the input par...") |
(→How to use the input parameters) |
||
Line 16: | Line 16: | ||
<pre style="display:block;font-family:monospace;white-space:pre;margin:1em 0;"> | <pre style="display:block;font-family:monospace;white-space:pre;margin:1em 0;"> | ||
+ | //============================================================================ | ||
+ | // Name : BasicStatistic.cpp | ||
+ | // Author : Giancarlo Panichi | ||
+ | // Version : | ||
+ | // Copyright : Your copyright notice | ||
+ | // Description : BasicStatistic in C++, Ansi-style | ||
+ | //============================================================================ | ||
+ | |||
+ | #include <iostream> | ||
+ | #include <stdio.h> | ||
+ | #include <stdlib.h> | ||
+ | #include <string.h> | ||
+ | using namespace std; | ||
+ | |||
+ | int main(int argc, char **argv) { | ||
+ | printf("BasicStatistic"); | ||
+ | printf("Operator: %s\n",argv[1]); | ||
+ | printf("Maximum number of items considered: %s\n",argv[2]); | ||
+ | printf("Input file: %s\n",argv[3]); | ||
+ | |||
+ | FILE * finput; | ||
+ | char * line = NULL; | ||
+ | size_t len = 0; | ||
+ | ssize_t read; | ||
+ | |||
+ | finput = fopen(argv[3], "r"); | ||
+ | if (finput == NULL){ | ||
+ | printf("No input file found: %s\n", argv[3]); | ||
+ | return -1; | ||
+ | } | ||
+ | |||
+ | int limit=atoi(argv[2]); | ||
+ | int i=0; | ||
+ | int value=0; | ||
+ | int result=0; | ||
+ | |||
+ | while (((read = getline(&line, &len, finput)) != -1) && (i<limit||limit==-1)) { | ||
+ | printf("Retrieved line of length %zu and value: %s", read,line); | ||
+ | value=atoi(line); | ||
+ | if(i==0){ | ||
+ | result=value; | ||
+ | } else { | ||
+ | if (strcmp(argv[1], "max") == 0) | ||
+ | { | ||
+ | if(value>result){ | ||
+ | result=value; | ||
+ | } | ||
+ | } else { | ||
+ | if (strcmp(argv[1], "min") == 0) | ||
+ | { | ||
+ | if(value<result){ | ||
+ | result=value; | ||
+ | } | ||
+ | } else { | ||
+ | if (strcmp(argv[1], "average") == 0) | ||
+ | { | ||
+ | result=(result+value)/2; | ||
+ | } else { | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | i++; | ||
+ | } | ||
+ | |||
+ | fclose(finput); | ||
+ | if (line){ | ||
+ | free(line); | ||
+ | } | ||
+ | |||
+ | //Write result | ||
+ | FILE *foutput = fopen("output.txt", "w"); | ||
+ | if (foutput == NULL) | ||
+ | { | ||
+ | printf("Error opening file!\n"); | ||
+ | return -1; | ||
+ | } | ||
+ | |||
+ | fprintf(foutput, "BasicStatistic Result: \n"); | ||
+ | fprintf(foutput, "%d\n", result); | ||
+ | fclose(foutput); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
</pre> | </pre> | ||
Revision as of 16:07, 23 October 2017
Contents |
F.A.Q. of Statistical Algorithms Importer (SAI), here are common mistakes we have found in Linux-compiled Project.
How to use the input parameters
- For example we consider BasicStatistic algorithm:
- DataMiner show the BasicStatistic algorithm in this way:
- C code in sample:
//============================================================================ // Name : BasicStatistic.cpp // Author : Giancarlo Panichi // Version : // Copyright : Your copyright notice // Description : BasicStatistic in C++, Ansi-style //============================================================================ #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; int main(int argc, char **argv) { printf("BasicStatistic"); printf("Operator: %s\n",argv[1]); printf("Maximum number of items considered: %s\n",argv[2]); printf("Input file: %s\n",argv[3]); FILE * finput; char * line = NULL; size_t len = 0; ssize_t read; finput = fopen(argv[3], "r"); if (finput == NULL){ printf("No input file found: %s\n", argv[3]); return -1; } int limit=atoi(argv[2]); int i=0; int value=0; int result=0; while (((read = getline(&line, &len, finput)) != -1) && (i<limit||limit==-1)) { printf("Retrieved line of length %zu and value: %s", read,line); value=atoi(line); if(i==0){ result=value; } else { if (strcmp(argv[1], "max") == 0) { if(value>result){ result=value; } } else { if (strcmp(argv[1], "min") == 0) { if(value<result){ result=value; } } else { if (strcmp(argv[1], "average") == 0) { result=(result+value)/2; } else { break; } } } } i++; } fclose(finput); if (line){ free(line); } //Write result FILE *foutput = fopen("output.txt", "w"); if (foutput == NULL) { printf("Error opening file!\n"); return -1; } fprintf(foutput, "BasicStatistic Result: \n"); fprintf(foutput, "%d\n", result); fclose(foutput); return 0; }
- test.txt:
- basicstatistic max -1 test.txt:
- BasicStatistic code: