| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 515 人关注过本帖
标题:根据内容编的程序,请大家来找茬!!
只看楼主 加入收藏
csbeng001
Rank: 1
来 自:湖南长沙市
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-13
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
根据内容编的程序,请大家来找茬!!
一个产品有两个版本,一个高级,5.5元,标准3.5元。提示用户输入产品版本和数量 ,然后根据输入的信息,算出价格。

#include <stdio.h>
int main(void)
{
    enum mobel{good,bad};
    float good =5.5;
    float bad =3.5;
    float pirce = 0.0f;
    int num =0;
    printf("Please enter the mobel\"good or bad\" and the number of book!");
    scanf("%c",&mobel);
    scanf("%d",&num);
    pirce = mobel * num;
    printf("You choose mobel is %c,the number is %d,and the price is %.2f\n",mobel,num,pirce);
    return 0;
}

这个是我根据内容来编的程序,请大家来找茬!!
搜索更多相关主题的帖子: 大家来找茬 
2011-09-28 14:36
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:20 
这是用较基本函数写的版本:
程序代码:
#include <locale.h>
#include <stdio.h>
#include <conio.h>

typedef struct
{
    wchar_t name[20];        // 书名
    int version;            // 版本
    double price;            // 单价
} book_t;

book_t books[2] = {
    {L"《C高级编程》", 1, 3.50},
    {L"《C高级编程》", 2, 5.50}
};
size_t book_number = 2;

void pause(void);

void main(void)
{
    size_t index;
    int choice;
    int quantity;
    double total = 0.0;

    setlocale(LC_ALL, "chs");        // 设定语言为中文输出

    for(index = 0; index != book_number; ++index)
    {
        wprintf_s(L"%d. %s\t版本:%d\t单价:%.2f\n", index + 1, books[index].name, books[index].version, books[index].price);
    }
    wprintf_s(L"0. 结束\n");
    do
    {
        wprintf_s(L"选择(0-%d):", book_number);
        do
        {
            choice = _getch();
        } while ((choice < '0') || (choice > '0' + book_number));
        _putch(choice);
        if (choice != '0')
        {
            wprintf_s(L"\n数量:");
            scanf_s("%d", &quantity);
            if (quantity > 0)
            {
                total += quantity * books[choice - '0' - 1].price;
            }
        }
    } while(choice != '0');
    wprintf_s(L"\n合计总价:%.2f\n", total);

    pause();
}

void pause(void)
{
    wprintf_s(L"\n====按Enter键结束程序====");
    while (_getwch() != 0x000D)
    {
    }
}


[ 本帖最后由 TonyDeng 于 2011-9-28 15:03 编辑 ]

授人以渔,不授人以鱼。
2011-09-28 14:56
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
2楼的程序是有个缺陷的,你自己找吧。

授人以渔,不授人以鱼。
2011-09-28 15:12
csbeng001
Rank: 1
来 自:湖南长沙市
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-13
收藏
得分:0 
回复 3楼 TonyDeng
谢谢,不过你的太高深了~~
看不懂~~
我只看了C语言入门经典 第四版,霍顿的。前两章内容,所以你所写的内容大部份我都没有看过~~~,更不能理解你的意思了~~~~
不过还是辛苦你了
2011-09-28 16:29
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
没学到循环、数组?那么你只能写出if语句的分支,而且每次只能买一种书。用goto语句可以实现循环,但不提倡。书上这个题目后面要逐渐扩充的吧?

授人以渔,不授人以鱼。
2011-09-28 16:47
csbeng001
Rank: 1
来 自:湖南长沙市
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-13
收藏
得分:0 
回复 5楼 TonyDeng
怎么说了,是我把问题想的太复杂了,下面这段是答案,
#include <stdio.h>
int main(void)
{
    double total_price =0;
    int type =0;
    int number =0;
    const double type1_price =3.5;
    const double type2_price =5.5;

    printf("Please choose the type (1 or 2): \n");
    scanf("%d",&type);
    printf("Enter the number: ");
    scanf("%d",&number);
    total_price = number * (type1_price + (type -1)*(type2_price-type1_price));
    printf("You choose %d of type %d ,and shold pay $%.2f .\n",number ,type ,total_price);
    return 0;
}
2011-09-29 11:33
快速回复:根据内容编的程序,请大家来找茬!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012951 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved