问题:在A+B和A-B中仍然存在问题。
ptd->linux Ronz.linux (*ptd).linux //三个等效
小知识:"."优先级高于"*"
#include<stdio.h>
#define MAXSIZE 1000
#include<windows.h>
typedef struct {
int row;
int col;
int value;
}Triple;
typedef struct {
Triple data[MAXSIZE - 1];
int rows;
int cols;
int nums;
}TSMatrix;
void InitMatrix(TSMatrix *M){
M->nums = M->rows = M->cols = 0;
}
void CreatMartrix(TSMatrix *M){
int coll,roww,valuee;
printf("请输入行数和列数和非零元素的个数:\n");
fflush(stdin);
scanf("%d %d %d",&M->rows,&M->cols,&M->nums);
printf("\n按行序输入,row,col,value为非零行的行数,列数,和值:\n");
for (int i = 1;i <= M->nums;i++){
fflush(stdin);
scanf("%d %d %d",&coll,&roww,&valuee);
M->data[i].row = roww;
M->data[i].col = coll;
M->data[i].value = valuee;
}
}
void Output (TSMatrix *M){
printf("\n行 列 元素值\n\n");
for (int i = 1;i<= M->nums;i++){
printf("%2d%4d%8d\n",M->data[i].row,M->data[i].col,M->data[i].value);
}
}
/*int add(TSMatrix M,TSMatrix N,TSMatrix *Q){
if(M.cols != N.cols ||M.rows != N.rows){
printf("\n 非同型矩阵无法相加!\n");
return 0;
}
int i = 1;
int j = 1;
int lenn;
for(; i <= M.nums && j <=N.nums; i++, j++){
if(M.data[i].col == N.data[j].col&& M.data[i].row == N.data[j].row){
lenn = ++Q->nums;
Q->data[lenn].value =M.data[i].value + N.data[j].value;
if(!Q->data[lenn].value)
continue;//不能为零
Q->data[lenn].col =M.data[i].col;
Q->data[lenn].row =M.data[i].row;
}
else{
if(M.data[i].col < N.data[j].col){
lenn = ++Q->nums;
Q->data[lenn].value =M.data[i].value;
if(Q->data[lenn].value);
Q->data[lenn].col= M.data[i].col;
Q->data[lenn].row= M.data[i].row;
}
else if(M.data[i].col >N.data[j].col) {
lenn = ++Q->nums;
Q->data[lenn].value =N.data[j].value;
Q->data[lenn].col= N.data[j].col;
Q->data[lenn].row= N.data[j].row;
}
else{
if(M.data[i].row<= N.data[j].row){
lenn = ++Q->nums;
Q->data[lenn].value= M.data[i].value;
Q->data[lenn].col= M.data[i].col;
Q->data[lenn].row= M.data[i].row;
}
else{
lenn = ++Q->nums;
Q->data[lenn].value= N.data[j].value;
Q->data[lenn].col= N.data[j].col;
Q->data[lenn].row= N.data[j].row;
}
}
}
}
//如果M中还有剩余...
for(; i <= M.nums; i++){
lenn = ++Q->nums;
Q->data[lenn].value = M.data[i].value;
Q->data[lenn].col =M.data[i].col;
Q->data[lenn].row =M.data[i].row;
}
//如果N中还有剩余...
for(; j <= N.nums; j++){
lenn = ++Q->nums;
Q->data[lenn].value = N.data[j].value;
Q->data[lenn].col =N.data[j].col;
Q->data[lenn].row =N.data[j].row;
}
}
int deletee(TSMatrix M,TSMatrix N,TSMatrix *Q){
if(M.cols != N.cols ||M.rows != N.rows){
printf("\n 非同型矩阵无法相减!\n");
return 0;
}
int max = M.nums;
if (max<N.nums){
max = N.nums;
}
int i = 1;
int j = 1;
int lenn = 0;
for(; i <= max; i++, j++){
if(M.data[i].col == N.data[j].col && M.data[i].row == N.data[j].row){
lenn = ++Q->nums;
Q->data[lenn].value = M.data[i].value - N.data[j].value;
if(!Q->data[lenn].value)
continue;
Q->data[lenn].row = N.data[i].row;
Q->data[lenn].col = N.data[i].col;
}
else{
if (M.data[i].row < N.data[j].row){
lenn = ++Q->nums;
Q->data[lenn].value = M.data[i].value - N.data[j].value;
Q->data[lenn].row = M.data[i].row;
Q->data[lenn].col = M.data[i].col;
}
else if(M.data[i].row >= N.data[j].row){
lenn = ++Q->nums;
Q->data[lenn].value = M.data[i].value - N.data[j].value;
Q->data[lenn].row = N.data[j].row;
Q->data[lenn].col = N.data[j].col;
}
else if(M.data[i].col < N.data[i].col){
lenn = ++Q->nums;
Q->data[lenn].value = M.data[i].value - N.data[j].value;
Q->data[lenn].row = M.data[i].row;
Q->data[lenn].col = M.data[i].col;
}
else if(M.data[i].col >= N.data[j].col){
lenn = ++Q->nums;
Q->data[lenn].value = M.data[i].value - N.data[j].value;
Q->data[lenn].row = N.data[j].row;
Q->data[lenn].col = N.data[j].col;
}
}
for(; i <= M.nums; i++){
lenn = ++Q->nums;
Q->data[lenn].value = M.data[i].value;
Q->data[lenn].col =M.data[i].col;
Q->data[lenn].row =M.data[i].row;
}
//如果N中还有剩余...
for(; j <= N.nums; j++){
lenn = ++Q->nums;
Q->data[lenn].value = N.data[j].value;
Q->data[lenn].col =N.data[j].col;
Q->data[lenn].row =N.data[j].row;
}
}
}*/
int main(void){
system("color e4");
TSMatrix M_one;
TSMatrix M_two;
TSMatrix Q;
InitMatrix(&M_one);
InitMatrix(&M_two);
InitMatrix(&Q);
CreatMartrix(&M_one);
Output(&M_one);
CreatMartrix(&M_two);
Output(&M_two);
// add(M_one,M_two,&Q);
// Output(&Q);
// printf("+++++++++++++++++++++++++++++++++++++++");
// deletee(M_one,M_two,&Q);
// Output(&Q);
}