C語言的基礎練習
一、實驗目的
對C語言的複習,增強學生對結構體數組和指針的'學習,尤以結構體的應用和指針的操作作為重點。
二、問題描述
1、 構造一個學生結構體,成員包括學號,姓名,四門成績,以及平均成績;
2、 從鍵盤上輸入學生的學號,姓名和四門成績;
3、 找出學生會考試沒有通過的學生姓名並輸出;找出考試在90分以上的學生並輸出。
三、實驗要求
1、 要求分別用數組和鏈表存儲學生的記錄,並設計出輸入和查找的基本操作算法。
2、 在實驗過程中,分析算法的時間複雜度和空間複雜度進行分析。
四、實驗環境
PC微機
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以上的學生//輸出未通過的學生
-
C語言數組的定義及引用
引導語:數組是在程序設計中,為了處理方便,把具有相同類型的若干變量按有序的形式組織起來的一種形式。以下是本站小編分享給大家的C語言數組,希望大家喜歡!1.1一維數組的定義、初始化和引用1.一維數組的定義方式為:類型説明符數組名[常量表達式](1)數組名的命名方法...
-
在VS下如何運行C語言程序
微軟後來對VC6.0進行了升級,並更名為VisualStudio(簡稱VS),支持更多的編程語言,更加強大的功能,不過VisualStudio文件很大,有2~3G左右,大部分功能初學者暫時不會用到;而且安裝繁瑣,需要將近半個小時的時間,也不容易卸載乾淨。VisualStudio還有一個缺點是佔用資源較多,不...
-
計算機c語言基礎知識
C語言程序設計是高效計算機專業學生必修的一門基礎課程,那麼你對計算機c語言瞭解多少呢?下面是小編整理的計算機c語言基礎知識,歡迎大家閲讀參考。計算機c語言的特性C語言是世界上最流行、使用最廣泛的高級程序設計語言之一。在操作系統和系統使用程序以及需要對...
-
2017年計算機二級考試C語言備考題及答案
計算機二級對於很多考生來説還是比較有難度的,那麼怎樣順利通過二級考試呢?這就需要大家平時多練習和找方法了。以下是本站小編整理的2017年計算機二級考試C語言備考題及答案,希望對大家有幫助!1.(A)是構成C語言程序的基本單位。A、函數B、過程C、子程序D、子例...