使用c ++创建程序以计算给定宽度和高度值的纵横比

时间:2012-12-08 16:06:48

标签: c++

创建一个程序,根据宽度
计算计算机屏幕的宽高比            和高度(以像素为单位),使用以下语句:            int width = 1280;             int height = 1024;             双重方面=宽度/高度; 输出结果时,你会得到什么答案?它是否令人满意 - 如果没有,怎么可能  你修改代码,而不再添加任何变量?

#include<iostream>

using namespace std;

int main(){


int width = 1280;
int height = 1024;
double aspect = width / height;

cout << "aspect ration" << aspect << endl;


return 0;

}

我尝试了这段代码,但它给了我“1”的价值。我无法得到这个问题......他的意思是满意吗?如何在不添加任何变量的情况下修改代码?

1 个答案:

答案 0 :(得分:3)

您正在进行整数除法,即如果width为3且height为2,则在aspect中存储1而不是1.5。其中一个值应该加倍以使其成为双重除法。以下应该工作:

#include<iostream>

using namespace std;

int main(){

    int width = 1280;
    int height = 1024;
    double aspect = (double)width / height;

    cout << "aspect ration" << aspect << endl;


    return 0;

}
相关问题