当前位置:编程学习 > C/C++ >>

挑错:C语言函数计算器

挑错  函数代码如下

 

 

/*****************************************************************************
*高级计算器                                                                  *
*带参数有返回值                                                              *
*功能:加、减、乘、除、求余、累加、阶乘、组合。                              *
*                                                               *
*日期:2010年11月中旬                                                        *
******************************************************************************/


/*****************************************************************************/
//                               预  处  理                     
/*****************************************************************************/

#include<stdio.h>
#include<math.h>

/*****************************************************************************/
//                             函  数  声  明
/*****************************************************************************/

void displayMenu();
double add(double num1,double num2);
double sub(double num1,double num2);
double multiply(double num1,double num2);
double div(double num1,double num2);
int remain(int num1,int num2);
double addNToM(int n,int m);
double fator(int n);
int CNM(int n,int m);

/*****************************************************************************/
//                                主  函  数
/*****************************************************************************/
void main()
{
 double para1,para2;                                                   //加、减、乘、除参数值准备
 int N,M;                                                              //求余、累加、组合参数值准备
 int J;                                                                //阶乘参数值准备
    double result;                                                        //保存返回值准备
 int item;                                                             //保存选择菜单项准备

 while(1)
 {
  displayMenu();                                                    //主菜单调用
  printf("请选择一项功能:");
  scanf("%d",&item);
  switch(item)
  {
  case 1:                                                           //加法运算
   printf("请输入两个加数:");
   scanf("%lf%lf",¶1,¶2);
   result=add(para1,para2);
   printf("%lf + %lf = %lf\n",para1,para2,result);
   break;

  case 2:                                                           //减法运算
   printf("请输入两个减数:");
   scanf("%lf%lf",¶1,¶2);
   result=sub(para1,para2);
   printf("%lf - %lf = %lf\n",para1,para2,result);
   break;

  case 3:                                                           //乘法运算
   printf("请输入两个乘数:");
   scanf("%lf%lf",¶1,¶2);
   result=multiply(para1,para2);
   printf("%lf * %lf = %lf\n",para1,para2,result);
   break;

  case 4:                                                           //除法运算
   printf("请输入被除数和除数:");
   scanf("%lf%lf",¶1,¶2);
   result=div(para1,para2);
   printf("%lf + %lf = %lf\n",para1,para2,result);
   break;

  case 5:                                                           //累加运算
   printf("请输入整数n和m (n<m):");
   scanf("%d%d",&N,&M);
   result=addNToM(int n,int m);
   printf("%d累加到5%d的值为:%d\n",N,M,result);
   break;

  case 6:                                                           //求余运算
   printf("请输入求余的两个数:");
   scanf("%d%d",&N,&M);
   result=remain(int n,int m);
   break;

  case 7:                                                           //阶乘运算
   printf("请输入n:");
   scanf("%d",&J);
   result=fator(int n);
   printf("%d ! = %d\n",J,result);
   break;
   
  case 8:                                                           //组合运算
   printf("请输入总体数n和选出个数m:");
   scanf("%d%d",&N,&M);
   result=CNM(int n,int m);
   printf("从%d个数中选出%d个数的组合次数为%d\n",n,m,result);
   break;
   
  case 0:                                                           //退出运算
   exit(0);
   
defanlt:
   printf("输入有误,请重新输入!!!\n");
  }
  while(getchar()!='\n');                                           //清除错误输入内容
 }
}

 

 


/*****************************************************************************/
//                                主  菜  单
/*****************************************************************************/

void displayMenu()
{
 printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
 printf("+                      高  级  计  算  器                    +\n");
 printf("+  =    +    =    +    =    +    +    =    +    =    +    =  +\n");
 printf("+                       ++displayMenu++                      +\n");
 printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
 printf("+                         1.加    法                         +\n");
 printf("+                         2.减    法                         +\n");
 printf("+                         3.乘    法                         +\n");
 printf("+                         4.除    法                         +\n");
 printf("+                         5.取    余                         +\n");
 printf("+                         6.累    加                         +\n");
 printf("+                         7.阶    乘                         +\n");
    printf("+                         8.组    合                         +\n");
 printf("+                         0.退    出                         +\n");
 printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}

/*****************************************************************************/
//                             各功能函数定义区
/*****************************************************************************/

//加法函数定义

