show_bytes 代码解析,心得

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

  首先建立头文件,头文件是扩展名为 .h 的文件,包含了 C 函数声明和宏定义。

  stdio 就是指 “standard input & output"即标准的输入输出,所以,源代码中如用到标准输入输出函数时,就要包含这个头文件!

  stdlib 头文件即standard library标准库函数头文件,stdlib 头文件里包含的函数有:calloc; free ; malloc ; realloc; rand ; abort ; exit  ; getenv  ; putenv ; labs; atof  ; atoi  ; atol  ; ecvt  ; fcvt .

  C语言里面关于字符数组的函数定义的头文件,常用函数有strlen、strcmp、strcpy等等。

typedef unsigned char *byte_pointer;

然后定义一种无符号的char型的指针,这样一个字节指针引用一个字节序列,其中每个字节都会被认为是一个非负整数。

void show_bytes(byte_pointer start, size_t len) {
    size_t i;
    for (i = 0; i < len; i++)
    printf("%p\t0x%.2x\n", &start[i], start[i]); 
    printf("\n");
}

这个函数将传入无符号字符对象的指针,和该对象的字节数,通过循环,最后输出前面为地址,后面是数值。

void show_int(int x) {
    show_bytes((byte_pointer) &x, sizeof(int)); 
}

void show_float(float x) {
    show_bytes((byte_pointer) &x, sizeof(float));
}

void show_pointer(void *x) {
    show_bytes((byte_pointer) &x, sizeof(void *));
}

 这三个函数,分别转化为int, float型,并用sizeof记录位置,以确定储存方式。

void test_show_bytes(int val) {
    int ival = val;
    double fval = (double) ival;
    int *pval = &ival;
    printf("Stack variable ival = %d\n", ival);
    printf("(int)ival:\n");
    show_int(ival);
    printf("(float)ival:\n");
    show_float(fval);
    printf("&ival:\n");
    show_pointer(pval);
}

开始检测,并分别显示float,int 型表示方式,并显示地址

void simple_show_a() {
/* $begin simple-show-a */
int val = 0x87654321;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show-a */
}

void simple_show_b() {
/* $begin simple-show-b */
int val = 0x12345678;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show-b */
}

这里简单地显示是大端存储还是小端存储。

这是运行结果:

很明显,这是小端存储。

void float_eg() {
  int x = 3490593;
  float f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

  x = 3510593;
  f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

}

这一段的意思是将int强制转化为float,看是否有变化并查看存储方式。

void string_ueg() {
/* $begin show-ustring */
const char *s = "ABCDEF";
show_bytes((byte_pointer) s, strlen(s)); 
/* $end show-ustring */
}

void string_leg() {
/* $begin show-lstring */
const char *s = "abcdef";
show_bytes((byte_pointer) s, strlen(s)); 
/* $end show-lstring */
}

这两段分别为:上面的是大写字母存储方式,下面的是小写字母存储方式。

这是运行结果

void show_twocomp() 
{
/* $begin show-twocomp */
    short x = 12345; 
    short mx = -x; 
    
    show_bytes((byte_pointer) &x, sizeof(short)); 
    show_bytes((byte_pointer) &mx, sizeof(short)); 
/* $end show-twocomp */
}

这里是求一个数的相反数

int main(int argc, char *argv[])
{
    int val = 12345;
    if (argc > 1) {
        val = strtol(argv[1], NULL, 0);
    printf("calling test_show_bytes\n");
    test_show_bytes(val);
    } else {
    printf("calling show_twocomp\n");
    show_twocomp();
    printf("Calling simple_show_a\n");
    simple_show_a();
    printf("Calling simple_show_b\n");
    simple_show_b();
    printf("Calling float_eg\n");
    float_eg();
    printf("Calling string_ueg\n");
    string_ueg();
    printf("Calling string_leg\n");
    string_leg();
    }
    return 0;
}

最后为主函数,分为两条路径,分别是如果加入参数,那么就会运行输入的数字,如果没有输入,那么就会走下面的路。

 

整个代码为:

/* show-bytes - prints byte representation of data */
/* $begin show-bytes */
#include <stdio.h>
/* $end show-bytes */
#include <stdlib.h>
#include <string.h>
/* $begin show-bytes */

typedef unsigned char *byte_pointer;
//typedef char *byte_pointer;
//typedef int *byte_pointer;

void show_bytes(byte_pointer start, size_t len) {
    size_t i;
    for (i = 0; i < len; i++)
    printf("%p\t0x%.2x\n", &start[i], start[i]); 
    printf("\n");
}

void show_int(int x) {
    show_bytes((byte_pointer) &x, sizeof(int)); 
}

void show_float(float x) {
    show_bytes((byte_pointer) &x, sizeof(float));
}

void show_pointer(void *x) {
    show_bytes((byte_pointer) &x, sizeof(void *));
}
/* $end show-bytes */


