【发布时间】:2018-05-20 15:39:46
【问题描述】:
所以我正在尝试编写一个机械程序。
该程序很长,但以下是导致我出现问题的函数:recherche_noe 和 creation_noe。剩下的就不用管了。
它是法语,所以请耐心等待,但想法是这样的:首先在main 中,我询问用户lst_noe 中的noe 的编号(这是noe 的列表)。通过creation_noe,他在向用户询问结构信息的同时做到了这一点。最后recherche_noe 返回我正在寻找的noe。所有信息都存储在struct maillage 中,您在其中还有其他结构。感谢您的帮助。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*==============================================*/
/* Déclaration des structures */
/*==============================================*/
struct matrice
{
char nom[20];
int n,m;
double **tab;
struct matrice *next;
};
struct element
{
int num;
int n1, n2;
double k;
struct element *next;
};
struct noeud
{
int num;
double u;
double f;
struct noeud *next;
};
struct maillage
{
int nb_noe, nb_elt;
struct noeud *lst_noe;
struct element *lst_elt;
struct matrice *K, *U, *F;
};
typedef struct matrice* matrices;
typedef struct element* elements;
typedef struct noeud* noeuds;
typedef struct maillage* maillages;
char buffer[100];
/*==============================================*/
/* Recherche */
/*==============================================*/
noeuds recherche_noe(maillages mail,int num){
int i;
maillages temp=mail;
while(temp->lst_noe!=NULL){
if(temp->lst_noe->num == num)
return temp;
temp->lst_noe=temp->lst_noe->next;
}
printf("Le noeud recherche n'existe pas");
return temp;
}
elements recherche_elt(maillages mail,int num){
int i;
maillages temp=mail;
while(temp->lst_elt->num != num /*&& temp->lst_elt->next!=NULL*/){
temp->lst_elt=temp->lst_elt->next;
}
if(temp->lst_elt->num != num /*&& temp->lst_elt->next==NULL*/){
printf("L'element recherche n'existe pas");
}else{
return mail->lst_elt;
}
}
/*==============================================*/
/* creation */
/*==============================================*/
matrices creation_noeud(maillages mail){
int i;
for (i=0;i<mail->nb_noe;i++){
noeuds new = (noeuds)malloc(sizeof(struct noeud));
new->num = i+1;
printf("Deplacement du noeud %d: ",i+1);
buffer[0]='\0';
getchar(); //reinitialisation de buffer
scanf("%[^\n]",buffer);
if((int) strlen(buffer)){ //si la taille du buffer différente 0
new->u= (double)atof(buffer);
}
else{
printf("Donner l'effort %d du noeuds",i+1);
scanf("%lf", &new->f);
}
new->next=mail->lst_noe;
mail->lst_noe=new;
}
}
void creation_element(maillages mail)
{
int i;
for (i=0;i<mail->nb_elt;i++){
elements new= (elements)malloc(sizeof(struct element));
new->num=i+1;
printf("Donner le noeud 1 de l'element %d: ",i+1);
scanf("%d", &new->n1);
printf("Donner le noeud 2 de l'element %d: ",i+1);
scanf("%d", &new->n2);
printf("Donner la raideur de l'element %d: ",i+1);
scanf("%lf",&new->k);
new->next= mail->lst_elt;
mail->lst_elt=new;
}
}
matrices creation_mat(int n,int m, char *nom){
int i,j;
matrices new=(matrices)malloc(sizeof(struct matrice));
strcpy(new->nom,nom);
new->n = n;
new->m = m;
new->tab = (double**)malloc((n)*sizeof(double*));
for (i=0; i<n; i++)
new->tab[i] = (double*)malloc((n)*sizeof(double));
for (i=0;i<n;i++) /* mise a zero des composantes */
for (j=0;j<m;j++)
new->tab[i][j] =0;
return new;
}
/*==============================================*/
/* Assemblage */
/*==============================================*/
void assemblage(maillages mail){
int a,b,i,j,k;
mail->K = creation_mat(mail->nb_noe, mail->nb_noe,"K");
mail->U = creation_mat(mail->nb_noe, 1,"U");
for (j=0; j<mail->nb_noe; j++){ //Initialisation de K
for(k=0; k<mail->nb_noe; k++){
mail->K->tab[j][k]=0;
}
}
printf("%d",recherche_elt(mail,i+1)->n1);
for (i=0; i<mail->nb_elt; i++){ // Assemblage matrice K
a = recherche_elt(mail,i+1)->n1-1;
b = recherche_elt(mail,i+1)->n2-1;
mail->K->tab[a][a] +=recherche_elt(mail,i+1)->k;
mail->K->tab[a][b] -=recherche_elt(mail,i+1)->k;
mail->K->tab[b][a] -=recherche_elt(mail,i+1)->k;
mail->K->tab[b][b] +=recherche_elt(mail,i+1)->k;
}
for (i=0; i<mail->nb_noe; i++){ // Assemblage matrice U
mail->U->tab[i][0] = recherche_noe(mail,i+1)->u;
}
}
/*==============================================*/
/* Produit */
/*==============================================*/
matrices produit(matrices mat1,matrices mat2,char *nom){
int i,j,k;
matrices prod;
if(mat1->m!=mat2->n){
printf("Erreur, les matrices ne sont pas compatibles\n\n");
}else{
prod=malloc(sizeof(struct matrice));
strcpy(prod->nom,nom);
prod->next=NULL;
prod->n=mat1->n;
prod->m=mat2->m;
prod->tab= (double **)malloc(prod->n * sizeof(double *));
for (i=0; i<prod->n; i++)
prod->tab[i] = (double *)malloc(prod->m * sizeof(double));
for (i=0;i<prod->n;i++){
for (j=0;j<prod->m;j++){
prod->tab[i][j]=0;
for (k=0;k<mat1->m;k++){
prod->tab[i][j]+=mat1->tab[i][k] * mat2->tab[k][j];
}
}
}
return prod;
}
}
/*==============================================*/
/* Affichage */
/*==============================================*/
void affiche_mat(matrices mats){
int i,j;
printf("Matrice %s de dimensions %d*%d:\n",mats->nom,mats->n,mats->m);
for (i=0;i<mats->n;i++){
for (j=0;j<mats->m;j++){
printf("%s[%d][%d]: %lf\n",mats->nom,i,j,mats->tab[i][j]);
}
}
printf("\n");
}
int main(){
int i;
elements lst_elt;
noeuds lst_noe;
maillages mail=malloc(sizeof(struct maillage));
mail->lst_noe=NULL;
mail->lst_elt=NULL;
printf("Donner le nombre de noeuds voulu: ");
scanf("%d",&mail->nb_noe);
printf("Donner le nombre d'elements voulu: ");
scanf("%d",&mail->nb_elt);
creation_noeud(mail);
creation_element(mail);
printf("%d",recherche_elt(mail,2+1)->n1+45);
assemblage (mail);
produit(mail->K,mail->U,"F");
/*affiche_mat(mail->K);
printf("\n");
affiche_mat(mail->U);
printf("\n");
affiche_mat(mail->F);
printf("\n");*/
}
【问题讨论】:
-
复制/粘贴错误::
new->tab[i] = (double*)malloc((n)*sizeof(double));-->>new->tab[i] = (double*)malloc((m)*sizeof(double));(或相反)[而且,恕我直言,您应该删除演员表和愚蠢的类型定义] -
感谢您发现这个错误。但这并不能解决我的 noe 列表的主要问题。为什么 main 中的 printf 不能正常工作(段错误的来源)。我追溯到 recherche_noe。
-
另外,您忘记在 product() 中初始化新矩阵并将其归零。而且你的链表代码很麻烦。
-
矩阵的归零是在创建时完成的,在 creation_mat 中。是的,我很抱歉所有这些基本上都是自学的,老师没用。这就是为什么我仍然对链接列表有一些问题。
-
是的,你完全正确,我很抱歉。
标签: c linked-list segmentation-fault