练习写C++代码(1)--实现简单的时钟类1

昨天的问题,实现一个简单的时钟类,包含小时和分钟,方法有显示和相加。


0.cpp

#include <iostream>

using namespace std;

///define class Clock
class Clock
{
public:
    Clock(int, int);
    void showTime();
    Clock& add(Clock& c1, Clock& c2);
    
    int hour;
    int minutes;    
};
///ctor
Clock::Clock(int h, int m)
{
    hour = h;
    minutes = m;
}

void Clock::showTime()
{
    cout<<"The time is:"<<hour<<"h"<<minutes<<"m"<<endl;
}

Clock& add(Clock& c1, Clock& c2)
{
    c1.hour += c2.hour;
    c1.minutes += c2.minutes;
    return c1;
}

int main()
{
    cout<<"Please enter two integers:";
    int h, m;
    cin>>h>>m;
    Clock clock1(h, m);
    clock1.showTime();

    cout<<"Please enter two integers:";
    cin>>h>>m;
    Clock clock2(h, m);
    clock2.showTime();

    cout<<"Add the two Clock object:";
    clock1 = add(clock1, clock2);
    clock1.showTime();

    return 0;

}


结果为:


上面的代码很简陋,今天的目标是改进它。主要有几点:

1.类里成员访问限制改为public。

2.检查小时和分钟的范围,更加合理。

3.使用函数操作符重载,如+ -。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SHA-1算法是一种哈希算法,用于将任意长度的消息压缩成一个160位的哈希值。C++标准库中提供了SHA-1算法的实现,可以通过以下代码调用: ```c++ #include <iostream> #include <string> #include <sstream> #include <iomanip> #include <openssl/sha.h> std::string sha1(const std::string& input) { unsigned char hash[SHA_DIGEST_LENGTH]; SHA1((const unsigned char*)input.c_str(), input.length(), hash); std::stringstream ss; for (int i = 0; i < SHA_DIGEST_LENGTH; i++) { ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; } return ss.str(); } int main() { std::string input = "hello world"; std::string output = sha1(input); std::cout << output << std::endl; return 0; } ``` 以上代码使用了OpenSSL库中的SHA1函数来计算哈希值,并将结果转换为字符串输出。需要注意的是,SHA_DIGEST_LENGTH常量定义了SHA-1算法的输出长度,为20字节。 ### 回答2: SHA-1是一种加密散列算法,将任意长度的数据转换为160位的哈希值。它被广泛应用于数字签名、数据完整性验证等领域。C++标准库中也提供了SHA-1算法的实现,可以通过引入头文件<openssl/sha.h>来使用。 使用SHA-1算法进行加密,需要先创建一个SHA_CTX结构体,并使用SHA1_Init函数对其进行初始化。然后使用SHA1_Update函数对输入数据逐个进行哈希处理,最后使用SHA1_Final函数生成160位的哈希值。 下面是SHA-1实现的示例代码: ``` #include <openssl/sha.h> #include <iostream> #include <string.h> using namespace std; int main(int argc, char *argv[]) { SHA_CTX sha_ctx; char data[] = "Hello, world!"; unsigned char sha[SHA_DIGEST_LENGTH]; SHA1_Init(&sha_ctx); SHA1_Update(&sha_ctx, data, strlen(data)); SHA1_Final(sha, &sha_ctx); for (int i = 0; i < SHA_DIGEST_LENGTH; i++) { cout << hex << (int)sha[i]; } cout << endl; return 0; } ``` 以上代码输出的哈希值为: ```2ef7bde608ce5404e97d5f042f95f89f1c232871```。 在实际应用中,SHA-1算法的加密强度已经逐渐较差,因此,已经被慢慢取代。SHA-2算法、SHA-3算法等较为安全,应用广泛于现在的网络安全领域。 ### 回答3: SHA-1算法是一种常见的哈希算法,用于生成指定长度的散列值。其全称为Secure Hash Algorithm 1,是由美国国家安全局(NSA)设计开发的。 SHA-1算法的实现可以使用C语言来完成。C语言中提供了很多标准库函数,可以方便地实现SHA-1算法。下面给出一个简单的SHA-1算法的C语言代码实现示例: ``` #include <stdio.h> #include <stdint.h> #include <string.h> void sha1(const char *message, uint32_t hash[5]) { uint32_t h0 = 0x67452301; uint32_t h1 = 0xEFCDAB89; uint32_t h2 = 0x98BADCFE; uint32_t h3 = 0x10325476; uint32_t h4 = 0xC3D2E1F0; uint8_t buffer[64]; // SHA-1使用64字节的块 uint32_t w[80]; uint64_t bit_len = strlen(message) * 8; // 消息长度的位数 uint32_t padding_size = 56 - (strlen(message) % 64); // 填充的字节数 if (padding_size < 0) padding_size += 64; memcpy(buffer, message, strlen(message)); buffer[strlen(message)] = 0x80; // 置1后补0 memset(buffer + strlen(message) + 1, 0, padding_size - 1); // 填充0 for (int i = 0; i < 8; i++) { // 填充长度 buffer[strlen(message) + padding_size + i] = (bit_len >> ((7 - i) * 8)) & 0xFF; } for (int i = 0; i < (strlen(message) + padding_size + 8) / 64; i++) { memcpy(w, buffer + i * 64, 64); // 将块复制到w数组中 for (int j = 16; j < 80; j++) { // 扩展消息 w[j] = w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16]; w[j] = (w[j] << 1) | (w[j] >> 31); } uint32_t a = h0; uint32_t b = h1; uint32_t c = h2; uint32_t d = h3; uint32_t e = h4; for (int j = 0; j < 80; j++) { // 压缩块 uint32_t f, k; if (j < 20) { f = (b & c) | ((~b) & d); k = 0x5A827999; } else if (j < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; } else if (j < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; } else { f = b ^ c ^ d; k = 0xCA62C1D6; } uint32_t temp = (a << 5) | (a >> 27); temp += f + e + k + w[j]; e = d; d = c; c = (b << 30) | (b >> 2); b = a; a = temp; } h0 += a; h1 += b; h2 += c; h3 += d; h4 += e; } hash[0] = h0; hash[1] = h1; hash[2] = h2; hash[3] = h3; hash[4] = h4; } int main() { char *message = "Hello, world!"; uint32_t hash[5]; sha1(message, hash); printf("The SHA-1 hash of \"%s\" is %08X%08X%08X%08X%08X.\n", message, hash[0], hash[1], hash[2], hash[3], hash[4]); return 0; } ``` 在上述代码中,sha1函数接收一个字符串消息和一个长度为5的整数数组,用于存储计算得到的SHA-1散列值。函数首先初始化5个32位的哈希值,然后将消息按照SHA-1算法进行处理,最终得到长度为160位的消息摘要,保存到哈希值数组中。 在主函数中,我们使用sha1函数对一个字符串消息进行哈希计算,并输出其结果。 总的来说,SHA-1算法的C语言实现简单而又高效,可以适用于各种应用场景,并且在计算安全性上是有保障的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值