Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

1. Подсчитать среднее арифметическое чисел из последовательности.



1. Подсчитать среднее арифметическое чисел из последовательности.

 

#include <stdio.h>

 

#define NO_ERROR 0

#define EMPTY_FILE -1

#define INCORRECT_DATA -2

 

int err;

 

double mean(FILE * f);

 

int main(int argc, char ** argv)

{

err = NO_ERROR;

double result;

FILE * f;

 

if (argc!= 2)

{

if (argc == 1)

{

printf("No input file\n");

}

else if (argc > 2)

{

printf("Too many parametres\n");

}

return -1;

}

 

f = fopen(argv[1], "r");

if (f == NULL)

{

printf("File does not exist\n");

return -1;

}

 

result = mean(f);

if (err!= NO_ERROR)

{

printf("Error with code %d occured, program stopped\n", err);

return -1;

}

printf("Correctly finished, the result is %lf\n", result);

return NO_ERROR;

}

 

double mean(FILE * f)

{

int r;

double temp;

int n = 0;

double s = 0;

while (!feof(f))

{

r = fscanf(f, "%lf", &temp);

if (r == 1)

{

s += temp;

++n;

}

else if (r == 0)

{

err = INCORRECT_DATA;

return -1;

}

}

if (n == 0)

{

err = EMPTY_FILE;

return 0;

}

return s / n;

}

 

2. Подсчитать среднее геометрическое чисел из последовательности.

 

#include <stdio.h>

#include <math.h>

 

#define NO_ERROR 0

#define EMPTY_FILE -1

#define INCORRECT_DATA -2

#define NEGATIVE_NUMBER -3

 

int err;

 

double geom(FILE * f);

 

int main(int argc, char ** argv)

{

err = NO_ERROR;

double result;

FILE * f;

 

if (argc!= 2)

{

if (argc == 1)

{

printf("No input file\n");

}

else if (argc > 2)

{

printf("Too many parametres\n");

}

return -1;

}

 

f = fopen(argv[1], "r");

if (f == NULL)

{

printf("File does not exist\n");

return -1;

}

 

result = geom(f);

if (err!= NO_ERROR)

{

printf("Error with code %d occured, program stopped\n", err);

return -1;

}

printf("Correctly finished, the result is %lf\n", result);

return NO_ERROR;

}

 

double geom(FILE * f)

{

int r;

double temp;

int n = 0;

double s = 1;

while (!feof(f))

{

r = fscanf(f, "%lf", &temp);

if (r == 1)

{

if (temp < 0)

{

err = NEGATIVE_NUMBER;

return -1;

}

s *= temp;

++n;

}

else if (r == 0)

{

err = INCORRECT_DATA;

return -1;

}

}

if (n == 0)

{

err = EMPTY_FILE;

return 0;

}

return pow(s, 1.0 / n);

}

 

3. Подсчитать среднее гармоническое чисел из последовательности.

 

#include <stdio.h>

 

#define NO_ERROR 0

#define EMPTY_FILE -1

#define INCORRECT_DATA -2

#define NON_POSITIVE_NUMBER -3

 

int err;

 

double harm(FILE * f);

 

int main(int argc, char ** argv)

{

err = NO_ERROR;

double result;

FILE * f;

 

if (argc!= 2)

{

if (argc == 1)

{

printf("No input file\n");

}

else if (argc > 2)

{

printf("Too many parametres\n");

}

return -1;

}

 

f = fopen(argv[1], "r");

if (f == NULL)

{

printf("File does not exist\n");

return -1;

}

 

result = harm(f);

if (err!= NO_ERROR)

{

printf("Error with code %d occured, program stopped\n", err);

return -1;

}

printf("Correctly finished, the result is %lf\n", result);

return NO_ERROR;

}

 

double harm(FILE * f)

{

int r;

double temp;

int n = 0;

double s = 0;

while (!feof(f))

{

r = fscanf(f, "%lf", &temp);

if (r == 1)

{

if (temp > 0)

{

s += 1 / temp;

++n;

}

else

{

err = NON_POSITIVE_NUMBER;

return -1;

}

}

else if (r == 0)

{

err = INCORRECT_DATA;

return -1;

}

}

if (n == 0)

{

err = EMPTY_FILE;

return 0;

}

return n / s;

}

 

