| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 696 人关注过本帖
标题:如何编写这个函数?
只看楼主 加入收藏
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
结帖率:71.88%
收藏
已结贴  问题点数:18 回复次数:10 
如何编写这个函数?


Write a program that does the following:

Displays your name on the screen

Calls a function

- to ask for 2 numbers and

- a character input to say whether they want to add or multiply the 2 numbers

- a Switch to determine which function  to execute

                        - do the add

                        Or

                        - do the multiply

Back in main a printf that will show the values of both numbers, the action taken (+ or *) and the result.
不求程序,只求思路。
搜索更多相关主题的帖子: ask character determine following 
2013-01-31 12:40
zxr_fx
Rank: 1
等 级:新手上路
帖 子:28
专家分:7
注 册:2012-11-21
收藏
得分:3 
这应该很简单吧,我的思路是这样的:1.printf()输出你的名字

2.定义三个变量,两个是你要输入的数字,一个是字符来判断是加是乘,,用scanf()输入

3.用switch判断字符变量,进行两种运算

菜鸟成长中
2013-01-31 12:48
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
老师要求要做函数的调用(function),下面是我编写的程序,但是怎么也compile不了,请大侠给我看看,谢谢。
#include<stdio.h>
int add(int, int);
int multiply(int, int);
main()
{
char choice;

printf("A for add\n");
printf("M for multiply\n");
printf("\nPlease input your choice:");
scanf("%c", &choice);

switch (choice)
{
case 'A':
     int a, b, total1;
     printf("Enter the first integer: ");
     scanf("%d", &a);
     printf("Enter the second integer: ");
     scanf("%d", &b);
     total1=add(a, b);
     printf("\nThe total1 is%d", total1);
     break;
case 'M':
     int c, d, total2;
     printf("Enter the first integer: ");
     scanf("%d", &c);
     printf("Enter the second integer: ");
     scanf("%d", &d);
     total2=multiply(c, d);
     printf("\nThe total is%d", total2);
     break;
default: printf("Invalid choice, please try again.\n\n");

}
}
int add(int x, int y)
     {
     int z;
     z=x+y;
     return z;
     }
int multiply(int g, int h)
     {
     int i;
     i=g*h;
     return i;
     }
2013-02-02 11:02
mfkblue
Rank: 5Rank: 5
等 级:职业侠客
帖 子:472
专家分:343
注 册:2008-12-21
收藏
得分:3 
vs2008环境下正常
#include<stdio.h>
#include "stdafx.h"
int add(int, int);
int multiply(int, int);
void main()
{
    char choice;

    printf("A for add\n");
    printf("M for multiply\n");
    printf("\nPlease input your choice:");
    scanf("%c", &choice);

    switch (choice)
    {
    case 'A':
        int a, b, total1;
        printf("Enter the first integer: ");
        scanf("%d", &a);
        printf("Enter the second integer: ");
        scanf("%d", &b);
        total1=add(a, b);
        printf("\nThe total1 is %d", total1);
        getchar();
        break;
    case 'M':
        int c, d, total2;
        printf("Enter the first integer: ");
        scanf("%d", &c);
        printf("Enter the second integer: ");
        scanf("%d", &d);
        total2=multiply(c, d);
        printf("\nThe total is %d", total2);
        getchar();
        break;
    default: printf("Invalid choice, please try again.\n\n");

    }
    getchar();
}
int add(int x, int y)
{
    int z;
    z=x+y;
    return z;
}
int multiply(int g, int h)
{
    int i;
    i=g*h;
    return i;
}
2013-02-02 11:16
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
谢谢楼上的相助。让我烦恼的是,如何让case A break出来就运行
int add(int x, int y)
     {
     int z;
     z=x+y;
     return z;
     }
而case M出来就运行
     int multiply(int g, int h)
     {
     int i;
     i=g*h;
     return i;
     }
呢?
2013-02-02 12:01
你不想去那儿
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:56
专家分:114
注 册:2012-9-21
收藏
得分:3 
你的意思是数据在一开始就输入,然后根据A or M来调用函数吗?
2013-02-03 01:57
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:3 
(choce),改为(‘choce’)是不

同学习......同进步....你帮我......我帮你.....上善若水.....
2013-02-04 00:09
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:0 
还有break作用域到哪,case后面的语句太多,直接那些都扔涵数里得了,朱程序,case掉用下就好,看起了简洁。

同学习......同进步....你帮我......我帮你.....上善若水.....
2013-02-04 00:15
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
以下是引用你不想去那儿在2013-2-3 01:57:49的发言:

你的意思是数据在一开始就输入,然后根据A or M来调用函数吗?

是的。
2013-02-04 04:03
张伟波
Rank: 2
等 级:论坛游民
帖 子:4
专家分:10
注 册:2013-2-1
收藏
得分:3 
没问题呀,VC6.0运行正常
]
2013-02-04 09:36
快速回复:如何编写这个函数?
数据加载中...
 
   



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

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