C++程序设计谭浩强 第四章(函数与预处理)习题答案(部分有改进)

4.1 最大公约数最小公倍数

#include <iostream>
using namespace std;
int main()
  {int hcf(int,int);
   int lcd(int,int,int);
   int u,v,h,l;
   cin>>u>>v;
   h=hcf(u,v);
   cout<<"H.C.F="<<h<<endl;
   l=lcd(u,v,h);
   cout<<"L.C.D="<<l<<endl;
   return 0;
  }


int hcf(int u,int v)
 {
  int t,r;
  if (v>u)
    {t=u;u=v;v=t;}
     while ((r=u%v)!=0)
        {
         u=v;
         v=r;   }
         return(v);
  }


int lcd(int u,int v,int h)
 {
    return(u*v/h);
 }

4.2 求ax^2+bx+c=0的值

#include <iostream>
#include <math.h>
using namespace std;
float x1,x2,disc,p,q;

int main()
{void greater_than_zero(float,float);
 void equal_to_zero(float,float); 
 void smaller_than_zero(float,float); 
 float a,b,c;
 cout<<"input a,b,c:";
 cin>>a>>b>>c;
 disc=b*b-4*a*c;
 cout<<"root:"<<endl;
 if (disc>0)
  {
   greater_than_zero(a,b);
   cout<<"x1="<<x1<<",x2="<<x2<<endl;
  }
 else if (disc==0)
  {equal_to_zero(a,b);
   cout<<"x1="<<x1<<",x2="<<x2<<endl;
  }
 else
  {smaller_than_zero(a,b);
   cout<<"x1="<<p<<"+"<<q<<"i"<<endl;
   cout<<"x2="<<p<<"-"<<q<<"i"<<endl;
  }
 return 0;
}

void greater_than_zero(float a,float b)   /* 定义一个函数,用来求disc>0时方程的根 */
 {x1=(-b+sqrt(disc))/(2*a);
  x2=(-b-sqrt(disc))/(2*a);
 }

void equal_to_zero(float a,float b)     /* 定义一个函数,用来求disc=0时方程的根 */
 {
  x1=x2=(-b)/(2*a);
 }

void smaller_than_zero(float a,float b)  /* 定义一个函数,用来求disc<0时方程的根 */
 {
  p=-b/(2*a);
  q=sqrt(-disc)/(2*a);
 }

4.3 判别素数的函数

#include <iostream>
using namespace std;
int main()
 {int prime(int);                /* 函数原型声明 */
  int n;
  cout<<"input an integer:";
  cin>>n;
  if (prime(n))
    cout<<n<<" is a prime."<<endl;
  else
    cout<<n<<" is not a prime."<<endl;
  return 0;
 }

 int prime(int n)
  {int flag=1,i;
   for (i=2;i<n/2 && flag==1;i++)
     if (n%i==0)
       flag=0;
   return(flag);
  }

4.4求a!+b!+c!的值

#include <iostream>
using namespace std;
int main()
 {int fac(int);               
  int a,b,c,sum=0;
  cout<<"enter a,b,c:";
  cin>>a>>b>>c;
  sum=sum+fac(a)+fac(b)+fac(c);
  cout<<a<<"!+"<<b<<"!+"<<c<<"!="<<sum<<endl;  
  return 0;
 }


 int fac(int n)
  {int f=1;
   for (int i=1;i<=n;i++)
     f=f*i;
   return f;
  }
 

4.5 求sinh(x)的值,其中用一个函数求e^x

#include <iostream>
#include <cmath>
using namespace std;
int main()
 {double e(double);               
  double x,sinh;
  cout<<"enter x:";
  cin>>x;
  sinh=(e(x)+e(-x))/2;
  cout<<"sinh("<<x<<")="<<sinh<<endl;  
  return 0;
 }

 double e(double x)
  {return exp(x);}
 


4.6牛顿迭代法求根。ax^3+bx^2+cx+d=0。abcd值为1234,求在1附近的实根

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
 double solut(double ,double ,double ,double );
 double a,b,c,d;
 cout<<"input a,b,c,d:";
 cin>>a>>b>>c>>d;
 cout<<"x="<<solut(a,b,c,d)<<endl;
 return 0;
}
 
double solut(double a,double b,double c,double d)
 {double x=1,x0,f,f1;
  do
   {x0=x;
    f=((a*x0+b)*x0+c)*x0+d;
    f1=(3*a*x0+2*b)*x0+c;
    x=x0-f/f1;
   }
  while(fabs(x-x0)>=1e-5);
  return(x);
}

4.7写一个函数验证哥德巴赫猜想:一个不小于6的偶数可以表示为两个素数之和

#include <iostream>
#include <cmath>
using namespace std;
int main()
{void godbaha(int);
 int n;
 cout<<"input n:";
 cin>>n;
 godbaha(n);
 return 0;
}
void godbaha(int n)    //和
{int prime(int);
 int a,b;
 for(a=3;a<=n/2;a=a+2)
   {if(prime(a)) 
      {b=n-a;
       if (prime(b))
		   cout<<n<<"="<<a<<"+"<<b<<endl;}
    }
}

