全國計(jì)算機(jī)等級考試三級C語言上機(jī)題61-65

字號:

★題目61
    已知在文件IN.DAT中存有100個產(chǎn)品銷售記錄,每個產(chǎn)品銷售記錄由產(chǎn)品代碼dm(字符型4位),產(chǎn)品名稱mc(字符型10位),單價dj(整型),數(shù)量sl(整型),金額je(長整型)五部分組成。其中:金額=單價*數(shù)量計(jì)算得出。函數(shù)ReadDat()是讀取這100個銷售記錄并存入結(jié)構(gòu)數(shù)組sell中。請編制函數(shù)SortDat(),其功能要求:按金額從小到大進(jìn)行排列,若金額相同,則按產(chǎn)品代碼從大到小進(jìn)行排列,最終排列結(jié)果仍存入結(jié)構(gòu)數(shù)組sell中,最后調(diào)用函數(shù)WriteDat()把結(jié)果輸出到文件OUT2.DAT中。
    部分源程序存在文件prog1.c中。
    請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
    #include
    #include
    #include
    #include
    #include
    #define MAX 100
    typedef struct{
    char dm[5]; /*產(chǎn)品代碼*/
    char mc[11]; /*產(chǎn)品名稱*/
    int dj; /*單價*/
    int sl; /*數(shù)量*/
    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.je>sell[j].je||sell.je==sell[j].je&&strcmp(sell.dm,sell[j].dm)<0)
    {xy=sell; sell=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.dm,str,4);
    memcpy(sell.mc,str+4,10);
    memcpy(ch,str+14,4);ch[4]=0;
    sell.dj=atoi(ch);
    memcpy(ch,str+18,5);ch[5]=0;
    sell.sl=atoi(ch);
    sell.je=(long)sell.dj*sell.sl;
    }
    fclose(fp);
    }
    void WriteDat(void)
    {
    FILE *fp;
    int i;
    fp=fopen("OUT2.DAT","w");
    for(i=0;i<100;i++){
    printf("%s %s %4d %5d %5d\n", sell.dm,sell.mc,sell.dj,sell.sl,sell.je);
    fprintf(fp,"%s %s %4d %5d %5d\n", sell.dm,sell.mc,sell.dj,sell.sl,sell.je);
    }
    fclose(fp);
    }
    ☆ 題目62
    函數(shù)ReadDat()實(shí)現(xiàn)從文件ENG.IN中讀取一篇英文文章,存入到字符串?dāng)?shù)組xx中;請編制函數(shù)encryptChar(),按給定的替代關(guān)系對數(shù)組xx中的所有字符進(jìn)行替代,仍存入數(shù)組xx的對應(yīng)的位置上,最后調(diào)用函數(shù)WriteDat()把結(jié)果xx輸出到文件PS2.DAT中。
    替代關(guān)系:f(p)=p*13 mod 256(p是數(shù)組中某一個字符的ASCII值,f(p)是計(jì)算后新字符的ASCII值),如果計(jì)算后f(p)值小于等于32或其ASCII值是偶數(shù),則該字符不變,否則將f(p)所對應(yīng)的字符進(jìn)行替代。
    部分源程序存在文件prog1.c中。原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符。
    請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
    #include
    #include
    #include
    #include
    unsigned char xx[50][80];
    int maxline=0;/*文章的總行數(shù)*/
    int ReadDat(void);
    void WriteDat(void);
    void encryptChar()
    { int i,j;
    for(i=0;i    for(j=0;j    if(xx[j]*13%256<=32||(xx[j]*13%256)%2==0) continue;
    else xx[j]=xx[j]*13%256;
    }
    void main()
    {
    clrscr();
    if(ReadDat()){
    printf("數(shù)據(jù)文件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,80,fp)!=NULL){
    p=strchr(xx,'\n');
    if(p)*p=0;
    i++;
    }
    maxline=i;
    fclose(fp);
    return 0;
    }
    void WriteDat(void)
    {
    FILE *fp;
    int i;
    fp=fopen("ps2.dat","w");
    for(i=0;i    printf("%s\n",xx);
    fprintf(fp,"%s\n",xx);
    }
    fclose(fp);
    }