您的位置:首页 > 编程语言

第三周作业~

2014-03-28 09:13 302 查看
第 2 章  基本数据类型、运算符与表达式
 n      学习要求
了解数据类型概念。
掌握常量和变量的使用。
掌握各种运算符的运算结合性和优先级,掌握各类表达式正确格式。
掌握数据类型转换。
进一步熟悉VC集成编程环境的使用,熟练掌握程序设计过程和步骤,积累程序调试经验。
n      实验作业
1.输入课本各个例题,调试运行程序,并分析程序,将每一个程序改写2到3个版本,自己分析程序结果,然后再调试运行,核对分析结果的对错。
2.编写程序输入一个三角形的三条边,计算其面积和周长;
3.编写程序计算并输出课本本章习题3表达式的值并分析结果。
4.编写一个程序,输入一个一元二次方程的三个系数,并计算其方程的解,然后输出。
5.编写程序,自己确定一个加密算法,将自己的音标姓名(英文)加密,并输出加密后结果,请注释你的加密算法。
6.在一个自动控制设备中,控制字位数16位,控制设备产生机械动作(如削,压等)的是指令字的低8位,其中保护强制停机动作的控制命令是低8位是全为0,控制报警声音是指令的高第1位,0为报警,1为不报警。请编写程序,在紧急状况启动时,向控制器输入控制指令。
7.积累调试程序经验,收集错误信息原因(每个同学收集3-5条错误信息原因,并输入电脑形成文字)。
n      作业要求
网上提交各个程序和相关要求提交的文字。
例2.1布尔类型使用举例
// 布尔类型.cpp : Defines the entry point for the console application.
/************************************************
***      功能:布尔类型使用举例      ***
************************************************/

#include "stdafx.h"
#include<iostream>         //编译预处理命令

#include<iomanip>          //使用控制符boolalpha需使用此头文件
using namespace std;       //使用标准名空间std

int main()                 //主函数
{
bool flag = true;      //定义布尔型变量flag,并初始化为true
cout<<flag<<endl;      //默认情况下为bool字母(noboolalpha),输出整型值1
cout<<boolalpha<<flag<<endl;  //使用输出格式控制符boolalpha,输出布尔型值
cout<<flag + 5<<endl;  //在算术运算中,把布尔数据当作整型数据,输出6
flag = 0;
cout<<"执行语句flag=0;后flag的值为:"<<boolalpha<<flag<<endl;
flag = 0.0;            //0.0为double类型的数值
cout<<"执行语句flag=0.0;后flag的值为:"<<boolalpha<<flag<<endl;

return 0;
}


 
结果:



 
例2.2赋值表达式语句举例

 

// 赋值表达式语句.cpp : Defines the entry point for the console application.
/******************************************************
**      功能:赋值表达式语句的使用    **
******************************************************/
#include "stdafx.h"
#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;
}


结果:


  

例2.3数据溢出举例

// 数据溢出.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#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;
}


结果:



 

例2.4 运算符使用举例

// 运算符使用.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
int i= 6, j, k, temp;
j= ++i;           //先对变量i自增,i的值变为7,之后把i的值7赋给变量j
k= i++;           //先把变量i的值7赋给变量k,然后i的值自增,i的值变为8
++i= 1;        //++i可以作为左值,执行完该语句后变量i的值为1
cout<<"i= "<<i<<endl
<<"j= "<<j<<endl
<<"k= "<<k<<endl;

return 0;
}


 

结果:



 

例2.5

// 字符转换.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#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;
//上述语句等价于ch=ch>='a'&&ch<='z'?ch-32:ch;
cout<<"The result is:"<<ch<<endl;

return 0;
}


 

结果:



 

例2.6

// 自动类型转换.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
char ch = 'c';            //定义字符型变量ch,并进行初始化
int a, b = 13;            //定义基本整型变量a和b,并对变量b进行初始化
float x, y;              //定义float型变量x和y
x=y= 2.0                  /* 变量x和y均赋值为float型的2.0,此处把double类型的2.0自动转化成float型的2.0,编译时会有警告信息*/
a= ch + 5;                /* a赋值为104,ch先转化为int型(即取字符c的ASCII值),再参与运算*/
x= b/2/x;               //x赋值为3.0,先做整除运算,然后再转换成double与x运算
y= b/y/2;               /* y赋值为3.25,b和y先转换成double型再做除法,同时2也转化为成double型,然后做除法运算*/

 

例2.7

// 强制类型转换.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#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;
}


 

 

2、计算三角形面积和周长

// 求三
4000
角形面积和周长.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float x,y,z,H,S,C;                    //定义△三个边长变量x,y,z,中间变量H,面积S,周长C
cout<<"请输入△的三个边长度"<<endl;
cin>>x;
cin>>y;
cin>>z;                               //用户输入三条边长度
if(x+y>z&&x+z>y&&y+z>x)               //用条件语句判断是否能组成△
{
H=(x+y+z)/2;
S=sqrt(H*(H-x)*(H-y)*(H-z));     //△面积
C=x+y+z;                          //△周长
cout<<"△面积S="<<S<<endl;
cout<<"△周长C="<<C<<endl;
}
else
{
cout<<"您输入的数据不能组成相应的△"<<endl;

}
return 0;
}


结果:



 

3、编写程序计算并输出课本本章习题3表达式的值并分析结果。
// 3333.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<cmath>
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;
}

/*(e+f)/g的值为2,因为e,f,g均为整型,其运算结果是整型;sqrt((double)n)*1.2/g的值为1.2,其结果被强制为double型,所以最终结果是13.7*/


结果:

 


4、编写一个程序,输入一个一元二次方程的三个系数,并计算其方程的解,然后输出。
#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
int a,b,c,x1,x2,t;
cout<<"请分别输入方程二次项、一次项、常数项系数"<<endl;
cin>>a>>b>>c;
t=b*b-4*a*c;
if(t>=0)
{
x1=(-b+sqrt(t))/(2*a);
x2=(-b-sqrt(t))/(2*a);
cout<<"x1="<<x1<<endl<<"x2="<<x2<<endl;
}
else
{
cout<<"方程没有实数解";
}
return 0;
}


 

5、编写程序,自己确定一个加密算法,将自己的音标姓名(英文)加密,并输出加密后结果,请注释你的加密算法。

#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;

int main()
{  char ch1,ch2,ch3;
cout<<"请输入名字音标:";
cin>>ch1,ch2,ch3;
ch1=ch1>='a'&&ch1<='z'?ch1-'a'+'9':ch1;
ch2=ch2>='a'&&ch2<='z'?ch2-'a'+'4':ch2;
ch3=ch3>='a'&&ch3<='z'?ch3-'a'+'8':ch3;
cout<<"加密后:"<<ch1<<ch2<<ch3<<endl;
return 0;
}


老师,第六的还不是很懂。。。

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  编程