第二章作业

由于我帐号被封,所以这次作业做的有点晚。。。望老师见谅。。。

1.由于截图的结果都在学校电脑里望了拿u盘,所以结果就没有了。。。

1.

// homework1.cpp : Defines the entry point for the console application.
//

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

int main()
{
    cout<<"number of bytes in int is"<<sizeof(int)<<endl;
    cout<<"number of bytes in long int is"<<sizeof(long)<<endl;
    cout<<"number of bytes in short int is"<<sizeof(short)<<endl;

    return 0;
}

2.

#include "stdafx.h"
#include<iostream>
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;后flag的值为:"<<boolalpha<<flag<<endl;
return 0;
}

3.

#include "stdafx.h"
#include<iostream>     
using namespace std;    
    
int main()    
{    
    int a,b,c,d,e;    
    a=4;    
    b=a;    
    a=5;    
    c=d=6;    
    c*=a;    
    d%=a+b;  
    n=a*b;  
    cout<<"a="<<a<<endl    
        <<"b="<<b<<endl    
        <<"c="<<c<<endl    
        <<"d="<<d<<endl  
        <<"n="<<e<<endl;    
   
    return 0;    
}  

4.

#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;
}

5.

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

6.

#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;    
}  

7.

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这题我一开始没注意math函数,所以一直识别不了sqrt。。。

#include "stdafx.h"
#include<iostream>     
using namespace std;    
    
int main()    
{    
    float a,b,c;
	cout<<"请输入三角形的边长a:";
	cin>>a;
	cout<<"请输入三角形的边长b:";
	cin>>b;
   	cout<<"请输入三角形的边长c:";
	cin>>c;
	while(a+b<=c||a+c<=b||b+c<=a)
	{
	cout<<"不存在这样的三角形,请重新输入:"<<endl;
	cout<<"请输入三角形的边长a:";
	cin>>a;
	cout<<"请输入三角形的边长b:";
	cin>>b;
   	cout<<"请输入三角形的边长c:";
	cin>>c;	
	}
    float d,e,f;
	d=a+b+c;
	e=d/2;
	f=sqrt(e*(e-a)*(e-b)*(e-c));
    cout<<"三角形的面积为:"<<f<<endl;                    
    cout<<"三角形的周长为:"<<d<<endl;

    return 0;    
}  


3.这题主要是熟悉一下强制类型的转换。。。
3.1

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


3.2


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

int main()
{
   float x=2.5,y=4.7;
   int a=7;
   cout<<"k="<<(x+a%3*(int(x+y)%2)/4);
   
   return 0;
}


4这一题主要的难点是if函数的运用。。。还有=与==的区别。。。

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

int main()
{
   float a,b,c;
   double x1,x2,k;
   cout<<"请输入二次项系数a:"<<endl;
   cin>>a;
   cout<<"请输入一次项系数b:"<<endl;
   cin>>b;
   cout<<"请输入常数项c:"<<endl;
   cin>>c;

   k=b*b-4*a*c;
   x1=(-b+sqrt(k))/(2*a);
   x2=(-b-sqrt(k))/(2*a);

   if(k>=0)
   {if(x1==x2)
      cout<<"方程只有一个解,x="<<x1<<endl;
   else
	  cout<<"方程有两个解,x1="<<x1<<endl;
	  cout<<"x2="<<x2<<endl;
   }
   else
	   cout<<"方程无解"<<endl;
	      
   return 0;
}




5.这题开始就不会了,只能参考和借鉴光荣榜的同学的程序和上网查了。。。我还有很多不理解。。。我佩服做出来的同学

******加密与解密程序******/  
  
#include <iostream>  
using namespace std;  
  
int main()  
{  
char a[25],b[25],c[25],e[25];             
    int k;      
    cout<<"欢迎进行名字加密与解密程序"<<endl;         
    cout<<"加密请输入“8”,解密请输入任意数字"<<endl;    
    cin>>k;     
        
    if(k==8)                                                                 
    {    
        int i;    
        cout<<"请输入你要加密的名字"<<endl;             
        fflush(stdin);                                  
        cin>>a;                                                                          
        cout<<"该名字的加密成果为:"<<endl;            
        for(i=0;i<25;i++)    
        {    
            if(a[i]==0)    
            {    
                break;    
            }    
            b[i]=a[i];    
            b[i]=b[i]+10;                              
            c[i]=b[i];    
            cout<<c[i];  
    
        }    
        cout<<endl;    
    }    
    else                                                                                          
    {    
            
        cout<<"请输入你要解密的名字"<<endl;            
        fflush(stdin);                                
        cin>>a;    
            
        cout<<"该名字的解密成果为:"<<endl;             
        for(int i=0;i<25;i++)    
        {    
            if(a[i]==0)    
            {    
                break;    
            }    
            b[i]=a[i];    
            b[i]=b[i]-10;                             
            e[i]=b[i];    
            cout<<e[i];    
        }    
        cout<<endl;    
    }    
  
    return 0;  
}  


6这题题目看不懂,只好参考光荣榜的同学了。。。

#include<iostream>  
using namespace std;  
void xianshi(int x,int y);  
int main()  
{  
    char k,h,g;  
    int al,ah;       
      
   cout<<"按T键启动设备"<<endl;  
   cin>>k;  
   if(k=='t'||k=='T')  
   {  
      ah=0xff;  
      al=0xff;  
      cout<<"设备已启动!!!!"<<endl;  
      xianshi(ah,al);  
      cout<<"按F键可打开报警装置,按其他键则不打开报警装置"<<endl;  
      cin>>h;  
      if(h=='F'||h=='f')  
      {  
          ah=ah&0xfe;  
          cout<<"报警装置已开启!!如遇紧急情况会自动报警"<<endl;  
          xianshi(ah,al);  
      }  
      cout<<"机器正在运行中........"<<endl;  
      cout<<"机器正在运行中........"<<endl;        
      cout<<"机器正在运行中........"<<endl;        
      cout<<"机器正在运行中........"<<endl;        
      cout<<"机器正在运行中........"<<endl;        
      cout<<"机器正在运行中........"<<endl;        
      cout<<"机器正在运行中........"<<endl;        
      cout<<"紧急情况!!!请马上按S键强制停止机器"<<endl;  
      cin>>k;  
      if(k=='S'||k=='s')  
      {  
          al=al&0x00;  
          xianshi(ah,al);  
          cout<<"机器已经停止完毕!!!";  
      }  
  
   }  
    return 0;  
}  
  
void xianshi(int x,int y)  
{  
            int b,i;  
  
        cout<<"输出高八位ah:";  
        for(i=0;i<8;i++)  
        {  
            b=x&0x01;  
            x=x>>1;  
            cout<<b<<' ';  
              
        }  
        cout<<endl;  
        cout<<"输出低八位al:";  
        for(i=0;i<8;i++)  
        {  
            b=y&0x01;  
            y=y>>1;  
            cout<<b<<' ';  
              
        }  
        cout<<endl;  
}  


7错误搜集
1经常忘记家";"号。。。
2经常把cin>>a写成cin<<a。。。
3我的错误通常都是
--------------------Configuration: 4 (2) - Win32 Debug--------------------
Linking...
LINK : fatal error LNK1168: cannot open Debug/4 (2).exe for writing
执行 link.exe 时出错.

这是为什么???
错误就大概是这些。。。希望我以后能改正。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值