4. Подсчитать количество чисел, больших предыдущего.



 

#include <stdio.h>

 

#define NO_ERROR 0

#define EMPTY_FILE -1

#define INCORRECT_DATA -2

 

int err;

 

int grow(FILE * f);

 

int main(int argc, char ** argv)

{

err = NO_ERROR;

int result;

FILE * f;

 

if (argc!= 2)

{

if (argc == 1)

{

printf("No input file\n");

}

else if (argc > 2)

{

printf("Too many parametres\n");

}

return -1;

}

 

f = fopen(argv[1], "r");

if (f == NULL)

{

printf("File does not exist\n");

return -1;

}

 

result = grow(f);

if (err!= NO_ERROR)

{

printf("Error with code %d occured, program stopped\n", err);

return -1;

}

printf("Correctly finished, the result is %d\n", result);

return NO_ERROR;

}

 

int grow(FILE * f)

{

int r;

double temp_1, temp_2;

int result = 0;

int n = 0;

 

r = fscanf(f, "%lf", &temp_1);

if (r == 1)

{

++n;

}

else if (r == 0)

{

err = INCORRECT_DATA;

return -1;

}

 

while (!feof(f))

{

temp_2 = temp_1;

r = fscanf(f, "%lf", &temp_1);

if (r == 1)

{

if (temp_1 > temp_2)

++result;

++n;

}

else if (r == 0)

{

err = INCORRECT_DATA;

return -1;

}

}

if (n == 0)

{

err = EMPTY_FILE;

return 0;

}

return result;

}

 

5. Определить есть ли в последовательности число X (для вещественных чисел — с точностью ε).

 

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

#define NO_ERROR 0

#define EMPTY_FILE -1

#define INCORRECT_DATA -2

 

int err;

double X;

 

const double epsilon = 1.0e-15;

 

int cont(FILE * f);

 

int main(int argc, char ** argv)

{

err = NO_ERROR;

int result;

FILE * f;

char * end_ptr;

 

if (argc!= 3)

{

if (argc == 1)

{

printf("No input file\n");

}

else if (argc == 2)

{

printf("No standard number\n");

}

else if (argc > 3)

{

printf("Too many parametres\n");

}

return -1;

}

 

X = strtod(argv[2], &end_ptr);

if (*end_ptr)

{

printf("Incorrect standarrd number\n");

return -1;

}

 

f = fopen(argv[1], "r");

if (f == NULL)

{

printf("File does not exist\n");

return -1;

}

 

result = cont(f);

 

if (err!= NO_ERROR)

{

printf("Error with code %d occured, program stopped\n", err);

return -1;

}

if (result == 1)

printf("Correctly finished, row contains %lf\n", X);

else

printf("Correctly finished, row does not contain %lf\n", X);

return NO_ERROR;

}

 

int cont(FILE * f)

{

double temp;

int r;

int n = 0;

while (!feof(f))

{

r = fscanf(f, "%lf", &temp);

if (r == 1)

{

if (fabs(temp - X) < epsilon)

return 1;

++n;

}

else if (r == 0)

{

err = INCORRECT_DATA;

return -1;

}

}

if (n == 0)

{

err = EMPTY_FILE;

return -1;

}

return 0;

}

 

6. Определить номер последнего числа, равного X (для вещественных чисел — с точностью ε).

 

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

#define NO_ERROR 0

#define EMPTY_FILE -1

#define INCORRECT_DATA -2

 

int err;

double X;

 

const double epsilon = 1.0e-15;

 

int last(FILE * f);

 

int main(int argc, char ** argv)

{

err = NO_ERROR;

int result;

FILE * f;

char * end_ptr;

 

if (argc!= 3)

{

if (argc == 1)

{

printf("No input file\n");

}

else if (argc == 2)

{

printf("No standard number\n");

}

else if (argc > 3)

{

printf("Too many parametres\n");

}

return -1;

}

 

X = strtod(argv[2], &end_ptr);

if (*end_ptr)

{

printf("Incorrect standard number\n");

return -1;

}

 

f = fopen(argv[1], "r");

if (f == NULL)

{

printf("File does not exist\n");

return -1;

}

 

result = last(f);

 

if (err!= NO_ERROR)

{

printf("Error with code %d occured, program stopped\n", err);

return -1;

}

if (result == -1)

printf("Correctly finished, there are no elements equal to %lf\n", X);

else

printf("Correctly finished, last element equal to %lf has number %d\n", X, result);

return NO_ERROR;

}

 