double add(double num1,double num2)
{
  double result;
  result=num1+num2;
  return result;
}

//减法函数定义

double sub(double num1,double num2)
{
  double result;
  result=num1-num2;
  return result;
}

//乘法函数定义

double multiply(double num1,double num2)
{
  double result;
  result=num1*num2;
  return result;
}

//除法函数定义

double div(double num1,double num2)
{
 double result;
 if(fabs(num2-0)<le-6)
 {
  printf("错误!!!除数不能为0!!!");
 }
 else
 {
  result=num1/num2;
  return result;
 }
}

//取余函数定义

int remain(int n,int m)
{
 int result;
 if(fabs(num2-0)<le-6)
 {
  printf("错误!!!除数不能为0!!!");
 }
 else
 {
  result=num1%num2;
  return result;
 }
}

//累加函数定义

double addNToM(int n,int m)
{
 double result=0.0;
 int tmp;
 int index;
 if(n>m)
 {
  tmp=n;n=m;m=tmp;
 }
 for(index=n;index<=m;index++)
 {
  result+=index;
 }
 retrun result;
}

//阶乘函数定义

double fator(int n)
{
 double result=1.0;
 int index;
 for(index=n;index<=m;index++)
 {
  result*=index;
 }
 retrun result;
}

//组合函数定义

int CNM(int n,int m)
{
 int result;
 result=n!/(m!*(n-m)!);
 retrun result;
}

/*****************************************************************************/
//                                E   N   D
/*****************************************************************************/

追问:主要是case5、6、7、8里的错误   小错误我都改完了

 

答案:重新完善了一下,现在所有的功能都可以实现。。。#include<stdio.h>
#include<math.h>

void displayMenu();
double add(double num1,double num2);
double sub(double num1,double num2);
double multiply(double num1,double num2);
double div(double num1,double num2);
int remain(int num1,int num2);
double addNToM(int n,int m);
double fator(int n);
int CNM(int n,int m);

void main()
{
 double para1,para2;                                                   //加、减、乘、除参数值准备
 int N,M;                                                              //求余、累加、组合参数值准备
 int J;                                                                //阶乘参数值准备
    double result;                                                        //保存返回值准备
 int item;                                                             //保存选择菜单项准备

 while(1)
 {
  displayMenu();                                                    //主菜单调用
  printf("请选择一项功能:");
  scanf("%d",&item);
  switch(item)
  {
  case 1:                                                           //加法运算
   printf("请输入两个加数:");
   scanf("%lf%lf",&para1,&para2);
   result=add(para1,para2);
   printf("%lf + %lf = %lf\n",para1,para2,result);
   break;

  case 2:                                                           //减法运算
   printf("请输入两个减数:");
   scanf("%lf%lf",&para1,&para2);
   result=sub(para1,para2);
   printf("%lf - %lf = %lf\n",para1,para2,result);
   break;

  case 3:                                                           //乘法运算
   printf("请输入两个乘数:");
   scanf("%lf%lf",&para1,&para2);
   result=multiply(para1,para2);
   printf("%lf * %lf = %lf\n",para1,para2,result);
   break;

  case 4:                                                           //除法运算
   printf("请输入被除数和除数:");
   scanf("%lf%lf",&para1,&para2);
   result=div(para1,para2);
   printf("%lf + %lf = %lf\n",para1,para2,result);
   break;

  case 5:                                                           //求余运算
   printf("请输入求余的两个数:");
   scanf("%d%d",&N,&M);
   result=remain(N,M);
   printf("余数为:%f\n",result);
   break;

  case 6:                                                           //累加运算
   printf("请输入整数n和m (n<m):");
   scanf("%d%d",&N,&M);
   result=addNToM(N,M);
   printf("%d累加到%d的值为:%lf\n",N,M,result);
   break;

  case 7:                                                           //阶乘运算
   printf("请输入n:");
   scanf("%d",&J);
   result=fator(J);
   printf("%d ! = %lf\n",J,result);
   break;
  
  case 8:                                                           //组合运算
   printf("请输入总体数n和选出个数m:");
   scanf("%d%d",&N,&M);
   result=CNM(N,M);
   printf("从%d个数中选出%d个数的组合次数为%lf\n",N,M,result);
   break;
  
  case 0:                                                  

上一个:c语言快速入门?高手进!
下一个:c语言程序,急啊

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,