לוגו אתר Fresh          
 
 
  אפשרות תפריט  ראשי     אפשרות תפריט  צ'אט     אפשרות תפריט  מבזקים     אפשרות תפריט  צור קשר     חץ שמאלה ‎print ‎"Hello World!"; if‎ ‎not rules.‎know ‎then rules.‎read(); חץ ימינה  

לך אחורה   לובי הפורומים > מחשבים > תכנות ובניית אתרים
שמור לעצמך קישור לדף זה באתרי שמירת קישורים חברתיים
תגובה
 
כלי אשכול חפש באשכול זה



  #1  
ישן 08-07-2009, 16:52
צלמית המשתמש של Blood of Lambs
  משתמש זכר Blood of Lambs Blood of Lambs אינו מחובר  
 
חבר מתאריך: 04.10.08
הודעות: 524
C access violation

קוד PHP:
 // SparseMatrix.cpp

#include "stdafx.h"                                    
#include <stdlib.h>
#include <stdio.h>

typedef int Elem;
typedef struct cell Cell;
typedef Cell Matrix;

struct cell {
    
int row;
    
int col;
    
Cell right;
    
Cell down;
    
Elem value;
};

Matrix newSparseMatrix();

void freeSparseMatrixMatrix m );

void setElemMatrix mint rowint colElem value );

Elem getElemMatrix mint rowint col );


void showByRowsMatrix m );

void showByColsMatrix m );

Matrix readSparseMatrix();

Matrix transposeMatrix m );

Matrix addMatrixMatrix mMatrix other );


bool rowExists(Matrix mint nRow);

bool colExists(Matrix mint nCol);

Matrix findRow(Matrix m,int nRowint nCol);

Matrix findCol(Matrix mint nRowint nCol);

void deleteCell(Matrix mint nRowint nCol);

void createNewCell(Matrix m,int nRowint nColElem Value);

void main()
{
    
Matrix A;
    
newSparseMatrix();
    
readSparseMatrix();

    
system("pause");
}



Matrix newSparseMatrix()
{
    
Matrix M = (Matrix)calloc(1,sizeof(Cell));
    
//printf("%d\n",M->col);
    
return M;
}
//Matrix newSparseMatrix()

void freeSparseMatrix(Matrix m)
{
    
Cell *NextValue;
        
    while(
m->down)
    {
        
m->right;
        while(
m->right)
        {
            
NextValue m->right;
            
free(m);
            
NextValue;
        }
        
m->down;
    }
    
free(m);
}
//free matrix


void setElem(Matrix mint nRowint nColElem Value)
{
    if(
Value == 0)
    {
        
deleteCell(m,nRow,nCol);
    }

    else
    {
        
createNewCell(m,nRow,nCol,Value);
    }
}