int last(FILE * f)

{

double temp;

int r;

int n = 0;

int result = -1;

while (!feof(f))

{

r = fscanf(f, "%lf", &temp);

if (r == 1)

{

if (fabs(temp - X) < epsilon)

result = n;

++n;

}

else if (r == 0)

{

err = INCORRECT_DATA;

return -1;

}

}

if (n == 0)

{

err = EMPTY_FILE;

return -1;

}

return result;

}

 

7. Определить все ли элементы последовательности равны между собой (для вещественных чисел — с точностью ε).

 

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

#define NO_ERROR 0

#define EMPTY_FILE -1

#define INCORRECT_DATA -2

 

int err;

 

const double epsilon = 1.0e-15;

 

int stat(FILE * f);

 

int main(int argc, char ** argv)

{

err = NO_ERROR;

int result;

FILE * f;

char * end_ptr;

 

if (argc!= 2)

{

if (argc == 1)

{

printf("No input file\n");

}

else if (argc > 2)

{

printf("Too many parametres\n");

}

return -1;

}

 

f = fopen(argv[1], "r");

if (f == NULL)

{

printf("File does not exist\n");

return -1;

}

 

result = stat(f);

 

if (err!= NO_ERROR)

{

printf("Error with code %d occured, program stopped\n", err);

return -1;

}

if (result == 1)

printf("Correctly finished, all elements are equal\n");

else

printf("Correctly finished, there are some different elements\n");

return NO_ERROR;

}

 

int stat(FILE * f)

{

double temp;

double first;

int r;

int n = 0;

 

r = fscanf(f, "%lf", &first);

if (r == 1)

{

++n;

}

else if (r == 0)

{

err = EMPTY_FILE;

return -1;

}

 

while (!feof(f))

{

r = fscanf(f, "%lf", &temp);

if (r == 1)

{

if (fabs(temp - first) > epsilon)

return 0;

++n;

}

else if (r == 0)

{

err = INCORRECT_DATA;

return -1;

}

}

if (n == 0)

{

err = EMPTY_FILE;

return -1;

}

return 1;

}

 

8. Определить является ли последовательность возрастающей, убывающей?

 

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

#define NO_ERROR 0

#define EMPTY_FILE -1

#define INCORRECT_DATA -2

 

int err;

 

const double epsilon = 1.0e-15;

 

int stat(FILE * f);

 

int main(int argc, char ** argv)

{

err = NO_ERROR;

int result;

FILE * f;

char * end_ptr;

 

if (argc!= 2)

{

if (argc == 1)

{

printf("No input file\n");

}

else if (argc > 2)

{

printf("Too many parametres\n");

}

return -1;

}

 

f = fopen(argv[1], "r");

if (f == NULL)

{

printf("File does not exist\n");

return -1;

}

 

result = stat(f);

 

if (err!= NO_ERROR)

{

printf("Error with code %d occured, program stopped\n", err);

return -1;

}

if (result == 1)

printf("Correctly finished, all elements are equal\n");

else

printf("Correctly finished, there are some different elements\n");

return NO_ERROR;

}

 

int stat(FILE * f)

{

double temp;

double first;

int r;

int n = 0;

 

r = fscanf(f, "%lf", &first);

if (r == 1)

{

++n;

}

else if (r == 0)

{

err = EMPTY_FILE;

return -1;

}

 

while (!feof(f))

{

r = fscanf(f, "%lf", &temp);

if (r == 1)

{

if (fabs(temp - first) > epsilon)

return 0;

++n;

}

else if (r == 0)

{

err = INCORRECT_DATA;

return -1;

}

}

if (n == 0)

{

err = EMPTY_FILE;

return -1;

}

return 1;

}


Дата добавления: 2015-11-05; просмотров: 17 | Нарушение авторских прав




<== предыдущая лекция | следующая лекция ==>
Club of Intercultural Communication “Alliance” | system(title Моя программа!);

mybiblioteka.su - 2015-2024 год. (0.079 сек.)