Приложение г
Исходный код программы 5.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
const n=3;
long proizved (int A[n][n]);
void test();
void test ()
{
int A[n][n]={{1,1,1},{2,2,2},{3,3,3}};
long result =proizved(A);
if (result!=36)
{
printf ("error function \"proizved\"");
if (!getch())
getch();
exit (1);
}
}
void main()
{
clrscr();
test();
long result;
int A[3][3]={{1,2,3},{1,2,3},{1,2,3}};
result = proizved (A);
printf ("\n result=%d",result);
if (!getch())
getch();
}
long proizved (int A[n][n])
{
int j;
j=n-1;
long sumg = 0, sump=0;
for (int i = 0; i < n; i++)
{
sumg+=A[i][i];
sump+=A[i][j];
j--;
}
sump*=sumg;
return sump;
}
Приложение д
Исходный код программы 8.
#include <string.h>
void OutErr(int numErr);
void main(int argc,char *argv[])
{
clrscr();
int count=0, buf=0;
char str [127];
FILE *fold, *fnew;
if((fold=fopen(argv[1],"rt"))==NULL)
{
OutErr(1);
}
while (!feof(fold))
{
if (fgets(str,125,fold))
{
printf ("%s",str);
count++;
}
}
printf("\nKolichestvo strok v file %d\n",count);
buf=count-3;
if((fnew=fopen(argv[2],"wt"))==NULL)
{
OutErr(2);
}
fclose(fold);
if((fold=fopen(argv[1],"rt"))==NULL)
{
OutErr(1);
}
for (int i=0;i<count;i++)
{
fgets(str,125,fold);
if (i>=buf)
{
fprintf(fnew, "%s",str);
printf ("%s",str);
}
}
fcloseall();
if (!getch())
getch();
}
void OutErr (int numErr)
{
printf ("Error #%d",numErr);
if (!getch())
getch();
exit(numErr);
}
Приложение ж
Исходный код программы 12.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void test ();
float f(float x);
float integralp (float a, float b, float (*pf)(float));
float integralt (float a, float b, float (*pf)(float));
//----------------------------------------------------------------
float f (float x)
{
return (log(1.5+tan(x/2)));
}
//----------------------------------------------------------------
float integralp (float a, float b, float (*pf)(float))
{
float h=(b-a)/10000;
float s=0;
for (int i=0;i<10000;i++)
{
s+=pf(a+(i-1)*h+h/2)*h;
}
//printf ("pryamoug %f",s);
return s;
}
//-----------------------------------------------------------------------
float integralt (float a, float b, float (*pf)(float))
{
float h=(b-a)/1000;
float s=0;
for (int i=0;i<1000;i++)
{
s+=pf(a+i*h)*h;
}
return s;
}
//-----------------------------------------------------------------------
float f1(float x)
{
return x;
}
//-----------------------------------------------------------------------
void test()
{
if (fabs(integralt(0,1,f1)-0.5)>1e-3)
printf ("error1");
if (fabs(integralp(0,1,f1)-0.5)>1e-3)
printf ("error1");
}
//-----------------------------------------------------------------------
void main ()
{
test();
clrscr();
float p=0,t=0;
p=integralp(0,2,f);
t=integralt(0,2,f);
printf ("p=%f, t=%f",p,t);
if (!getch())
getch();
}