Тема: Структура на С. Робота з векторами
задача має вигляд:
введення координат вектора з файла(клавіатури);
вивід координат вектора в файл (на екран);
сформувати координати вектора за формулами (використовувати вказівник на функцію).
Вибиває помилку.(
Чи можливо записати цю програму в іншому вигляді.......
#ifndef __SMATRIX_H
#define __SMATRIX_H
typedef struct Vector
{
unsigned int VectorSize;
double *Data;
} SVector;
typedef struct Matrix
{
unsigned int Row;
unsigned int Col;
double **Data;
}SMatrix;
SVector VectorCreate(int);
void GetVector(SVector*);
void VectorDisplay(SVector);
SVector VectorAdd(SVector,SVector);
SVector VectorDiff(SVector,SVector);
double VectorScalar(SVector,SVector);
SVector VectorMultConst(SVector,double);
void VectorInFile(char*,const SVector);
SVector VectorOutFile(char*);
SVector VectorPtr(SVector v,double(*func)(double));
double Function(double);
void VectorDelete(SVector*);
SMatrix MatrixCreate(unsigned int row, unsigned int col);
void GetMatrix(SMatrix*);
void DisplayMatrix(SMatrix);
SMatrix MakeEMatrix(SMatrix);
SMatrix MatrixMMMult(SMatrix,SMatrix);
SMatrix MatrixMMAdd(SMatrix,SMatrix);
SMatrix MatrixMMDiff(SMatrix,SMatrix);
SMatrix MatrixMVMult(SMatrix m,SVector v);
void MatrixDelete(SMatrix*);
SVector VectorCopy(SVector);
SMatrix MatrixCopy(SMatrix);
#endif
#include <stdlib.h>
#include <stdio.h>
#include "smatrix.h"
SVector nullvector = {0,NULL};
SMatrix nullmatrix = {0,0,NULL};
SVector VectorCreate(int NumElement)
{
SVector v;
v.VectorSize=NumElement;
v.Data=calloc(NumElement,sizeof(double));
return v;
}
void GetVector(SVector *v)
{
int i,test;
for(i=0;i<v->VectorSize;i++)
{
test = scanf("%lf",&v->Data[i]);
if(test != 1)
exit(1);
}
}
void VectorDisplay(SVector v1)
{
int i;
printf("\nvector[%d]=",v1.VectorSize);
for(i=0;i<v1.VectorSize;i++)
printf(" %g",v1.Data[i]);
}
SVector VectorAdd(SVector v1,SVector v2)
{
if(v1.VectorSize != v2.VectorSize)
return nullvector;
SVector v3;
v3 = VectorCreate(v1.VectorSize);
int i;
for(i=0;i<v3.VectorSize;i++)
v3.Data[i] = v1.Data[i] + v2.Data[i];
return v3;
}
SVector VectorDiff(SVector v1,SVector v2)
{
if(v1.VectorSize != v2.VectorSize)
return nullvector;
SVector v3;
v3 = VectorCreate(v1.VectorSize);
int i;
for(i=0;i<v3.VectorSize;i++)
v3.Data[i] = v1.Data[i] - v2.Data[i];
return v3;
}
double VectorScalar(SVector v1,SVector v2)
{
if(v1.VectorSize != v2.VectorSize)
return 0;
double res=0;
int i;
for(i=0;i<v1.VectorSize;i++)
res += v1.Data[i] * v2.Data[i];
return res;
}
SVector VectorMultConst(SVector v1,double c)
{
SVector v3;
v3 = VectorCreate(v1.VectorSize);
int i;
for(i=0;i<v3.VectorSize;i++)
v3.Data[i] = v1.Data[i] * c;
return v3;
}
void VectorInFile(char *nameoffile,const SVector v1)
{
unsigned int i;
FILE *txtFile;
if((txtFile=fopen(nameoffile,"w"))==NULL)
{
printf("Can not open file");
exit(1);
}
fprintf(txtFile,"%d ",v1.VectorSize);
for(i=0;i<v1.VectorSize;i++)
fprintf(txtFile,"%g ",v1.Data[i]);
fclose(txtFile);
}
SVector VectorOutFile(char *nameoffile)
{
unsigned int i=0,test;
SVector v3;
FILE *txtFile;
if((txtFile = fopen(nameoffile,"r"))==NULL)
{
printf("\nCan not open file \"%s\"",nameoffile);
exit(1);
}
test = fscanf(txtFile,"%d",&v3.VectorSize);
if(test != 1)
{
printf("\nCan\'t read first number (size of vector)!");
exit(2);
}
if(!v3.VectorSize)
{
printf("\nSize of vector = 0, error!");
exit(1);
}
v3.Data = calloc(v3.VectorSize,sizeof(double));
while(i<v3.VectorSize)
{
fscanf(txtFile,"%lf",&v3.Data[i]);
i++;
}
fclose(txtFile);
return v3;
}
SVector VectorPtr(SVector v,double(*func)(double))
{
SVector v3 = VectorCreate(v.VectorSize);
int i;
for(i=0;i<v.VectorSize;i++)
v3.Data[i] = (*func)(v.Data[i]);
return v3;
}
double Function(double x)
{
return x*=x+1;
}
void VectorDelete(SVector *v)
{
free(v->Data);
}
SMatrix MatrixCreate(unsigned int row,unsigned int col)
{
int i;
SMatrix m;
m.Data = (double **)calloc(row,sizeof(double*));
for(i=0;i<row;++i)
m.Data[i] = calloc(col,sizeof(double));
m.Row = row;
m.Col = col;
return m;
}
void GetMatrix(SMatrix *m)
{
printf("\nGet matrix:\n");
int i,j,test;
for(i=0;i<m->Row;i++)
{
for(j=0;j<m->Col;j++)
{
test = scanf("%lf",&m->Data[i][j]);
if(test != 1)
exit(1);
}
printf("\n");
}
}
void DisplayMatrix(SMatrix m)
{
printf("\n");
int i,j;
for(i=0;i<m.Row;i++)
{
for(j=0;j<m.Col;j++)
printf("%g\t",m.Data[i][j]);
printf("\n");
}
}
SMatrix MakeEMatrix(SMatrix m1)
{
int i,j;
SMatrix m3;
m3 = MatrixCreate(m1.Row,m1.Col);
for(i=0;i<m3.Row;i++)
for(j=0;j<m3.Col;j++)
m3.Data[i][j] = ((i==j)? 1.0:0.0);
return m3;
}
SMatrix MatrixMMMult(SMatrix m1,SMatrix m2)
{
int i,j,l;
if(m1.Col != m2.Row)
{
printf("\nUmnojenie nevozmojno, tak kak m1.Col != m2.Row");
return nullmatrix;
}
SMatrix m3;
m3.Data = (double **)calloc(m1.Row,sizeof(double*));
for(i=0;i<m1.Row;++i)
m3.Data[i] = calloc(m2.Col,sizeof(double));
for(i = 0; i < m1.Row; i++)
for(j = 0; j < m1.Row; j++)
{
m3.Data[i][j] = 0;
for(l = 0; l < m2.Col; l++)
m3.Data[i][j] += m1.Data[i][l]
* m2.Data[l][j];
}
m3.Row = m1.Row;
m3.Col = m2.Col;
return m3;
}
SMatrix MatrixMMAdd(SMatrix m1,SMatrix m2)
{
if((m1.Row != m2.Row) || (m1.Col != m2.Col))
return nullmatrix;
int i, j;
SMatrix m3;
m3.Data = (double **)calloc(m1.Row,sizeof(double*));
for(i=0;i<m1.Row;++i)
m3.Data[i] = calloc(m1.Col,sizeof(double));
for(i=0;i<m1.Row;++i)
{
for(j=0;j<m1.Col;++j)
{
m3.Data[i][j]=m1.Data[i][j]+m2.Data[i][j];
}
}
m3.Row = m1.Row;
m3.Col = m1.Col;
return m3;
}
SMatrix MatrixMMDiff(SMatrix m1,SMatrix m2)
{
if((m1.Row != m2.Row) || (m1.Col != m2.Col))
return nullmatrix;
int i, j;
SMatrix m3;
m3.Data = (double **)calloc(m1.Row,sizeof(double*));
for(i=0;i<m1.Row;++i)
m3.Data[i] = calloc(m1.Col,sizeof(double));
for(i=0;i<m1.Row;++i)
{
for(j=0;j<m1.Col;++j)
{
m3.Data[i][j]=m1.Data[i][j]-m2.Data[i][j];
}
}
m3.Row = m1.Row;
m3.Col = m1.Col;
return m3;
}
SMatrix MatrixMVMult(SMatrix m,SVector v)
{
int i,j,l;
if(m.Col != 1)
return nullmatrix;
SMatrix m3;
m3 = MatrixCreate(m.Row,v.VectorSize);
for(i = 0; i < m.Row; i++)
for(j = 0; j < m.Row; j++)
{
m3.Data[i][j] = 0;
for(l = 0; l < v.VectorSize; l++)
m3.Data[i][j] += m.Data[i][l] * v.Data[l];
}
m3.Row = m.Row;
m3.Col = v.VectorSize;
return m3;
}
void MatrixDelete(SMatrix *m)
{
int i;
for(i=0;i<m->Row;i++)
free(m->Data[i]);
free(m->Data);
}