C语言 百分网手机站

C语言的基础练习

时间:2020-11-21 17:11:28 C语言 我要投稿

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].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分以上的学生位置//使用a[]存放未通过的学生位置 {

  loop:; } //以上为查找操作 if (k>0)

  { for (i=0;i<=k-1;i++) cout<<stu[a[i]].name<<" ";

  cout<<"is(are) 90 above."<<endl;

  }             //输出90以上的学生 if (m>0) {

  for (i=0;i<=m-1;i++)

  } cout<<stu[b[i]].name<<" "; 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语言的基础练习】相关文章:

1.c语言基础习题

2.C语言基础常识

3.C语言程序基础练习题带答案

4.C 语言基础教程

5.C语言基础小知识

6.C语言练习题

7.c语言公共基础知识

8.C语言程序基础知识