钱能C++教材习题答案七



文件信息
文件来源  
文件作者  
更新时间 2005-5-8 10:45:27 
添加编辑 viewsnake 

辅助信息
打印功能 打印本文
背景颜色 杏黄 秋褐 胭红 芥绿 天蓝 雪青 炭灰 奶白
字体大小 特大号字 大号字 中号字 小号字
免责声明 本网站所有文章均来自网络,仅提供预览形式,不提供纸张形式,若涉及到版权的文章,请购买正版,毕竟在电脑上看也不舒服啊,呵呵,这是viewsnake个人网站,纯粹交流学习资料的地方。无商业行为。
选择更多免费考研资料:
阅读正文内容
 

//********************
//*     8.3.(2)      *
//********************
# include <iostream.h>
# include <stdlib.h>

int compare(const void * a,const void * b);
double list1[10]={32.1,456.87,332.67,442.0,98.12,451.79,340.12,54.55,99.87,72.5};
double * list[10]={& list1[0],& list1[1],&list1[2],&list1[3],&list1[4],&list1[5],&list1[6],&list1[7],&list1[8],&list1[9]};

void main()
{
        qsort((void *) list,10,sizeof(list[0]),compare);
        for(int i=0;i<10;i++)
        {
                cout<<*list<<endl;
        }
}
int compare(const void * a,const void * b)
{
        double temp1=**(double**)a;
        double temp2=**(double**)b;
        if(temp1<temp2)
                return -1;
        else
        {
                if(temp1==temp2)
                        return 0;
                else
                        return 1;
        }
}

//********************
//*     8.3.(3)*******
//********************
# include <iostream.h>
# include <stdlib.h>
# include <string.h>

int compare(const void * a,const void * b);

char * list[10]={"enter","number","size","begin","of","cat","case","program","centain","a"};

void main()
{
        qsort((void *) list,10,sizeof(list[0]),compare);
        for(int i=0;i<10;i++)
        {
                cout<<list<<endl;
        }
}
int compare(const void * a,const void * b)
{
        int l,m;
        const char * p;
        const char * q;
        p=(const char *)a;
        q=(const char *)b;

        for(l=0; * p != '\0';l++)
                p++;
       
        for(m=0; * q != '\0';m++)
                q++;

        if(l<m)
                return -1;
        else
        {
                if(l>m)
                        return 1;
                else
                        return strcmp(*(char**)a,*(char**)b);
        }
}

//********************
//*     8.4***********
//********************
# include <iostream.h>
# include <stdlib.h>
# include <string.h>

char list[]={"the result of 3 and 2 is not 8"};
//注意使用list[]
char unlist[]={"xqk\"zlvyuz\"wm#7>gpl's$ry\"vvw$A"};
int num[]={4,9,6,2,8,7,3};

char * convert(char * a);
char * unconvert(char * b);

void main()
{
        char * m=list;
        char * n=convert(m);
        cout<<endl;
        for(;*n!='\0'
        {
                cout<<*n;
                n++;
        }
        cout<<endl<<endl;

       char * x=unlist;
        char * y=unconvert(unlist);
        cout<<endl;
        for(;*y!='\0'
        {
                cout<<*y;
                y++;
        }
        cout<<endl;
}

char * convert(char * a)
{

        int i=0,j,k=0;
        for(;*(a+k)!='\0'
        {
               
                j=num;
                i=(i+1)%7;
                cout<<"("<<*(a+k)<<""<<int(*(a+k))<<"+"<<j<<",";
                        a[k]=char((int(*(a+k))-33+j)%91+33);

                k++;
        }

        return a;
}

char * unconvert(char * b)
{
        int i=0,j,k=0;
        for(;*(b+k)!='\0'
        {
               
                j=num;
                i=(i+1)%7;
                cout<<"("<<*(b+k)<<""<<int(*(b+k))<<"+"<<j<<",";

                if(int(*(b+k))-33<j)
                        b[k]=char(91+(int(*(b+k))-j));//如何转换为原来的代码?
                else
                        b[k]=char(int(*(b+k))-j);

                k++;
        }

        return b;
}

//**********
//*     8.5*
//**********
# include <iostream.h>

int strcmp(const char * str1,const char * str2);

void main()
{
        char list1[]={" "};
        char list2[]={" "};
        int a=strcmp(list1,list2);
        cout<<a<<endl;
}

 

int strcmp(const char * str1,const char * str2)
{
        if((*str1=='\0')&&(*str2=='\0'))
                return 0;
        if((*str1=='\0')&&(*str2!='\0'))
                return -1;
        if((*str1!='\0')&&(*str2=='\0'))
                return 1;

        if(int(*str1)<int(*str2))
                return -1;
        else
                        if(int(*str1)>int(*str2))
                                return 1;
                        else
                        {
                                str1++;str2++;
                                return strcmp(str1,str2);
                        }
}

//**********
//*     8.6*
//**********
# include <iostream.h>

char * strcpy(char * dest,const char * source);

void main()
{
        char list1[]={"sdfhdahaha"};
        char * list2=NULL;
        list2=strcpy(list2,list1);
        cout<<list2<<endl;
}

 

char * strcpy(char * dest,const char * source)
{
        int k=0;
        const char * l=source;
        for(;*l!='\0';k++)
                l++;
        dest=new char[k+1];

        for(int i=0;i<k;i++)
        {
                dest=*source;
                source++;
        }

        dest[k]='\0';

        return dest;
}

//*************
//*     8.7****
//*************
# include <iostream.h>

int strcmp(const char * str1,const char * str2);

void main(int argc,char * argv[])
{
       

       
        int i=strcmp(argv[0],argv[1]);
        if (i==0)
                cout<<"equal"<<endl;
        else
                if (i=-1)
                        cout<<"<"<<endl;
                else
                        if (i=1)
                                cout<<">"<<endl;

}

 

int strcmp(const char * str1,const char * str2)
{
        if((*str1=='\0')&&(*str2=='\0'))
                return 0;
        if((*str1=='\0')&&(*str2!='\0'))
                return -1;
        if((*str1!='\0')&&(*str2=='\0'))
                return 1;

        if(int(*str1)<int(*str2))
                return -1;
        else
                        if(int(*str1)>int(*str2))
                                return 1;
                        else
                        {
                                str1++;str2++;
                                return strcmp(str1,str2);
                        }
}



<<<返回上一页 <<<返回网站首页
<<<您的位置:首页>考研经验>考研笔记>计算机工程笔记>正文