- C語言程序基礎(chǔ)練習(xí)題帶答案 推薦度:
- 相關(guān)推薦
C語言的基礎(chǔ)練習(xí)
一、實驗?zāi)康?/strong>
對C語言的復(fù)習(xí),增強學(xué)生對結(jié)構(gòu)體數(shù)組和指針的學(xué)習(xí),尤以結(jié)構(gòu)體的應(yīng)用和指針的操作作為重點。
二、問題描述
1、 構(gòu)造一個學(xué)生結(jié)構(gòu)體,成員包括學(xué)號,姓名,四門成績,以及平均成績;
2、 從鍵盤上輸入學(xué)生的學(xué)號,姓名和四門成績;
3、 找出學(xué)生中考試沒有通過的學(xué)生姓名并輸出;找出考試在90分以上的學(xué)生并輸出。
三、實驗要求
1、 要求分別用數(shù)組和鏈表存儲學(xué)生的記錄,并設(shè)計出輸入和查找的基本操作算法。
2、 在實驗過程中,分析算法的時間復(fù)雜度和空間復(fù)雜度進行分析。
四、實驗環(huán)境
PC微機
DOS操作系統(tǒng)或 Windows 操作系統(tǒng)
Turbo C 程序集成環(huán)境或 Visual C++ 程序集成環(huán)境
五、實驗步驟
1、用所選擇的語言實現(xiàn)算法;
3、 測試程序,并對算法進行時間和空間復(fù)雜度分析。
結(jié)構(gòu)體數(shù)組方法及測試結(jié)果:
#include
using namespace std;
struct student
{
int num;
char name[20];
float score[4];
float ave;
}; //構(gòu)造結(jié)構(gòu)體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].num;
i=0;
while (stu[i].num)
{
cout<<"Please input the name:"; cin>>stu[;
cout<<"Please input the scores:"; stu[i].ave-0;
for (j=0;j<=3;j++)
{
cin>>stu[i].score[j];
stu[i].ave+=stu[i].score[j];
}
stu[i].ave=stu[i].ave/4.0;
i++;
cout<<"Please input the number:(0 is end)"; cin>>stu[i].num;
} n=i-1;
k=m=0;
for (i=0;i<=n;i++)
{
if (stu[i].ave>90)
a[k++]=i; else
{
for (j=0;j<=3;j++)
if (stu[i].score[j]<60)
{
b[m++]=i; goto loop;
}
} //以上為輸入操作 //使用a[]存放90分以上的學(xué)生位置//使用a[]存放未通過的學(xué)生位置 {
loop:; } //以上為查找操作 if (k>0)
{ for (i=0;i<=k-1;i++) cout<<stu[a[i]].name<<" ";
cout<<"is(are) 90 above."<<endl;
} //輸出90以上的學(xué)生 if (m>0) {
for (i=0;i<=m-1;i++)
} cout<<stu[b[i]].name<<" "; cout<<"did not pass the exam."<<endl; } //輸出未通過學(xué)生 return 0;
鏈表方法及測試結(jié)果:
#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以上的學(xué)生//輸出未通過的學(xué)生
【C語言的基礎(chǔ)練習(xí)】相關(guān)文章:
C語言練習(xí)08-22
c語言基礎(chǔ)習(xí)題10-13
C語言編程基礎(chǔ)08-17
C語言基礎(chǔ)常識10-28
C語言作業(yè)練習(xí)10-24
C語言練習(xí)試題07-01
C 語言基礎(chǔ)教程07-22
C語言基礎(chǔ)知識10-13