| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 430 人关注过本帖
标题:不是完全理解,求
只看楼主 加入收藏
一颗烂苹果
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-5-15
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:4 
不是完全理解,求
要求是这样的:
create a text-based, menu-driven program that allows the user to choose whether to calculate the circumference of a circle, the area of a circle or the volume of a sphere. The program should then input a radius from the user, perform the appropriate calculation and display the result. Use an array of function pointers in which each pointer represents a function that returns void and receives a double parameter. The corresponding functions should each display messages indicating which calculation was performed, the value of the radius and the result of the calculation.

以下是我写的程序:
#include<stdio.h>
#define P 3.14

void main()
{
    int choice, r;
    float C,A,V;
    printf("Press 1, calculate the circumference of a circle.\n");
    printf("Press 2, calculate the area of a circle.\n");
    printf("Press 3, calculate the volume of a sphere.\n");
    printf("Enter your choice: ");
    scanf ("%d", &choice);
    switch(choice)
    {
    case 1:
        printf("Please enter the radius: ");
        scanf ("%d",&r);
        C = 2*P*r;
        printf("\n The circumference of circle is %.2f",C);
        break;
    case 2:
        printf("Please enter the radius: ");
        scanf ("%d",&r);
        A = P*r*r;
        printf("The area of a circle is %.2f",A);
        break;
    case 3:
        printf("Please enter the radius: ");
        scanf ("%d",&r);
        V = 4/3*P*r*r*r;
        printf("The volume of a sphere is %.2f",V);
        break;
    }
}
我知道不能完全符合要求,但是又没什么思路,哪位大侠给指条明路,多谢啊!!
搜索更多相关主题的帖子: represents represents function function receives display returns 
2011-06-22 18:00
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:7 
有啥问题

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-06-22 18:13
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:7 
题目要求用函数指针,楼主的代码不符合题意
2011-06-22 18:17
hjywyj
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:1114
专家分:2611
注 册:2010-4-14
收藏
得分:7 
没学好英文的路过
2011-06-22 19:29
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:0 
刚发现之前贴的代码居然是错的

程序代码:
#include <stdio.h>
#include <math.h>

void show_circumference(double r) {
    printf("Circumference of a circle of radius %lf is %lf\n", r, 2 * M_PI * r);
}

void show_area(double r) {
    printf("Area of a circle of radius %lf is %lf\n", r, M_PI * pow(r, 2));
}

void show_volume(double r) {
    printf("Volume of a sphere of radius %lf is %lf\n", r, 4 * M_PI * pow(r, 3) / 3);
}

int main() {
    int choice = 0;
    double r = 0;
    void (* functions[4])(double) = {NULL, show_circumference, show_area, show_volume};
    printf("Press 1, calculate the circumference of a circle.\n");
    printf("Press 2, calculate the area of a circle.\n");
    printf("Press 3, calculate the volume of a sphere.\n");
    printf("Enter your choice: ");
    while (scanf("%d", &choice) == 0 || choice < 1 || choice > 3) {
        while (getchar() != '\n');
        printf("No kidding! Your choice can be only 1, 2 or 3: ");
    }
    while (getchar() != '\n');
    printf("Enter the radius: ");
    while (!scanf("%lf", &r)) {
        while (getchar() != '\n');
        printf("Please! A decimal number as radius: ");
    }
    functions[choice](r);
    return 0;
}


[ 本帖最后由 voidx 于 2011-6-22 21:57 编辑 ]
2011-06-22 19:56
快速回复:不是完全理解,求
数据加载中...
 
   



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

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