| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 853 人关注过本帖
标题:vc++的一个小问题,向大佬请教
只看楼主 加入收藏
情深腿自开
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2021-10-19
结帖率:0
收藏
已结贴  问题点数:10 回复次数:2 
vc++的一个小问题,向大佬请教
编写一个程序,将两个两位数的正整数a和b合并成一个整数放在c中。合并方式是:将a的十位和个位依次放在
c的个位和百位,将b的十位和个位放在c的十位和千位
搜索更多相关主题的帖子: 个位 百位 十位 vc++ 合并 
2021-10-19 16:25
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:5 
//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编辑过]

2021-10-19 16:57
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:5 
以下是引用自由而无用在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*

https://zh.
2021-10-21 11:55
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
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

2021-10-21 12:28
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
回复 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,
2021-10-21 13:01
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:0 
以下是引用自由而无用在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.....}; 可以编译生成可执行程序

问题是 这是一个面向新手的回答 新手不一定能理解到这一点
那么你这个做法就存在一定的误导性了

https://zh.
2021-10-21 14:54
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
回复 6楼 lin5161678
okay, I accept your advice, next time I will change another way~
Thank you for you kindness
2021-10-21 15:00
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
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
2021-10-22 13:01
快速回复:vc++的一个小问题,向大佬请教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020052 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved