如何将矩阵读入C++程序?(初学者)

microstoneee 2003-09-29 06:58:39
正在学习用C++, 但遇到一个矩阵的问题,现在不知如何读入,盼望高手指导.
...全文
407 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaby 2003-09-30
  • 打赏
  • 举报
回复
看看我的,可以操作任意大小矩阵.....不过还没完成
// Matrix.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

struct matrix
{
struct matrix* p;
struct matrix* n;
struct matrix* u;
struct matrix* d;
int data;
};

struct cursor
{
int row;
int line;
struct matrix* p;
int status;
};

struct row
{
int no;
struct matrix* p;
int summation;
int status;
};

struct line
{
int no;
struct matrix* p;
int summation;
int status;
};

struct matrix* initialize(int h,int w);
int display(struct matrix* handle);
int release(struct matrix* handle);
int cursor_l(struct cursor* handle);
int cursor_r(struct cursor* handle);
int cursor_u(struct cursor* handle);
int cursor_d(struct cursor* handle);

int main(int argc, char* argv[])
{
int h,w,i,c;
struct matrix* handle;
printf("Please input the height of Matrix!\n");
scanf("%d",&h);
printf("Please input the width of Matrix!\n");
scanf("%d",&w);

if(h==1&&w==1)
{
printf("The Height or Width is illegal!\n");
return 1;
}

printf("Initializing the Matrix");

handle=initialize(h,w);

while(i!=0)
{
printf("\nPlease select the function:\n\n");
printf("1.Display the Matrix\n");
printf("2.Function II\n");
printf("3.Function III\n");
printf("4.Function IV\n");
printf("5.Exit\n");
printf("\n\n\nSlect:");
scanf("%d",&c);
switch(c)
{
case 1:
display(handle);
break;
case 2:
printf("2");
break;
case 5:
printf("Releasing Memory.....\n");
release(handle);
//释放内存
i=0;
break;
}

}



return 0;
}

int display(struct matrix* handle)
{
struct matrix *row,*c;
printf("Starting display the Matrix.....\n\n");
row=handle;

printf("/nLeft->Right/Upper->Down:\n");
while(row!=NULL)
{
c=row;
while(c!=NULL)
{
printf("%d ",c->data);
c=c->n;
}
row=row->d;
printf("\n");
}
row=handle;

while(row->n!=NULL)
row=row->n;
while(row->d!=NULL)
row=row->d;

printf("\nUpper->Down/Left->Right:\n");
while(row!=NULL)
{
c=row;
while(c!=NULL)
{
printf("%d ",c->data);
c=c->p;
}
row=row->u;
printf("\n");
}
return 0;
}

int release(struct matrix* handle)
{
struct matrix* p,*temp;
while(handle!=NULL)
{
p=handle->n;
while(p!=NULL)
{
temp=p;
p=p->n;
free(temp);
}
temp=handle;
handle=handle->d;
free(temp);
}


return 0;
}

struct matrix* initialize(int h,int w)
{
int a,b,i=1;
struct matrix* current,*p=NULL,*frist=NULL;
frist=(struct matrix*)malloc(sizeof(matrix));
frist->p=NULL;
frist->u=NULL;
frist->n=NULL;
frist->d=NULL;
frist->data=i++;
p=frist;
for(b=1;b<w;b++)
{
current=(struct matrix*)malloc(sizeof(matrix));
current->u=NULL;
current->d=NULL;
current->p=p;
current->n=NULL;
current->data=i++;
p->n=current;
p=current;
}
for(a=1;a<h;a++)
{
current=(struct matrix*)malloc(sizeof(matrix));
current->u=p;
current->d=NULL;
current->p=NULL;
current->n=NULL;
current->data=i++;
p->d=current;
p=current;
}

for(b=1;b<w;b++)
{
current=(struct matrix*)malloc(sizeof(matrix));
current->u=NULL;
current->d=NULL;
current->n=p;
current->p=NULL;
current->data=i++;
p->p=current;
p=current;
}

for(a=2;a<h;a++)
{
current=(struct matrix*)malloc(sizeof(matrix));
current->p=NULL;
current->d=p;
current->n=NULL;
current->u=NULL;
current->data=i++;
p->u=current;
p=current;
}

frist->d=current;
p->u=frist;
while(frist->d->d!=NULL)
{
p=frist->n;
while(p->n!=NULL)
{
current=(struct matrix*)malloc(sizeof(matrix));
p->d=current;
p->p->d->n=current;
current->p=p->p->d;
current->n=NULL;
current->u=p;
p->p->d->n=current;
current->data=i++;
p=p->n;
}
current->n=p->d;
p->d->p=current;
frist=frist->d;
}
p=frist->n;
while(p->n!=NULL)
{
p->d=p->p->d->n;
p=p->n;
}

p=frist->d->n;
while(p->n!=NULL)
{
p->u=p->p->u->n;
p=p->n;
}



while(frist->u!=NULL)
frist=frist->u;
return frist;
}

