易错程序分析题 C++(dating)

本文仅整理C++中易错(不一定难)的题目,有疑问欢迎私聊。题目的答案需要使用编译器运行后自行对照,不便之处,敬请谅解。程序已经测试可以运行。

注意每题都有坑,认真分析。

1

#include <bits/stdc++.h>
using namespace std;
main()
{
    int a=12;
    a+=a-=a*a;
    cout<<a<<endl;
    return 0;
 } 
#include <bits/stdc++.h>
using namespace std;
main()
{
    int a=12;
    a+=a-=a*a;//从右往左赋值 
    cout<<a<<endl;
    return 0;
 } 
提示

 

2

#include <bits/stdc++.h>
using namespace std;
main()
{
    int i,x=4,y=5;
    i=++x==5||++y==6;
    cout<<"x="<<x<<endl<<"y="<<y<<endl<<"i="<<i<<endl;
    return 0; 
 } 

 

#include <bits/stdc++.h>
using namespace std;
main()
{
    int i,x=4,y=5;
    i=++x==5||++y==6;//在||运算中判断 ++x==5为真就不用判断后面的 ++y==6
    cout<<"x="<<x<<endl<<"y="<<y<<endl<<"i="<<i<<endl;
    return 0; 
 } 
提示

 

 3

#include <bits/stdc++.h>
using namespace std;
main()
{
    int i,x=4,y=5;
    i=x++==5&&y++==6;
    cout<<"x="<<x<<endl<<"y="<<y<<endl<<"i="<<i<<endl;
    return 0; 
 } 

 

#include <bits/stdc++.h>
using namespace std;
main()
{
    int i,x=4,y=5;
    i=x++==5&&y++==6;//在&&运算时,前者为假后者不需要判断 
    cout<<"x="<<x<<endl<<"y="<<y<<endl<<"i="<<i<<endl;
    return 0; 
 } 
提示

 

 4

#include <bits/stdc++.h>
using namespace std;
main()
{
    int x=1,y=2,z=3;
    x+=y+=z;
    cout<<(x<y?y:x)<<endl;
    cout<<(x<y?x++:y++)<<endl;
    cout<<x<<","<<y<<endl;
    cout<<(z+=x>y?x++:y++)<<endl;
    cout<<y<<","<<z<<endl;
    x=3;y=z=4;
    cout<<(z>y&&y==x?1:0)<<endl;
    cout<<(z>=y&&y>=x)<<endl;
    return 0;
 } 
#include <bits/stdc++.h>
using namespace std;
main()
{
    int x=1,y=2,z=3;
    x+=y+=z;//y=5,x=6 
    cout<<(x<y?y:x)<<endl;//6
    cout<<(x<y?x++:y++)<<endl;//5 x=6,y=6 没有执行x++ 
    cout<<x<<","<<y<<endl;//6,6 
    cout<<(z+=x>y?x++:y++)<<endl;//9 x=6,y=7
    cout<<y<<","<<z<<endl;//7,9
    x=3;y=z=4;
    cout<<(z>y&&y==x?1:0)<<endl;//0
    cout<<(z>=y&&y>=x)<<endl;//1
    return 0;
 } 
提示

 

5

本题输入为2473

#include <bits/stdc++.h>
using namespace std;
main()
{
    char ch;
    while(cin.get(ch)&&ch!='\n')
    switch(ch-'2')
        {case 0:
         case 1:cout<<(char)(ch+4);
         case 2:cout<<(char)(ch+4);break;
         case 3:cout<<(char)(ch+3);
         default:cout<<(char)(ch+2);break;
        }
    cout<<endl;
    return 0;
 } 

 

#include <bits/stdc++.h>
using namespace std;
main()
{
    char ch;//定义字符ch 
    while(cin.get(ch)&&ch!='\n')//从键盘输入ch且ch不能是换行键 
    switch(ch-'2')//将ch转换成对应的ACSII码值-2 
        {case 0:
         case 1:cout<<(char)(ch+4);
         case 2:cout<<(char)(ch+4);break;
         case 3:cout<<(char)(ch+3);
         default:cout<<(char)(ch+2);break;
        }//注意:如果没有碰到breakswitch语句会一直运行到default运行结束为止 
    cout<<endl;
    return 0;
 } 
