《c++ primer》第二章学习笔记

1.char 和wchar_t有什么区别,分别应有到什么场合?
解释:它们都是字符类型。char字长为8位,wchar_t字节为16位;wchar_t常用于扩展字符集,比如汉字、日语等。wchar_t可以用wprintf()函数显示。

<pre class="cpp" name="code">#include <iostream>
using namespace std;
int main ()
{
       printf("Hello QueYouPeng!\n");
       int cnt_b;
       char *b="没有了";
       wchar_t a[4]=L"长字符";
       setlocale(LC_ALL,".936");//配置地域化信息
// or setlocale(LC_ALL,"");//使用系统默认的设置
    cnt_b=strlen(b);
       printf("char: %s  %d\n",b,cnt_b);
       wprintf(L"%s\n",a);
       wprintf(a);
       return 0;
}

扩展:编写一个函数计算一篇文章的不同汉字的个数,并根据汉字出现个数的大小依次显示?

提示:1、汉字可由宽字节wchar_t来接收。

         2、wchar_t接受到的字符如果(wct>0xFF)这表示汉字。

         3、wchar_t输入输出方法:wscanf(L”%s”,wd); printf(“%C”,wd);参考代码如下:

#include <bitset>
#include <wchar.h>//宽字节的头文件
using namespace std;
typedef struct Words
{
       wchar_t wd;
       int count;
}Words;
typedef struct ListNode
{
       Words wds;
       ListNode *next;
}List;
wchar_t signs[22]=L",。?!…“” 《》—:、【】()‘’;·";//符号这里没有写全
static int cnt=0;//统计总字数
static int dst=0;//统计不同字数个数
int analyse(wchar_t wd)
{
       if(wd<=0xFF)
              return -1;
       cnt++;//中文字符++
      int i=0;
       while(signs[i])
       {
              if(wd==signs[i])
                     return -1;
              i++;
       }
       return 1;
}
void sortInseart(List* &h,wchar_t wd)
{
       List *p=h->next;
       List *front=h;//p前面上一级的结点
       List *precedent=h;//紧跟p前面的结点
       while(p!=NULL)
       {
              if(p->wds.wd!=wd)
              {
                     if(p->next!=NULL&&p->wds.count>p->next->wds.count)
                            front=p;
                     else if(p->next==NULL)
                            front=p;
              }
              else
              {
                     p->wds.count++;
                     //xchange
                  precedent->next=p->next;
                     p->next=front->next;
                     front->next=p;
                     break;
              }
              precedent=p;
              p=p->next;
       }
       if(p==NULL)
       {
              dst++;//
              p=new List;
              p->next=NULL;
              p->wds.wd=wd;
              p->wds.count=1;
              front->next=p;
       }
    return ;
}
void print(List *h)
{
       List *p=h->next;
       cout<<"中文字符总个数为:"<<cnt<<" 不同的汉字个数为:"<<dst<<endl;
       while(p!=NULL)
       { 
              printf("%C----%d个\n",p->wds.wd, p->wds.count); //大写C表示输出宽字节符
           p=p->next;
       }
   return ;
}
int main()
{
       printf("       文本汉字分析程序:……");
       printf("\n       * ** ** ** ** ** * ** **程序操作步骤** ** ** ** *** ** ***");
       printf("\n       *                                                        *");
       printf("\n       *    1.请把要分析的文章拷贝到当前目录下的“文字分析.in” *");
    printf("\n       * 的文件中。                                             *");
       printf("\n       *    2.之后双击运行“汉字统计.exe”文件,并看到此说明;  *");
       printf("\n       *    3.按任意键后程序会自动关闭,请在当前目录下的“文字  *");
       printf("\n       * 分析.out”下查看分析结果!                             *");
       printf("\n       *                                                        *");
       printf("\n       ** ** ** ** ** ** ** *谢谢使用本程序!** ** ** ** *** ** *\n\n");
    system("pause");
       wchar_t *wds=new wchar_t[10000];//接受宽字节申请缓冲区
       setlocale(LC_ALL,"");//配置环境
       List *h;
       h=new List; //初始化头结点,头结点不保存汉字
       h->next=NULL;
       freopen("文字分析.in","r",stdin);
       freopen("文字分析.out","w",stdout);
       int i=0;
       while(wscanf(L"%s",wds))//重要语句,如何输入宽字节 
      {
              i=0;
              while(wds[i])
              {
                     if(analyse(wds[i])==1)
                         sortInseart(h,wds[i]);
                     i++;
              }
       }
    print(h);
    return 0;
}

2.后缀L、U、F分别表示什么,科学计数法的表示方法,十六进制表示法?八进制表示法?二进制表示法?
解释:L表示长整形,U表示无符,F表示单精度。

