Skip to content

Instantly share code, notes, and snippets.

@vertrigo
Created December 4, 2012 16:39
Show Gist options
  • Select an option

  • Save vertrigo/4205948 to your computer and use it in GitHub Desktop.

Select an option

Save vertrigo/4205948 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main(void)
{
FILE *f,*g;
f=fopen("lab8.dat","w");
printf("Вводите целые числа, признак конца - 0:\n");
int k;
float x;
do {
scanf("%i",&k);
if (k!=0)
{
x=(pow(-1,k)*pow(0.3,k))/(k+1);
fwrite(&x,sizeof(x),1,f);
}
} while (k);
fclose(f);
printf("Содержимое файла:\n");
if ((f=fopen("lab8.dat","r"))==NULL)
{
printf("Ошибка при открытии файла!\n");
return;
}
g=fopen("lab8.txt","w");
fprintf(g,"Содержимое файла:\n");
float s=0;
do {
fread(&x,sizeof(x),1,f);
if (!feof(f))
{
printf("%8.3f",x);
fprintf(g,"%8.3f",x);
s+=x;
}
} while (!feof(f));
printf("\nСумма = %8.4f\n",s);
fprintf(g,"\nСумма = %8.4f\n",s);
fclose(f); fclose(g);
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment