打印本文 打印本文  关闭窗口 关闭窗口  
三级C语言程序设计上机考试习题集(91100)
作者:佚名  文章来源:不详  点击数  更新时间:2007/12/21 18:07:08  文章录入:杜斌  责任编辑:杜斌

*****************************************************************************************
题目91(无忧id  52 整数统计运算题)

请编制程序prog1.c,从文件in.dat中读取200个整数至数组xx中,求出奇数的个数cnt1和偶数的个数cnt2以及数组xx下标为偶数的元素值的算术平均值pj(保留2位小数)。
     结果cnt1,cnt2,pj输出到out.dat中。
     部分程序、读数据函数read_dat(int xx[200])及输出格式已给出。
#include <conio.h>
#include <stdio.h>
#define n 200

void read_dat(int xx[n])
{
 int i,j;
 file *fp;
fp=fopen('in.dat','r');
 for(i=0;i<20;i++){
     for(j=0;j<10;j++){
        fscanf(fp,'%d,',&xx[i*10+j]);
        printf('%d ',xx[i*10+j]);
  }
  printf('/n');
 }
 fclose(fp);
}

void main()
{
 int i,j,sum;
 int cnt1,cnt2,xx[n];
 float pj;
 file *fw;
 clrscr();
 fw=fopen('out.dat','w');
 read_dat(xx);
/**************************************/
 sum=0;  pj=0.0;  cnt1=cnt2=0;
 for(i=0;i<n;i++)
  { if(xx[i]%2) cnt1++;
    else cnt2++;
    if(i%2==0) {pj+=xx[i];sum++;}
  }
 pj/=sum;

/**************************************/

printf('/n/ncnt1=%d,cnt2=%d,pj=%6.2f/n',cnt1,cnt2,pj);
fprintf(fw,'%d/n%d/n%6.2f/n',cnt1,cnt2,pj);
fclose(fw);
}
*****************************************************************************************
☆题目92(无忧id 56 整数统计运算题)

请编制程序prog1.c,从文件in.dat中读取200个整数至数组xx中,求出最大数max及最大数的个数cnt和数组xx中能被3整除或能被7整除的算术平均值pj(保留2位小数)。
     结果max,cnt,pj输出到out.dat中。
     部分程序、读数据函数read_dat(int xx[200])及输出格式已给出。
#include <conio.h>
#include <stdio.h>
#define n 200

void read_dat(int xx[n])
{
 int i,j;
 file *fp;
fp=fopen('in.dat','r');
 for(i=0;i<20;i++){
     for(j=0;j<10;j++){
        fscanf(fp,'%d,',&xx[i*10+j]);
        printf('%d',xx[i*10+j]);
  }
  printf('/n');
 }
 fclose(fp);
}

void main()
{
 int m,temp,n,sum;
 int cnt,xx[n],max ;
 float pj;
 file *fw;
clrscr();
 fw=fopen('out.dat','w');
 read_dat(xx);
/********************************************/
 cnt=0;  max=xx[0];  pj=0.0;  n=0;
 for(m=0;m<n;m++)
   if(max<xx[m]) max=xx[m];
 for(m=0;m<n;m++)
   {if(xx[m]==max) cnt++;
    if(xx[m]%3==0||xx[m]%7==0)
       { pj+=xx[m];  n++; }
   }
 pj/=n;

/********************************************/

printf('/n/nmax=%d,cnt=%d,pj=%6.2f/n',max,cnt,pj);
fprintf(fw,'%d/n%d/n%6.2f/n',max,cnt,pj);
fclose(fw);
}
*****************************************************************************************
★题目93(无忧id 59 方差运算题)
此题南开标准解法有误!

请编制函数readdat()实现从文件in.dat中读取1000个十进制整数到数组xx中;请编制函数compute(),分别计算出xx中奇数的个数odd,偶数的个数even,平均值aver以及方差totfc的值,最后调用函数writedat()把结果输出到out.dat文件中。
     计算方差的公式如下:
             n        2
     totfc=1/n ∑  (xx[i]-aver)
              i=1
     原始数据文件存放的格式是:每行存放10个数,并用逗号隔开。(每个数均大于0且小于等于2000)
     部分源程序存在文件prog1.c中。
     请勿改动主函数main()和输出数据函数writedat()的内容。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 1000

