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


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
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
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
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
以下是引用你不想去那儿在2013-2-3 01:57:49的发言:

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

是的。
2013-02-04 04:03
快速回复:如何编写这个函数?
数据加载中...
 
   



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

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