2015-01-28 2 views
0

제발 도와 주실 수 있습니까?구조체에 포인터를 새 구조체에 직접 할당하는 방법은 무엇입니까?

은행 계좌를 관리하는 이중 연결된 목록을 조작하고 싶지만 모든 실수를 잘못 수정하려고해도 왜 작동하지 않는지 알 수 없습니다. 확실히, 그것은이 중첩 된 구조자를 사용하고 다른 구조체 (idt, idf)에 액세스하는 방법을 모르겠습니다.

2 질문 :

는 "pnt-> idt.dn.j_n"(PNT는 포인터가 CompteBancaire에)을 받아이 방법을 -is?

포인터를 사용하여 주요 연결 목록을 트래버스 할 때 특정 구조체를 내 새 목록 (YoungCustomers)에 추가하려면 구조체 포인터를 새 구조체에 할당 할 가능성이 있습니다. 내 새 liste 대신 그것에 각 요소를 할당? 미리 감사드립니다.

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h > 
typedef struct 
{ int j_n,m_n,a_n; 
}dateDeNaissance; 

typedef struct 
{ char nom[15]; 
    char prenom[15]; 
    dateDeNaissance dn; 
}identite; 

typedef struct 
{ int numCompte; 
    char nomBanque[20]; 
}identifiant; 
/* ------------THE MAIN STRUCT-------------*/ 
typedef struct 
{ identite idt; 
    identifiant idf; 
    float solde; 
    struct CompteBancaire *next; 
    struct CompteBancaire *prc; 
}CompteBancaire; 
/* -----------STRUCT OF YOUNG CUSTOMERS----------------*/ 
typedef struct 
{ CompteBancaire *Cpt; 
struct ClientJeune *next; 
}ClientJeune; 
/*-------------GLOBAL POINTERS------------- */ 
CompteBancaire *first=NULL; 
CompteBancaire *first2=NULL; 
/*---------- CREATE A BANC ACOUNTE ---------*/ 
void saisiNovCompt (char nom[],char prn[],int j,int m,int a,int numCpt,char nomB[],float Solde) //21:32 
{ CompteBancaire *nov=malloc(sizeof(CompteBancaire)),pnt=first; 
    strcpy (nov->idt.nom,nom); 
    strcpy (nov->idt.prenom,prn); 
    nov->idt.dn.j_n=j; 
    nov->idt.dn.m_n=m; 
    nov->idt.dn.a_n=a; 
    nov->idf.numCompte=numCpt; 
    nov->prc=NULL; 
    strcpy (nov->idf.nomBanque,nomB); 
    nov->solde=Solde; 
    if (first==NULL) 
    { nov->next=NULL; 
     nov->prc=NULL; 
     first=nov; 
    } 
    else 
    { first=nov; 
     nov->next=pnt; 
     pnt->prc=nov; 
    } 
} 
/* --------SAVE AN ACCOUNTE IN FILE-------------*/ 
void fichier (CompteBancaire *pnt) 
{ FILE *Compte; 
    Compte=fopen("comptBancaire.txt","w"); 
    fprintf("nom: %s\nprenom: %s\ndate de naissance: %d/%d/%d\nnum de compte: %d\nnom de la banque: %s\nsolde: %.2f\n", 
      pnt->idt.nom,pnt->idt.prenom,pnt->idt.dn.j_n,pnt->idt.dn.m_n,pnt->idt.dn.a_n,pnt->idf.nomCompte,pnt->idf.nomBanque); 
    fclose(Compte); 
} 
/* -------------- SAVE AN ACCOUNT THAT IT'S NUMBER IS GIVEN BY THE USER ------------------*/ 
void afficheParNum (int numCpt) 
{ CompteBancaire *pnt=first; 
    if (first==NULL) 
     printf ("la liste est vide \n"); 
    else 
    { while (pnt!=NULL && pnt->idf.numDeCompt!=numCpt) 
      pnt=pnt->next; 
     if (pnt==NULL) 
     printf("Compte non trouve !\n"); 
     else 
     fichier(pnt); 
    } 
} 
/*-----------------REMOVE AN ACCOUNT THAT IT'S NUMBER IS GIVEN BY THE USER--------------------*/ 
void suppCompt (int numCpt) 
{ CompteBancaire *pnt=first; 
    if (first==NULL) 
     printf("la liste vide !\n"); 
    else 
    { while (pnt!=NULL && pnt->idf.numCompte!=numCpt) 
       pnt=pnt->next; 
     if (pnt==first) // remove in beginning 
     { first=pnt->next; 
      free(pnt); 
     } 
     else if (pnt->next==NULL) // remove at the end 
     { (pnt->prc)->next=NULL; 
      free(pnt); 
     } 
     else // remove in the middle 
     { (pnt->prc)->next=pnt->next; 
      (pnt->next)->prc=pnt->prc; 
      free(pnt); 
     } 
     } 
    } 
    } 
/* ------------------REMOVE THE ACCOUNT IF THE BALANCE IS LESS THEN 0 DH--------------------------*/ 
void suppInf00() 
{CompteBancaire *pnt2=first,*pnt=NULL; 
    if (first==NULL) 
     printf("la liste vide !\n"); 
    else 
    { while (pnt2!=NULL) { 
     pnt=pnt2; 
     if (pnt->solde <0) { 
     if (pnt==first) // remove in the beginning 
     { first=pnt->next; 
      free(pnt); 
     else if (pnt->next==NULL) // remove at the end 
     { (pnt->prc)->next=NULL; 
      free(pnt); 
     } 
     else // remove in the middle 
     { (pnt->prc)->next=pnt->next; 
      (pnt->next)->prc=pnt->prc; 
      free(pnt); 
     } 
     } 
     pnt2=pnt2->next; 
    } 
    } 
    } 

} 
/* -----------CREAT A LISTE OF YOUNG PEAPLE--------------------- */ 
void under35() 
{ ClientJeune nov,*pnt=first; 
    if (first==NUll) 
     printf("liste vide !\n"); 
    else 
    { while (pnt!=NULL) 
     if (2015-(pnt->idt.dn.a_n)<=35) 
     { nov->Cpt=pnt; 
      if (first2==NULL) 
      nov->next=NULL; 
      else 
      { nov->next=first2; 
       first2=nov; 
      } 
     } 
    } 
} 
/* -----------------DISPLAY ALL ACCOUNTS---------------------*/ 
void afficage() 
{CompteBancaire *pnt=first; 
if(first==NULL) 
    printf("liste vide !\n"); 
else 
{ while (pnt!=NULL) 
    printf("nom: %s\nprenom: %s\ndate de naissance: %d/%d/%d\n num de compte: %d\nnom de la banque: %s\nsolde: %.2f\n\n", 
      pnt->idt.nom,pnt->idt.prenom,pnt->idt.dn.j_n,pnt->idt.dn.m_n,pnt->idt.dn.a_n,pnt->idf.nomCompte,pnt->idf.nomBanque); 
    pnt=pnt->next; 
} 

} 
int main() 
{int n=0,c,j,m,a,numC; 
float S=0; 
char A[15],B[15],C[15]; 
printf("-----------------Menu-------------------\n"); 
printf("1----------------------------------Saisi\n"); 
printf("2----------------------------------affichage par num de compte \n"); 
printf("3----------------------------------Suppression d'un compte \n"); 
printf("4----------------------------------Supp inf 00 DH\n"); 
printf("5----------------------------------under 35ans\n"); 
printf("6----------------------------------Affichage\n\n"); 
while (n==1) 
{ 
    printf("donner votre choix :"); 
    scanf("%c",&c); 
    switch (c) 
    { case 1:printf("donner le nom:");scanf("%s",A); 
      printf("donner le prenom:");scanf("%s",B); 
      printf("donner la date de naissance jj mm aa:");scanf("%d %d %d",&j,&m,&a); 
      printf("donner le num du compte:");scanf("%d",&numC); 
      printf("donner le nom de la banque:");scanf("%s",C); 
      printf("donner le solde :");scanf("%f",&S); 
      saisiNovCompt(A,B,j,m,a,numC,C,S);break; 
    case 2:printf("donner le num du compte:");scanf("%d",&numC); 
      afficheParNum(numC);break; 
    case 3:printf("donner le num du compte:");scanf("%d",&numC); 
      suppCompt(numC);break; 
    case 4:suppInf00(); 
      printf("suppression solde inf 0 à ete efectue\n");break; 
    case 5:under35(); 
      printf("une nouvelle liste a ete creer\n");break; 
    case 6:affichage(); 
    default:printf("tapper 1/2/3/4/5/6 !!\n"); 
    } 
    printf("tapper 1:"); 
    scanf("%d",&n); 
} 
    return 0; 
} 
+0

무엇이 문제입니까? 귀하의 코드가 작동합니까? 그렇지 않습니까? 또한 코딩하기에 더 읽기 쉬운 스타일을 선택하십시오. 코드를 따라하기가 매우 어렵습니다. 모든 문자를 서로 나란히 놓으십시오. 컴파일러에서 코드를 쉽게 토큰화할 수 있지만 인간의 눈에는 모두 하나의 거대한 토큰입니다. 공백을 많이 사용하십시오. 물론 조언 일뿐입니다. –

+0

'n'을 0으로 초기화합니다.'main'의'while' 루프가 실행되지 않습니다. –

+0

당신이 사용하고있는 툴셋을 모르겠지만, * 컴파일해서는 안됩니다. 'CompteBancaire' 나'ClientJeune'도 제대로 선언되지 않았고'CompteBancaire * nov = malloc (sizeof (CompteBancaire)), pnt = first;'포인터 * ('nov')와 정규 * 인스턴스 * (' 'pnt'), 후자는 포인터가 할당된다. 이것은 합법적이지 않다. 'FILE * '을 전달하지 않고'fprintf'를 호출하고 있습니다. 솔직히이 코드는 엉망입니다. – WhozCraig

답변

1

내가 제대로 이해하면 나도 몰라,하지만 당신은 구조체를 초기화 할 및 할당하는 경우는 포인터로 ADRESS의

당신이 할 수 있습니다 당신이

struct XXX 
{ 
    T1 arg1; 
    T2 arg2; 
    T3 arg3; 
    . 
    . 
    . 
}; 

있다고 가정

struct XXX *ptr = &(struct XXX){arg1, arg2, arg3, ...}; 

아마도 이것이 가장 우아한 방법은 아니지만 작동합니다.

+0

'struct XXX'는 어디에 있나요? 어쩌면 이것이 가장 우아한 방법은 아니지만 작동합니다. 너 시험 했니? –

+0

@iharob 나는 나의 대답을 편집 할 것이다. 그리고 네, 테스트 해 보았습니다. 저는 이것을 프로그램에서 사용합니다. – Chostakovitch

+0

지금 내가 무슨 뜻인지 이해하길 바란다. –

관련 문제