一、實驗目的
對C語言的複習,增強學生對結構體陣列和指標的'學習,尤以結構體的應用和指標的操作作為重點。
二、問題描述
1、 構造一個學生結構體,成員包括學號,姓名,四門成績,以及平均成績;
2、 從鍵盤上輸入學生的學號,姓名和四門成績;
3、 找出學生會考試沒有通過的學生姓名並輸出;找出考試在90分以上的學生並輸出。
三、實驗要求
1、 要求分別用陣列和連結串列儲存學生的記錄,並設計出輸入和查詢的基本操作演算法。
2、 在實驗過程中,分析演算法的時間複雜度和空間複雜度進行分析。
四、實驗環境
PC微機
DOS作業系統或 Windows 作業系統
Turbo C 程式整合環境或 Visual C++ 程式整合環境
五、實驗步驟
1、用所選擇的語言實現演算法;
3、 測試程式,並對演算法進行時間和空間複雜度分析。
結構體陣列方法及測試結果:
#include
using namespace std;
struct student
{
int num;
char name[20];
float score[4];
float ave;
}; //構造結構體student int i,j,k,m,n,a[100],b[100];
struct student stu[100];
int main()
cout<<"Please input the number:(0 is end)"; cin>>stu[0];
i=0;
while (stu[i])
{
cout<<"Please input the name:"; cin>>stu[;
cout<<"Please input the scores:"; stu[i]-0;
for (j=0;j<=3;j++)
{
cin>>stu[i]e[j];
stu[i]+=stu[i]e[j];
}
stu[i]=stu[i];
i++;
cout<<"Please input the number:(0 is end)"; cin>>stu[i];
} n=i-1;
k=m=0;
for (i=0;i<=n;i++)
{
if (stu[i]>90)
a[k++]=i; else
{
for (j=0;j<=3;j++)
if (stu[i]e[j]<60)
{
b[m++]=i; goto loop;
}
} //以上為輸入操作 //使用a[]存放90分以上的學生位置//使用a[]存放未通過的學生位置 {
loop:; } //以上為查詢操作 if (k>0)
{ for (i=0;i<=k-1;i++) cout<<stu[a[i]]<<" ";
cout<<"is(are) 90 above."<<endl;
} //輸出90以上的學生 if (m>0) {
for (i=0;i<=m-1;i++)
} cout<<stu[b[i]]<<" "; cout<<"did not pass the exam."<<endl; } //輸出未通過學生 return 0;
連結串列方法及測試結果:
#include
using namespace std;
struct student
{
long num;
char name[20];
float score[4];
float ave;
struct student *next;
};
int main()
{
struct student *head,*p,*q;
int number,k,j,m,i;
char *a[100],*b[100];
head=0;
k=0;
cout<<"input the number of student:"; cin>>number;
while (number!=0)
{
k++;
p=new student;
p->num=number;
cout<<"Please input the name:"; cin>>p->name;
cout<<"Please input the scores:"; p->ave=0;
for (j=0;j<=3;j++)
{
cin>>p->score[j];
p->ave+=p->score[j];
}
p->ave=p->ave/4.0;
if (k==1)
head=p;
else q->next=p;
q=p;
cout<<"Please input the number:(0 is end)"; cin>>number;
} p->next=0;
p=head;
k=m=0;
while (p) //以上為輸入操作
{
if (p->ave>90)
a[k++]=p->name; else
{
for (j=0;j<=3;j++) if="" p-="">score[j]<60) {
b[m++]=p->name; goto loop;
}
}
loop: p=p->next;
} if (k>0)
{
for (i=0;i<=k-1;i++)
cout<<a[i]<<" ";
cout<<"is(are) 90 above."<0)
{
for (i=0;i<=m-1;i++)
cout<<b[i]<<" ";
cout<<"did not pass the exam."<<endl; } return 0;
} //以上為查詢操作 //輸出90以上的學生//輸出未通過的學生