#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
void multiplier_matrice_vecteur (char * v0, char * A, int N)
{
int i,j,k;
char somme;
for (i = 0; i < N; i++) //On fait N calculs car c'est une matrice 1xN
{
//On fait N additions + multiplications
somme = 0;
for (j = 0; j < N; j++)
{
somme += v0[i] * A[i * N + j];
}
v0[i] = somme;
}
}
int main(void)
{
bool premiereLignefaite = false;
//Lire le fichier
FILE * graphe = fopen("graphe.txt", "r");
//Fichier de sortie
FILE * resultat = fopen("resultat.txt", "w");
int nbr1, nbr2;
int N;
char *matrice; //pointeur vers la matrice d'adjacence
//Ligne lue
static char ligne[50];
while (fgets(ligne, 50, graphe) != NULL) //retourne 0 quand on a end-of-file
{
//La premiere ligne est différente
if (premiereLignefaite == false) {
//Initialiser une matrice d'adjacence NxN
sscanf(ligne, "%d %d", &nbr1, &nbr2);
N = nbr1;
matrice = new char(nbr1 * nbr1); //Memoire dynamique pour la matrice dadjacence n x n
memset(matrice, 0, nbr1*nbr1);
premiereLignefaite = true;
continue;
}
//On construit notre matrice d'adjacence
sscanf(ligne, "%d %d", &nbr1, &nbr2);
matrice[nbr1 * N + nbr2 ] = 1;
}
printf("Matrice d'adjacence %dx%d : \n", N, N);
//Affichage de la matrice d'adjacence
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
printf("%c ", matrice[i * N + j] + '0');
}
printf("\n");
}
//Application de l'algo étapes par étapes
double tolerance = 0.00001; //Niveau de tolérance de la méthode
char * v0; //vecteur propre taille N
char * v; //vecteur tampon
int valeur; //valeur propre
int valeur_tamp; //valeur propre tampon
//Initialiser v0
v0 = new char(N);
memset(v0, 1, N);
//Initialiser A (déja fait)
//Initialiser tolérance (deja fait)
valeur = 0;
while (1)
{
valeur_tamp = valeur;
//Multiplication du vecteur par la matrice
multiplier_matrice_vecteur(v0, matrice, N);
}
//Désallocation de la mémoire
delete matrice;
//Fermeture des fichiers etc
fclose(graphe);
fclose(resultat);
return 0;
}
これは私のラップトップで2PM以来コーディングしているプログラムです。私はVisual Studio 2008とWindows 7の64ビットを使用しています。私はコーディングしている、すべてうまくいっている。プログラムをテストしようとすると、次の行が表示されます。newとmallocによってNULLポインタが返されたのはなぜですか?
v0 = new char(N);
私には例外があります。私はmallocとcallocで私の記憶を得ようとしています。何が得られますか?ヌルポインタ!!私は私のマシンに4ギガのラムを持っています。私はここで9バイトのメモリを得ることができません。私はこれを全く理解していません。 Visual Studio 2008を持ち、同じエンバイロメントの下でテストしたい人のために
、ファイルgraphe.txtが必要になり、ここでそのファイルがあります:
9 20
0 1
0 2
1 0
1 2
1 3
1 5
2 0
2 1
2 3
3 1
3 2
3 4
4 3
5 1
5 6
5 7
6 5
6 8
7 5
8 6
私は、これは機械の問題だと思いました私は家に帰って、私はデスクトップコンピュータでプログラムを試してみると同じ問題です...
私はGCCを試してみたいと思いますが、私はいつもVisual C++を使っていますので、この問題を解決したい環境...
EDIT:以下のコードが機能するようになりました。何らかの理由で、最初の新規でカッコを使用するとうまくいきますが、次回新しいときには機能しません! []構文を使用して両方の新しい割り当てを行うと、それは機能します。私が最初の新しい割り当てを[]で置き、2番目の割り当てを()で行うと、それも機能します。 WEIRD。とにかく、今から[]を使用しています...ありがとうございました。
制御が例外を伴ってその行に達したときに「N」と表示される場合の値は? – Kos
例外がここにないのですか? 'memset(v0、1、N);' – Cam
いいえ、ここでmemsetを実行しようとする前に起こります。 – toto