Cout<}
Return0;
}
(3)
#include
Usingnamespacestd;
Intmian()
{
Charstr[180];
Cin>>str;
Intk=strlen(str);
For(inti=0;iCout<Return0;
}
(4)
#include
Usingnamespacestd;
Intmian()
{
Charstr[180];
Charc=‘c’;
Cin>>str;
For(inti=0;j=0;str[i]!
=’/0’;i++,j++)
If(str[j]==c)
i--;
else
str[i]=str[j];
cout<return0;
}
(5)
#include
Usingnamespacestd;
Intmian()
{
Chars2[180]s1[180];
Cin>>s2;
For(inti=0;s2[i]!
=’/0’;i++)
S1[i]=s2[i];
Cout<Return0;
}
(6)
#include
Usingnamespacestd;
Intmian()
{
CharSStr[180],DStr[180];
Cin>>SStr>>DStr;
IntSL=strlen(SStr);
IntDL=strlen(DStr);
For(inti=0;i
SStr[i+SL]=DStr[i];
cout<return0;
}
(7)
#include
Usingnamespacestd;
Intmian()
{
ConstintM=3,K=2,N=4;
Inta[M][N]={{1,2},
{3,4},
{5,6}};
Intb[K][N]={{1,0,1,1},
{0,1,0,1};
Intc[M][N]={0};
For(inti=0;iFor(intj=0;jiFor(intk=0;kc[i][j]=c[i][j]+c[i][k]*b[k][j];
For(i=0;i{
For(intj=0;jCout<Cout<}
Return0 ;
}
(8)
#include
Usingnamespacestd;
Boolpalindrome(char*str)
{
Inth=strlen(str);
For(inti=0;iIf(str[i]!
=str[h-i-1])
Returntrue;
}
Intmain()
{
Chartext[180];
Cin>>text;
If(palindrome(text))
Cout<<”apalindromestring!
”<Else
Cout<<”notapalindromestring!
”<Return0;
}
(9)
#include
Usingnamespacestd;
IntSubStrNum(char*str,char*substr)
{
IntNum=0;
Inth1,h2;
Intp1,p2;
H1=strlen(str);
H2=strlen(substr);
P1=0;
While(p1
{
P2=0;
While(str[p1]==substr[p2]&&p2
{
P1++;
P2++;
}
If(p1==h1&&2
Break;
If(p2==h2)
Num++;
Else
P1=p1-p2+1;
}
ReturnNum;
}
Intmain()
{
Charstr[200];
Charsubstr[20];
Cout<<”inputsourcestring:
”;
Cin>>str;
Cout<<”inputsourcestring:
”;
Cin>>substr;
Cout<<”matchtimes:
”<Return0;
}
(10)
#include
Usingnamespacestd;
Char*lsub(char*s1.char*s2)
{
Intn1,n2,n;
Char*res,c=0;
N1=strlen(s1) ;
N2=strlen(s2);
N=n1>n2?
n1:
n2;
Res=newchar[n+2];
Res[0]=’0’;
Res[1]=’/0’;
Res[n+1]=’/0’;
If(n1Res[0]=’+’ ;
ElseIf(n1Res[0]=’-’ ;
Else
For(inti=0;iIf(s1[i]{
Res[0]=’-’;
Break;
}
If(s1[i]{
Res[0]=’+’ ;
Break;
}
}
If(Res[0]==’-’)
{
For(inti=0;i>0;i--)
Res[i]=i>n-n1?
s1[i+n1-n2-1]:
’0’;
For(i=n;i>0;i--)
{If(s2[i-1]-c>=res[i])
{
Res[i]=s2[i-1]-c-res[i]+’0’;
C=0;
}
Else{
Res[i]=s2[i-1]-c-res[i]+10+’0’;
C=1;}
}
}
If(Res[0]==’+’)
{
For(inti=0;i>0;i--)
Res[i]=i>n-n2?
s2[i+n2-n1-1]:
’0’;
For(i=n;i>0;i--)
{If(s1[i-1]-c>=res[i])
{
Res[i]=s1[i-1]-c-res[i]+’0’;
C=0;
}
Else{
Res[i]=s1[i-1]-c-res[i]+10+’0’;
C=1;}
}
}
Returnres;
}
Intmain()
{
Charnum1[100],num2[100],*num;
Cin>>num1>>num2;
Num=lsub(num1,num2);
Cout<Delete[]num;
Return0;
}
(11)
#include
Usingnamespacestd;
StructNode
{
Intno;
Node*next;
};
Intmain()
{
Intm,n,s,remainder;
Cout<<”inputm,n,s:
”;
Cin>>m>>n>>s;
Node*first,*last;
First=last=newNode;
First->no=1;
For(inti=1;i{
Node*p=newnode;
p->no=i+1;
last->next=p;
last=p;
}
Last->next=;
For(i=1;i
{first=first->next;
Last=last->next;
}
Remainder=n;
While(remainder>1)
{
For(intcount=1;count{first=first->next;
Last=last->next;
}
last->next=first->next;
cout<no<<”\t”;
deletefirst;
first=last->next;
remainder--;
}
Cout<no;
Deletefirst;
Return0;
}
(12)
#include
Usingnamespacestd;
StructPhone
{
Charnumber[20];
Charname[16];
}
phoneBock[]={{“027888888888”,”张建设”},
{“”,”’},
{“”,””},
{“”,””},
{“”,””},
{“”,””},
{“”,””},
{“”,””},
{“”,””}
};
BoolSubStrNum(char*str,char*substr){
Inth1,h2;
Intp1,p2;
H1=strlen(str);
H2=strlen(substr);
If(h2>h1)
Returnfalse;
P1=0;
While(p1
{
P2=0;
While(str[p1]==substr[p2]&&p2
{
P1++;
P2++;
}
If(p1==h1&&p2
Returnfalse;
If(p1==p2)
Returntrue;
Else
P1=p1-p2+1;
}
Returnfalse;
}
Intmain()
{
Intphones;
Charstring[20];
Phones=sizeof(phonesBook)/sizeof(phone);
Cout<<”inputphonenumberorname:
”
Cin>>string;
For(inti=0;i{
If(SubStrNum(phonebook[i].number,string))
Cout<
}
For(i=0;i{
If(SubStrNum(phonebook[i].number,string))
Cout<
}
Return0;
}
1.6习题六解答
1.填空题
(1)全局变量,局部变量,局部变量
(2)auto,extern,register,static,静态(static)
(3)全局数据,代码区,栈区,堆区,区局数据区,代码区,栈区,堆区
(4)静态,动态
(5)函数原型,块作用域,文件
(6)#,编译
(7)个别使用声明方式,全局声明方式,全局声明个别成员
2.填空题
1,A2,D3D4C5B6B7B8C9B10A
3.程序填空
#include
Externintsumchar(charstr[]);
Intmain()
{
Usingstd:
:
cin;
Usingstd:
:
cout;
Char*p=newchar[180];
Cin>>p;
Cout<Delete[]p;
Return0;
}
4,分析程序,写出运行结果
#include
#include
Usingnamespacestd;
ConstintPI=3.14;
Constint*Fun(void);
{
Constinta=5;
Constint*p=&a;
Cout<<”Valueoflocalconstvariablea:
”<Cout<<”Addressoflocalconstvariablea:
”<<&a<Cout<<”Valueoflocalconstpointerp:
”<
Cout<<”Valueoflocalconstvariablea:
”<<*p<Returnp;
}
Intmain()
{
Constint*q;
Q=Fun();
Cout<<”Main():
”<Cout<<”Valueoflocalconstpointerq:
”<Cout<<”ThereturnValueofthefunctionFunc():
”<<*q<Constchar*str=”123ABC”;
Cout<<”Addressofstringconstvariable:
”<<(void*)str<Cout<<”Valueofstringconstvariablestr:
”<Cout<<”AddressofglobalconstvariablePI:
”<<&P<Cout<<”AddressofthefunctionFunc():
”<Return0;
}
运行结果:
Valueoflocalconstvariablea:
5
Addressoflocalconstvariablea:
0012FF20
Valueoflocalconstpointerp:
0012FF20
Valueoflocalconstvariablea:
5
Main():
Valueoflocalconstpointerq:
0012FF20
ThereturnValueofthefunctionFunc():
1245056
Addressofstringconstvariable:
0046F14c
Valueofstringconstvariablestr:
123ABC
[注释]本程序主要测试常变量的使用。
程序定义了局部变量a,它是一个局部变量,其作用域仅限于函数()内部。
生存周期不是整个源程序,它同普遍变量一样,局限在函数Fun()内部,当函数调用完成后,局部常变量a被释放,当将函数Fun()返回值赋给指针q后,输出指针q所指向的存储空间内存储的不在是变量a的值,而是1245056(随机值)。
程序中指针常变量a,所以指针p的值就是局部常变量a的地址.