int xx[max],odd=0,even=0;
double aver=0.0,totfc=0.0;
void writedat(void);
int readdat(void)
{
 int i;
 file *fp;
 if((fp=fopen('in.dat','r'))==null) return 1;
/***************编制函数readdat()*****************/
 for(i=0;i<max;i++)
  { fscanf(fp,'%d,',&xx[i]);
    if((i+1)%10==0)
    fscanf(fp,'/n');
  }
/*********************************************/
 fclose(fp);
 return 0;
}

void compute(void)
{  int i;
  for(i=0;i<max;i++)
   { if(xx[i]%2)  odd++;
     else  even++;
     aver+=xx[i];
   }
  aver/=max;
  for(i=0;i<max;i++)
    totfc+=(xx[i]-aver)*(xx[i]-aver);
  totfc/=max;
}

void main()
{
 int i;
 for(i=0;i<max;i++)xx[i]=0;
 if(readdat()){
   printf('数据文件in.dat不能打开!/007/n');
   return;
 }
 compute();
 printf('odd=%d/noven=%d/naver=%f/ntotfc=%f/n',odd,even,aver,totfc);
 writedat();
}

void writedat(void)
{
 file *fp;
 int i;
 fp=fopen('out.dat','w');
 fprintf(fp,'%d/n%d/n%f/n%f/n',odd,even,aver,totfc);
 fclose(fp);
}
*****************************************************************************************
★题目94(无忧id 50 整数统计运算题)

请编制程序prog1.c,从文件in.dat中读取200个整数至数组xx中,求出奇数的个数cnt1和偶数的个数cnt2以及数组xx中值为偶数的算术平均值pj(保留2位小数)。
     结果cnt1,cnt2,pj输出到out.dat中。
     部分程序、读数据函数read_dat(int xx[200])及输出格式已给出。
#include <conio.h>
#include <stdio.h>
#define n 200

void read_dat(int xx[n])
{
 int i,j;
 file *fp;
fp=fopen('in.dat','r');
 for(i=0;i<20;i++){
     for(j=0;j<10;j++){
        fscanf(fp,'%d,',&xx[i*10+j]);
        printf('%d ',xx[i*10+j]);
  }
  printf('/n');
 }
 fclose(fp);
}

void main()
{
 int m,sum;
 int cnt1,cnt2,xx[n];
 float pj;
 file *fw;
  fw=fopen('out.dat','w');
 clrscr();
 read_dat(xx);
/****************************/
 cnt1=0;  cnt2=0;  pj=0.0;
 for(m=0;m<n;m++)
   if(xx[m]%2) cnt1++;
   else { cnt2++; pj+=xx[m];}
 if(cnt2==0) pj=0;
 else pj/=cnt2;
/****************************/
printf('/n/ncnt1=%d,cnt2=%d,pj=%6.2f/n',cnt1,cnt2,pj);
fprintf(fw,'%d/n%d/n%6.2f/n',cnt1,cnt2,pj);
fclose(fw);
}
*****************************************************************************************
题目95(无忧id 7字符替换题)

函数readdat()实现从文件eng.in中读取一篇英文文章,存入到字符串数组xx中;请编制函数encryptchar(),按给定的替代关系对数组xx中的所有字符进行替代,仍存入数组xx的对应的位置上,最后调用函数writedat()把结果xx输出到文件ps4.da中。
  替代关系:f(p)=p*11 mod 256(p是数组中某一个字符的ascii值,f(p)是计算后新字符的ascii值),如果计算后f(p)值小于等于32或f(p)对应的字符是大写字母,则该字符不变,否则将f(p)所对应的字符进行替代。
  部分源程序存在文件prog1.c中。原始数据文件存放的格式是:每行的宽度均小于80个字符。
  请勿改动主函数main()、读数据函数readdat()和输出数据函数writedat()的内容。
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

unsigned char xx[50][80];
int maxline=0;/*文章的总行数*/
int readdat(void);
void writedat(void);
void encryptchar()
{ int i,j;
  for(i=0;i<maxline;i++)
   for(j=0;j<strlen(xx[i]);j++)
    if(xx[i][j]*11%256<=32||xx[i][j]*11%256>='a'&&xx[i][j]*11%256<='z') continue;
    else xx[i][j]=xx[i][j]*11%256;
}

void main()
 {
 clrscr();
 if(readdat()){
  printf('数据文件eng.in不能打开!/n/007');
  return;
 }
 encryptchar();
 writedat();
}

