Ejemplo n.º 1
0
// reads in a bit-map file on construction
bmp::bmp(const char * f) : filename(f) {
   FILE * fp = fopen(filename, "rb");
   
   if (fp==NULL) { printf("Error: couldn't open file %s for reading, exiting.\n", f); exit(-1); }

   header.read(fp);
   infoheader.read(fp);

   // simplify my life by only considering uncompressed images
   assert(infoheader.compression == 0);

   // so for 3 bytes per pixel (RGB)
   array = new unsigned char[3 * infoheader.width * infoheader.height];


   // Need a colortable if <= 8 bits per pixel
   if (infoheader.bitcount <= 8) {
      colortable.read(infoheader.bitcount, fp);
      readarray(&colortable, fp);      
   } else {
      // 24-bit color does not need colortable
      assert(infoheader.bitcount == 24); // Not 16 or 32 (or other!)
      readarray(NULL, fp);
   }
   if (fp) { fclose (fp); }
}
std::vector<iArray*> getTestIndividuals(const char *pName){
  iArray *testList = typecast_stringarray_to_iArray(readarray(std::string(pName)));
  
  int numTrues=0;
  for(int i=0;i<testList->x;i++)
    if(testList->array[i])
      numTrues++;
  //printf("numTrues:%d\n",numTrues);
  //testList->print("testlist",NULL);
  std::vector<iArray*> returnVal;
  int pos1 = getNext(testList,0);
  while(pos1!=-1){
    int pos2 = getNext(testList,pos1+1);
    while(pos2!=-1){
      if(pos2!=-1){
	//printf("p1:%d\tp2:%d\n",pos1,pos2);
	iArray *pr = allocIntArray(2);
	pr->array[0]=pos1;pr->array[1]=pos2;
	returnVal.push_back(pr);
      }
      pos2 = getNext(testList,pos2+1);
    }
    pos1 = getNext(testList,pos1+1);
  }
  killArray(testList);
  return returnVal;

}
Ejemplo n.º 3
0
main()
{
	int size_local;
	double most, least;

	double price_list[MAX_PRICES];
	
	printf("\nPrices Before Sorting of Array:");	
	readarray(price_list, &size_local);

	sortarray(price_list, size_local, &most, &least);
	
	printf("\n\nPrices After Sorting of Array:");
	displayarray(price_list, size_local); 
	printf("\n");

	display(most, least);
}
Ejemplo n.º 4
0
static int
readdata (char* file,
          int *nfoods_p, double **cost_p, double **lb_p, double **ub_p, 
          int *nnutr_p, double **nutrmin_p, double **nutrmax_p,
          double ***nutrper_p)
{
   int status = 0;

   int ncost, nlb, nub;
   int nmin, nmax;

   int  i, n;
   char ch;
   FILE *in = NULL;

   in = fopen(file, "r");
   if ( in == NULL ) {
      status = -1;
      goto TERMINATE;
   }

   if ( (status = readarray(in, &ncost, cost_p)) ) goto TERMINATE;
   if ( (status = readarray(in, &nlb,   lb_p))   ) goto TERMINATE;
   if ( (status = readarray(in, &nub,   ub_p))   ) goto TERMINATE;
   if ( ncost != nlb  ||  ncost != nub ) {
      status = -1;
      goto TERMINATE;
   }
   *nfoods_p = ncost;

   if ( (status = readarray(in, &nmin, nutrmin_p)) ) goto TERMINATE;
   if ( (status = readarray(in, &nmax, nutrmax_p)) ) goto TERMINATE;
   if ( nmax != nmin ) {
      status = -1;
      goto TERMINATE;
   }
   *nnutr_p = nmin;

   *nutrper_p = (double**)malloc(nmin * sizeof(double*));
   if ( *nutrper_p == NULL ) {
      status = CPXERR_NO_MEMORY;
      goto TERMINATE;
   }

   for (;;) {
      fscanf (in, "%c", &ch);
      if ( ch == '\t' ||
           ch == '\r' ||
           ch == ' '  ||
           ch == '\n'   ) continue;
      if ( ch == '[' ) break;
      status = -1;
      goto TERMINATE;
   }
   for ( i = 0; i < nmin; i++ ) {
      if ( (status = readarray(in, &n, (*nutrper_p)+i)) ) goto TERMINATE;
      if ( n != ncost ) {
         status = -1;
         goto TERMINATE;
      }
      fscanf (in, "%c", &ch);
      if ( i < nmin-1  &&  ch != ',' ) {
         status = -1;
         goto TERMINATE;
      }
   }
   if ( ch != ']' ) {
      status = -1;
      goto TERMINATE;
   }


TERMINATE:
   if ( in != NULL )
      fclose (in);

   return (status);

} /* END readdata */
Ejemplo n.º 5
0
int main(int argc, char* argv[])
{
    FILE *f;        // исходный файл
    FILE *f1;       // файл для печати
    int *n=0, *c=0; // указатель на начало и после конца массива
    int *m=0, *d=0; // указатель на начало и после конца отфилтрованного массива
    setlocale(0, "russian");

    if (argc < 4)
    {
        printf("Нет достаточного числа аргументов!");
        return BADARG;
    }
    f = fopen(argv[1], "r");
    if (f == NULL)
    {
        printf("Невозможно открыть исходный файл!");
        return BADINFILE;
    }
    if (readarray(f, &n, &c))
        {
            printf("Невозможно выделить память под исходный массив!");
            fclose(f);
            return FAILEDINMEM;
	}
    f1 = fopen(argv[2], "w");
    if (f1 == NULL)
    {
        printf("Невозможно открыть файл результата!");\
        fclose(f);
        free(n);
        return BADOUTFILE;
    }

    if (strcmp(argv[3], "-flt") == 0)
    {
        if (fltr(n, c, &m, &d))
        {
            printf("Невозможно выделить память под отфильтрованный массив!");
            fclose(f);
            fclose(f1);
            free(n);
            return FAILEDOUTMEM;
        }
        myqsort(m, d - m, sizeof(*m), compare_int);
        printmass(f1, m, d);
        free(n);
        free(m);
    }
    else
    {
        myqsort(n, c - n, sizeof(*n), compare_int);
        printmass(f1, n, c);
        free(n);
    }
    fclose(f);
    fclose(f1);
    printf("Успешно!");
    getch();
    return 0;
}
Ejemplo n.º 6
0
int main(void)
{
    int *n, *c;           // указатель на начало и после конца массива
    int k;                // количество элементов в файле
    int a;                // значение readarray
    int d;                // детектор
    FILE *f;
    setlocale(0, "russian");
    printf("Тестирование функции count из readarray.c");
    f = fopen("tests/test1.txt", "r");
    count(f,&k);
    if (k==4)
        printf("\nTest 1 - OK!");
    else
        printf("\nTest 1 - FAILED");
    fclose(f);

    f = fopen("tests/test2.txt", "r");
    count(f,&k);
    if (k==4)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED");
    fclose(f);

    f = fopen("tests/test3.txt", "r");
    count(f,&k);
    if (k==10)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED");
    fclose(f);

    f = fopen("tests/test4.txt", "r");
    count(f,&k);
    if (k==10)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED");
    fclose(f);

    f = fopen("tests/test5.txt", "r");
    count(f,&k);
    if (k==8)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED");
    fclose(f);

    f = fopen("tests/test6.txt", "r");
    count(f,&k);
    if (k==0)
        printf("\nTest 6 - OK!");
    else
        printf("\nTest 6 - FAILED");
    fclose(f);

    f = fopen("tests/test7.txt", "r");
    count(f,&k);
    if (k==7)
        printf("\nTest 7 - OK!");
    else
        printf("\nTest 7 - FAILED");
    fclose(f);

    f = fopen("tests/test8.txt", "r");
    count(f,&k);
    if (k==5)
        printf("\nTest 8 - OK!");
    else
        printf("\nTest 8 - FAILED");
    fclose(f);

    f = fopen("tests/test9.txt", "r");
    count(f,&k);
    if (k==1)
        printf("\nTest 9 - OK!");
    else
        printf("\nTest 9 - FAILED");
    fclose(f);

    f = fopen("tests/test10.txt", "r");
    count(f,&k);
    if (k==1)
        printf("\nTest 10 - OK!");
    else
        printf("\nTest 10 - FAILED");
    fclose(f);

    f = fopen("tests/test11.txt", "r");
    count(f,&k);
    if (k==3)
        printf("\nTest 11 - OK!");
    else
        printf("\nTest 11 - FAILED");
    fclose(f);

    f = fopen("tests/test12.txt", "r");
    count(f,&k);
    if (k==0)
        printf("\nTest 12 - OK!");
    else
        printf("\nTest 12 - FAILED");
    fclose(f);

    printf("\n\nТестирование функции readarray из readarray.c");
    f = fopen("tests/test1.txt", "r");
    d = 0;
    if (!(a=readarray(f, &n, &c)))
    {
        if (*n != 10) {d++;}
        if (*(n+1) !=1) {d++;}
        if (*(n+2) !=1) {d++;}
        if (*(n+3) !=1) {d++;}
    }
    if ((a == 0) && (d == 0))
        printf("\nTest 1 - OK!");
    else
        printf("\nTest 1 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test2.txt", "r");
    d = 0;
    if (!(a=readarray(f, &n, &c)))
    {
        if (*n != 55) {d++;}
        if (*(n+1) !=55) {d++;}
        if (*(n+2) !=55) {d++;}
        if (*(n+3) !=789) {d++;}
    }
    if ((a == 0) && (d == 0))
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test7.txt", "r");
    if ((readarray(f, &n, &c)) || (n-c > 0))
        printf("\nTest 3 - FAILED!%d", n-c);
    else
        printf("\nTest 3 - OK!");
    free(n);
    fclose(f);


    printf("\n\nТестирование функции unique из podsch.c");
    f = fopen("tests/test1.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 2)
        printf("\nTest 1 - OK!");
    else
        printf("\nTest 1 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test2.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 2)
        printf("\nTest 2 - OK!");    
    else
        printf("\nTest 2 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test3.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 5)
        printf("\nTest 3 - OK!");   
    else
        printf("\nTest 3 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test4.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 6)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test5.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 2)
        printf("\nTest 5 - OK!");    
    else
        printf("\nTest 5 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test6.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 0)    
        printf("\nTest 6 - OK!");    
    else
        printf("\nTest 6 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test7.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 1)
        printf("\nTest 7 - OK!");
    else
        printf("\nTest 7 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test8.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 1)
        printf("\nTest 8 - OK!");
    else
        printf("\nTest 8 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test9.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 1)
        printf("\nTest 9 - OK!");
    else
        printf("\nTest 9 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test10.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 1)
        printf("\nTest 10 - OK!");
    else
        printf("\nTest 10 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test11.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 3)
        printf("\nTest 11 - OK!");
    else
        printf("\nTest 11 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test12.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 0)
        printf("\nTest 12 - OK!");
    else
        printf("\nTest 12 - FAILED!");
    free(n);
    fclose(f);

    f = fopen("tests/test13.txt", "r");
    readarray(f, &n, &c);
    if (unique(n, c) == 5)
        printf("\nTest 13 - OK!");
    else
        printf("\nTest 13 - FAILED!");
    free(n);
    fclose(f);


    getch();
    return 0;
}
dArray *getPos(const char *pName){
  return (typecast_stringarray_to_dArray(readarray(std::string(pName))));
}
iArray *getChr(const char *pName){
  return typecast_stringarray_to_iArray(readarray(std::string(pName)));

}