pub_priv.cpp

本文展示了一个简单的C++程序,用于定义一个图书类,并在其中包含标题、作者、价格和出版社等属性。通过实例化该类并设置具体图书信息,演示了如何显示图书详情。

  #include <iostream.h>
#include <iomanip.h>
#include <string.h>

class Book
{
  public:
    char title[256];
    char author[64];
    float price;
    void show_title(void) { cout << title << '/n'; };
    float get_price(void) { return(price); };
    void show_book(void)
    {
      show_title();
      show_publisher();
    };
   
    void assign_publisher(char *name) { strcpy(publisher, name); };
  private:
    char publisher[256];
    void show_publisher(void) { cout << publisher << '/n'; };
};

void main(void)
 {
   Book bible;
 
   strcpy(bible.title, "Jamsa's C/C++ Programmer's Bible");
   strcpy(bible.author, "Jamsa and Klander");
   bible.price = 49.95;
   bible.assign_publisher("Jamsa Press");

   bible.show_book();
 }

 

testLogger.cpp 的代码是独立的可执行程序即节点;其代码如下; #include <ros/ros.h> #include <math.h> #include "geometry_msgs/Twist.h" #include "string" #include "std_msgs/String.h" #include "std_msgs/Int32.h" // #include "geometry_msgs/PoseStamped.h" using namespace std; // ros::Publisher servo_encode_pub; // double last_recieved_stamp; // double servo_angle; // double servo_velocity; // ros::Time now; // void recv_servo_angle(const control_msgs::JointControllerState::ConstPtr &msg) // { // servo_angle = msg->process_value; // now = msg->header.stamp; // } // void recv_servo_velocity(const control_msgs::JointControllerState::ConstPtr &msg) // { // servo_velocity = msg->process_value; // } // void encoder_send_callback(const ros::TimerEvent &) // { // common::servo_encoder se; // se.se_theta = servo_angle * 180 / 3.1415926; // se.header.stamp = ros::Time::now();// now; // se.se_vel = servo_velocity / 2 / 3.1415926 * 60; // std::cout<<"show curent secones:"<<se.header.stamp.toSec()<<endl; // servo_encode_pub.publish(se); // } int main(int argc, char *argv[]) { ros::init(argc, argv, "logger_node"); ros::NodeHandle nh; ros::NodeHandle nh_priv("~"); cout<<"hello world"<<endl; // ros::Subscriber servo_angle_sub = nh_priv.subscribe("/sim_robot/servo_angle_controller/state", 1000, recv_servo_angle); // ros::Subscriber servo_velocity_sub = nh_priv.subscribe("/sim_robot/servo_velocity_controller/state", 1000, recv_servo_velocity); // servo_encode_pub = nh_priv.advertise<common::servo_encoder>("/servo_encoder", 1); // ros::Timer timer = nh_priv.createTimer(ros::Duration(pub_period), encoder_send_callback); ros::spin(); return 0; } 其对应的CMakeLists.txt如下;该如何修改;
05-15
1>D:\vc\first-openssl\first-openssl\实验一.cpp(142,13): error C2374: “a”: 重定义;多次初始化 1> D:\vc\first-openssl\first-openssl\实验一.cpp(91,13): 1> 参见“a”的声明 1>D:\vc\first-openssl\first-openssl\实验一.cpp(143,13): error C2374: “b”: 重定义;多次初始化 1> D:\vc\first-openssl\first-openssl\实验一.cpp(92,13): 1> 参见“b”的声明 1>D:\vc\first-openssl\first-openssl\实验一.cpp(144,13): error C2374: “p”: 重定义;多次初始化 1> D:\vc\first-openssl\first-openssl\实验一.cpp(93,13): 1> 参见“p”的声明 1>已完成生成项目“first-openssl.vcxproj”的操作 - 失败。 ========== 生成: 0 成功,1 失败,0 最新,0 已跳过 ========== 帮我改一下#include <openssl/bn.h> #include <openssl/err.h> #include <fstream> #include <iostream> #include <string> #include <cstring> #include <cctype> #include <vector> // 欧几里得算法实现 BIGNUM* euclidean_gcd(BIGNUM* a, BIGNUM* b, BN_CTX* ctx) { BIGNUM* r = BN_new(); BIGNUM* tmp = BN_new(); // 确保 a >= b if (BN_cmp(a, b) < 0) { BN_copy(tmp, a); BN_copy(a, b); BN_copy(b, tmp); } // 欧几里得算法核心 while (!BN_is_zero(b)) { BN_mod(r, a, b, ctx); // r = a mod b BN_copy(a, b); // a = b BN_copy(b, r); // b = r } BN_free(tmp); BN_free(r); return BN_dup(a); // 返回最大公约数 } // 从文件读取十六进制数 BIGNUM* read_hex_from_file(const std::string& filename, const std::string& prefix) { std::ifstream file(filename); if (!file) { std::cerr << "错误: 无法打开文件 " << filename << std::endl; return nullptr; } std::string line; std::string hex_str; bool found_prefix = false; while (std::getline(file, line)) { // 查找前缀行 if (line.find(prefix) != std::string::npos) { found_prefix = true; continue; } // 读取十六进制数据行 if (found_prefix && !line.empty()) { hex_str = line; // 移除空白字符 hex_str.erase(std::remove_if(hex_str.begin(), hex_str.end(), [](unsigned char c) { return std::isspace(c); }), hex_str.end()); break; } } file.close(); if (hex_str.empty()) { std::cerr << "错误: 未找到 " << prefix << " 的数据" << std::endl; return nullptr; } BIGNUM* num = BN_new(); if (BN_hex2bn(&num, hex_str.c_str()) == 0) { std::cerr << "错误: 转换十六进制失败: " << hex_str << std::endl; BN_free(num); return nullptr; } return num; } int main() { // 初始化 OpenSSL BIGNUM 上下文 BN_CTX* ctx = BN_CTX_new(); if (!ctx) { std::cerr << "错误: 无法创建BN上下文" << std::endl; return 1; } BN_CTX_start(ctx); // 创建大数对象 BIGNUM* a = BN_new(); BIGNUM* b = BN_new(); BIGNUM* p = BN_new(); if (!a || !b || !p) { std::cerr << "错误: 内存分配失败" << std::endl; return 1; } // 生成两个512位随机数 if (!BN_rand(a, 512, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) { std::cerr << "错误: 生成随机数a失败" << std::endl; return 1; } if (!BN_rand(b, 512, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) { std::cerr << "错误: 生成随机数b失败" << std::endl; return 1; } // 生成1024位安全素数 (满足$p = 2q + 1$,q也是素数) if (!BN_generate_prime_ex(p, 1024, 1, NULL, NULL, NULL)) { std::cerr << "错误: 生成安全素数失败" << std::endl; return 1; } // 打开输出文件 std::ofstream outFile("data.txt"); if (!outFile) { std::cerr << "错误: 无法创建文件 data.txt" << std::endl; return 1; } // 转换为十六进制 char* a_hex = BN_bn2hex(a); char* b_hex = BN_bn2hex(b); char* p_hex = BN_bn2hex(p); // 输出到控制台 std::cout << "随机数a (512位):\n" << a_hex << "\n\n"; std::cout << "随机数b (512位):\n" << b_hex << "\n\n"; std::cout << "安全素数p (1024位):\n" << p_hex << std::endl; // 写入文件 outFile << "随机数a (512位):\n" << a_hex << "\n\n"; outFile << "随机数b (512位):\n" << b_hex << "\n\n"; outFile << "安全素数p (1024位):\n" << p_hex << std::endl; std::cout << "\n结果已保存至 data.txt" << std::endl; // 从文件读取数据 BIGNUM* a = read_hex_from_file("data.txt", "随机数a"); BIGNUM* b = read_hex_from_file("data.txt", "随机数b"); BIGNUM* p = read_hex_from_file("data.txt", "安全素数p"); if (!a || !b || !p) { std::cerr << "错误: 数据读取失败" << std::endl; return 1; } // 计算欧几里得GCD BIGNUM* euclidean_result = euclidean_gcd(a, b, ctx); // 计算OpenSSL GCD BIGNUM* openssl_result = BN_new(); BN_gcd(openssl_result, a, b, ctx); // 转换为十六进制字符串 char* euclidean_hex = BN_bn2hex(euclidean_result); char* openssl_hex = BN_bn2hex(openssl_result); // 比较结果 int comparison = BN_cmp(euclidean_result, openssl_result); // 输出结果 std::cout << "欧几里得算法结果: " << euclidean_hex << std::endl; std::cout << "OpenSSL BN_gcd结果: " << openssl_hex << std::endl; std::cout << "比较结果: "; if (comparison == 0) { std::cout << "两个结果相等" << std::endl; } else { std::cout << "两个结果不相等" << std::endl; } // 释放资源 OPENSSL_free(euclidean_hex); OPENSSL_free(openssl_hex); BN_free(a); BN_free(b); BN_free(p); BN_free(euclidean_result); BN_free(openssl_result); BN_CTX_end(ctx); BN_CTX_free(ctx); return 0; }
最新发布
09-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值