首页 > 代码库 > [c/c++] programming之路(8)、汇编、求模、自增自减

[c/c++] programming之路(8)、汇编、求模、自增自减

一、插入汇编

 1 #include<stdio.h> 
 2 
 3 void main(){
 4     int num=10;
 5     num=num+5;
 6     //插入汇编语言
 7     _asm{
 8         mov eax,num;//eax是一个存储器,将num的值移动到eax
 9         add eax,5    //eax值+5
10         mov num,eax    //eax值赋给num
11     }
12     printf("%d",num);
13     getchar();
14 }

二、求模运算,颠覆数

 1 #include<stdio.h> 
 2 #include<stdlib.h> 
 3 
 4 void main1(){
 5     //int num=5%9.0;//报错,求模运算只能是整数
 6     //int num=‘A‘%3;//65%3=2
 7     printf("%d",3%5);//3=0*5+3
 8     printf("\n%d",5%3);//5=1*3+2
 9     printf("\n%d",3%-5);//3=1*-5+3
10     printf("\n%d",-3%-5);//-3=0*-5-3
11     printf("\n%d",-3%5);//-3=0*5-3
12     printf("\n%d",5%-3);//5=-1*-3+2
13     getchar();
14 }
15 //面试题:不准用求模运算
16 /*
17     120%19->6=120-120/19*19
18     100%40->20=100-100/40*40
19     x%y=x-x/y*y
20 */
21 void main2(){
22     int x,y;
23     scanf("%d%d",&x,&y);
24     printf("x=%d,y=%d",x,y);
25     //printf("\n%d",x%y);
26     printf("\n%d",x-x/y*y);
27 
28     system("pause");
29 }
30 //颠覆数    123->321    456->654
31 void main(){
32     int num,ge,shi,bai;
33     scanf("%d",&num);
34 
35     ge=num%10;
36     shi=num/10%10;
37     bai=num/100;
38 
39     printf("%d",ge*100+shi*10+bai);
40     system("pause");
41 }

练习:四位数颠覆,不允许使用求模运算

 1 #include<stdio.h> 
 2 #include<stdlib.h> 
 3 void main(){
 4     int num,ge,shi,bai,qian;
 5     scanf("%d",&num);
 6 
 7     ge=num-num/10*10;
 8     shi=num/10-num/100*10;
 9     bai=num/100-num/1000*10;
10     qian=num/1000;
11 
12     printf("%d",ge*1000+shi*100+bai*10+qian);
13     system("pause");
14 }

三、自增自减

 1 #include<stdio.h> 
 2 #include<stdlib.h> 
 3 
 4 void main2(){
 5     //++--高于乘除,乘除高于+-
 6     int num=3;
 7     printf("%d",-num++);//-3
 8     printf("\n%d",num);//4
 9     system("pause");
10 }
11 void main(){
12     int a=3;
13     int b=4;
14     int num=10;
15     printf("%d",a+++b);//->(a++)+b
16     printf("\n%d",a);//4
17 
18     //printf("\n%d",(10*num)++);//报错,“++”需要左值(表达式不能用++--)
19     printf("\n%d",10*num++);//100
20     system("pause");
21 }

 

[c/c++] programming之路(8)、汇编、求模、自增自减