文章目录
一、用指针知识完成以下任务
1、判断输入的字符串是否“回文”,若是,输出YES
(1)构造bool型isPalinNum函数(判断字符串是否为回文数)
// 传入两个指针
bool isPalinNum(char *str, char *p){
bool a = true;
while(str<=p){
if(*str++ != *p--){
a = false;
break;
}
}
return a;
}
(2)main函数(输入字符串,调用函数)
int main()
{
char str[100];
char *p;
cin>>str;
p = &str[strlen(str)-1];
if (isPalinNum(str,p)) cout<<"YES!"<<endl;
else cout<<"NO!"<<endl;
return 0;
}
(3)完整代码
#include<iostream>
#include<cstring>
using namespace std;
bool isPalinNum(char *str, char *p){
bool a = true;
while(str<=p){
if(*str++ != *p--){
a = false;
break;
}
}
return a;
}
int main()
{
char str[100];
char *p;
cin>>str;
p = &str[strlen(str)-1];
if (isPalinNum(str,p)) cout<<"YES!"<<endl;
else cout<<"NO!"<<endl;
return 0;
}
(4)运行结果
输入1234567890987654321,可得YES结果
输入非回文字符串,可得NO结果
2、实现strcpy功能
(1)构造strcpy函数(字符串复制)
char* my_strcpy(char *dest, const char *src){
char *str = dest;
while(*src) *dest++ = *src++; //赋值
*dest = '\0'; //字符串结尾为'\0'
return str; //返回指针
}
(2)main函数(输入字符串)
int main()
{
char str1[100];
cin>>str1;
char *str2 = new char[strlen(str1)+1]; //开辟新的储存空间
str2 = my_strcpy(str2, str1);
while(*str2){
cout<<*str2; // 打印复制后的字符串
str2++;
}
return 0;
}
(3)完整代码
#include<iostream>
#include<cstring>
using namespace std;
char* my_strcpy(char *dest, const char *src){
char *str = dest;
while(*src) *dest++ = *src++;
*dest = '\0';
return str;
}
int main()
{
char str1[100];
cin>>str1;
char *str2 = new char[strlen(str1)+1];
str2 = my_strcpy(str2, str1);
while(*str2){
cout<<*str2;
str2++;
}
return 0;
}
(4)运行结果
可以成功赋值
二、实现长方体类
题目
解答
(1)定义长方体类
class Cuboid{
public:
// 构造函数
Cuboid(double length=0, double width=0, double height=0){
len = length;
hei = height;
wid = width;
cnt++;
}
// 析构函数
~Cuboid(){
cout<<"Clear!";
}
// 计算体积
double volumn(){
vol = len*hei*wid;
return vol;
}
// 读取对象创建次数
int count(){
return cnt;
}
private:
// 私有成员变量
double len,hei,wid;
double vol;
static int cnt;
};
(2)类外定义静态成员变量
int Cuboid::cnt = 0;
(3)main函数
int main()
{
double height, length, width;
// 输入长宽高
cout<<"Please enter length, width and height:";
cin>>length>>width>>height;
// 实例化类cub
Cuboid cub(length, width, height);
cout<<"cub.count() = "<<cub.count()<<endl; // 读取创建次数
cout<<"The volumn is "<<cub.volumn()<<"."<<endl; // 计算体积
cout<<"------------------------------"<<endl;
// 实例化对象数组
Cuboid cub_array[3] = {
Cuboid(1.0, 2.0, 3.0),
Cuboid(1.2, 2.2, 3.2),
Cuboid(1.4, 2.4, 3.4),
};
cout<<"cub_array[0].volumn() = "<<cub_array[0].volumn()<<endl; // 计算体积
cout<<"cub_array[2].count() = "<<cub_array[2].count()<<endl; // 读取创建次数
cout<<"------------------------------"<<endl;
return 0;
}
(4)完整代码
#include<iostream>
using namespace std;
class Cuboid{
public:
Cuboid(double length=0, double width=0, double height=0){
len = length;
hei = height;
wid = width;
cnt++;
}
~Cuboid(){
cout<<"Clear!";
}
double volumn(){
vol = len*hei*wid;
return vol;
}
int count(){
return cnt;
}
private:
double len,hei,wid;
double vol;
static int cnt;
};
int Cuboid::cnt = 0;
int main()
{
double height, length, width;
cout<<"Please enter length, width and height:";
cin>>length>>width>>height;
Cuboid cub(length, width, height);
cout<<"cub.count() = "<<cub.count()<<endl;
cout<<"The volumn is "<<cub.volumn()<<"."<<endl;
cout<<"------------------------------"<<endl;
Cuboid cub_array[3] = {
Cuboid(1.0, 2.0, 3.0),
Cuboid(1.2, 2.2, 3.2),
Cuboid(1.4, 2.4, 3.4),
};
cout<<"cub_array[0].volumn() = "<<cub_array[0].volumn()<<endl;
cout<<"cub_array[2].count() = "<<cub_array[2].count()<<endl;
cout<<"------------------------------"<<endl;
return 0;
}