鱼C论坛

 找回密码
 立即注册
查看: 1772|回复: 9

求人帮我改下,函数的调用那有点问题

[复制链接]
发表于 2014-4-4 22:34:55 | 显示全部楼层 |阅读模式
5鱼币
#include <stdio.h>
void main()
{
void add(int,int,int,int);
void sub(int,int,int,int);
void cheng(int,int,int,int);
int a,b,c,d,e;
printf("输入运算类型:1复数加,2复数减,3复数乘");
scanf("%d,%d,%d,%d,%d",&a,&b,&c,&d,&e);
switch(e)
{
case 1:printf("%d",add(a,b,c,d));break;
case 2:printf("%d",sub(a,b,c,d));break;
case 3:prinft("%d",cheng(a,b,c,d));break;
}
}


void add(int a,int b,int c,int d)
{
prinft("(%d+%d)+(%d+%d):",a,b,c,d);
printf("(%d+%d)+(%d+%d)i",a,c,b,d);
}
void sub(int a,int b,int c,int d)
{
printf("(%d+%d)-(%d+%d)i:",a,b,c,d);
printf("(%d-%d)+(%d-%d)",a,c,b,d);
}
void cheng(int a,int b,int c,int d)
{
printf("(%d+%di)(%d+%di):",a,b,c,d);
printf("(%d-%d)(%d+%d)i",a*c,b*d,b*c,a*d);
}

最佳答案

查看完整内容

以下代码编译没问题,你看一下: #include void add(int,int,int,int); void sub(int,int,int,int); void cheng(int,int,int,int); void main() { int a,b,c,d,e; printf("输入运算类型:1复数加,2复数减,3复数乘:\n"); scanf("%d",&e); switch(e) { case 1:add(a, b, c, d);break; case 2:sub(a ,b ,c , d);break; case 3:cheng(a, b, c, d);break; } } void add(int a,int b,int c,int d) { p ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-4-4 22:34:56 | 显示全部楼层
以下代码编译没问题,你看一下:

#include <stdio.h>

void add(int,int,int,int);
void sub(int,int,int,int);
void cheng(int,int,int,int);

void main()
{

int a,b,c,d,e;
printf("输入运算类型:1复数加,2复数减,3复数乘:\n");
scanf("%d",&e);
switch(e)
{
case 1:add(a, b, c, d);break;
case 2:sub(a ,b ,c , d);break;
case 3:cheng(a, b, c, d);break;
}
}


void add(int a,int b,int c,int d)
{
printf("(%d+%d)+(%d+%d):\n",a,b,c,d);
printf("(%d+%d)+(%d+%d)i\n",a,c,b,d);
}
void sub(int a,int b,int c,int d)
{
printf("(%d+%d)-(%d+%d)i:\n",a,b,c,d);
printf("(%d-%d)+(%d-%d)\n",a,c,b,d);
}
void cheng(int a,int b,int c,int d)
{
printf("(%d+%di)(%d+%di):\n",a,b,c,d);
printf("(%d-%d)(%d+%d)i\n",a*c,b*d,b*c,a*d);
}


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-4-4 22:51:15 | 显示全部楼层
函数类型名为void,当然不会有返回值,可将类型换为int。同时在函数定义里return(a+b+c+d)。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-4-4 22:58:08 | 显示全部楼层
这是运行后的结果:

QQ截图20140404225414.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-4-5 04:51:00 | 显示全部楼层
我是新手,基础较弱,有两点没看懂。
1、%di是什么意思?
2、在这个程序中,a,b,c,d的值是怎么定义的?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-4-5 11:00:24 | 显示全部楼层

1:%d它是整型变量的格式!意思是让程序输出后面的整型变量的值,比如:printf("%d", a);意思是输出整型变量a的值,如果你定义的是一个浮点型的话:float a; 那它的格式是%f,输出的时候是:printf("%f", f);它输出的就是浮点类型的值;
2:在这个程序中应该给a, b, c, d;初始化,一般是赋0,也就是:int a=0, b=0, c=0, d=0;然后往下输出的时候就不会出现像上图所输出的乱码了!
^_^   明白了吗!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-4-5 14:27:51 | 显示全部楼层
不还意思实在看不懂你这个程序(ˇ&#717;ˇ) 想~干嘛但我估计就是计算器是吧
我即兴写了一个 你参考下吧
#include<stdio.h>
void Input(double * n1,double * n2);
double add(double n1,double n2);
double sub(double n1,double n2);
double mul(double n1,double n2);
double div(double n1,double n2);

int main()
{
        char ch;
        double n1,n2;
s:        setbuf(stdin,NULL);
        printf("-----------\n");
        printf("-   1:+   -\n");
        printf("-   2:-   -\n");
        printf("-   3:*   -\n");
        printf("-   4:/   -\n");
        printf("-   5:quit-\n");
        printf("-----------\n");
        ch = getchar();
        switch(ch)
        {
        case '1':
        case '+':
                Input(&n1,&n2);
                printf("%lf + %lf = %lf\n",n1,n2,add(n1,n2));
                goto s;
        case '2':
        case '-':
                Input(&n1,&n2);
                printf("%lf - %lf = %lf\n",n1,n2,sub(n1,n2));
                goto s;
        case '3':
        case '*':
                Input(&n1,&n2);
                printf("%lf * %lf = %lf\n",n1,n2,mul(n1,n2));
                goto s;
        case '4':
        case '/':
                Input(&n1,&n2);
                printf("%lf / %lf = %lf\n",n1,n2,div(n1,n2));
                goto s;
        case '5':
                return 0;
default:
                printf("Error!\n");
                goto s;
        }

}

void Input(double * n1,double * n2)
{
        printf("Please Input n1:");
        scanf("%lf",n1);
        printf("Please Input n2:");
        scanf("%lf",n2);
}

double add(double n1,double n2)
{
        return n1 + n2;
}

double sub(double n1,double n2)
{
        return n1 - n2;
}

double mul(double n1,double n2)
{
        return n1 * n2;
}

double div(double n1,double n2)
{
        return n1 / n2;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-4-5 16:37:26 | 显示全部楼层
牡丹花下死做鬼 发表于 2014-4-5 14:27
不还意思实在看不懂你这个程序(ˇ&#717;ˇ) 想~干嘛但我估计就是计算器是吧
我即兴写了一个 你参考下吧

..............感谢,我找到问题了:shutup:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-4-5 16:47:10 | 显示全部楼层
只是想学C 发表于 2014-4-5 04:51
我是新手,基础较弱,有两点没看懂。
1、%di是什么意思?
2、在这个程序中,a,b,c,d的值是怎么定义的?

那个不是%di,是%d+个i.........printf("%d年%d月%d日"a,b,c,d)更这个意思差不多.............
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

我爱鱼C论坛 该用户已被删除
发表于 2015-2-19 21:19:17 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-2-24 05:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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