01-编写一个求方程ax2 + bx + c = 0的根 的程序,用3个函数分别求当b2-4ac大于零、等于零、和小于零时的方程的根。要求从主函数输入a,b,c的值并输出结果。
```cpp
#include <iostream>
**using namespace std;**
#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);
}
注明:在没有加using namespace std;时候,运行该程序会报出:cont未识别字符,
endl不是end1
#include <iostream>
using namespace std; //加上这句
cout输出流类声明和定义都在iostream之中(对于非标准C++而言是iostream.h)其名字位于std空间,对于某些编译器,特别是针对Windows平台的开发工具,都要显式声明命名空间。
cout是输出函数 std是命名空间, std::out表示,out函数属于std这个命名空间,std就相当于C当中的stdio.h但他们有本质的区别,只能说是相当于。std命名空间下的cout方法,要是你写using namespace std;就不用写std::直接cout、
**02-定义函数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;
在C语言中,每一个常用字符都有一个对应的ASCII值,大写字母A~Z对应的ASCII为65~90,
小写字母a~z对应的ASCII值为97~122。大写字母与小写字母的ASCII值相差32,
故可以通过这一点实现大写字母与小写字母的转换。