C語言之結構體和鏈表

1.實驗目的

1)掌握結構體的定義與使用;

2)掌握結構體數組的概念和應用;

3)掌握鏈表的概念、設計與實現、以及應用。

2.實驗內容

1)課本例題,9.7, 9.11, 9.12

 

2)輸入並運行以下程序:

#include <stdio.h>

union data

{

int i[2];

float a;

long b;

char c[4];

}u;

 

 

void main()

{

scanf("%d,%d",&u.i[0],&u.i[1]);

printf("i[0]=%d,i[1]=%d,a=%f,b=%ld,c[0]=%c,c[1]=%c,c[2]=%c,c[3]=%c\n",u.i[0],u.i[1],u. a,u.b,u.c[0],u.c[1],u.c[2],u.c[3]);

}

請分析以上程序運行的結果?

(3) 兒童信息鏈表構建

#include <stdio.h>

#include <stdlib.h>

 

struct Child  // 鏈表結點結構體

{

  // 結點數據

  float height;  float weight;  int age;  char gender;

  // 後繼結點地址

  struct Child *next;

};

 

typedef struct Child ChildNode;

 

void inputChild( ChildNode* child )  // 結點數據輸入函數

{

  printf( "Age: " );

  scanf( "%d", &child->age);

  getchar();

  printf( "Gender: " );

  scanf( "%c", &child->gender);

  printf( "Height: " );

  scanf( "%f", &child->height);

  printf( "Weight: " );

  scanf( "%f", &child->weight);

}

 

void printList( ChildNode* child )

{

  ChildNode *current;

  current = child;  // 遍歷指針初始狀態指向表頭

  while( current->next != NULL ){

    current = current->next;  // 當前指針移動到下一個結點

    printf( "Age  %d\t", current->age );

    printf( "Gender  %c\t", current->gender );

    printf( "Height  %f\t", current->height );

    printf( "Weight  %f\n", current->weight );

  }

}

 

ChildNode* createList( int n )  // 生成鏈表

{

  ChildNode *p, *child;  int i;

  // 建立一個空的頭結點

  child = (ChildNode*)malloc(sizeof(ChildNode));  

  child->next = NULL;  // 初始化指針域

  for( i = n; i > 0; --i ){  // 依次建立並向表頭插入結點

    // 創建新結點

    p = (ChildNode *)malloc(sizeof(ChildNode));

    inputChild( p );  // 輸入數據到新結點

    // 將新結點插入到表頭結點之後

    p->next = child->next;  child->next = p;

  }

  return child;

}

 

void main()

{

  ChildNode *childList = NULL;

  int n;

  printf( "Number of children: " );

  scanf( "%d", &n );

  childList = createList( n );  // 構造鏈表

  printList(childList);

}

 


例題1

#include<stdio.h>
#define N 3
struct Student
{   int num;
	char name[20];
	float score[3];
	float aver;
};
int main()
{
	void input(struct Student stu[]);
	struct Student max(struct Student stu[]);
	void print(struct Student stu);
	struct Student stu[N],*p=stu;
	input(p);
	print(max(p));
	return 0;
}



void input(struct Student stu[])
{   int i;
    printf("請輸入各學生的信息:學號、姓名、三門課成績:\n");
    for(i=0;i<N;i++)
	{scanf("%d%s%f%f%f",&stu[i].num ,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
    stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;}
}



struct Student max(struct Student stu[])
{   int i,m=0;
    for(i=0;i<N;i++)
		if(stu[i].aver>stu[m].aver) m=i;
	return stu[m];
}


void print(struct Student stud)
{   printf("\n成績最高的學生是:\n");
    printf("學號:%d\n姓名:%s\n三門課成績:%5.1f,%5.1f,%5.1f\n平均成績:%6.2f\n",
		stud.num ,stud.name ,stud.score[0],stud.score[1],stud.score[2],stud.aver);
}


例題2

#include<stdio.h>
struct
{ int num;
  char name[10];
  char sex;
  char job;
  union
  { int clas;
    char position[10];
  }category;
}person[2];

int main()
{ int i;
  for(i=0;i<2;i++)
  { printf("please enter the data of person:\n");
    scanf("%d%s%c%c",&person[i].num,&person[i].name,
		&person[i].sex,&person[i].job);
	if(person[i].job=='s')
		scanf("%d",&person[i].category.clas);
	else if(person[i].job=='t')
		scanf("%s",person[i].category.position);
	else
		printf("Input error!");
  }
  printf("\n");
  printf("No. name           sex job class/position\n");
  for(i=0;i<2;i++)
  {
	  if(person[i].job=='s')//若是學生
		  printf("%-6d%-10s%-4c%-4c%-10d\n",person[i].num,person[i].name,
		  person[i].sex,person[i].job,person[i].category.clas);
	  else//若是老師
		  printf("%-6d%-10s%-4c%-4c%-10d\n",person[i].num,person[i].name,
		  person[i].sex,person[i].job,person[i].category.clas);
  }
  return 0;
}


例題3

#include<stdio.h>
int main()
{
	enum Color{red,yellow,blue,white,black};                               //聲明枚舉類型enum Color
    int i,j,k,pri;                                                         //定義枚舉變量i,j,k,pri
	int n,loop;
	n=0;
	for(i=red;i<=black;i++)                                                //外循環使i的值從red變到black
		for(j=red;j<=black;j++)                                            //中循環使j的值從red變到black
			if(i!=j)                                                       //如果兩球不同色
			{ for(k=red;k<=black;k++)                                      //內循環使k的值從red變到black
			  if((k!=i)&&(k!=j))                                           //如果三球不同色
			  { n=n+1;                                                     //符合條件的次數加1
			    printf("%-4d",n);                                          //輸出當前是第幾個符合條件的組合
				for(loop=1;loop<=3;loop++)                                 //先後對三個球分別處理
				{ 
					switch(loop)                                           //loop的值從1變到3
					{ case 1:pri=i;break;                                  //loop的值爲1時,把第1個球的顏色賦給pri
					  case 2:pri=j;break;                                  //loop的值爲2時,把第2個球的顏色賦給pri
					  case 3:pri=k;break;                                  //loop的值爲3時,把第3個球的顏色賦給pri
					  default:break;
					}
					switch(pri)                                            //根據球的顏色輸出相應的文字
					{ case red:printf("%-10s","red");break;                //pri的值等於枚舉常量red時輸出"red"
					  case yellow:printf("%-10s","yellow");break;          //pri的值等於枚舉常量yellow時輸出"yellow"
					  case blue:printf("%-10s","blue");break;              //pri的值等於枚舉常量blue時輸出"blue"
					  case white:printf("%-10s","white");break;            //pri的值等於枚舉常量white時輸出"white"
					  case black:printf("%-10s","black");break;            //pri的值等於枚舉常量black時輸出"black"
					  default:break;
					}
				}
				printf("\n");
			}}
	printf("\ntotal:%5d\n",n);
	return 0;
}
    






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章