学历改变命运
24小时客服:4008135555/010-82335555
当前位置:首页 > 笔记串讲 > 自考“高级语言程序设计”上机考点串讲(2

自考“高级语言程序设计”上机考点串讲(2)

2007年05月29日    来源:   字体:   打印

  4、结构型的应用(第7章)

  1)内容:编写一个含有结构型数组的程序,包括结构型数据输入、加工、输出。

  2)要求:熟悉结构型的定义、结构型数据的定义、初始化和成员引用方法。

  3)案例:设有学生信息如下:学号(长整型)、姓名(字符串型)、出生年月(其中含有年份、月份、日,均为整型)。试编一个程序,输入10个学生的上述信息,输出所有学生的学号、姓名和年龄。(注意程序中年龄的求法,是用系统日期中的年份减去出生年月中的年份。获得系统日期的方法是通过系统函数getdate())(注:程序命名为e1_4.exe)

  程序清单:

  #define N 10

  #include “stdio.h”

  #include “dos.h”

  main( )

  {struct birthday

  {int year;

  int month;

  int day;

  };

  struct

  {long num;

  char name[20];

  struct birthday bir;

  }stu[N];

  struct date today; /*利用系统定义的日期型结构定义变量today*/

  int i;

  for(i=0;i

  {

  scanf(“%ld”,&stu[i].num);

  scanf(“%s”,stu[i].name);

  scanf(“%d,%d,%d”,&stu[i].bir.year, &stu[i].bir.month, &stu[i].bir.day);

  }

  getdate(&today); /*通过系统函数获得系统日期*/

  for(i=0;i

  {

  printf(“%-8ld”,stu[i].num);

  printf(“%-2ld”,stu[i].name);

  printf(“%-6ld\n”,today.da_year-stu[i].bir.year);

  }

  }

  5、文件的应用(第8章)

  1)内容:编写一个对文件进行简单处理的程序,包括对文件的读写。

  2)要求:熟悉文件型指针的定义和引用,以及文件处理函数的使用。

  3)案例:编一C程序,它能从当前目录下名为“string.txt”的文本文件中读取一个字符串或前20个字符组成的字符串,并显示在屏幕上。(注:可执行程序命名为e1_5.exe)

  程序清单:

  #include“stdio.h”

  main()

  {FILE *fp;

  char s[21];

  if ((fp=fopen(“string.txt”, “r”))==NULL)

  {printf(“string.txt can not open!\n”);

  exit(0);

  }

  fgets(s,21,fp);

  fputs(s,stdout);

  fclose(fp);

  }

  …………………………

关闭