int readdat(void)
{
 file *fp;
 int i=0;
 unsigned char *p;

 if((fp=fopen('eng.in','r'))==null) return 1;
 while(fgets(xx[i],80,fp)!=null){
   p=strchr(xx[i],'/n');
   if(p)*p=0;
   i++;
}
maxline=i;
fclose(fp);
return 0;
}

void writedat(void)
{
 file *fp;
 int i;
 fp=fopen('ps4.dat','w');
 for(i=0;i<maxline;i++){
 printf('%s/n',xx[i]);
 fprintf(fp,'%s/n',xx[i]);
 }
 fclose(fp);
}
此题还有许多解法,方法可看题8
*****************************************************************************************
★题目96(无忧id 87  字符替换题)

函数readdat()实现从文件eng.in中读取一篇英文文章,存入到字符串数组xx中;请编制函数encryptchar(),按给定的替代关系对数组xx中的所有字符进行替代,仍存入数组xx的对应的位置上,最后调用函数writedat()把结果xx输出到文件ps5.dat中。
  替代关系:f(p)=p*11mod 256 (p是数组中某一个字符的ascii值,f(p)是计算后新字符的ascii值),如果计算后f(p)值小于等于32或f(p)对应的字符是小写字母,则该字符不变,否则将f(p)所对应的字符进行替代。
  部分源程序存在文件prog1.c中。原始数据文件存放的格式是:每行的宽度均小于80个字符。
  请勿改动主函数main()、读数据函数readdat()和输出数据函数writedat()的内容。
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

unsigned char xx[50][80];
int maxline=0;/*文章的总行数*/
int readdat(void);
void writedat(void);
void encryptchar()
{ int i,j;
  for(i=0;i<maxline;i++)
   for(j=0;j<strlen(xx[i]);j++)
    if(xx[i][j]*11%256<=32||xx[i][j]*11%256>='a'&&xx[i][j]*11%256<='z') continue;
    else xx[i][j]=xx[i][j]*11%256;
}

void main()
 {
 clrscr();
 if(readdat()){
  printf('数据文件eng.in不能打开!/n/007');
  return;
 }
 encryptchar();
 writedat();
}

int readdat(void)
{
 file *fp;
 int i=0;
 unsigned char *p;
 if((fp=fopen('eng.in','r'))==null) return 1;
 while(fgets(xx[i],80,fp)!=null){
   p=strchr(xx[i],'/n');
   if(p)*p=0;
   i++;
}
maxline=i;
fclose(fp);
return 0;
}

void writedat(void)
{
 file *fp;
 int i;
 fp=fopen('ps5.dat','w');
 for(i=0;i<maxline;i++){
 printf('%s/n',xx[i]);
 fprintf(fp,'%s/n',xx[i]);
 }
 fclose(fp);
}
此题还有许多解法,方法可看题8
*****************************************************************************************
★☆题目97(无忧id 91 字符替换题)

函数readdat()实现从文件eng.in中读取一篇英文文章,存入到字符串数组xx中;请编制函数encryptchar(),按给定的替代关系对数组xx中的所有字符进行替代,仍存入数组xx的对应的位置上,最后调用函数writedat()把结果xx输出到文件ps9.dat中。
  替代关系:f(p)=p*11 mod 256(p是数组中某一个字符的ascii值,f(p)是计算后新字符的ascii值),如果原字符是数字字符0至9或计算后f(p)值小于等于32,则该字符不变,否则将f(p)所对应的字符进行替代。
  部分源程序存在文件prog1.c中。原始数据文件存放的格式是:每行的宽度均小于80个字符。
  请勿改动主函数main()、读数据函数readdat()和输出数据函数writedat()的内容。
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

unsigned char xx[50][80];
int maxline=0;/*文章的总行数*/
int readdat(void);
void writedat(void);
void encryptchar()
{ int i,j;
  for(i=0;i<maxline;i++)
   for(j=0;j<strlen(xx[i]);j++)
    if(xx[i][j]*11%256<=32||xx[i][j]>='0'&&xx[i][j]<='9') continue;
    else xx[i][j]=xx[i][j]*11%256;
}

void main()
 {
 clrscr();
 if(readdat()){
  printf('数据文件eng.in不能打开!/n/007');
  return;
 }
 encryptchar();
 writedat();
}

