首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

想问上这两个程序为什么运行结果不同

2013-01-20 
想问下这两个程序为什么运行结果不同?本帖最后由 first_1234 于 2013-01-01 21:18:23 编辑1,#includeiost

想问下这两个程序为什么运行结果不同?
本帖最后由 first_1234 于 2013-01-01 21:18:23 编辑 1,#include<iostream>
using namespace std;
char fun(char x,char y)
{
if(x<y)
return x;
return y;
}
void main()
{
int a='9',b='8',c='7';
cout<<fun(fun(a,b),fun(b,c))<<endl;
}
2,#include<iostream>
using namespace std;
int fun(int x,int y)
{
if(x<y)
return x;
return y;
}
void main()
{
int a='9',b='8',c='7';
cout<<fun(fun(a,b),fun(b,c))<<endl;
}
还有#include<iostream>
using namespace std;
void main()
{
int a=0,b=0,c=0,x=35;
if(!a)
x--;
else
if(b);
if(c)
x=3;
else
x=4;
cout<<x<<endl;
}
能解释下吗 iostream fun c
[解决办法]
对于char fun(char x,char y)
调用fun(fun(a,b),fun(b,c))最终返回char对象
对于int fun(int x,int y)
调用fun(fun(a,b),fun(b,c))最终返回int对象

cout对于不同的对象类型,输出方式不一样,int就输出数值,char输出对应的ASCII字符
即可以考虑以下代码:
cout << (int)'a' << endl;
cout << (char)97 << endl;


# include <iostream>

using namespace std;

int main()
{
    int a = 0, b = 0, c = 0, x = 35;

    if (!a)
        x--; // 执行这句
    else
        if (b)
            ;
    if (c)
        x = 3;
    else
        x = 4; // 执行这句

    cout << x << endl;

    return 0;
}

这样会不会更清晰?

int main()
{
    int a = 0, b = 0, c = 0, x = 35;

    if (!a)
    {
        x--;
    }
    else
    {
        if (b)
        {
            ;
        }
    }
    if (c)
    {
        x = 3;
    }
    else
    {
        x = 4;
    }

    cout << x << endl;

    return 0;
}

热点排行