注册 登录
编程论坛 C语言论坛

vc++的一个小问题,向大佬请教

情深腿自开 发布于 2021-10-19 16:25, 991 次点击
编写一个程序,将两个两位数的正整数a和b合并成一个整数放在c中。合并方式是:将a的十位和个位依次放在
c的个位和百位,将b的十位和个位放在c的十位和千位
2 回复
#2
自由而无用2021-10-19 16:57
//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>

int main(int argc, char *argv[])
{
    int a, b, c, i;
   
    for (i  = 0; i < 10; i++) {
#ifndef C18_PROTOCOL
        /* write your code here */
        //...
        /* write your code here */
        //sample
//#define V_TST
#ifdef V_TST
        a = 17;
        b = 25;
        c = 0;
#endif
#else /* C18_PROTOCOL */
        /************************** Special Declaration ******************************/
        /********* purely entertainment code for 18+ or experienced c-player *********/
        /* warning: if your are under 18 / a new fish the following segment is wrong */
        a = ((unsigned char *)main)[i + 1] % 100;
        b = ((unsigned char *)main)[i + 2] % 100;
        c = ((unsigned char *)main)[i + 5] % 100;
        /************************** Special Declaration ******************************/
#endif/* C18_PROTOCOL */
        printf("(BEF)->group[%d]:a = %d, b = %d, c = %d\n", i, a, b, c);
        c = b % 10 * 1000;
        c += a % 10 * 100;
        c += b % 100 / 10 * 10;
        c += a / 10;
        printf("(AFT)->group[%d]:a = %d, b = %d, c = %d\n", i, a, b, c);
    }
   
    return 0;
}


output sample:

(BEF)->group[0]:a = 17, b = 25, c = 0
(AFT)->group[0]:a = 17, b = 25, c = 5721
(BEF)->group[1]:a = 17, b = 25, c = 0
(AFT)->group[1]:a = 17, b = 25, c = 5721
(BEF)->group[2]:a = 17, b = 25, c = 0
(AFT)->group[2]:a = 17, b = 25, c = 5721
(BEF)->group[3]:a = 17, b = 25, c = 0
(AFT)->group[3]:a = 17, b = 25, c = 5721
(BEF)->group[4]:a = 17, b = 25, c = 0
(AFT)->group[4]:a = 17, b = 25, c = 5721
(BEF)->group[5]:a = 17, b = 25, c = 0
(AFT)->group[5]:a = 17, b = 25, c = 5721
(BEF)->group[6]:a = 17, b = 25, c = 0
(AFT)->group[6]:a = 17, b = 25, c = 5721
(BEF)->group[7]:a = 17, b = 25, c = 0
(AFT)->group[7]:a = 17, b = 25, c = 5721
(BEF)->group[8]:a = 17, b = 25, c = 0
(AFT)->group[8]:a = 17, b = 25, c = 5721
(BEF)->group[9]:a = 17, b = 25, c = 0
(AFT)->group[9]:a = 17, b = 25, c = 5721

[此贴子已经被作者于2021-10-22 12:56编辑过]

#3
lin51616782021-10-21 11:55
以下是引用自由而无用在2021-10-19 16:57:18的发言:

//online parser: https://www.bccn.net/run/
#include <stdio.h>

int main(int argc, char *argv[])
{
    int a, b, c, i;
   
    for (i  = 0; i < 10; i++) {
        a = ((unsigned char *)main)1] % 100;
        b = ((unsigned char *)main)2] % 100;
        c = ((unsigned char *)main)5] % 100;
        printf("(BEF)->group[%d]:a = %d, b = %d, c = %d\n", i, a, b, c);
        c = b % 10 * 1000;
        c += a % 10 * 100;
        c += b % 100 / 10 * 10;
        c += a / 10;
        printf("(AFT)->group[%d]:a = %d, b = %d, c = %d\n", i, a, b, c);
    }
   
    return 0;
}

output sample:
(BEF)->group[0]:a = 72, b = 37, c = 31
(AFT)->group[0]:a = 72, b = 37, c = 7237
(BEF)->group[1]:a = 37, b = 29, c = 36
(AFT)->group[1]:a = 37, b = 29, c = 9723
(BEF)->group[2]:a = 29, b = 72, c = 32
(AFT)->group[2]:a = 29, b = 72, c = 2972
(BEF)->group[3]:a = 72, b = 31, c = 37
(AFT)->group[3]:a = 72, b = 31, c = 1237
(BEF)->group[4]:a = 31, b = 36, c = 25
(AFT)->group[4]:a = 31, b = 36, c = 6133
(BEF)->group[5]:a = 36, b = 32, c = 36
(AFT)->group[5]:a = 36, b = 32, c = 2633
(BEF)->group[6]:a = 32, b = 37, c = 72
(AFT)->group[6]:a = 32, b = 37, c = 7233
(BEF)->group[7]:a = 37, b = 25, c = 37
(AFT)->group[7]:a = 37, b = 25, c = 5723
(BEF)->group[8]:a = 25, b = 36, c = 17
(AFT)->group[8]:a = 25, b = 36, c = 6532
(BEF)->group[9]:a = 36, b = 72, c = 24
(AFT)->group[9]:a = 36, b = 72, c = 2673

选择main做偏移的做法 糟糕
并且是错的
函数并不保证能转换到unsigned char*
#4
自由而无用2021-10-21 12:28
I m not a student doing exercises or matching online judgement issues
by using main[i] just to get some tens from anywhere(main, mian, mani) whatever it is
if you care this, I feel sorry for that, man

#5
自由而无用2021-10-21 13:01
回复 3楼 lin5161678
Do you mean that main is a function ? cant ensure the type uint32_t?
it really doesnt matter, I dont consider main is a function, its just a data_block[n]
dynamic data_block[n], I guess that you have no idea that what a function is
if you look back crt.asm
push arg1
push arg2
call dword ptr[_symbol_main]
right?
you consider it as a function, and I pretend to consider it as a data_block
and further more if the compiler generated a wrong block code, just debug it, is that a problem?
If you still insist that a function is a function, okay, thats your way
I could only say that I m not a ROM brain,
#6
lin51616782021-10-21 14:54
以下是引用自由而无用在2021-10-21 13:01:22的发言:

Do you mean that main is a function ? cant ensure the type uint32_t?
it really doesnt matter, I dont consider main is a function, its just a data_block[n]
dynamic data_block[n], I guess that you have no idea that what a function is
if you look back crt.asm
push arg1
push arg2
call dword ptr[_symbol_main]
right?
you consider it as a function, and I pretend to consider it as a data_block
and further more if the compiler generated a wrong block code, just debug it, is that a problem?
If you still insist that a function is a function, okay, thats your way
I could only say that I m not a ROM brain,

我当然理解main是数据块
函数编译之后是一些指令 这些指令也是10串组成 存放在内存里面
极端情况 char main[] = {0x.....}; 可以编译生成可执行程序

问题是 这是一个面向新手的回答 新手不一定能理解到这一点
那么你这个做法就存在一定的误导性了
#7
自由而无用2021-10-21 15:00
回复 6楼 lin5161678
okay, I accept your advice, next time I will change another way~
Thank you for you kindness
#8
自由而无用2021-10-22 13:01
I have added a friendly declaration in case that someone misunderstanding my intention
very much thank you 2 Mr.lin, thats a very sweet and feasible suggestion
1