Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
14
Добавлен:
17.04.2015
Размер:
3.75 Кб
Скачать
// files.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>

#define N 10
#define YES 1
#define NO 0

void vvod(float array[]);
int find_first_plus(float array[]);
int find_second_plus(float array[],int);
int get_summ(float array[],int,int);
int get_max(float array[]);
void sort(float array[]);
int readwritefile(float array[],FILE* file);

float array[N];
float readed[N];

int main()
{
	FILE* file;
	int imax,fplus,splus,j,k = 0;
	float summ,max,temp;

        file = fopen("file.txt","w");
        vvod(array);

        max = array[0];
        imax = 0;

        fplus = find_first_plus(array);
        splus = find_second_plus(array,fplus);

        imax = get_max(array);
		summ = get_summ(array,fplus,splus);

        sort(array);

		readwritefile(array,file);

        printf("MAX = %d\n",imax+1);
        printf("SUMM = %f\n",summ); 
}



int find_first_plus(float array[])
{
        int fplus = 0;
        for(int i = 0;i < N;i++)
        {
          if (array[i] > 0)
          {
            fplus = i;
            break;
          }
	}
        return fplus;
}

int find_second_plus(float array[],int fplus)
{
        int splus = 0;
        for(int i = fplus+1;i < N;i++)
        {
          if (array[i] > 0)
          {
            splus = i;
            break;
          }
	}
        return splus;
}

void vvod(float array[])
{
        for (int i = 0;i < N;i++)
        {
          printf("Vvedite element massiva %d: ",i+1);
          scanf("%f",&array[i]);
        }
}

int get_summ(float array[],int fplus,int splus)
{
        int summ = 0;
        for(int i = fplus+1;i < splus;i++)
          summ += array[i];
        return summ;
}

int get_max(float array[])
{
	int max = array[0];
        int imax = 0;
	for(int i = 1;i < N;i++)
	  if (fabs(array[i]) > fabs(max))
          {
             imax = i;
             max = array[i];
	  }
        return imax;
}

void sort(float array[])
{
        int k = 0;
        int is_zero = 1;
        for (int i = 0;i < N-k;i++)
        {
          if (array[i] == 0)
	  {
	       	for(int j = N - k - 1;j > i;j--)
        	  {
                     if (array[j] != 0)
                     {
                       for(int m = j;m >= 0;m--)   // j - 1
                       {
                         if (array[m] == 0)
						 {
							is_zero = YES;
                            break;
                         }
                         else
                           is_zero = NO;
                     }
                       if (is_zero == NO) break;
                         if (array[j] != 0 && is_zero == YES)
                            {
        	       	       printf("!\n");
	                       	array[i] = array[j];
        	                array[j] = 0;
                	        k++;
       	                	break;
			     }
                     }
		  }
       	  }
	}
}

int readwritefile(float array[],FILE* file)
{
        for(int i = 0;i < N;i++)
         fprintf(file,"%.2f%c",array[i],' ');

		char* str;
		fclose(file);
		fopen("file.txt","r");
		fseek(file,0,SEEK_END);
        int length = ftell(file);
		str = new char[length];
        int res = fseek(file,0,SEEK_SET);
		if (res != 0)
		{
			fprintf(stderr,"fseek error\n");
			getch();
			return -1;
		}
        str = fgets(str,length,file);
		if (str == NULL)
		{
			fprintf(stderr,"cannot read from file\n");
			getch();
			return -1;
		}
        fputs(str,stdout);

		delete str;
        fclose(file); 
        getch();
        return 0;
}
Соседние файлы в папке Лабораторные