int readdat(void)
{
 file *fp;
 int i=0;
 unsigned char *p;
 if((fp=fopen('eng.in','r'))==null) return 1;
 while(fgets(xx[i],80,fp)!=null){
   p=strchr(xx[i],'/n');
   if(p)*p=0;
   i++;
}
maxline=i;
fclose(fp);
return 0;
}

void writedat(void)
{
 file *fp;
 int i;
 fp=fopen('ps9.dat','w');
 for(i=0;i<maxline;i++){
 printf('%s/n',xx[i]);
 fprintf(fp,'%s/n',xx[i]);
 }
 fclose(fp);
}
此题还有许多解法,方法可看题8
*****************************************************************************************
题目98(无忧id  85 字符替题)
(无忧id  85只是将替代关系改为了f(p)=p*11 mod 256))

函数readdat()实现从文件eng.in中读取一篇英文文章,存入到字符串数组xx中;请编制函数encryptchar(),按给定的替代关系对数组xx中的所有字符进行替代,仍存入数组xx的对应的位置上,最后调用函数writedat()把结果xx输出到文件ps3.dat中。
  替代关系:f(p)=p*17 mod 256(p是数组中某一个字符的ascii值,f(p)是计算后新字符的ascii值),如果计算后f(p)值小于等于32或其ascii值是奇数,则该字符不变,否则将f(p)所对应的字符进行替代。
  部分源程序存在文件prog1.c中。原始数据文件存放的格式是:每行的宽度均小于80个字符。
  请勿改动主函数main()、读数据函数readdat()和输出数据函数writedat()的内容。
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

unsigned char xx[50][80];
int maxline=0;/*文章的总行数*/
int readdat(void);
void writedat(void);
void encryptchar()
{ int i,j;
  for(i=0;i<maxline;i++)
   for(j=0;j<strlen(xx[i]);j++)
    if(xx[i][j]*17%256<=32||(xx[i][j]*17%256)%2!=0) continue;
    else xx[i][j]=xx[i][j]*17%256;
}

void main()
 {
 clrscr();
 if(readdat()){
  printf('数据文件eng.in不能打开!/n/007');
  return;
 }
 encryptchar();
 writedat();
}

int readdat(void)
{
 file *fp;
 int i=0;
 unsigned char *p;

 if((fp=fopen('eng.in','r'))==null) return 1;
 while(fgets(xx[i],80,fp)!=null){
   p=strchr(xx[i],'/n');
   if(p)*p=0;
   i++;
 }
maxline=i;
fclose(fp);
return 0;
}

void writedat(void)
{
 file *fp;
 int i;

 fp=fopen('ps3.dat','w');
 for(i=0;i<maxline;i++){
 printf('%s/n',xx[i]);
 fprintf(fp,'%s/n',xx[i]);
 }
 fclose(fp);
}
此题还有许多解法,方法可看题8
*****************************************************************************************
★题目99(无忧id  74结构体排列题)

已知在文件in.dat中存有100个产品销售记录,每个产品销售记录由产品代码dm(字符型4位),产品名称mc(字符型10位),单价dj(整型),数量sl(整型),金额je(长整型)五部分组成。其中:金额=单价*数量计算得出。函数readdat()是读取这100个销售记录并存入结构数组sell中。请编制函数sortdat(),其功能要求:按金额从小到大进行排列,若金额相等,则按产品代码从小到大进行排列,最终排列结果仍存入结构数组sell中,最后调用函数writedat()把结果输出到文件out1.dat中。
     部分源程序存在文件prog1.c中。
  请勿改动主函数main()、读数据函数readdat()和输出数据函数writedat()的内容。
#include <stdio.h>
#include <mem.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#define max 100
typedef struct{
  char dm[5];  /*产品代码*/
  char mc[11]; /*产品名称*/
  int dj;      /*单价*/
  int sl;      /*数量*/
  long je;     /*金额*/
}pro;
pro sell[max];
void readdat();
void writedat();
void sortdat()
{int i,j;
 pro xy;
 for(i=0;i<99;i++)
  for(j=i+1;j<100;j++)
    if(sell[i].je>sell[j].je||sell[i].je==sell[j].je&&strcmp(sell[i].dm,sell[j].dm)>0)
       {xy=sell[i];sell [i]=sell[j];sell[j]=xy;}
}

void main()
 {
 memset(sell,0,sizeof(sell));
 readdat();
 sortdat();
 writedat();
 }

