The C++ Programming Language 第七章 作业

The C++ Programming Language 第七章 作业
james chen
050317

7.10.1
写出下面声明:一个函数,它以指向字符的指针和对整数的引用为参数,不返回值;一个指向这个函数的指针;一个以这种指针为参数的函数;以及一个返回这种指针的函数。写出一个函数的定义,它以一个这样的指针作为参数,并返回其参数作为返回值。
void f(char*,int&);
void (*g)(char*,int&);
typedef void (*h)(char*,int&)
void i(h);
h j(h);
------------------
#include<iostream>
using namespace std;
typedef void (*G)(char*,int&);  //定义函数指针
void f(char* a,int& b)   //目标函数
{
cout<<"a:"<<a<<endl;
cout<<"b:"<<b<<endl;
}
void i(G t,char* x,int& y)  //以函数指针为形参
{
t(x,y);
}
G h(G t)    //以函数指针为参数,返回一个函数指针
{return t;}
void main()
{
char a[]="i love you!!!";
int b=54321;
G p1=&f;
i(p1,a,b);
G p2=h(&f);
p2(a,b);
}


7.10.2
下面的代码是什么意思?他会有什么用处?
typedef int (&rifii)(int,int);
这是一个自定义类型,它引用一个函数,返回int,所引用的这个函数形参为两个int.
它可以将函数当变量来用,使用程序简化。例:
#include<iostream>
using namespace std;
typedef int(&rifii)(int,int);
int add(int a,int b)
{return a+b;}
int max(int a,int b)
{return (a>b)?a:b;}
void main()
{
 int a=10,b=15;
 rifii cx=add,cz=max;
 cout<<cz(a,b)<<endl;
 cout<<cx(a,b)<<endl;
}


7.10.3
写一个类似"hello world!"的函数,它以一个名字作为命令行参数,并写出"hello name"。修改这个函数,使它能以一系列名字作为参数,并对每个名字分别说hello。
#include<iostream>
using namespace std;
int main(int argc,char* argv[])
{
 for(int i=1;i<argc;i++)   //argv[0]为程序自身名字
  cout<<"hello,"<<argv[i]<<"!"<<endl;
 return 0;
}

D:/>7103 aaaa bbbb
hello,aaaa!
hello,bbbb!

7.10.7
考虑
struct Tnode{
 string word;
 int count;
 Tnode* left;
 Tnode* right;
};
写一个函数向Tnode的树中插入新单词。写一个函数将Tnode的树打印出来。写一个函数将Tnode的树按照单词的字典顺序打印出来。

#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;

//  Tnode* newTnode(Tnode*)  //插入新节点
//  void printt(Tnode*)   //打印节点
//  void prints(Tnode*)   //按顺序打印节点

struct Tnode
{
 string word;
 static int count;  //节点数置为静态全局变量
 Tnode* left;
 Tnode* right;
};
int Tnode::count = 0;   //初始化Tnode::count
Tnode* newTnode(Tnode* b)
{
 Tnode* a=new Tnode;
 cin>>a->word;
 a->right=0;
 a->count++;
 if(a->count<2)
  a->left=0;    //第一节点,左节点无为0
 else
  {
  a->left=b;    //新左节点等于原当前节点
  b->right=a;    //原右节点等于新节点
  }
 return a;
}
void printt(Tnode* temp)
{
 int n=temp->count;
 while(n--)
 {
  cout<<setw(10)<<temp->word<<":"<<temp->left<<","<<temp<<","<<temp->right<<endl;    
  temp=temp->left;
 }
}
void prints(Tnode* temp)
{
 vector<string> cxbb;
 int n=temp->count;
 while(n--)
 {
  cxbb.push_back(temp->word);
  temp=temp->left;
 }
 sort(cxbb.begin(),cxbb.end());    //排序
 vector<string>::iterator cz;
 for(cz=cxbb.begin();cz!=cxbb.end();cz++)
  cout<<*cz<<endl;
}
int main()
{
 Tnode* curr;
 int n=5;
 while(n--)curr=newTnode(curr);
 printt(curr);     //打印节点
 prints(curr);     //按单词顺序打印 
 return 0;
}


zzzz
ddd
sasd
yyyy
ffff
      ffff:00370D10,00372478,00000000
      yyyy:00370C70,00370D10,00372478
      sasd:00370988,00370C70,00370D10
       ddd:00371B88,00370988,00370C70
      zzzz:00000000,00371B88,00370988
ddd
ffff
sasd
yyyy
zzzz
Press any key to continue


7.10.9
写一个加密程序,它从cin输入,并将编码后的字符序列写到cout。可以采用如下的简单加密模式:字符c的加密形式是c^key[i],其中key是通过命令行参数提供的一个字符串。这个程序以循环的方式使用key中的字符,直到读完全输入。用同一个key重新加密编码后的正文就能得到原来的正文。如果不提供key则不加密。

#include<iostream>
using namespace std;
void enc(char s,char* key)
{
 char x;
 while(*key!=0)
 {
  x=s^*key++;
  cout<<x;
 }
 cout<<endl;
}
int main(int argc,char* argv[])
{
 if(argc>1)
  enc('s',argv[1]);
 return 0;
}

D:/>7109 2342
A@GA
D:/>7109 A@GA
2342
D:/>7109
D:/>

7.10.16
实现7.5节的print,void print(int value,int base=10),base为基数,实现16,2进制输出
#include<iostream>
using namespace std;
void bb(int a)
{
 int n=a/2,t=a%2;
 if(n>1)
  {bb(n);cout<<t;}
 else
  cout<<n<<t;
}
void aa(int a)

 int n=a/16,t=a%16;
 if(n>16)aa(n);
 if(n<10)cout<<n;
  else cout<<char(n+55);
 if(t<10)cout<<t;
  else cout<<char(t+55);
 cout<<endl;
}
void print(int value,int base=10)
{
 switch(base)
 {
 case 10:
  cout<<value<<endl;
  break;
 case 16:
  aa(value);
  break;
 case 2:
  bb(value);
  cout<<endl;
  break;
 default:
  cout<<"error!!"<<endl;
 }
}
void main()
{
print(192);
print(192,10);
print(192,16);
print(192,2);
print(192,3);
}

192
192
C0
11000000
error!!
Press any key to continue


7.10.18
写一个不用递归的阶乘函数。
#include<iostream>
using namespace std;
int bb(int a)      //循环求阶乘
{
 int temp=a;
 while(a-->1)temp*=a;
 return temp;
}
int aa(int a)      //递归求阶乘
{
 return (a>1)?a*aa(a-1):1;
}
void main()
{
 cout<<aa(4)<<endl;
 cout<<aa(5)<<endl;
 cout<<bb(4)<<endl;
 cout<<bb(5)<<endl;
}

24
120
24
120
Press any key to continue

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xchenbb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值