//科学计数法、L、U、F等的表示方法
short Short;
	int Int;
	long Long;
	float Float;
	double Double;
	Short=-10U;//没有发现有什么变化,结果还是-10
	Int=-1000000000UL;//好像没有什么变化
	Long=1000000000L;
	Float=3.14f;
	Double=314.159E-2f; //科学计数法表示
	printf("Short=%d,Int=%d,Long=%ld,Float=%f,Double=%f\n",Short,Int,Long,Float,Double);
     int erJZ;//目前还没有找到能直接表示二进制的前缀
	int baJZ;
	int shiLiuJZ;
	baJZ=017;//八进制表示
	shiLiuJZ=0x80;//十六进制表示
printf("八进制017=%d,十六进制0x80=%d\n",baJZ,shiLiuJZ);

输出结果如下图:

3. 运算符>>和<<只能重载为友元函数,因为他要使用istream或ostream的应用,如:

istream& operator >>(istream & is,DataClass d)
{
   is>>……;
   return is;
}
ostream& operator <<(ostream & os,DataClass d)
{
os<<……;
return os;
}

4.extern可以声明一个变量,如果变量后面加了初始化数值,则为定义并分配空间。(c++ primer P46)

如:
extern double pi=314.159E-2f;//definition
extern double pi;//ok:declaration not definition
extern double pi=31415.9E-4;//error:redefinition of pi
5.string和char[]相互转换?
解释:

string s2="string and char[]";
	char a[20];
	string s3(20,0);//为s3分配了20个空间,不申请空间会提示内存写错误。
	int i=0;
	while(s2[i]) //string 2 char[]
		a[i++]=s2[i];//i++必须放在前面,若使用a[i]=s2[i++]出错,自行解释。
	a[i]=0;
	i=0;
	while(a[i]) //char[] 2 string
		s3[i]=a[i++];//为什么这里i++又要放到后面???然道是运算速度的问题?对于这种情况最好把i++单独用一行来写!
	cout<<"s2:"<<s2<<endl<<"a[]:"<<a<<endl<<"s3:"<<s3;
	return 0;

6.局部变量和全局变量的名字可以相同,当出现名字相同的变量时,全局变量会被局部变量代替,即把全局变量隐藏了(hide)。(P47)
7.extern和const的联合使用来扩展常量的使用范围,const对象默认为本文件的局部变量,而非const对象则默认可以被其他文件扩展使用,如下(P50)
(1)//file_1.c
int counter;//definition
//file_2.c
extern int counter;//uses counter from file_1.c
++count;
(2)//file_1.c
extern const int bufSize=512;//definition 只有在const前面加extern了才能被其他文件扩展使用
//file_2.c
extern const int bufSize;//uses bufSize from file_1.c
扩展:const使用的位置不同,表示的意思有什么不同??(四个const位置)
8.const引用。对于常量需要使用引用对象时,必须使用const引用,否则编译错误。(p51)
如:
const int ival=0xA1;
const int & refVal=ival; //ok:both reference anf object are const
int & ref2=ival; //error:nonconst reference to a const object
9.枚举的几种定义方法:默认从零开始依次自加1。
如:
//input is 0,output is 1,and append is 2
enum open_modes{ input , output , append};
//shape is 1, sphere is 2, cylinder is 3
enum Forms{ shape =1 , sphere , cylinder};
// point2d is 2, point2w is 3,point3d is 3 , point3w is 4
enum points{  point2d =2, point2w, point3d =3 , point3w };
int length=siezof(Forms or shape);//length is 4, 说明枚举类型的长度为4字节
10.class和struct在C++可以替换使用?如下面的程序完全正确,并和class定义输出的结果相同!但也有区别:class默认的访问权限是private的,而struct的为public的。

#include <iostream>
using namespace std;
   int main()
{
typedef struct Item
	{
	private:
		int cnt;
		double price;
	public:
		double arithmetic()
		{
			return cnt*price;
		}
		void setCnt(int cnt)
		{
			this->cnt=cnt;
		}
		void setPrice(double price)
		{
			this->price=price;
		}
		Item()
		{
			cnt=10;
			price=3.14;
		}
		Item(int cnt,double price)
		{
			this->cnt=cnt;
			this->price=price;
		}
		~Item(){}
	}Item;
	Item item(5,3.14);
//	item.setCnt(4);
//	item.setPrice(3.14);
	int len=sizeof(item);//in any case , len is always 16
	printf("%.2f  %d",item.arithmetic(),len);
	return 0;
}//注意到没有:不管Item里面的内容有多少,输出item的长度总为16字节
<span style="color:#3333ff;"></span>

扩展:在struct中的成员变量赋值表示什么意思?如上面的Item中:int cnt:7; 表示什么意思?

 

猜你喜欢

转载自blog.csdn.net/fripy/article/details/7270386