C++编程题

1.编写一个求方程ax2 + bx + c = 0的根 的程序,用3个函数分别求当b2-4ac大于零、等于零、和小于零时的方程的根。要求从主函数输入a,b,c的值并输出结果。
#include < iostream.h >
#include < math.h >
void equation_1 (int a, int b, int c)
{
          double x1, x2, temp;
          temp = b*b - 4 * a * c;
          x1 = (-b + sqrt(temp) ) / (2 * a * 1.0);
          x2 = (-b - sqrt(temp) ) / (2 * a * 1.0);
          cout<<"两个不相等的实根"<< endl;
          cout<<"x1 = "<< x1<<",  x2 = "<< x2<< endl;
}
void equation_2 (int a, int b, int c)
{
          double x1, x2, temp;
          temp = b*b - 4 * a * c;
          x1 = (-b + sqrt(temp) ) / (2 * a * 1.0);
          x2 = x1;
          cout<<"两个相等的实根"<< endl;
          cout<<"x1 = "<< x1<<",  x2 = "<< x2<< endl;
 
}
void equation_3 (int a, int b, int c)
{
          double temp, real1, real2, image1, image2;
          temp = - (b*b - 4 * a * c);
          real1 = -b / (2 * a *1.0);
          real2 = real1;
          image1 = sqrt(temp);
          image2 = - image1;
          cout<<"两个虚根"<< endl;
          cout<<"x1 = "<< real1<<" + "<< image1<<"j"<< endl;
          cout<<"x2 = "<< real2<<" + "<< image2<<"j"<< endl;
          
}
void main()
{
          int a, b, c;
          double temp;
          cout<<"输入a,b,c的值"<< endl;
          cin>>a>>b>>c;
          cout<<"方程为:"<< a<<"x*x+"<< b<<"x+"<< c<<" = 0"<< endl;
          temp = b*b - 4 * a * c;
          if(temp > 0)
                    equation_1 (a, b, c);
          if(temp == 0)
                    equation_2 (a, b, c);
          if(temp < 0)
                    equation_3 (a, b, c);
 
}
2.定义函数up(ch),如字符变量ch是小写字母就转换成大写字母并通过up返回,否则字符ch不改变。要求在短小而完全的程序中显示这个程序是怎样被调用的。
#include < iostream >
using namespace std;
char up (char c)
{
          
          if(c >= 97 && c <= 122)
                    return (c - 32) ;
          else
                    return c;
}
void main()
{
          int i;
          char c[15] = {'A','v','e','t','E','T','%','&','4','Y','e','i','@','9','^'};
          for(i = 0 ; i < 15 ; i++)
                    cout<< up(c[i])<<", ";
          cout<< endl;
}
3.编写主程序条用带实数r和整数n两个参数的函数并输出r的n次幂。
#include < iostream.h >
#include < math.h >
double power(double a, int b)
{
          int i;
          double result = 1.0;
          for(i=0;i< b;i++)
                    result = result * a;
          return result;
}
void main()
{
          double r;
          int n;
          cout<<"r = ";
          cin>>r;
          cout<<"n = ";
          cin>>n;
          cout<< r<<"的"<< n<<"次幂是:"<< power(r,n)<< endl;}
4.编写有字符型参数C和整形参数N的函数,让他们显示出由字符C组成的三角形。其方式为第1行有1个字符C,第2行有2个字符C ,等等。
#include < iostream >
using namespace std;
void print_triangle(char c, int n)
{
          int i, j;
          for(i=0; i< n; i++)
          {
                    for(j=0; j<=i; j++)
                    {
                               cout<< c;
                    }
                    cout<< endl;
          }
}
void main()
{
          print_triangle('a',10);
}
5.编写一个ieqiu字符串长度的函数,strlen(),再用strlen()函数编写一个函数revers(s)的倒序递归程序,使字符串s逆序。
#include < iostream >
#include < string >
using namespace std;
int strlen(char *str)
{
          int len = 0;
          while(str[len] != '\0')
          {
                    len++;
          }
          return len;
}
void revers(char *b)
{
char c;   
    int j, len;   
    len=strlen(b);   
    j=len/2-1;   
    while(j>=0)   
    {   
          c=*(b+j);   
        *(b+j)=*(b+len-j-1);   
        *(b+len-j-1)=c;   
         j--;   
          }
          b[len]='\0';
}
void main()
{
          char str[]={"1234567890"};
          cout<< str<<"----的长度:"<< strlen(str)<< endl;
          cout<< str<< endl;//倒序前
          revers(str);//
          cout<< str<< endl;//倒序后
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值