网站首页 语言 会计 电脑 医学 资格证 职场 文艺体育 范文
当前位置:书香门第 > 计算机 > C语言

C语言的基础练习

栏目: C语言 / 发布于: / 人气:2.24W

一、实验目的

C语言的基础练习

对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以上的学生//输出未通过的学生

Tags:语言