My_String完善

#include "my_string_ok.h"

My_string_Ok::My_string_Ok():size(20)

{

len = 0;

ptr = new char[size];

ptr[len] = '\0';

}

My_string_Ok::My_string_Ok(int num,char c)

{

cout<<"有参构造"<<endl;

ptr = new char [20] ;

len = 0;

for(int i=0; i<num; i++)

{

// cout<<"i ="<<i<<endl;

*(ptr+i) = c;

len++;

}

*(ptr+num)='\0';

// cout<<data()<<endl;

// cout<<len<<endl;

}

My_string_Ok::My_string_Ok(const char* arr)

{

cout<<"有参构造"<<endl;

if(if_empty(strlen(arr)))

{

len = 0;

ptr = new char[20];

ptr[len] = '\0';

return ;

}

/*if(strlen(arr)>size)

{

cout<<"输入数据过大"<<endl;

return;

}*/

ptr = new char [20];

strcpy(ptr,arr);

len = strlen(arr);

//cout<<data()<<endl;

}

My_string_Ok::My_string_Ok(const My_string_Ok & other)

{

cout<<"拷贝构造函数"<<endl;

this->ptr = new char[20];

strcpy(this->ptr,other.ptr);

this->len = other.len;

// cout<<data()<<endl;

}

My_string_Ok&My_string_Ok ::operator=(const My_string_Ok &other)

{

cout<<"拷贝赋值函数"<<endl;

if(this == &other)

{

return *this;

}

if(!if_empty(other.len))

{

delete ptr;

}

ptr = new char[20];

strcpy(ptr,other.ptr);

len = other.len;

// cout<<data()<<endl;

return *this;

}

My_string_Ok&My_string_Ok ::operator=(const char* arr)

{

cout<<"拷贝赋值函数2"<<endl;

delete ptr;

ptr = new char[20];

strcpy(ptr,arr);

len = strlen(arr);

// cout<<data()<<endl;

return *this;

}

My_string_Ok&My_string_Ok :: operator+(const char c)

{

cout<<"+函数"<<endl;

// My_string_Ok s1;

// ptr = new char[20];

//s1.len = this->len;

*(this->ptr+this->len) = c;

this->len++;

*(this->ptr + len) = '\0';

// strcpy(s1.ptr,this->ptr);

return *this;

}

char My_string_Ok :: operator[](int num)

{

return ptr[num];

}

bool My_string_Ok :: operator>(const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i]>other.ptr[i])

return 1;

else

return 0;

}

bool My_string_Ok :: operator<(const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i]<other.ptr[i])

return 1;

else

return 0;

}

bool My_string_Ok :: operator == (const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i] == other.ptr[i])

return 1;

else

return 0;

}

bool My_string_Ok :: operator >= (const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i] >= other.ptr[i])

return 1;

else

return 0;

}

bool My_string_Ok :: operator <= (const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i] <= other.ptr[i])

return 1;

else

return 0;

}

bool My_string_Ok :: operator != (const My_string_Ok &other)

{

int i =0 ;

while(1)

{

if(this->ptr[i] == other.ptr[i] && this->ptr[i]!='\0' && other.ptr[i]!='\0')

{

i++;

}

else

break;

}

if(this->ptr[i] != other.ptr[i])

return 1;

else

return 0;

}

My_string_Ok&My_string_Ok :: operator+=(const char *arr)

{

len = len + strlen(arr);

if(len>size)

{

cout<<"超出堆区"<<endl;

len = len - strlen(arr);

}

else

{

ptr = strcat(ptr,arr);;

}

return *this;

}

My_string_Ok&My_string_Ok :: operator+=(const char c)

{

if(len+1>size)

cout<<"超出堆区"<<endl;

else

{

*(ptr+len)=c;

len+=1;

*(ptr+len)='\0';

}

return *this;

}

My_string_Ok::~My_string_Ok()

{

delete ptr;

/* len = 0;

ptr = NULL;*/

}

char *My_string_Ok::data()

{

return ptr;

}

bool My_string_Ok::if_empty(int num)

