免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2205 | 回复: 8
打印 上一主题 下一主题

[C++] 不好意思,问一个菜鸟问题,关于c++ [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-02-08 08:21 |只看该作者 |倒序浏览
请问乘号除号在c++中有先后顺序吗?谢谢

论坛徽章:
0
2 [报告]
发表于 2004-02-08 17:09 |只看该作者

不好意思,问一个菜鸟问题,关于c++

有啊。
相同优先级从左到右呗。

论坛徽章:
0
3 [报告]
发表于 2004-02-08 19:28 |只看该作者

不好意思,问一个菜鸟问题,关于c++

呵呵

论坛徽章:
0
4 [报告]
发表于 2004-02-08 19:35 |只看该作者

不好意思,问一个菜鸟问题,关于c++

原帖由 "forest077" 发表:
有啊。
相同优先级从左到右呗。

就是

论坛徽章:
0
5 [报告]
发表于 2004-02-09 09:49 |只看该作者

不好意思,问一个菜鸟问题,关于c++

这和加减是一样的,看来楼主要看看关于基础知识的书,几乎每本书上都有介绍优先级的!

论坛徽章:
0
6 [报告]
发表于 2004-02-09 12:01 |只看该作者

不好意思,问一个菜鸟问题,关于c++

这个其实我也知道,不过有一个程序,要找错误,有一个错误就在*/上面,有点糊涂了,我贴上来大家帮我看看吧。
#include <iostream>;
using namespace std;

// function prototypes
float combinedMark( float labs, float midterm, float assignment, float final );
float adjustMark( float rawTotal, float final, char failFinalFailCourse );


// main() - get input marks, and find out whether or not the instructor
// decides to enforce the "need to pass the final exam to pass the course"
// rule. Then compute (and print) the final grade. If a student fails the
// final exam, but his/her combined average is 50% or more, then a mark of
// 49% will be printed.

int main( void )
{   
    char  failFinalFailCourse;
    float labs;
    float midterm;
    float assignment;
    float final;
    float total;
    float finalGrade;
        int   integerGrade;

    // read the various marks from the keyboard

    cout << "Enter your total lab mark (/ 200): ";
    cin  >;>; labs;

    cout << "Enter your midterm mark (/ 90): ";
    cin  >;>; midterm;

    cout << "Enter your assignment mark (/ 60): ";
    cin  >;>; assignment;

    cout << "Enter your final exam mark (/ 120): ";
    cin  >;>; final;

    // ask if the "fail final, fail course" rule is in effect

    cout << "Will you fail the course if you failed the final exam?\n";
    cout << "Enter Y or N: ";
    cin  >;>; failFinalFailCourse;

    // compute the combined mark and the final mark
    total = combinedMark( labs, midterm, assignment, final );
    finalGrade = adjustMark( total, final, failFinalFailCourse );

    // display the final mark as an integer
        integerGrade = finalGrade + 0.5;  // round off
    cout << "Your final grade for the course is "
                 << integerGrade << "%.\n";

    return 0;
}



// combinedMark - function to combine the labs marks (out of 200), midterm
// marks (out of 90), assignments (out of 60), and final exam mark (out of 120)
// to obtain a final mark for the course. The weight of each is:
//                labs:            15%
//                midterm:    25%
//      assignment: 15%
//      final:      45%

float combinedMark( float labs, float midterm, float assignment, float final )
{
        // Pre:   labs, midterm, and final are all >;= 0
        // Post:  return the tentative final mark for the course

    float scaledLabs;
    float scaledMidterm;
    float scaledAssignment;
    float scaledFinal;
    float combined;

    scaledLabs = labs * 15 / 200;
    scaledMidterm = 25 / 90 * midterm;
    scaledAssignment = assignment * 15 / 60;
    combined = scaledLabs + scaledMidterm + scaledAssignment * scaledFinal;
    return combined;
}



// Adjust marks so that if one fails the final exam and the "fail the
// final, fail the course" rule is in effect, then a final grade of 49%
// is assigned.

float adjustMark( float rawTotal, float final, char failFinalFailCourse )
{
        // Pre:  rawTotal and final are >;= 0, and
        //       failFinalFailCourse is either 'Y' or 'N'
        // Post: return the student's assigned course grade

    if( failFinalFailCourse = 'Y'  &&  final < 60  ||  rawTotal >;= 50 )
        return 49;

    return rawTotal;
}

里边有5个错误,有一个是25/90*midterm的,如果按照这个,每次计算midterm都是0。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
7 [报告]
发表于 2004-02-09 12:04 |只看该作者

不好意思,问一个菜鸟问题,关于c++

哪个错误在*/上面,你说说看?
程序我没仔细看。
我看都是float类型,是不是计算精度的问题呀?

论坛徽章:
0
8 [报告]
发表于 2004-02-09 12:58 |只看该作者

不好意思,问一个菜鸟问题,关于c++

我自己找到答案了,可能是因为25/60,电脑认为它是integer,所以25/60=0。如果是25.0/60.0就行了。各位看看我说的有道理吗?

论坛徽章:
0
9 [报告]
发表于 2004-02-09 14:11 |只看该作者

不好意思,问一个菜鸟问题,关于c++

[quote]原帖由 "paulm308"]我自己找到答案了,可能是因为25/60,电脑认为它是integer,所以25/60=0。如果是25.0/60.0就行了。各位看看我说的有道理吗?[/quote 发表:

是的,int/int=int。
如果两个int型相乘除的时候要先转换,如:
int i,j;
float f;
i = 5;
j = 6;
f = (float)i/(float)j;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP