这是我的作业。我可以返回无效,但我不知道如何使用int
以及如何检查出现问题并返回-1;
ADTSmatrix是:
航向
整数的稀疏矩阵(具有维行x cols)。
功能:
int SMTX_read (SMatrixType A)
::= read in a matrix from stdin and stores the matrix in A.
::= returns SMTX_ERROR if something went wrong
--------/* For this project, the input format will be
Line 1: two integers, rows & cols, dimension of the matrix Line 2 to rows+1: contains cols number of integers
Should take the input and convert to your proper ADT format */
int SMTX_print(SMatrixType A)
::= print the sparse matrix A in the following format
/* Line 1: print “Rows = ??, Cols = ??, # of non-zero entries = ??” Line 2 ~ ??: print “< Ri, Ci, entry-value>,” one 3-tuple per line */
int SMTX_add (SMatrixType A, B, C)
::= C <= A + B
::= returns SMTX_ERROR if something went wrong
int SMTX_subtract (SMatrixType A, B, C)
::= C <= A - B
::= returns SMTX_ERROR if something went wrong
int SMTX_transpose (SMatrixType A, B) ::= B <= AT
::= returns SMTX_ERROR if something went wrong
int SMTX_multiply (SMatrixType A, B, C)
::= C <= A x B
::= returns SMTX_ERROR if something went wrong /* please use the quick
algorithm given in lecture */
数据输入:
define SMTX_ERROR -1
define MAX_SMTX_SIZE 100
typedef struct SMatrix {
. ..
} SMatrixType
答案 0 :(得分:0)
错误处理旨在通过返回值执行。当期望完全没有错误时,Void可以是例如打印功能。读取功能和其他操作应检查输入:e。 G。矩阵具有相应的维度,或者可能出现其他问题,结果与期望不符。
int SMTX_read (SMatrixType A)
{
int err = 0;
/* reading input */
if( outcome != expected ) { err = SMTX_ERROR; }
/* .. */
return err;
}
或另一个例子:
int division( int dividend, int divisor, int * quotient_ptr )
{
int err = -1;
if( 0 != divisor ) { err = 0; *quotient_ptr = dividend / divisor; }
return err;
}