提示

 

6

#include <bits/stdc++.h>
using namespace std;
main()
{
    int a=10,y=0;
    do
    {
        a+=2;y+=a;
        cout<<"a="<<a<<",y="<<y<<endl;
        if(y>50) break;
    }
    while(a=14);
    return 0;
 } 

 

#include <bits/stdc++.h>
using namespace std;
main()
{
    int a=10,y=0;
    do
    {
        a+=2;y+=a;
        cout<<"a="<<a<<",y="<<y<<endl;
        if(y>50) break;
    }
    while(a=14);//条件是赋值语句而不是判断语句 
    return 0;
 } 
提示

 

7

#include <bits/stdc++.h>
using namespace std;
main()
{
    int i;
    for (i=1;i<=5;i++)
        {
            if(i%2) cout<<"*";
            else continue;
            cout<<"#";
        }
        cout<<"$\n";
        return 0;
 } 

 

#include <bits/stdc++.h>
using namespace std;
main()
{
    int i;
    for (i=1;i<=5;i++)
        {
            if(i%2) cout<<"*";
            else continue;// continue是忽略之后的所有语句 
            cout<<"#";
        }
        cout<<"$\n";
        return 0;
 } 
提示

 

8

#include <bits/stdc++.h>
using namespace std;
main()
{
    int k=0;char c='A';
    do
    {switch(c++)
        {case'A':k++;break;
         case'B':k--;
         case'C':k+=2;break;
         case'D':k=k%2;continue;
         case'E':k=k*10;break;
         default:k=k/3;
         } 
    k++;
    }while(c<'G');
    cout<<"k="<<k<<endl;
    return 0; 
 } 

 

#include <bits/stdc++.h>
using namespace std;
main()
{
    int k=0;char c='A';
    do
    {switch(c++)//字符的自增 
        {case'A':k++;break;
         case'B':k--;
         case'C':k+=2;break;
         case'D':k=k%2;continue;//continue是忽略了switch和循环中在它之后的所有语句 
         case'E':k=k*10;break;
         default:k=k/3;
         } 
    k++;
    }while(c<'G');
    cout<<"k="<<k<<endl;
    return 0; 
 } 
提示

 

9

#include <bits/stdc++.h>
using namespace std;
void fun (int x,int y)
{
    x=x*10;
    y=y+x;
    cout<<x<<'\t'<<y<<endl;
}

main()
{
    int a=2,b=3;
    fun(a+b,a*b);
    cout<<a<<'\t'<<b<<endl;
    return 0;
} 

 

#include <bits/stdc++.h>
using namespace std;
void fun (int x,int y)
{
    x=x*10;
    y=y+x;
    cout<<x<<'\t'<<y<<endl;
}//此函数并不会改变实际参数的值 

main()
{
    int a=2,b=3;
    fun(a+b,a*b);
    cout<<a<<'\t'<<b<<endl;
    return 0;
} 
提示

 

 

10

#include <bits/stdc++.h>
using namespace std;
int x;
void cude()
{
    x=x*x*x;
    cout<<x<<endl;
}

int main()
{
    int x=5;
    cude();
    cout<<x<<endl;
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
int x;//全局变量初始化值为0 
void cude()
{
    x=x*x*x;
    cout<<x<<endl;
}//函数没有给实际参数x赋值 

int main()
{
    int x=5;
    cude();//调用时不改变x的值 
    cout<<x<<endl;
    return 0;
}
提示

 

11

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a=1,b=2,c=3;
    ++a;
    c+=++b;
    {
        int b=4,c;
        c=b*3;
        a+=c;
        cout<<"first:"<<a<<'\t'<<b<<'\t'<<c<<endl;
        a+=c;
        cout<<"second:"<<a<<'\t'<<b<<'\t'<<c<<endl;
    }
    cout<<"thrid:"<<a<<'\t'<<b<<'\t'<<c<<endl;
}

 

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a=1,b=2,c=3;
    ++a;
    c+=++b;
    {
        int b=4,c;//局部变量优先于全局变量 
        c=b*3;
        a+=c;
        cout<<"first:"<<a<<'\t'<<b<<'\t'<<c<<endl;
        a+=c;
        cout<<"second:"<<a<<'\t'<<b<<'\t'<<c<<endl;
    }
    cout<<"thrid:"<<a<<'\t'<<b<<'\t'<<c<<endl;
}
提示

 