{

return num==0;

}

void My_string_Ok :: show()

{

cout<<data()<<endl;

}

int My_string_Ok :: get_len()

{

int i=0;

while(1)

{

if(ptr[i]!='\0')

i++;

else

break;

}

return i;

}

std::ostream & operator<<(ostream & out,const My_string_Ok &other)

{

out<<"该内容是:"<<other.ptr<<endl;

return out;

}

std::istream & operator>>(istream &in,My_string_Ok &other)

{

delete other.ptr;

other.ptr = new char [20];

in.getline(other.ptr,20);

//other.len = strlen(other.len);

return in;

}

mian()

#include "my_string_ok.h"

int main()

{

My_string_Ok s1(3,'b');

s1.show();

My_string_Ok s2("abcdefg");

s2.show();

My_string_Ok s3 (s1);

s3.show();

My_string_Ok s4;

s4 = s2;

s4.show();

My_string_Ok s5;

s5 = "ABCDEF";

s5.show();

s5+'G';

s5.show();

cout<<s5.data()[6]<<endl;

cout<<s5.get_len()<<"**"<<s1.get_len()<<endl;

if(s5>s1)

cout<<"s5>s1"<<endl;

else

cout<<"s5<s1"<<endl;

if(s1<s2)

cout<<"s2>s1"<<endl;

else

cout<<"s2<s1"<<endl;

My_string_Ok s6;

s6 = s2;

if(s6 == s2)

{

cout<<"s6=s2"<<endl;

}

else

{

cout<<"s6!=s2"<<endl;

}

cout<<s5.get_len()<<endl;

s6 += "123456";

s6.show();

s6 += 'X';

s6.show();

cout<<s1;

My_string_Ok s7;

cout<<"请输入值"<<endl;

cin>>s7;

cout<<s7;

cout<<s7.get_len()<<endl;

//cout << "Hello World!" << endl;

return 0;

}

在实际项目中,直接将硬编码的用户密码用于验证并不安全,你应该考虑使用更安全的方式来存储和验证用户密码。这里给出一个基本的例子,使用常见的`SharedPreferences`来保存加密后的哈希值和盐,并使用`Bcrypt`库(或类似的安全哈希库)进行密码安全处理。 首先,在你的应用初始化时(例如在`Application`的`onCreate()`方法),可以生成并存储一个随机盐(salt): ```java public class MyApplication extends Application { private static final String SALT_KEY = "bcrypt_salt"; private static final String PASSWORD_HASH_KEY = "bcrypt_hash"; @Override public void onCreate() { super.onCreate(); SharedPreferences prefs = getSharedPreferences("SecureData", Context.MODE_PRIVATE); String salt = prefs.getString(SALT_KEY, null); if (salt == null) { // 生成新的随机盐 byte[] saltBytes = new byte[BCrypt.GENSALT().getWorkFactor()]; SecureRandom random = new SecureRandom(); random.nextBytes(saltBytes); String saltStr = Base64.encodeToString(saltBytes, Base64.DEFAULT); prefs编辑者.putString(SALT_KEY, saltStr); // 存储盐值 } } } ``` 然后在`validateCredentials`函数中,先读取之前存储的盐和哈希值,再用用户输入的密码和盐计算哈希值进行对比: ```java private boolean validateCredentials(String username, String password) { SharedPreferences prefs = getSharedPreferences("SecureData", Context.MODE_PRIVATE); String storedSalt = prefs.getString(SALT_KEY, ""); String storedHash = prefs.getString(PASSWORD_HASH_KEY, ""); // 使用Bcrypt进行哈希和比较 try { BCrypt.checkpw(password, storedHash); } catch (IllegalArgumentException e) { // 密码不匹配,抛出异常 return false; } // 验证用户名是否一致(假设数据库查询) User user = getUserFromDatabase(username); return user != null; // 用户存在 } // 假设的用户查询方法 User getUserFromDatabase(String username) { // 实际从数据库获取用户信息 return DatabaseManager.getInstance().getUserByUsername(username); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值