计算器程序(参考过资料)

Posted on 2006-08-11 11:40  abcdeft  阅读(338)  评论(0)    收藏  举报

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

void SubMain_f();     
void SolveForX_f();   
void MmToM_f();        
void InchesToFeet_f(); 
void CelsiusToFahrenheit_f();
void Quadformula_f();  
void SqRoot_f();       
void CalcAdd_f();      
void CalcSub_f();      
void CalcDiv_f();      
void CalcMult_f();     
void Menu_f();         

int nNumber1,nNumber2,nResult;

int main(void)
{
   int nMenuChoice;
   Menu_f();
   scanf("%d",&nMenuChoice);    
   switch(nMenuChoice)
   {
      case 1:
        SolveForX_f();        
     break;
      case 2:
        SubMain_f();  
     break;
      case 3:
        MmToM_f(); 
     break;
      case 4:
        InchesToFeet_f();
     break;
   case 5:
        CelsiusToFahrenheit_f();
           break;
      case 6:
        SqRoot_f();            
     break;
      case 7:
        Quadformula_f();
     break;
      case 8:
        return 0;
      default:
        printf("Please choose from one of the choices above!\n");
     break;
   }
   return 0;
}

void Menu_f()  
{
   printf("\n\t\t Math Homework Helper Vesion 1.0");
   printf("\n1.Solve For The XValue");
   printf("\n2.Simple Calculation");
   printf("\n3.Covert Millimeter To Meter");
   printf("\n4.Covert Feet To Inches");
   printf("\n5.Covert Celsius To fahrenhiet");
   printf("\n6.Find the square root of anumber");
   printf("\n7.QUADRATIC FORMULA");
   printf("\n8.Exit");
   printf("\nChoice:");
   getch();
   getch();
}

void SubMain_f()
{
   char chChoice;
   system("cls");
   printf("=====================================================\n");
   printf("1.Add some numbers\n");
   printf("2.Subtract two numbers\n");
   printf("3.Divide two numbers\n");
   printf("4.Multiply two numbers\n");
   printf("5.Back MainMenu\n");
   printf("=====================================================\n");
   printf("Choice:");
   scanf("%c",&chChoice);
   switch(chChoice)
   {
      case '1':
        CalcAdd_f();
     break;
      case '2':
        CalcSub_f();
     break;
      case '3':
        CalcDiv_f();
     break;
      case '4':
        CalcMult_f();
     break;
      case '5':
        exit(0);
      default:
        printf("Please choose from one of the choices above\n");
     break;
   }
   getch();
   getch();
}

void CalcAdd_f()
{

   float a[10],sum=0;
   int N,flag=1,i;
   while(flag)
    {
      printf("Please enter the number of numbers you want to add (<=10):");
      scanf("%d",&N);
      if(0<N&&N<=10)
    flag=0;
    }
   printf("The Add Function:\n");
   for(i=0;i<N;i++)
    scanf("%f",&a[i]);
   for(i=0;i<N;i++)
    sum+=a[i];
   printf("The sum is %f\n",sum);
   printf("Press any key to continue\n");
   getch();
   getch();
   SubMain_f();
}

void CalcSub_f()
{
  printf("The Subtract Function:\n");
  printf("First number: ");
  scanf("%d",&nNumber1);
  printf("Second number:");
  scanf("%d",&nNumber2);
  nResult=nNumber1-nNumber2;
  printf("%d-%d=%d\n",nNumber1,nNumber2,nResult);
  printf("Press any key to continue\n");
  getch();
  getch();
  SubMain_f();
}

void CalcDiv_f()
{
  printf("The Divide Function\n");
  printf("First number:");
  scanf("%d",&nNumber1);
  printf("Second number:");
  scanf("%d",&nNumber2);
  nResult=nNumber1/nNumber2;
  printf("%d/%d=%d\n",nNumber1,nNumber2,nResult);
  printf("Press any key to continue\n");
  getch();
  getch();
  SubMain_f();
}

