0% found this document useful (0 votes)
8 views

My Array

Uploaded by

nanco8692
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

My Array

Uploaded by

nanco8692
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <iostream>

#include <stdio.h>
#include <fstream>

using namespace std;


struct MyArray{
int *a;
int num;
};
struct MyMatrix{
int **a;
int row,col;
};
//----------------- Array ---------------------------
void initialArray(MyArray &my);
void printArray(MyArray my);
void deleteArray(MyArray &my);
MyArray inputArray();
MyArray inputArrayFromFile(char *st,char ch);
//-------------------- Matrix -------------------
void initialMatrix(MyMatrix &a);
MyMatrix inputMatrix();
MyMatrix inputMatrixFromFile(char*, char);
void outputMatrix(MyMatrix a);
void deleteMatrix(MyMatrix &a);
long sumValueMatrix(MyMatrix);
int main() {
cout<<"Matrix program!\n";
MyMatrix mma;
initialMatrix(mma);
//mma=inputMatrix();
mma = inputMatrixFromFile("c:\\test\\c\\inputdata.txt", '0');
outputMatrix(mma);
cout<<"\nSum of all values from matrix = "<<sumValueMatrix(mma);
deleteMatrix(mma);
return 0;
}
MyMatrix inputMatrix(){
int m,n;
MyMatrix ar;
int **my;
cout<<"\nNumber of rows = ";cin>>m;
cout<<"Number of cols = ";cin>>n;
if(m<=0 || n<=0){
cout<<"Input data is error!\n";
exit(0);
}
ar.row=m;
ar.col=n;
my=new int*[m];
if(!my){
cout<<"Not enough memory!\n";
exit(0);
}
for(int i=0;i<m;i++){
my[i] = new int [n];
if(!my[i]){
cout<<"Not enough memory!\n";
exit(0);
}
}
int tmp;
cout<<"\n";
for(int i=0;i<m;i++)
for(int j=0;j<n;j++){
cout<<"A["<<i<<","<<j<<"]=";
cin>>tmp;
my[i][j]=tmp;
}
ar.a = my;
return ar;
}
void outputMatrix(MyMatrix my){
for(int i=0;i<my.row;i++) {
cout << "\n";
for (int j = 0; j <my.col; j++) {
cout << my.a[i][j];
cout << "\t";
}
}
}

void deleteMatrix(MyMatrix &my){


for(int i=0;i<my.col;i++)
if(my.a[i])delete [] my.a[i];
if(my.a)delete []my.a;
}
void initialMatrix(MyMatrix &my){
my.row = my.col= -1;
my.a = NULL;

}
long sumValueMatrix(MyMatrix my){
long sum =0;
for(int i=0;i<my.row;i++)
for(int j=0;j<my.col;j++)
sum+=my.a[i][j];
return sum;
}
MyMatrix inputMatrixFromFile(char* st, char mode){
// read data from file,
// st is the path and name of file
//mode is kind of file:
// 0:text and have the number of data
// 1:text and don't have the number of data
// 2:binary, each items was written
// 3: binary, all data was written
FILE *fr;
MyMatrix my;
int tmp, row_f;
initialMatrix(my);
char *moderead=NULL;
switch (mode){
case '0': case '1':strcpy(moderead,"rt");break;
case '2': case '3':cout<<"The number of rows is
";cin>>row_f;strcpy(moderead,"rb");break;
default : {cout<<"\nMode is wrong!";return my;}
}
fr = fopen(st,moderead);
if(!fr){
cout<<"\nCan not read file";
exit(0);
}
switch (mode){
case '0':{
// having the number of rows and cols at first line
fscanf(fr,"%d",&tmp);
if(tmp<=0){
cout<<"\nData is error!";
fclose(fr);
return my;
}
my.row = tmp;
fscanf(fr,"%d",&tmp);
if(tmp<=0){
cout<<"\nData is error!";
fclose(fr);
return my;
}
my.col = tmp;

//create memory
int **ma;
ma=new int*[my.row];
if(!ma){
cout<<"Not enough memory!\n";
fclose(fr);
exit(0);
}
for(int i=0;i<my.row;i++){
ma[i] = new int [my.col];
if(!ma[i]){
cout<<"Not enough memory!\n";
fclose(fr);
exit(0);
}
}
my.a = ma;
//check the number of data is enough, it mean equal my.row*my.col
int num = my.row*my.col, i=0;
while(!feof(fr)&&i<num){
fscanf(fr, "%d", &tmp);
my.a[i/my.row][i%my.row]=tmp;
i++;
}
if(i<num-1){
cout<<"\nData is error!";
fclose(fr);
deleteMatrix(my);
}
fclose(fr);
}
case '1':{
// text file don't have number of rows and cols
int count = 0;
while(!feof(fr)){
fscanf(fr,"%d",&tmp);
count++;
}
count--;
fseek(fr,0L,0);
int lines = 0;
char ch;
fscanf(fr,"%d",&tmp);
while(!feof(fr)){
ch = getc(fr);
if(ch=='\n')lines++;
fscanf(fr,"%d",&tmp);
}
if(count%lines){
cout<<"Data is error!";
fclose(fr);
exit(0);
}
fseek(fr,0L,0);
my.row = lines;
my.col = count/lines;
int **ma;
ma=new int*[my.row];
if(!ma){
cout<<"Not enough memory!\n";
fclose(fr);
exit(0);
}
for(int i=0;i<my.row;i++){
ma[i] = new int [my.col];
if(!ma[i]){
cout<<"Not enough memory!\n";
fclose(fr);
exit(0);
}
}
my.a = ma;
count = 0;
while(!feof(fr)){
fscanf(fr,"%d",&tmp);
my.a[count/my.row][count%my.row]=tmp;
count++;
}
fclose(fr);
}
case '2':{
// read binary file, each item is integer was written
// we know the number of rows
int count = 0;
int tmp;
fseek( fr, 0, SEEK_END );
int fsize = ftell( fr );
my.row = row_f;
my.col = fsize/sizeof(int)/my.row;
fseek( fr, 0, 0 );
int **ma;
ma=new int*[my.row];
if(!ma){
cout<<"Not enough memory!\n";
fclose(fr);
exit(0);
}
for(int i=0;i<my.row;i++){
ma[i] = new int [my.col];
if(!ma[i]){
cout<<"Not enough memory!\n";
fclose(fr);
exit(0);
}
}
my.a = ma;
while(!feof(fr)){
fread(&tmp,sizeof(int), 1,fr);
my.a[count/my.row][count%my.row] = tmp;
count++;

}
fclose(fr);

}
case '3':{
// read binary file, all items that each is integer were written
fseek( fr, 0, SEEK_END );
int fsize = ftell( fr );
my.row = row_f;
my.col = fsize/sizeof(int)/my.row;
fseek( fr, 0, 0 );
int **ma;
ma=new int*[my.row];
if(!ma){
cout<<"Not enough memory!\n";
fclose(fr);
exit(0);
}
for(int i=0;i<my.row;i++){
ma[i] = new int [my.col];
if(!ma[i]){
cout<<"Not enough memory!\n";
fclose(fr);
exit(0);
}
}
my.a = ma;
fread(&my.a,sizeof(int),my.row*my.col,fr);
fclose(fr);
}

return my;
}
//----------------------------- Array --------------------
void initialArray(MyArray &my){
my.num=-1;
my.a=NULL;
}
void printArray(MyArray my){
cout<<"\n Values of Array\n";
for(int i=0;i<my.num;i++)
cout<<my.a[i]<<"\t";
}
void deleteArray(MyArray &my){
if(my.a)delete []my.a;
}
MyArray inputArray(){
MyArray ma;
initialArray(ma);
int tmp;
cout<<"The number of array is ";
cin>>tmp;
if(tmp<=0){
cout<<"Data is error";
return ma;
}
ma.num = tmp;
ma.a = new int[ma.num];
if(!ma.a){
cout<<"Not enough memory!";
initialArray(ma);
return ma;
}
for(int i=0;i<ma.num;i++){
cout<<"Array["<<i<<"]=";
cin>>ma.a[i];
}
return ma;
}
MyArray inputArrayFromFile(char *st,char ch){
ifstream fs;
MyArray my;
initialArray(my);
switch (ch) {
case 0: case 1:fs.open(st);break;
case 2: case 3:fs.open(st,ios::binary);break;
default: cout<<"Incorrect kind of file!";return my;
}

if(fs.fail()){
cout<<"Can not open files";
fs.close();
exit(0);
}
int tmp,i;
switch (ch) {
case '0':{
fs>>tmp;
if(tmp<=0){
cout<<"Data is error!";
fs.close();
return my;
}
my.num = tmp;
my.a = new int[tmp];
if(!my.a){
cout<<"not enough memory!";
fs.close();
initialArray(my);
return my;
}
i=0;
while (!fs.eof()){
fs>>tmp;
my.a[i++]=tmp;
}
break;
}
case '1':{
i=0;
while (!fs.eof()){
fs>>tmp;
i++;
}
fs.seekg(0);
my.num = i;
my.a = new int[i];
if(!my.a){
cout<<"not enough memory!";
fs.close();
initialArray(my);
return my;
}
i=0;
while (!fs.eof()){
fs>>tmp;
my.a[i++]=tmp;
}
break;
}
case '2':{
// file binary, each item was written
long be=0,en=0;
be = fs.tellg();
fs.seekg(0,ios_base::end);
en = fs.tellg();
my.num = int((en-be)/sizeof(int));
my.a = new int[my.num];
if(!my.a){
cout<<"not enough memory!";
fs.close();
initialArray(my);
return my;
}
i=0;
fs.seekg(0,ios_base::beg);
int sis = sizeof(int);
while (!fs.eof()){
fs.read((char*)(&tmp),sis);
my.a[i++]=tmp;
}
break;
}
case '3':{
// file binary, all items were written
long be=0,en=0;
be = fs.tellg();
fs.seekg(0,ios_base::end);
en = fs.tellg();
my.num = int((en-be)/sizeof(int));
my.a = new int[my.num];
if(!my.a){
cout<<"not enough memory!";
fs.close();
initialArray(my);
return my;
}
fs.seekg(0,ios_base::beg);
fs.read((char*)(&my.a),sizeof(int)*my.num);
break;
}
}
fs.close();
return my;
}

You might also like