就业数据资源平台
当前位置:首页 > C语言程序设计
计算机等级考试二级C语言程序设计实战(96-100)

题目:计算字符串中子串出现的次数
  1.程序分析:源:中华考试
  2.程序源代码:
  #include "string.h"
  #include "stdio.h"
  main()
  { char str1[20],str2[20],*p1,*p2;
  int sum=0;
  printf("please input two strings\n");
  scanf("%s%s",str1,str2);
  p1=str1;p2=str2;
  while(*p1!='\0')
  {
  if(*p1==*p2)考试用书
  {while(*p1==*p2&&*p2!='\0')
  {p1++;
  p2++;}
  }
  else
  p1++;
  if(*p2=='\0')
  sum++;
  p2=str2;
  }
  printf("%d",sum);
  getch();}

97.题目:从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存。输入的字符串以!结束。
  1.程序分析:
  2.程序源代码:
  #include "stdio.h"
  main()
  {FILE *fp;
  char str[100],filename[10];
  int i=0;
  if((fp=fopen("test","w"))==NULL)
  { printf("cannot open the file\n");
  exit(0);}
  printf("please input a string:\n");
  gets(str);
  while(str[i]!='!')
  { if(str[i]>='a'&&str[i]<='z')
  str[i]=str[i]-32;
  fputc(str[i],fp);


98.题目:从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存。输入的字符串以!结束。
  1.程序分析:
  2.程序源代码:
  #include "stdio.h"
  main()
  {FILE *fp;采集者退散
  char str[100],filename[10];
  int i=0;
  if((fp=fopen("test","w"))==NULL)
  { printf("cannot open the file\n");
  exit(0);}
  printf("please input a string:\n");
  gets(str);考试大-全国最大教育类网站(www.Examda。com)
  while(str[i]!='!')
  { if(str[i]>='a'&&str[i]<='z')
  str[i]=str[i]-32;
  fputc(str[i],fp);
  i++;}
  fclose(fp);
  fp=fopen("test","r");
  fgets(str,strlen(str)+1,fp);
  printf("%s\n",str);
  fclose(fp);
  }


99.题目:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件C中。
  1.程序分析:
  2.程序源代码:
  #include "stdio.h"
  main()
  { FILE *fp;
  int i,j,n,ni;
  char c[160],t,ch;
  if((fp=fopen("A","r"))==NULL)
  {printf("file A cannot be opened\n");
  exit(0);}
  printf("\n A contents are :\n");
  for(i=0;(ch=fgetc(fp))!=EOF;i++)
  {c[i]=ch;
  putchar(c[i]);源:中华考试
  }
  fclose(fp);考试大-全国最大教育类网站(www.Examda。com)
  ni=i;
  if((fp=fopen("B","r"))==NULL)
  {printf("file B cannot be opened\n");
  exit(0);}
  printf("\n B contents are :\n");
  for(i=0;(ch=fgetc(fp))!=EOF;i++)
  {c[i]=ch;
  putchar(c[i]);
  }
  fclose(fp);
  n=i;
  for(i=0;i  for(j=i+1;j  if(c[i]>c[j])
  {t=c[i];c[i]=c[j];c[j]=t;}
  printf("\n C file is:\n");
  fp=fopen("C","w");
  for(i=0;i  { putc(c[i],fp);
  putchar(c[i]);
  }
  fclose(fp);
  }
100.题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
  1.程序分析:
  2.程序源代码:
  #include "stdio.h"
  struct studenthttp://www.Examw.com
  { char num[6];
  char name[8];
  int score[3];
  float avr;
  } stu[5];
  main()
  {int i,j,sum;
  FILE *fp;
  /*input*/
  for(i=0;i<5;i++)
  { printf("\n please input No. %d score:\n",i);
  printf("stuNo:");
  scanf("%s",stu[i].num);
  printf("name:");
  scanf("%s",stu[i].name);
  sum=0;
  for(j=0;j<3;j++)
  { printf("score %d.",j+1);
  scanf("%d",&stu[i].score[j]);
  sum+=stu[i].score[j];
  }
  stu[i].avr=sum/3.0;
  }
  fp=fopen("stud","w");
  for(i=0;i<5;i++)
  if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
  printf("file write error\n");
  fclose(fp);
  }
就业数据资源平台