void readdat()
{
 file *fp;
 char str[80],ch[11];
 int i;

 fp=fopen('in.dat','r');
 for(i=0;i<100;i++){
   fgets(str,80,fp);
   memcpy(sell[i].dm,str,4);
   memcpy(sell[i].mc,str+4,10);
   memcpy(ch,str+14,4);ch[4]=0;
   sell[i].dj=atoi(ch);
   memcpy(ch,str+18,5);ch[5]=0;
   sell[i].sl=atoi(ch);
   sell[i].je=(long)sell[i].dj*sell[i].sl;
  }
 fclose(fp);
}

void writedat()
{
 file *fp;
 int i;

 fp=fopen('out1.dat','w');
 for(i=0;i<100;i++){
  printf('%s %s %4d %5d %5d/n', sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
  fprintf(fp,'%s %s %4d %5d %5d/n', sell[i].dm,sell[i].mc,sell[i].dj,sell[i].sl,sell[i].je);
 }
 fclose(fp);
}

******************************************************************
eng.in(输入文件)
  isam enhances the functionality of your programs through its
flexibility. if you add a section to a book, remove a few pages,
or rearrange paragraphs or sections, you have to recreate your
index, since the keywords must appear in relation to each other.
in this case, the relationship of the keywords to each other is
alphabetic order. a isam index changes automatian employee, or
change anything in a record, isam immediately updates all indexes.
  you can create an index on any field, on several fields to be used
together, or on parts thereof, that you want to use as a key. the
keys in indexes allow you quick access to specific records and define
orders for sequential processing of a isam file. after you no longer
need an index, you can delete it. addition and indexes have no effect
on the data records or on other indexes.
  you may want a field in field in each record to uniquely identify that
record from all other records in the file. for example, the employee
number field is unique if you do not assign the same number to two
different employees, and you never reassign these numbers to other
employees. if you wish to find or modify the record belonging to a
specific employee, this unique field saves the thouble of determining
whether you have the correct record.
  if you do not have a unique field, you must find the first record
the matches your key and determine whether the record is the one you
want. if it is not the correct one, you must search again to find others.
  if you know that you have a unique field within your records, you
can include this fact in the key description, and isam will allow only
unique keys. for example, if you specify that the employee numbers are
unique, isam only lets you add records to the file for, or change
numbers to, employee numbers that do not alreadly exist int file.


****************************************************************************
ps6.out(输出文件)
isamenhancesfunctionalityitsofprogramsthethroughyour
ifaaaaddbookfewflexibilitypagesremovesectiontoyou
haveororparagraphsrearrangerecreatesectionstoyouyour
appeareachinindexkeywordsmustotherrelationsincetheto
incaseeachiskeywordsofotherrelationshipthethethisto
aisamalphabeticautomatianchangesemployeeindexororder
isamaallanythingchangeimmediatelyinindexesrecordupdates
youananybecancreatefieldfieldsindexononseveraltoused
theaaskeyonorpartsthatthereoftotogetherusewantyou
accessallowanddefineinindexeskeysquickrecordsspecifictoyou
afterisamafileforlongernoofordersprocessingsequentialyou
additionanandcandeleteeffecthaveindexindexesitneednoyou
dataindexesononorotherrecordsthe
youaeachfieldfieldidentifyininmayrecordthattouniquelywant
employeeforallexamplefilefrominotherrecordrecordsthethe
numberassigndofieldifisnotnumbersamethetotwouniqueyou
anddifferentemployeesnevernumbersotherreassignthesetoyou
ifabelongingemployeesfindmodifyorrecordthetotowishyou
determiningemployeefieldofsavesspecificthethisthoubleunique
correcthaverecordthewhetheryou
ifadofieldfindfirsthavemustnotrecordtheuniqueyouyou
anddetermineiskeymatchesonerecordthethethewhetheryouyour
ifagaincorrectfindisitmustnotoneotherssearchthetowantyou
ifafieldhaveknowrecordsthatuniquewithinyouyouyouyour
isamallowandcandescriptionfactinincludekeyonlythethiswill
forareemployeeexampleifkeysnumbersspecifythattheuniqueyou
isamaddchangefileforletsonlyorrecordsthetouniqueyou
alreadlydoemployeeexistfileintnotnumbersnumbersthatto

打印本文 打印本文  关闭窗口 关闭窗口