rainbow1216 2003-09-30
  • 打赏
  • 举报
回复

//随机数产生矩阵
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int m; //列数
int n; //行数

int lie[1]; //每行的元素
void generate_jz(void); //产生jz例方法
void main(){

printf("\n请输入列数: m=");
scanf("%d",&m);
printf("\n请输入行数: n=");
scanf("%d",&n);

generate_jz();
}

void generate_jz(void){
FILE *fp1;
if((fp1=fopen("c:\\000\\juzh.txt","w"))==NULL) {
printf("can not open file!\n");exit(1);
}
//按指定的格式(第1行为列、行熟,第2行为空行)存盘
fprintf(fp1,"%6d%6d\n",m,n);
fputc('\n',fp1);//把字符转换为unsigned char类型后写到输出流(此处为一空行)
//第3行起为m行n列矩阵数据:
srand((unsigned)time( NULL ));//随机数种子
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++){
lie[j]=1+rand()%10000;
fprintf(fp1,"%6d",lie[j]);
}
fprintf(fp1,"\n",lie[j]);
}
fclose(fp1);
}

//文件读入矩阵
#include <stdio.h>
#include<stdlib.h>

int a[50][100];
int n,m; //n=行;m=列
void readdata();

void main(void) {
readdata();
}

void readdata(){
char wj[20];
printf("请输入文件名,可带盘符例如c:\\000\\juzh.txt否则为当前目录:");
scanf("%s",wj);
FILE *fp;
if((fp=fopen(wj,"r"))==NULL)
{
printf("can't open file1\n");
exit(1);
}
fscanf(fp,"%d%d",&m,&n);
printf("%d ",m);
printf("%d\n",n);

while(!feof(fp)){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
fscanf(fp,"%6d",&a[i][j]);
printf("%6d",a[i][j]);
}
printf("\n");
}
}
fclose(fp);
}
lyr311 2003-09-30
  • 打赏
  • 举报
回复
TO plainsong(短歌):
我是说你的方法不错嘛?!
yulongcn 2003-09-29
  • 打赏
  • 举报
回复
用一个二维数组来储存矩阵,例如
Array[4][4]={1,2,3,4,
5,6,7,8,
9,0,1,2,
3,4,5,6}
短歌如风 2003-09-29
  • 打赏
  • 举报
回复
还不错呢……是个死循环!

#include <iostream>

istream& operator >> (istream& strm, matrix& m)
{
int row, col;
matrix temp;
strm >> row >> col;
if (strm)
{
temp.set_row_col(row, col);
int i(0), j(0);
while(strm && i < row)
while(strm && j < col)
strm >> temp[i++][j++];
if (strm)
m = temp;
}
}
lyr311 2003-09-29
  • 打赏
  • 举报
回复
plainsong(短歌)的方法不错,是C++风格的
subtop 2003-09-29
  • 打赏
  • 举报
回复
没有文件格式怎么说啊,是么 :)
lemon520 2003-09-29
  • 打赏
  • 举报
回复
一般的矩阵,用循环就可以了!
subtop 2003-09-29
  • 打赏
  • 举报
回复
没有文件格式怎么说啊,是么 :)
playboyxp 2003-09-29
  • 打赏
  • 举报
回复
这样就可以了
#include <iostream.h>
void main()
{
int a[5][5];
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
cin>>a[i][j];
}
sdtea 2003-09-29
  • 打赏
  • 举报
回复
首先定数据结构,你的矩阵是稀疏矩阵吗,如果是,定义以机构
struct matrix_element{
unsigned int row,
unsigned int col,
int val;
};

matrix_element array[];
短歌如风 2003-09-29
  • 打赏
  • 举报
回复
输入格式?

比如如果是用下面的格式:

行数 列数
用空白字符分隔的第一行数据
用空白字符分隔的第二行数据
……
用空白字符分隔的最后一行数据

可以这样写:

#include <iostream>

istream& operator >> (istream& strm, matrix& m)
{
int row, col;
matrix temp;
strm >> row >> col;
if (strm)
{
temp.set_row_col(row, col);
int i(0), j(0);
while(strm && i < row)
while(strm && j < col)
strm >> temp[i][j];
if (strm)
m = temp;
}
}

假设用matrix::set_row_col设置行列数,可以用m[i][j]返回第i行第j列的元素引用,据有缺省构造函数和拷贝赋值操作符。

64,318

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