struct list {
char name[12]; //科目名
int sub; //科目コード
struct test{
int seito; //生徒番号
int kekka[5]; //テスト結果
//※kekka[1]=1のように「問1ができたら1、できなかったら0」を入力する
}
struct list *next;
};
という入れ子の構造体にデータ入力をしたいのですが、入れ子状態でどのように使うかわかりません。
入れ子になっていない状態での入力は以下のプログラムを使っています。
struct list *write(int code, struct list *head)
{
char name[10];
int sub;
printf( "科目を入力:" ); scanf( "%s", name );
printf( "コードを入力:" ); (scanf("%d",&sub);
head = add_sort( name, sub, head );
return(head);
}
struct list *add_sort( char *str, int sub, struct list *head )
{
struct list *p, *new_p;
if ( ( new_p = ( struct list * )malloc( sizeof( struct list ) ) ) == NULL ) {
printf( "malloc error\n" ); exit( 1 );
}
strcpy( new_p->name, str );
new_p->sub = sub;
if ( head == NULL || sub > head->sub ) {
new_p->next = head;
return new_p;
}
for ( p = head; p->next != NULL; p = p->next )
if ( sub > p->next->sub ) break;
new_p->next = p->next;
p->next = new_p;
return head;
}
void dispall( struct list *p )
{
while ( p != NULL) {
printf( "%s %d\n", p->name, p->sub );
p = p->next;
}
}
void free_list( struct list *p )
{
struct list *p2;
while ( p != NULL ) {
p2 = p->next;
free( p );
p = p2;
}
}
int main( void )
{
struct list *head= NULL;
int code;
while( 1 ) {
printf( "入力:%d 表示:%d 終了:%d\n" ,1,2,3);
printf( " 処理>>" );
scanf( "%d", &code );
if ( code == 3 ) break;
else if ( code == 1 ){ head = write(code, head);
}else if ( code == 2T ){ dispall(head);
}else{
fflush(stdin);
}
}
free_list( head );
return 0;
}
write関数で入れ子の構造体データにデータ入力するように変更するにはどのように記述したらいいのでしょうか?
複数の生徒のデータを入力したいのですが、このソースコードで可能でしょうか?
どなたか教えていただけると助かります。