Elem getElem(Matrix mint nRowint nCol
{
    
Cell *Value;

    if(
rowExists(m,nRow) && colExists(m,nCol))
    {
        
Value findRow(m,nRow,nCol);
        
Value Value->right;
        return 
Value->value;
    }

    return 
0;

}
//get elem


void showByRows(Matrix m)
{
    
Cell *CurrRow m;
    
Cell *NextRow m;
    
Cell *CurrCol m;
    
Cell *NextCol m;

    while(
CurrRow)
    {
        while(
CurrCol)
        {
            
printf("(%d,%d) = %d\n",CurrCol->row,CurrCol->col,CurrCol->value);
            
NextCol CurrCol->right;
            
CurrCol NextCol;
        }
        
NextRow CurrRow->down;
        
CurrRow NextRow;
    }
}


void showByCols(Matrix m
{
    
Cell *CurrRow m;
    
Cell *NextRow m;
    
Cell *CurrCol m;
    
Cell *NextCol m;

    while(
CurrCol)
    {
        while(
CurrRow)
        {
            
printf("(%d,%d) = %d\n",CurrRow->row,CurrRow->col,CurrRow->value);
            
NextRow CurrRow->down;
            
CurrRow NextRow;
        }
        
NextCol CurrCol->right;
        
CurrCol NextCol;
    }
}
//show by cols


Matrix readSparseMatrix() 
{
    
Matrix M newSparseMatrix();
    
Cell *NewCol newSparseMatrix();
    
Cell *NewRow newSparseMatrix();
    
int nRow;
    
int nCol;
    
int nValue;

    
printf("Enter the row number:\n");
    
scanf("%d",&nRow);
    
printf("Enter the col number:\n");
    
scanf("%d",&nCol);
    
printf("Enter the value of the cell:\n");
    
scanf("%d",&nValue);
    
    if(
nValue!=0&&nRow!=0&&nCol!=0)
    {
        
NewCol->col nCol;
        
NewCol->row 0;
        
M->right NewCol;
        
NewCol->right NULL;
        
NewCol->down NULL;

        
NewRow->row nRow;
        
NewRow->col 0;
        
M->down NewRow;
        
NewRow->right NULL;
        
NewRow->down NULL;
    }
        
    while(
nRow>&& nCol>0)
    {
        
setElem(M,nRow,nCol,nValue);

        
printf("Enter the row number:\n",nRow);
        
scanf("%d",&nRow);
        
printf("Enter the col number:\n",nCol);
        
scanf("%d",&nCol);
        
printf("Enter the value of the cell:\n",nValue);
        
scanf("%d",&nValue);
    }
    return 
M;
}
//read sparse matrix
/*
Matrix transpose( Matrix m ) {

}//matrix transpose

Matrix addMatrix( Matrix m, Matrix other ) {
         
}//add matrix
*/
bool rowExists(Matrix mint nRow)
{
    
Cell *NextRow;
    
Cell *CurrRow;

    
bool bRowFound false;

    
CurrRow m;

    
NextRow m->down;

    while(
NextRow&&!bRowFound)
    {
        if(
CurrRow->row == nRow)
        {
            return 
true;
        }
       
        
NextRow CurrRow->down;
        
CurrRow NextRow;
    }

    return 
false;
}


bool colExists(Matrix mint nCol)
{
    
Cell *NextCol;
    
Cell *CurrCol;

    
bool bColFound false;

    
CurrCol m;

    
NextCol m->right;

    while(
NextCol&&!bColFound)
    {
        if(
CurrCol->col == nCol)
        {
            return 
true;
        }
       
        
NextCol CurrCol->right;
        
CurrCol NextCol;
    }

    return 
false;
}

Matrix findRow(Matrix m,int nRowint nCol)
{
    
Cell *CurrRow m;
    
Cell *NextRow m;

    
Cell *CurrCol;
    
Cell *NextCol;

    while(
CurrRow->row != nRow)
    {    
        
NextRow CurrRow->down;
        
CurrRow NextRow;
    }
   
    
CurrCol CurrRow;
    
NextCol CurrCol->right;

    while(
NextCol->col != nCol)
    {
        
NextCol CurrCol->right;
        
CurrCol NextCol;
    }

    return 
CurrCol;
}

Matrix findCol(Matrix mint nRowint nCol)
{
    
Cell *CurrCol m;
    
Cell *NextCol m;

    
Cell *CurrRow;
    
Cell *NextRow;

    while(
CurrCol->col != nCol)
    {    
        
NextCol CurrCol->right;
        
CurrCol NextCol;
    }
   
    
CurrRow CurrCol;

    while(
NextRow->row != nRow)
    {
        
NextRow CurrRow->right;
        
CurrRow NextRow;
    }

    return 
CurrRow;
}

void deleteCell(Matrix mint nRowint nCol)
{
    
Cell *LeftCell;
    
Cell *AboveCell;
    
Cell *CurrCell;

    
Cell *AboveIndex;
    
Cell *LeftIndex;

    if(
rowExists(m,nRow))
    {
        
LeftCell findRow(m,nRow,nCol);
        
AboveCell findCol(m,nRow,nCol);
       
        
CurrCell LeftCell->right;

        
LeftCell->right CurrCell->right;
        
AboveCell->down CurrCell->down;

        
free(CurrCell);

        if(
LeftCell->row == && LeftCell->right == NULL)
        {
            
AboveIndex findCol(m,LeftCell->row,LeftCell->col);
            
AboveIndex->down LeftCell->down;
            
free(LeftCell);
        }

        if(
AboveCell->col == && AboveCell->down == NULL)
        {
            
LeftIndex findRow(m,AboveCell->row,AboveCell->col);
            
LeftIndex->right AboveCell->right;
            
free(AboveCell);
        }    
    }
}

void createNewCell(Matrix m,int nRowint nColElem Value)
{
    
Cell *NewCell newSparseMatrix();
    
Cell *NewRow newSparseMatrix();
    
Cell *NewCol newSparseMatrix();

    
Cell *Left m;
    
Cell *Above m;

    
NewCell->col nCol;
    
NewCell->row nRow;
    
NewCell->value Value;
   
    if(
rowExists(m,nRow))
    {
        
Left findRow(m,nRow,nCol);
        
NewCell->right Left->right;
        
Left->right NewCell;
    }

    if(!
colExists(m,nCol))
    {
        
NewCol->col nCol;
        
Left findRow(m,0,nCol);
        
NewCol Left->right;
        
Left->right NewCol;
        
NewCol->right NewCell;
    }

    if(
colExists(m,nCol))
    {
        
Above findCol(m,nRow,nCol);
        
NewCell->down Above->down;
        
Above->down NewCell;
    }

    if(!
rowExists(m,nRow))
    {
        
NewRow->row nRow;
        
Above findCol(m,nRow,0);
        
NewRow Above->down;
        
Above->down NewRow;
        
NewRow->down NewCell;
    }


זה ניסיון למימוש של מטריצה דלילה בשפת C.
אני מקבל access violation בזמן הריצה בשורה while(NextCol->col != nCol)
בפונקציה findRow. הפונקציה מוצאת ומחזירה את התא שמצביע מצד שמאל לתא שנמצא בשורה nRow ועמודה nCol.
הפונקציה findCol עושה אותו דבר רק מחזירה את התא שמצביע מלמעלה.

הבנתי שהבעיה היא שכאשר אני מוסיף את התא הראשון, אין שום דבר שיצביע עליו. ולכן הוספתי לפונקציה readSparseMatrix() קטע קוד שבודק אם הערך שנכנס אינו 0, אז הוא ייצור תא עמודה 0 ושורה 0 (התאים שנמצאים שם אינם מכילים ערכים אלא רק מצביעים לשורה/עמודה) שתא 0,0 (מטריצה שרק נוצרה) יצביע אליהם וכך הפונציות findRow ו findCol יחזירו לי אותם ויקשרו לתא החדש. אבל עדיין קיבלתי את אותה שגיאה באותו מקום.

תודה מראש.


תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
תגובה

כלי אשכול חפש באשכול זה
חפש באשכול זה:

חיפוש מתקדם
מצבי תצוגה דרג אשכול זה
דרג אשכול זה:

מזער את תיבת המידע אפשרויות משלוח הודעות
אתה לא יכול לפתוח אשכולות חדשים
אתה לא יכול להגיב לאשכולות
אתה לא יכול לצרף קבצים
אתה לא יכול לערוך את ההודעות שלך

קוד vB פעיל
קוד [IMG] פעיל
קוד HTML כבוי
מעבר לפורום



כל הזמנים המוצגים בדף זה הם לפי איזור זמן GMT +2. השעה כעת היא 22:15

הדף נוצר ב 0.58 שניות עם 12 שאילתות

הפורום מבוסס על vBulletin, גירסא 3.0.6
כל הזכויות לתוכנת הפורומים שמורות © 2024 - 2000 לחברת Jelsoft Enterprises.
כל הזכויות שמורות ל Fresh.co.il ©

צור קשר | תקנון האתר