12

#include <bits/stdc++.h>
using namespace std;
int i=0;

int workover(int i)
{
    i=(i%i)*((i*i)/(2*i)+4);
    cout<<"i="<<i<<endl;
    return i;
}

int rest(int i)
{
    i=i<2?5:0;
    return i;
}

main()
{
    int i=5;
    rest(i/2);
    cout<<"i="<<i<<endl;
    rest(i=i/2);
    cout<<"i="<<i<<endl;
    i=rest(i/2);
    cout<<"i="<<i<<endl;
    workover(i);
    cout<<"i="<<i<<endl;
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
int i=0;

int workover(int i)
{
    i=(i%i)*((i*i)/(2*i)+4);
    cout<<"i="<<i<<endl;//函数中的输出 
    return i;
}

int rest(int i)
{
    i=i<2?5:0;
    return i;
}

main()
{
    int i=5;
    rest(i/2);//没有给i赋值 
    cout<<"i="<<i<<endl;
    rest(i=i/2);//给i赋值i/2 
    cout<<"i="<<i<<endl;
    i=rest(i/2);//给i赋 rest(i/2)的返回值 
    cout<<"i="<<i<<endl;
    workover(i);//调用函数 
    cout<<"i="<<i<<endl;
    return 0;
}
提示

 

13 此题仅便于理解宏定义

#include <bits/stdc++.h>
using namespace std;
#define PRICE 30
int main()
{
    int num,total;
    num=10;
    total=num*PRICE;
    cout<<"total="<<total<<endl;
    return 0;
}

 

14

#include <bits/stdc++.h>
using namespace std;
int func (int a,int b)
{
    static int m=0,i=2;
    i+=m+1;
    m=i+a+b;
    return m;
}

main()
{
    int k=4,m=1,p;
    p=func(k,m); cout<<p<<endl;
    p=func(k,m); cout<<p<<endl;
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
int func (int a,int b)
{
    static int m=0,i=2;//静态变量 该赋值语句只执行一次 
    i+=m+1;
    m=i+a+b;
    return m;
}

main()
{
    int k=4,m=1,p;
    p=func(k,m); cout<<p<<endl;
    p=func(k,m); cout<<p<<endl;
    return 0;
}
提示

 

15 本题无需计算 查看结果 了解宏定义即可

#include <bits/stdc++.h>
using namespace std;
#define PI 3.1415926
#define S(r) PI*r*r
#define Q(r) PI*(r)*(r)
int main()
{
    float a,b,area1,area2;
    a=1;b=2;
    area1=S(a+b);
    cout<<"area1="<<area1<<endl;
    area2=Q(a+b);
    cout<<"area2="<<area2<<endl;
    if (area1==area2)    cout<<"True"<<endl;
    else cout<<"Flase"<<endl;
    return 0;
}

 

16

#include <bits/stdc++.h>
using namespace std;
#define DEBUG
int main()
{
    int a=14,b=15,c;
    c=a/b;
    #ifdef DEBUG
    cout<<"a="<<oct<<a<<endl<<"b="<<b<<endl;
    #endif
    cout<<"c="<<dec<<c<<endl;
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
#define DEBUG
int main()
{
    int a=14,b=15,c;
    c=a/b;
    #ifdef DEBUG//如果定义了DEBUG就编译以下的语句 
    cout<<"a="<<oct<<a<<endl<<"b="<<b<<endl;
    #endif
    cout<<"c="<<dec<<c<<endl;
    return 0;
}
提示

 

17

#include <bits/stdc++.h>
using namespace std;
int main()
{
    static int n[2],i,j,k;
    for (i=0;i<2;i++)
        n[j++]=n[i]+i+1;
    cout<<n[k]<<'\t'<<n[k++]<<endl;
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
int main()
{
    static int n[2],i,j,k;//静态变量 
    for (i=0;i<2;i++)
        n[j++]=n[i]+i+1;
    cout<<n[k]<<'\t'<<n[k++]<<endl;//k是静态变量,注意数值变化 
    return 0;
}
提示

 

18

#include <bits/stdc++.h>
using namespace std;
main()
{
    int *p1,*p2,*p,a=10,b=100;
    p1=&a;
    p2=&b;
    if(a<b)
        {
            p=p1;
            p1=p2;
            p2=p;
        }
    cout<<a<<'\t'<<b<<endl;
    cout<<*p1<<'\t'<<*p2<<endl;
    return 0; 
} 

 

#include <bits/stdc++.h>
using namespace std;
main()
{
    int *p1,*p2,*p,a=10,b=100;//定义指向整型的指针*p1,*p2,*p和整型变量a,b 
    p1=&a; //将a的地址赋给p1 
    p2=&b;
    if(a<b)
        {
            p=p1;
            p1=p2;
            p2=p;
        }//仅对指针(地址)进行交换 
    cout<<a<<'\t'<<b<<endl;
    cout<<*p1<<'\t'<<*p2<<endl;
    return 0; 
} 
提示

 

19 对比下列两个程序理解引用与指针的区别

#include <bits/stdc++.h>
using namespace std;
void change (int &x,int &y)
{
    int t;
    t=x;x=y;y=t;
}
main()
{
    int a=3,b=5;
    change(a,b);
    cout<<a<<'\t'<<b<<endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
void change (int *x,int *y)
{
    int t;
    t=*x;*x=*y;*y=t;
}
main()
{
    int a=3,b=5;
    change(&a,&b);
    cout<<a<<'\t'<<b<<endl;
    return 0;
}

 

20

#include <bits/stdc++.h>
using namespace std;
void dd (int&x,int&y,int z)
{
    x=x+z;
    y=y-x;
    z=10;
    cout<<"(2)"<<x<<'\t'<<y<<'\t'<<z<<endl;
}
main()
{
    int a=3,b=4,c=5;
    for (int i=0;i<2;i++)
        dd(a,b,c);
    cout<<"(1)"<<a<<'\t'<<b<<'\t'<<c<<endl;
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
void dd (int &x,int &y,int z)// x,y是对实参a,b的引用 ,而z仅仅是一个形式参数 
{
    x=x+z;
    y=y-x;
    z=10;
    cout<<"(2)"<<x<<'\t'<<y<<'\t'<<z<<endl;
}
main()
{
    int a=3,b=4,c=5;
    for (int i=0;i<2;i++)
        dd(a,b,c);
    cout<<"(1)"<<a<<'\t'<<b<<'\t'<<c<<endl;
    return 0;
}
提示

 

21

#include <bits/stdc++.h>
using namespace std;
int a=4;
int &f(int x)
{
    a=a+x;
    return a;
}
main()
{
    int t=5;
    cout<<f(t)<<endl;
    f(t)=20;
    cout<<f(t)<<endl;
    t=f(t);
    cout<<f(t)<<endl;
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
int a=4;
int &f(int x)//对函数返回值a的引用 
{
    a=a+x;
    return a;
}
main()
{
    int t=5;
    cout<<f(t)<<endl;
    f(t)=20;
    cout<<f(t)<<endl;
    t=f(t);
    cout<<f(t)<<endl;
    return 0;
}
提示

 

22

#include <bits/stdc++.h>
using namespace std;
int &f(int &x)
{
    static int t=2;
    t=x++;
    return t;
}
main()
{
    int a=3;
    cout<<f(a)<<endl;
    f(a)=20;
    a=a+5;
    cout<<f(a)<<endl;
    a=f(a);
    cout<<f(a)<<endl;
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
int &f(int &x)//f()是对返回值t的引用,而x是对实际参数的引用 
{
    static int t=2;//静态变量
    t=x++;
    return t;
}
main()
{
    int a=3;
    cout<<f(a)<<endl;
    f(a)=20;//即使f(a)在左边也需要运行一次函数 
    a=a+5;
    cout<<f(a)<<endl;
    a=f(a);
    cout<<f(a)<<endl;
    return 0;
}
提示

 

posted @ 2021-06-02 23:42  柏木カケル  阅读(361)  评论(2)    收藏  举报