/* $begin test-show-bytes */
void test_show_bytes(int val) {
    int ival = val;
    //float fval = (float) ival;
    double fval = (double) ival;
    int *pval = &ival;
    printf("Stack variable ival = %d\n", ival);
    printf("(int)ival:\n");
    show_int(ival);
    printf("(float)ival:\n");
    show_float(fval);
    printf("&ival:\n");
    show_pointer(pval);
}
/* $end test-show-bytes */

void simple_show_a() {
/* $begin simple-show-a */
int val = 0x87654321;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show-a */
}

void simple_show_b() {
/* $begin simple-show-b */
int val = 0x12345678;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show-b */
}

void float_eg() {
  int x = 3490593;
  float f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

  x = 3510593;
  f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

}

void string_ueg() {
/* $begin show-ustring */
const char *s = "ABCDEF";
show_bytes((byte_pointer) s, strlen(s)); 
/* $end show-ustring */
}

void string_leg() {
/* $begin show-lstring */
const char *s = "abcdef";
show_bytes((byte_pointer) s, strlen(s)); 
/* $end show-lstring */
}

void show_twocomp() 
{
/* $begin show-twocomp */
    short x = 12345; 
    short mx = -x; 
    
    show_bytes((byte_pointer) &x, sizeof(short)); 
    show_bytes((byte_pointer) &mx, sizeof(short)); 
/* $end show-twocomp */
}

int main(int argc, char *argv[])
{
    int val = 12345;
    if (argc > 1) {
        val = strtol(argv[1], NULL, 0);
    printf("calling test_show_bytes\n");
    test_show_bytes(val);
    } else {
    printf("calling show_twocomp\n");
    show_twocomp();
    printf("Calling simple_show_a\n");
    simple_show_a();
    printf("Calling simple_show_b\n");
    simple_show_b();
    printf("Calling float_eg\n");
    float_eg();
    printf("Calling string_ueg\n");
    string_ueg();
    printf("Calling string_leg\n");
    string_leg();
    }
    return 0;
}

 

 

总结:

  其实我也是第一次去理解代码,以往都是自己去写代码。但是这个代码明明老师在课上讲过,但在自己看的时候,却是一次又一次地向同学讨教。虽然这个代码已经搞清楚了,但是我也会走向更加深的层次上,走向代码的路上不会只有这个难度。加油加油加油!!!

 

如果有错误的地方可以和我联系改正,并希望谅解,新手上路,有错误请求指正,谢谢

 

 

 

 

 

代码存在的逻辑性问题有: 1. 在函数 clip_1_1 和 clip_4_3 中,进行宽高的重新赋值操作,但是并没有对 n_bytes 进行调整,导致输出的图片信息不准确。 2. 在函数 clip_4_3 中,对于宽高比例不符合 4:3 的情况,使用了嵌套的 if-else 结构,使得代码的逻辑复杂,容易出错。同时,在进行浮点数运算时,使用了整数除法,导致计算结果不准确。 3. 在 main 函数中,多次使用了相同的变量名,容易引起混淆。 针对这些问题,可以进行如下的改进: 1. 在进行宽高赋值操作时,同时对 n_bytes 进行调整。 2. 在函数 clip_4_3 中,可以使用简单的 if-else 结构,避免嵌套和复杂的计算。同时,可以将浮点数计算转换为整数计算,避免精度问题。 3. 修改 main 函数中的变量名,使其更具有描述性。 改进后的代码如下: ```c++ #include<bits/stdc++.h> using namespace std; class Photo { private: int width; int height; int n_bytes; public: static int num; Photo():width(0),height(0),n_bytes(0){ num++; }; Photo(int w, int h, int n):width(w),height(h),n_bytes(n){ num++; }; Photo(const Photo& p){ this->width = p.width; this->height = p.height; this->n_bytes = p.n_bytes; num++; }; void show_info(){ cout << num << " " << width << " " << height << " " << n_bytes << endl; };//显示图片信息,输出格式见样例 void clip_1_1(){ if(width > height){ height = width; width = height * 1.0 / 1.0 + 0.5; n_bytes = n_bytes * width * height / (width + height) / (width + height); }else{ width = height; height = width * 1.0 / 1.0 + 0.5; n_bytes = n_bytes * width * height / (width + height) / (width + height); } };//将图片按最大的正方形裁剪,计算新的高和宽。结果截断小数部分,下同 void clip_4_3(){ if(width * 3 > height * 4){ height = width * 4 / 3; n_bytes = n_bytes * width * height / (width + height) / (width + height); }else{ width = height * 4 / 3; n_bytes = n_bytes * width * height / (width + height) / (width + height); } };//将图片按最大的4:3形状裁剪,计算新的高和宽 }; int Photo::num = 0; int main(){ Photo photo1(4000, 3000, 4); photo1.clip_1_1(); photo1.show_info(); Photo photo2(4000, 3000, 4); photo2.clip_4_3(); photo2.show_info(); Photo photo3(photo2); photo3.clip_1_1(); photo3.show_info(); int input_height, input_width, input_n_bytes; cin >> input_height >> input_width >> input_n_bytes; Photo photo4(input_height, input_width, input_n_bytes); photo4.clip_4_3(); photo4.show_info(); photo4.clip_1_1(); photo4.show_info(); return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值