佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 4387|回复: 146

各位C++高手请进来。小弟有大大问题。

[复制链接]
发表于 1-11-2009 07:13 PM | 显示全部楼层 |阅读模式
#include<stdio.h>

void main()
{

        float productPrice[10], totalPrice=0, totalRoundedPrice=0;

        int i, item=10;

        char buf[20], cent; // char array is used for identifying last character of total price

        for(i=0;i<10;i++)
        {
                printf("Please enter %d prices of products:", item); //input 10 prices of products
                scanf("%f",&productPrice);

                //calculate the total price of 10 prices of products.
                totalPrice = totalPrice + productPrice;
                item--;

        }
        
        printf("\nProduct Price\n(RM)");
        printf("\n----------------");
        for(i=0;i<10;i++)
        {
                printf("\n%.2f",productPrice); //display all of product price
        }

        printf("\n----------------");

        printf("\nTotal Price (RM)\t %.2f\n",totalPrice); //display total price
        printf("\n----------------");

        sprintf(buf,"%.2f",totalPrice); //convert float to char array

        cent= buf[strlen(buf)-1]; //identify the last character of the total price

        //minus 1cent if the last character of total price is 1 or 6
        if(cent == '1' || cent == '6')
                totalRoundedPrice = totalPrice - 0.01;

        //minus 2cents if the last character of total price is 2 or 7
        if(cent == '2' || cent == '7')
                totalRoundedPrice = totalPrice - 0.02;

        //add 2cents if the last character of total price is 3 or 8
        if(cent == '3' || cent == '8')
                totalRoundedPrice = totalPrice + 0.02;

        //add 1cent if the last character of total price is 4 or 9
        if(cent == '4' || cent == '9')
                totalRoundedPrice = totalPrice + 0.01;

        //remain the total price if the last character of total price is 0 or 5
        if(cent == '0' || cent == '5')
                totalRoundedPrice = totalPrice;

        printf("\nTotal Price Rounded (RM)\t %.2f",totalRoundedPrice); //display total price after rounded
        printf("\n________________");
        printf("\n----------------\n");


[ 本帖最后由 spannar90^_^ 于 4-11-2009 06:19 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 1-11-2009 07:14 PM | 显示全部楼层
请问为什么C++不能出output的。里面是否有什么?
回复

使用道具 举报

发表于 1-11-2009 07:49 PM | 显示全部楼层
帮你correct好了

#include<stdio.h>#include<string.h>


void main()
{



float productPrice[10] , totalPrice=0, totalRoundedPrice=0;


        int i, item=10;


        char buf[20], cent; // char array is used for identifying last character of total price


        for(i=0;i<10;i++)
        {
                printf("lease enter %d prices of products:", item); //input 10 prices of products
                scanf("%f",&productPrice);


                //calculate the total price of 10 prices of products.
                totalPrice=totalPrice+productPrice;
                item--;


        }


        printf("\nProduct Price\n(RM)";
        printf("\n----------------";
        for(i=0;i<10;i++)
        {
                printf("\n%.2f",productPrice ); //display all of product price
        }


        printf("\n----------------";


        printf("\nTotal Price (RM)\t %.2f\n",totalPrice); //display total price
        printf("\n----------------";


        sprintf(buf,"%.2f",totalPrice); //convert float to char array


        cent= buf[strlen(buf)-1]; //identify the last character of the total price


        //minus 1cent if the last character of total price is 1 or 6
        if(cent == '1' || cent == '6')
                totalRoundedPrice = totalPrice - 0.01;


        //minus 2cents if the last character of total price is 2 or 7
        if(cent == '2' || cent == '7')
                totalRoundedPrice = totalPrice - 0.02;


        //add 2cents if the last character of total price is 3 or 8
        if(cent == '3' || cent == '8')
                totalRoundedPrice = totalPrice + 0.02;


        //add 1cent if the last character of total price is 4 or 9
        if(cent == '4' || cent == '9')
                totalRoundedPrice = totalPrice + 0.01;


        //remain the total price if the last character of total price is 0 or 5
        if(cent == '0' || cent == '5')
                totalRoundedPrice = totalPrice;


        printf("\nTotal Price Rounded (RM)\t %.2f",totalRoundedPrice); //display total price after rounded
        printf("\n________________";
        printf("\n----------------\n";



}

[ 本帖最后由 freeangel 于 1-11-2009 08:01 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 1-11-2009 07:57 PM | 显示全部楼层
开得到了。不过为什么一定有加#include<string,h>
回复

使用道具 举报

发表于 1-11-2009 08:03 PM | 显示全部楼层
有strlen         
回复

使用道具 举报

 楼主| 发表于 1-11-2009 08:05 PM | 显示全部楼层

回复 5# freeangel 的帖子

你的意思是???我不太明白.,你的意思是在这里?
cent= buf[strlen(buf)-1]; //identify the last character of the total price

[ 本帖最后由 spannar90^_^ 于 1-11-2009 08:07 PM 编辑 ]
回复

使用道具 举报

Follow Us
发表于 1-11-2009 08:12 PM | 显示全部楼层
如果program 里有strlen ,strcmp ,strcpy ,...都要放#include<string.h>
回复

使用道具 举报

 楼主| 发表于 1-11-2009 08:13 PM | 显示全部楼层
谢谢C++高手。本人小弟是中六学生,读电脑很多东西。但是在pahang全校只有我的学校有这个科目。再加上只有我一个华人拿这科。学的东西要记很多。profesional老师已经在terengganu教书。所以我很惨。C++是上年学的东西,可是没有学到一个月,老师就没有教下去。我读的科目是programming,multimedia,system development,和ICT
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 1-11-2009 08:16 PM | 显示全部楼层
请问高手你是专门读这科吗?
回复

使用道具 举报

发表于 1-11-2009 08:27 PM | 显示全部楼层
我是读AM,上semester 有读到这科
中六有这个科(可能我不知道吧)吗?你还拿什么科呢?

[ 本帖最后由 freeangel 于 1-11-2009 08:28 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 1-11-2009 08:28 PM | 显示全部楼层
我正式的科目是computing。另外的是geografi,business,general form。
回复

使用道具 举报

 楼主| 发表于 1-11-2009 08:32 PM | 显示全部楼层
我的这科不同少少。那就是它读的是重要四个大题。很多人读学院应付一小部分。而我现在才知道有读那么多的。全部少过半桶水。stpm要到了。TT
回复

使用道具 举报

 楼主| 发表于 1-11-2009 08:48 PM | 显示全部楼层
如果真的有书,我大概想到有8cm厚的书吧。
回复

使用道具 举报

 楼主| 发表于 1-11-2009 08:55 PM | 显示全部楼层
你不介意我问的问题是对你来说很简单的吗?这些的#include<conio.h>又怎样的用法?
回复

使用道具 举报

发表于 2-11-2009 09:57 PM | 显示全部楼层
原帖由 spannar90^_^ 于 1-11-2009 08:55 PM 发表
你不介意我问的问题是对你来说很简单的吗?这些的#include又怎样的用法?


有空去找几本c++从基础学起,以后比较容易上手

#include 可以理解成“引用“。
假设我已经编写了常运用到的function (如字串处理@string manipulation), 以后写新程序有用到类似功能时当然没必要重新写过咯,只需要告诉编译器(compiler)可以从哪里拿到代码, 我就可以在接下来的程序重复使用那些function
回复

使用道具 举报

 楼主| 发表于 2-11-2009 10:27 PM | 显示全部楼层

回复 15# yeenfei 的帖子

可是我没有那么多时间来应付。。要考stpm了。
回复

使用道具 举报


ADVERTISEMENT

发表于 3-11-2009 09:43 PM | 显示全部楼层
原帖由 spannar90^_^ 于 2-11-2009 10:27 PM 发表

可是我没有那么多时间来应付。。要考stpm了。


如果你学来只是为了应付考试,当作我什么都没讲过。。。
社会又多了个浪费青春者了。。
回复

使用道具 举报

 楼主| 发表于 3-11-2009 11:28 PM | 显示全部楼层

回复 17# yeenfei 的帖子

哦。你的意思是?我的目标是上大学的。应该会读sains computer。
回复

使用道具 举报

发表于 4-11-2009 08:21 AM | 显示全部楼层
原帖由 spannar90^_^ 于 3-11-2009 11:28 PM 发表
哦。你的意思是?我的目标是上大学的。应该会读sains computer。


你的兴趣,人生目标是什么?不用太远,30岁想达到什么,40岁要有什么成就?
上大学只是接受达到目标所需知识的一种管道,如果你有明确的方向,不上大学不一定不能成功;反之没有理想的话,博士毕业到头来只不过庸庸碌碌过一生。
在任何行业没有热诚就不会有持续改进的动力,如果一味跟着‘流行’决定职业,很可能变成自己的包袱
回复

使用道具 举报

 楼主| 发表于 4-11-2009 04:33 PM | 显示全部楼层

回复 19# yeenfei 的帖子

你所说的话满深刻一下。我不敢确定我的未来人生是怎样,但是我要确定是我一定要搞定我的这个重要读书任务。否则一定会后悔。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 28-5-2024 12:12 PM , Processed in 0.070034 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表