第一题:
例1
/*************************************
** 功能:布尔类型使用举例 **
*************************************/
# include<iostream>
# include<iomanip>
using namespace std;
int main()
{
bool flag = true;
cout <<flag<<endl;
cout <<boolalpha<<flag<<endl;
cout <<flag + 5<<endl;
flag = 0;
cout<<"执行语句 flag=0;后 flag 的值为:"<<boolalpha<<flag<<endl;
flag = 0.0;
cout<<"执行语句 flag=0.0;后 flag 的值为:"<<boolalpha<<flag<<endl;
return 0;
}
例2
/*************************************
** 功能:赋值表达式语句的使用 **
*************************************/
# include<iostream>
using namespace std;
int main()
{
int a, b, c, d;
a = 4;
b = a;
a = 5;
c = d =6;
c *= a;
d %= a + b;
cout<<"a = "<<a<<endl
<<"b = "<<b<<endl
<<"c = "<<c<<endl
<<"d = "<<d<<endl;
return 0;
}
例3
# include<iostream>
using namespace std;
int main()
{
short i, j, m, n;
i = 1000;
j = 1000;
m = i + j;
n = i * j;
cout<<"m = "<<m<<endl;
cout<<"n = "<<n<<endl;
return 0;
}
例4
# include<iostream>
using namespace std;
int main()
{
int i = 6, j, k, temp;
j = ++i;
k = i++;
++i = 1;
cout<<"i = "<<i<<endl
<<"j = "<<j<<endl
<<"k = "<<k<<endl;
return 0;
}
例5
# include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"please input a character:";
cin>>ch;
ch = ch>= 'a'&& ch<= 'z'? ch - 'a' + 'A' : ch;
cout<<"The result is: "<<ch<<endl;
return 0;
}
例7
# include<iostream>
using namespace std;
int main()
{
int ab, ac;
double b = 3.14;
char c = 'A';
ab = int(b);
ac = int(c);
cout<<"b = "<<b<<endl;
cout<<"ab = "<<ab<<endl;
cout<<"c = "<<c<<endl;
cout<<"ac = "<<ac<<endl;
return 0;
}
第二题
三角形周长和面积
# include<iostream>
# include<math.h>
using namespace std;
int main()
{
int a, b, c;
cout<<"请输入三个数:";
cin>>a>>b>>c;
int p, d, s;
p = a + b + c;
d = p/2;
s = sqrt(d*(d-a)*(d-b)*(d-c));
cout<<"p = "<<p<<endl;
cout<<"s = "<<s<<endl;
return 0;
}
第三题
(1).
# include<iostream>
# include<math.h>
using namespace std;
int main()
{
int e=1, f=4, g=2;
float m=10.5, n=4.0, k;
k=(e+f)/g+sqrt((double)n)*1.2/g+m;
cout<<"k= "<<k<<endl;
return 0;
}
k的运算顺序是先算括号中的e+f后再除以g,接着先运算(double)n之后再sqrt然后再乘以1.2接着除以g,再进行三个数的加法
(2).
# include<iostream>
using namespace std;
int main()
{
float x=2.5, y=4.7, m;
int a=7;
m= x+a%3*(int(x+y)%2)/4;
cout<<"m= "<<m<<endl;
return 0;
}
第四题
// 计算一元二次方程
#include <iostream>
#include <math.h> //函数调用
using namespace std;
int main()
{
float a ,b ,c;
double anto ;
cout << "请输入二次项系数 a :";
cin >> a ;
cout << "请输入一次项系数 b :";
cin >> b ;
cout << "请输入常数项 c :";
cin >> c ;
if(a==0&&b==0)
{
cout << "该方程不是一元二次方程"<< endl;
}
anto = (b*b)-(4*a*c) ;
if (anto >= 0){
if (anto > 0){
cout << "方程的实数根 X1= " << (-b+sqrt(anto))/(2*a) << endl;
cout << " 方程的实数根X2= " << (-b-sqrt(anto))/(2*a) << endl;
}
else cout << "方程的实数根 X1 = X2 = " << (-b)/(2*a) << endl;
}
else {
anto=abs(anto);
cout << "方程的虚根X=" << -b/(2*a) <<"±"<<sqrt(anto)/(2*a) <<"j"<< endl;
}
return 0;
}
博主太懒,很多知识点都还没学会,导致作业迟迟未能上交,如今还交了个半成品,望老师见谅,日后必补上。
第七题:
错误:warning C4305: 'initializing' : truncation from 'const double' to 'float'(第三题的第二个小题出现的错误,很是疑惑)