void CalcMult_f()
{
  printf("The Multiply Function\n");
  printf("First number:");
  scanf("%d",&nNumber1);
  printf("Second number:");
  scanf("%d",&nNumber2);
  nResult=nNumber1*nNumber2;
  printf("%d*%d=%d\n",nNumber1,nNumber2,nResult);
  printf("Press any key to continue\n");
  getch();
  getch();
  SubMain_f();
}

void SqRoot_f()
{
  int flag=1;
  printf("The Square Root Function\n");
  while(flag)
   {
    printf("Please input a number");
    scanf("%d",&nNumber1);
 if(nNumber1>=0)
 flag=0;
   }
  nResult=sqrt(nNumber1);
  printf("The square root of %d is %d\n",nNumber1,nResult);
  printf("Press any key to continue\n");
  getch();
  getch();
  Menu_f();
}

void SolveForX_f()
{
  int nCoef,nWholeNumber,nEqual,nX;
  printf("\nThis will run you through solving for x in the since that the");
  printf("\nProgram reds like y=2x+3");
  printf("\nOnly put in one number at a time then push enter");
  printf("\nMore instructions will come after you put that number in and push enter\n\n");
  printf("y=");
  scanf("%d",&nCoef);
  printf("%dx+",nCoef);
  scanf("%d",&nWholeNumber);
  printf("%dx+%d=",nCoef,nWholeNumber);
  scanf("%d",&nEqual);
  for(;;)
   {
     nX=rand()%100;
  if(((nCoef*nX)+nWholeNumber)==nEqual)
   {
     printf("\nWe found out that x is equal to %d\n\n",nX);
  system("PAUSE");
  system("PAUSE");
  getch();
  exit(0);
   }
   }
}

void MmToM_f()
{
  double dlMeter;
  double dlMilimeter;
  printf("Enter the value in Milimeters:");
  scanf("%lf",&dlMilimeter);
  if(dlMilimeter<1000)
   {
     printf("Way to low buddy.Number MUST be over 1000!!!\n");
  MmToM_f();
   }
   dlMeter=(dlMilimeter/1000);
   printf("\n^The answer to your question is:%lf meters\n\n",dlMeter);
   getch();
}

void InchesToFeet_f()
{
  float flFeet;
  float flInch;
  printf("\nEnter the number of feet to get the length in inches:");
  scanf("%f",&flInch);
  flFeet=12*flInch;
  printf("\n\nOh that is %f  feet\n\n",flFeet);
  system("PAUSE");
  getch();
  exit(0);
}

void CelsiusToFahrenheit_f()
{
  int nFahrenheit;
  int nFactor;
  int nCelsius;
  printf("Enter the temperature in Celsius:");
  scanf("%d",&nCelsius);
  nFactor=212-32;
  nFahrenheit=nFactor*nCelsius/100+32;
  printf("Fahrenheit value is:%d\n",nFahrenheit);
  getch();
}

void Quadformula_f()
{
  int nCoefa,nCoefb,nCoefc;
  float flHalfDone;
  float flTopA;
  float flTopB;
  float flWholeA;
  float flWholeB;
  printf("\nWhat is the value of a:");
  scanf("%d",&nCoefa);
  printf("\nWhat is the value of b:");
  scanf("%d",&nCoefb);
  printf("\nWhat is the value of c:");
  scanf("%d",&nCoefc);
  if(nCoefb<0)
   nCoefb+=nCoefb*nCoefb;
   else nCoefb-=nCoefb*nCoefb;
   flHalfDone=sqrt((nCoefb*nCoefb)-(4*nCoefa*nCoefb));
   flTopA=nCoefb+flHalfDone;
   flTopB=nCoefb-flHalfDone;
   flWholeA=flTopA/(2*nCoefa);
   flWholeB=flTopB/(2*nCoefa);
   printf("X's Are %f,%f\n",flWholeA,flWholeB);
   getch();
   exit(0);
}


    就像当初在学校里做程序设计一样,由于编译软件或是电脑的问题导致中间出了好多麻烦.还好我坚持下来了.这应该也算是一点进步吧.
    感觉自己真的爱上了这份职业,现在应该还只是自己的专业.但是我想有了这份喜好,有了这份执着,终有一天我能享受它给我带来的快乐!