int prime(int m)     //素数
{int i,k=sqrt(m);
 for(i=2;i<=k;i++)
	if(m%i==0) break;
 if (i>k) return 1;
 else     return 0;
}

4.8给出年月日计算该日是该年的第几天

#include <iostream>
using namespace std;
int main()
{
    int sum_day(int month, int day);
    int is_leap(int year);
    int year, month, day, days;
    cout << "Enter date(for example:2020 6 14):"<<endl;
    cin >> year >> month >> day;

    days = sum_day(month, day);
    if (is_leap(year) && month > 2)   //闰年且月份大于2 ,则+1
        days += 1;
    cout << year << "/" << month << "/" << day << "is the " << days << " day in this year "<<endl;
    return 0;
}


int sum_day(int month, int day)
{
    int day_tab[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31 };
    int i;
    for (i = 1; i < month; i++)
        day += day_tab[i];
    return day;
}


int is_leap(int year)  //平年
{
    return ((year%4==0 && year%100!=0) || year%400==0);
}

4.9用递归方法求n阶勒让德多项式的值

#include <iostream>
using namespace std;
int main()
 {int x,n;
  float p(int,int);
  cout<<"input n & x:";
  cin>>n>>x;
  cout<<"n="<<n<<",x="<<x<<endl;;
  cout<<"P"<<n<<"(x)="<<p(n,x)<<endl;
  return 0;
 }

float p(int n,int x)
 {if (n==0)
    return(1);
  else if (n==1)
    return(x);
  else
    return(((2*n-1)*x*p((n-1),x)-(n-1)*p((n-2),x))/n);
 }
  

4.10汉诺塔问题,A有64盘子,移到C

#include <iostream>
using namespace std;
int main()
{void hanoi(int n,char one,char two,char three);
 int m;
 cout<<"input the number of diskes:";
 cin>>m;
 cout<<"The steps of moving "<<m<<" disks:"<<endl;
 hanoi(m,'A','B','C');
 return 0;
}

void hanoi(int n,char one,char two,char three)
	                    //将n个盘从one座借助two座,移到three座
 {void move(char x,char y);
  if(n==1) move(one,three);  //只有一个盘子,从A到C
  else
  {hanoi(n-1,one,three,two);
   move(one,three);
   hanoi(n-1,two,one,three);
  }
}
 
void move(char x,char y)
 {cout<<x<<"-->"<<y<<endl;}

4.11递归法将一个整数n转换成字符串

#include <iostream>
using namespace std;
int main()
{void convert(int n);
 int number;
 cout<<"input an integer:";
 cin>>number;
 cout<<"output:"<<endl;
 if (number<0)
  {cout<<"-";
   number=-number;
   }
  convert(number);
  cout<<endl;
  return 0;
} 


void convert(int n)
 {int i;
  char c;
  if ((i=n/10)!=0)
    convert(i);
  c=n%10+'0';
  cout<<" "<<c;
  }

4.12f(x)=\sum_{i=1}^{n}i^2

#include <iostream>
using namespace std;
int main()
{int f(int);
 int n,s;
 cout<<"input the number n:";
 cin>>n;
 s=f(n);
 cout<<"The result is "<<s<<endl;
 return 0;
} 

int f(int n)
 {;
  if (n==1)
     return 1;
  else
     return (n*n+f(n-1));
}

4.13 求三角形面积,定义两个带参数的宏

#include <iostream>
#include <cmath>
using namespace std;
#define S(a,b,c)  (a+b+c)/2
#define AREA(a,b,c) sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c))
int main()
 {float a,b,c;
  cout<<"input a,b,c:";
  cin>>a>>b>>c;
  if (a+b>c && a+c>b && b+c>a)
    cout<<"area="<<AREA(a,b,c)<<endl;
  else
    cout<<"It is not a triangle!"<<endl;
  return 0;
 } 

4.15条件编译。电报文字两种功能

#include <iostream>
using namespace std;
#define CHANGE 1
int main()
{
    char ch[40];
    cout << "input text:" << endl;
    cin >> ch;
#if (CHANGE)
    {for (int i = 0; i < 40; i++)
    {
        if (ch[i] != '\0')
            if (ch[i] >= 'a' && ch[i] < 'z' || ch[i]>'A' && ch[i] < 'Z')
                ch[i] += 1;
            else if (ch[i] == 'z' || ch[i] == 'Z')
                ch[i] -= 25;
    }
    }
#endif
    cout << "output:" << endl << ch << endl;
    return 0;
}

4.16条件编译,字母大小写互换

#include <iostream>
using namespace std;
//#define LETTER 1                 
int main()
{char c;
 cin>>c;
 #if LETTER                     
     if(c>='a' && c<='z')
       c=c-32;
 #else                          
     if(c>='A' && c<='Z')        
       c=c+32;
 #endif
 cout<<c<<endl;
 return 0;
}



我下载的习题程序里 有几个错的地方都改正了。

不知道是不是真的错,我的vs2019说它错,我改成能用的了。

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

国服最强貂蝉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值