第三周作业

一:书中例题

1.

 
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout<<"numerb of bytes in int is "<<sizeof(int)<<endl;
  6. cout<<"numerb of bytes in long int is "<<sizeof(long)<<endl;
  7. cout<<"numerb of bytes in shott int is "<<sizeof(short)<<endl;
  8. cout<<"numerb of bytes in long long int is "<<sizeof(long long)<< endl;
  9. cout<<"numerb of bytes in float int is " << sizeof(float) << endl;
  10. return 0;
  11. }
#include <iostream>
using namespace std;

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

    return 0;
}

2.

 
  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. int main()
  5. {
  6. bool flag=true;
  7. cout<<flag<<endl;
  8. cout<<boolalpha<<flag<<endl;
  9. cout<<flag+5<<endl;
  10. flag=0;
  11. cout<<"执行语句flag=0;后flag的值为:"<<boolalpha<<flag<<endl;
  12. flag=0.0;
  13. cout<<"执行语句a=0.0;后flag的值为:"<<boolalpha<<flag<<endl;
  14. cout <<noboolalpha<<flag <<endl;
  15. return 0;
  16. }
#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<<"执行语句a=0.0;后flag的值为:"<<boolalpha<<flag<<endl;
    cout <<noboolalpha<<flag <<endl;
    return 0;
}

3.

 
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a,b,c,d;
  6. a=4;
  7. b=a;
  8. a=5;
  9. c=d=6;
  10. c*=a;
  11. d%=a+b;
  12. cout<<"a = "<<a<<endl;
  13. cout<<"b = "<<b<<endl;
  14. cout<<"c = "<<c<<endl;
  15. cout<<"d = "<<d<<endl;
  16. return 0;
  17. }
#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;
    cout<<"b = "<<b<<endl;
    cout<<"c = "<<c<<endl;
    cout<<"d = "<<d<<endl;
    return 0;
}

4.

 
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. short i,j,m,n;
  6. long a,b;
  7. i=1000;
  8. j=1000;
  9. m=i+j;
  10. n=i*j;
  11. cout<<"i="<<i<<endl;
  12. cout<<"j="<<j<<endl;
  13. cout<<"m="<<m<<endl;
  14. cout<<"n="<<n<<endl;
  15. a=i*j;
  16. cout<<"a="<<a<<endl;
  17. return 0;
  18. }
#include<iostream>
using namespace std;
int main()
{
    short i,j,m,n;
    long a,b;
    i=1000;
    j=1000;
    m=i+j;
    n=i*j;
    cout<<"i="<<i<<endl;
    cout<<"j="<<j<<endl;
    cout<<"m="<<m<<endl;
    cout<<"n="<<n<<endl;
    a=i*j;
    cout<<"a="<<a<<endl;
    return 0;
}

5.

 
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int i = 6,j,k,l,m,temp;
  6. j=++i;
  7. k=i++;
  8. ++i=1;
  9. l=i++;
  10. --m=1;
  11. cout << " i = " <<i<< endl
  12. << " j = " <<j<< endl
  13. << " k = " <<k<< endl
  14. << " l = " <<l<< endl
  15. << " m = " <<m<< endl;
  16. return 0;
  17. }
#include<iostream>
using namespace std;
int main()
{
    int i = 6,j,k,l,m,temp;
    j=++i;
    k=i++;
    ++i=1;
    l=i++;
    --m=1;
    cout << " i = " <<i<< endl
         << " j = " <<j<< endl
         << " k = " <<k<< endl
         << " l = " <<l<< endl
         << " m = " <<m<< endl;
    return 0;
}

6.

 

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char ch;
  6. cout << "Please input a character:";
  7. cin >> ch;
  8. ch= ch >= 'a'&&ch<='z'?ch-'a'+'A':ch;
  9. cout << "The result is :"<< ch << endl;
  10. ch= ch >='A' &&ch<='Z'?ch+'a'-'A':ch;
  11. cout << "The result is :"<< ch << endl;
  12. return 0;
  13. }
#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;
    ch= ch >='A' &&ch<='Z'?ch+'a'-'A':ch;
    cout << "The result is :"<< ch << endl;
    return 0;
}


7

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int ab,ac;
  6. double b = 3.14;
  7. char c = 'A',ad;
  8. ab = int(b);
  9. ac = int(c);
  10. ad = char(int(b));
  11. cout << "b = "<<b<<endl;
  12. cout << "ab = "<<ab<<endl;
  13. cout << "c = "<< c << endl;
  14. cout << "ac = " << ac <<endl;
  15. cout << "ad = " << ad << endl;
  16. return 0;
  17. }
#include<iostream>
using namespace std;
int main()
{
    int ab,ac;
    double b = 3.14;
    char c = 'A',ad;
    ab = int(b);
    ac = int(c);
    ad = char(int(b));
    cout << "b = "<<b<<endl;
    cout << "ab = "<<ab<<endl;
    cout << "c = "<< c << endl;
    cout << "ac = " << ac <<endl;
    cout << "ad = " << ad << endl;
    return 0;
}





二:计算三角形的面积以及周长

[cpp] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
一:书中例题
 
1.
 



[cpp] view plaincopy
01.#include <iostream>  
02.using namespace std;  
03.  
04.int main()  
05.{  
06.    cout<<"numerb of bytes in int is "<<sizeof(int)<<endl;  
07.    cout<<"numerb of bytes in long int is "<<sizeof(long)<<endl;  
08.    cout<<"numerb of bytes in shott int is "<<sizeof(short)<<endl;  
09.    cout<<"numerb of bytes in long long int is "<<sizeof(long long)<< endl;  
10.    cout<<"numerb of bytes in float int is " << sizeof(float) << endl;  
11.  
12.    return 0;  
13.}  

 2. 




[cpp] view plaincopy
01.#include<iostream>  
02.#include<iomanip>  
03.using namespace std;  
04.int main()  
05.{  
06.    bool flag=true;  
07.    cout<<flag<<endl;  
08.    cout<<boolalpha<<flag<<endl;  
09.    cout<<flag+5<<endl;  
10.    flag=0;  
11.    cout<<"执行语句flag=0;后flag的值为:"<<boolalpha<<flag<<endl;  
12.    flag=0.0;  
13.    cout<<"执行语句a=0.0;后flag的值为:"<<boolalpha<<flag<<endl;  
14.    cout <<noboolalpha<<flag <<endl;  
15.    return 0;  
16.}  

 3. 




[cpp] view plaincopy
01.#include<iostream>  
02.using namespace std;  
03.int main()  
04.{  
05.    int a,b,c,d;  
06.    a=4;  
07.    b=a;  
08.    a=5;  
09.    c=d=6;  
10.    c*=a;  
11.    d%=a+b;  
12.    cout<<"a = "<<a<<endl;  
13.    cout<<"b = "<<b<<endl;  
14.    cout<<"c = "<<c<<endl;  
15.    cout<<"d = "<<d<<endl;  
16.    return 0;  
17.}  

 4. 




[cpp] view plaincopy
01.#include<iostream>  
02.using namespace std;  
03.int main()  
04.{  
05.    short i,j,m,n;  
06.    long a,b;  
07.    i=1000;  
08.    j=1000;  
09.    m=i+j;  
10.    n=i*j;  
11.    cout<<"i="<<i<<endl;  
12.    cout<<"j="<<j<<endl;  
13.    cout<<"m="<<m<<endl;  
14.    cout<<"n="<<n<<endl;  
15.    a=i*j;  
16.    cout<<"a="<<a<<endl;  
17.    return 0;  
18.}  

 5. 




[cpp] view plaincopy
01.#include<iostream>  
02.using namespace std;  
03.int main()  
04.{  
05.    int i = 6,j,k,l,m,temp;  
06.    j=++i;  
07.    k=i++;  
08.    ++i=1;  
09.    l=i++;  
10.    --m=1;  
11.    cout << " i = " <<i<< endl  
12.         << " j = " <<j<< endl  
13.         << " k = " <<k<< endl  
14.         << " l = " <<l<< endl  
15.         << " m = " <<m<< endl;  
16.    return 0;  
17.}  

 6. 




[cpp] view plaincopy
01.#include<iostream>  
02.using namespace std;  
03.int main()  
04.{  
05.    char ch;  
06.    cout << "Please input a character:";  
07.    cin >> ch;  
08.    ch= ch >= 'a'&&ch<='z'?ch-'a'+'A':ch;  
09.    cout << "The result is :"<< ch << endl;  
10.    ch= ch >='A' &&ch<='Z'?ch+'a'-'A':ch;  
11.    cout << "The result is :"<< ch << endl;  
12.    return 0;  
13.}  

 


7.
 



[cpp] view plaincopy
01.#include<iostream>  
02.using namespace std;  
03.int main()  
04.{  
05.    int ab,ac;  
06.    double b = 3.14;  
07.    char c = 'A',ad;  
08.    ab = int(b);  
09.    ac = int(c);  
10.    ad = char(int(b));  
11.    cout << "b = "<<b<<endl;  
12.    cout << "ab = "<<ab<<endl;  
13.    cout << "c = "<< c << endl;  
14.    cout << "ac = " << ac <<endl;  
15.    cout << "ad = " << ad << endl;  
16.    return 0;  
17.}  

 











二:计算三角形的面积以及周长
 



[cpp] view plaincopy
01.#include <iostream>  
02.  
03.  
04.using namespace std;  
05.  
06.  
07.int main()  
08.{  
09.    float side_1, side_2,side_3,gap_1,gap_2,gap_3;  
10.    cout << "Please enter the first side length:";  
11.    cin >> side_1;  
12.    cout << endl;  
13.    cout << "Please enter the second side length:";  
14.    cin >> side_2;  
15.    cout << endl;  
16.    cout << "Please enter the third side length:";  
17.    cin >> side_3;  
18.    cout << endl;  
19.    gap_1=side_2+side_3-side_1;  
20.    gap_2=side_1+side_3-side_2;  
21.    gap_3=side_1+side_2-side_3;  
22.    if(gap_1>0&&gap_2>0&&gap_3>0){  
23.    cout << "Perimeter of the triangle is :" << side_1+side_2+side_3 << endl;//计算周长并输出  
24.    cout << "Area of the triangle is :" <<  (side_1+side_2+side_3)/2 << endl;//计算面积并输出  
25.    }  
26.    else cout << "This is not a triangle" << endl;  
27.    return 0;  
28.}  
29.//由于考虑到三角形边长可以为非整数,所以用float型,而且就算边长为整数,面积也可能得出非整数  
30.//为减少麻烦起见一律用float型,结果直接输出,不另用变量储存。  

 三:例题3_1 




[cpp] view plaincopy
01.#include <iostream>  
02.#include <cmath>  
03.using namespace std;  
04.  
05.int main()  
06.{  
07.    int e = 1, f = 4, g = 2;  
08.    float m = 10.5 ,n = 4.0 ,k;  
09.    k = (e+f)/g+sqrt((double)n)*1.2/g+m ;//求平方数学函数需要加上cmath头文件才能使用  
10.    cout << "k = " << k << endl;  
11.    return 0;  
12.}  



[cpp] view plaincopy
01.//e+f等于5,再处于g,由于是整型,得到结果为2  



[cpp] view plaincopy
01.//n开平方再乘以1.2才除以2再加上m再加上前面的结果,得到最后结果为13.7,根据类型转换规则,得到的类型为double型  

 例题3_2: 




[cpp] view plaincopy
01.#include <iostream>  
02.  
03.using namespace std;  
04.  
05.int main()  
06.{  
07.    float x = 2.5, y = 4.7,z;  
08.    int a = 7 ;  
09.    z=x+a%3*(int(x+y)%2)/4;  
10.    cout << "结果为: " << z << endl;  
11.    return 0;  
12.}  



[cpp] view plaincopy
01.//a%3结果为1,int(x+y)%2结果也为1,两者相乘结果为1且为int型,除以4之后得到结果为0,最后加上x,所以最后结果为2.5  
 



四:求一元二次方程的解 






[cpp] view plaincopy
01.#include <iostream>  
02.#include <cmath>  
03.#include <cstdlib>  
04.using namespace std;  
05.  
06.  
07.  
08.  
09.int main()  
10.{  
11.    float a ,b ,c;  
12.    double delta ;  
13.    cout << "Please enter a :";  
14.    cin >> a ;  
15.    cout << endl;  
16.    cout << "Please enter b :";  
17.    cin >> b ;  
18.    cout << endl;  
19.    cout << "Please enter c :";  
20.    cin >> c ;  
21.    cout << endl;  
22.    if(a==0&&b==0)  
23.    {  
24.        cout << "该方程无解。"<< endl;  
25.        exit(0);  
26.    }  
27.  
28.  
29.    delta = (b*b)-(4*a*c) ; //求delta  
30.    if (delta >= 0){  
31.        if (delta > 0){  
32.            cout << "方程的实数根 X1= " << (-b+sqrt(delta))/(2*a) << endl;  
33.            cout << " 方程的实数根X2= " << (-b-sqrt(delta))/(2*a) << endl;  
34.            }  
35.        else cout << "方程的实数根 X1 = X2 = " << (-b)/(2*a) << endl;  
36.    }  
37.    else {  
38.        delta=abs(delta);  
39.        cout << "方程的虚根X1=" << -b/(2*a) <<"+"<<sqrt(delta)/(2*a) <<"j"<< endl;  
40.        cout << "方程的虚根X2=" << -b/(2*a) <<"-"<<sqrt(delta)/(2*a) <<"j"<< endl;  
41.    }  
42.  
43.  
44.  
45.  
46.    return 0;  
47.}  

 



五:加密算法
 






[cpp] view plaincopy
01.#include <iostream>   
02.#include <string>    
03.using namespace std;    
04.    
05.int main()    
06.{    
07.    const int Size=20;    
08.    string name;    
09.    int num,i,name_num[Size];    
10.    cout << "Pleast enter your name :";    
11.    cin >> name;    
12.    cout << endl;    
13.    cout << "Please enter a number you like and don't forget: ";    
14.    cin >> num;    
15.    cout << endl;    
16.    for (i=0;name[i]!='\0';i++)    
17.    {    
18.       name_num[i]=int(name[i]);    
19.       name_num[i]=name_num[i] ^ num;    
20.    }    
21.    cout << "After the name Encryption :";    
22.    for (i=0;name[i]!='\0';i++)    
23.    {    
24.       cout << name_num[i]<<" ";    
25.    }    
26.    cout << endl;   
27.    return 0;    
28.}    
29.//输入音标姓名,先将每个字母转换为ASCII码,再与输入的数做异或运算,最后输出加密后的结果  
 
六:
 






[cpp] view plaincopy
01.#include<iostream>  
02.#include<bitset>  
03.using namespace std;  
04.int main()  
05.{  
06.    bitset<16> ctrl_1;  
07.    bitset<16> ctrl_2 = 1000000011111111;  
08.    while (1)  
09.    {  
10.        cout << "请输入16位的二进制指令:";  
11.        cin >>ctrl_1;  
12.        ctrl_1=ctrl_1&ctrl_2;  
13.        if(ctrl_1==0||ctrl_1==1000000000000000){  
14.             if(ctrl_1==1000000000000000){  
15.                cout << '\a'<<endl;  
16.                break;  
17.             }  
18.             if(ctrl_1==0)break;  
19.        }  
20.    }  
21.  
22.    return 0;  
23.}  

 错误:未出现。 


问题:不明白第六题的要求是什么。只能照自己理解去编程。

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. float side_1, side_2,side_3,gap_1,gap_2,gap_3;
  6. cout << "Please enter the first side length:";
  7. cin >> side_1;
  8. cout << endl;
  9. cout << "Please enter the second side length:";
  10. cin >> side_2;
  11. cout << endl;
  12. cout << "Please enter the third side length:";
  13. cin >> side_3;
  14. cout << endl;
  15. gap_1=side_2+side_3-side_1;
  16. gap_2=side_1+side_3-side_2;
  17. gap_3=side_1+side_2-side_3;
  18. if(gap_1>0&&gap_2>0&&gap_3>0){
  19. cout << "Perimeter of the triangle is :" << side_1+side_2+side_3 << endl;//计算周长并输出
  20. cout << "Area of the triangle is :" << (side_1+side_2+side_3)/2 << endl;//计算面积并输出
  21. }
  22. else cout << "This is not a triangle" << endl;
  23. return 0;
  24. }
  25. //由于考虑到三角形边长可以为非整数,所以用float型,而且就算边长为整数,面积也可能得出非整数
  26. //为减少麻烦起见一律用float型,结果直接输出,不另用变量储存。
#include <iostream>


using namespace std;


int main()
{
    float side_1, side_2,side_3,gap_1,gap_2,gap_3;
    cout << "Please enter the first side length:";
    cin >> side_1;
    cout << endl;
    cout << "Please enter the second side length:";
    cin >> side_2;
    cout << endl;
    cout << "Please enter the third side length:";
    cin >> side_3;
    cout << endl;
    gap_1=side_2+side_3-side_1;
    gap_2=side_1+side_3-side_2;
    gap_3=side_1+side_2-side_3;
    if(gap_1>0&&gap_2>0&&gap_3>0){
    cout << "Perimeter of the triangle is :" << side_1+side_2+side_3 << endl;//计算周长并输出
    cout << "Area of the triangle is :" <<  (side_1+side_2+side_3)/2 << endl;//计算面积并输出
    }
    else cout << "This is not a triangle" << endl;
    return 0;
}
//由于考虑到三角形边长可以为非整数,所以用float型,而且就算边长为整数,面积也可能得出非整数
//为减少麻烦起见一律用float型,结果直接输出,不另用变量储存。

三:例题3_1

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. int main()
  5. {
  6. int e = 1, f = 4, g = 2;
  7. float m = 10.5 ,n = 4.0 ,k;
  8. k = (e+f)/g+sqrt((double)n)*1.2/g+m ;//求平方数学函数需要加上cmath头文件才能使用
  9. cout << "k = " << k << endl;
  10. return 0;
  11. }
#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 ;//求平方数学函数需要加上cmath头文件才能使用
    cout << "k = " << k << endl;
    return 0;
}
  1. //e+f等于5,再处于g,由于是整型,得到结果为2
//e+f等于5,再处于g,由于是整型,得到结果为2
  1. //n开平方再乘以1.2才除以2再加上m再加上前面的结果,得到最后结果为13.7,根据类型转换规则,得到的类型为double型
//n开平方再乘以1.2才除以2再加上m再加上前面的结果,得到最后结果为13.7,根据类型转换规则,得到的类型为double型

例题3_2:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. float x = 2.5, y = 4.7,z;
  6. int a = 7 ;
  7. z=x+a%3*(int(x+y)%2)/4;
  8. cout << "结果为: " << z << endl;
  9. return 0;
  10. }
#include <iostream>

using namespace std;

int main()
{
    float x = 2.5, y = 4.7,z;
    int a = 7 ;
    z=x+a%3*(int(x+y)%2)/4;
    cout << "结果为: " << z << endl;
    return 0;
}
  1. //a%3结果为1,int(x+y)%2结果也为1,两者相乘结果为1且为int型,除以4之后得到结果为0,最后加上x,所以最后结果为2.5
//a%3结果为1,int(x+y)%2结果也为1,两者相乘结果为1且为int型,除以4之后得到结果为0,最后加上x,所以最后结果为2.5


四:求一元二次方程的解


 
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7. float a ,b ,c;
  8. double delta ;
  9. cout << "Please enter a :";
  10. cin >> a ;
  11. cout << endl;
  12. cout << "Please enter b :";
  13. cin >> b ;
  14. cout << endl;
  15. cout << "Please enter c :";
  16. cin >> c ;
  17. cout << endl;
  18. if(a==0&&b==0)
  19. {
  20. cout << "该方程无解。"<< endl;
  21. exit(0);
  22. }
  23. delta = (b*b)-(4*a*c) ; //求delta
  24. if (delta >= 0){
  25. if (delta > 0){
  26. cout << "方程的实数根 X1= " << (-b+sqrt(delta))/(2*a) << endl;
  27. cout << " 方程的实数根X2= " << (-b-sqrt(delta))/(2*a) << endl;
  28. }
  29. else cout << "方程的实数根 X1 = X2 = " << (-b)/(2*a) << endl;
  30. }
  31. else {
  32. delta=abs(delta);
  33. cout << "方程的虚根X1=" << -b/(2*a) <<"+"<<sqrt(delta)/(2*a) <<"j"<< endl;
  34. cout << "方程的虚根X2=" << -b/(2*a) <<"-"<<sqrt(delta)/(2*a) <<"j"<< endl;
  35. }
  36. return 0;
  37. }
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;




int main()
{
    float a ,b ,c;
    double delta ;
    cout << "Please enter a :";
    cin >> a ;
    cout << endl;
    cout << "Please enter b :";
    cin >> b ;
    cout << endl;
    cout << "Please enter c :";
    cin >> c ;
    cout << endl;
    if(a==0&&b==0)
    {
        cout << "该方程无解。"<< endl;
        exit(0);
    }


    delta = (b*b)-(4*a*c) ; //求delta
    if (delta >= 0){
        if (delta > 0){
            cout << "方程的实数根 X1= " << (-b+sqrt(delta))/(2*a) << endl;
            cout << " 方程的实数根X2= " << (-b-sqrt(delta))/(2*a) << endl;
            }
        else cout << "方程的实数根 X1 = X2 = " << (-b)/(2*a) << endl;
    }
    else {
        delta=abs(delta);
        cout << "方程的虚根X1=" << -b/(2*a) <<"+"<<sqrt(delta)/(2*a) <<"j"<< endl;
        cout << "方程的虚根X2=" << -b/(2*a) <<"-"<<sqrt(delta)/(2*a) <<"j"<< endl;
    }




    return 0;
}



五:加密算法


  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. const int Size=20;
  7. string name;
  8. int num,i,name_num[Size];
  9. cout << "Pleast enter your name :";
  10. cin >> name;
  11. cout << endl;
  12. cout << "Please enter a number you like and don't forget: ";
  13. cin >> num;
  14. cout << endl;
  15. for (i=0;name[i]!='\0';i++)
  16. {
  17. name_num[i]=int(name[i]);
  18. name_num[i]=name_num[i] ^ num;
  19. }
  20. cout << "After the name Encryption :";
  21. for (i=0;name[i]!='\0';i++)
  22. {
  23. cout << name_num[i]<<" ";
  24. }
  25. cout << endl;
  26. return 0;
  27. }
  28. //输入音标姓名,先将每个字母转换为ASCII码,再与输入的数做异或运算,最后输出加密后的结果
#include <iostream> 
#include <string>  
using namespace std;  
  
int main()  
{  
    const int Size=20;  
    string name;  
    int num,i,name_num[Size];  
    cout << "Pleast enter your name :";  
    cin >> name;  
    cout << endl;  
    cout << "Please enter a number you like and don't forget: ";  
    cin >> num;  
    cout << endl;  
    for (i=0;name[i]!='\0';i++)  
    {  
       name_num[i]=int(name[i]);  
       name_num[i]=name_num[i] ^ num;  
    }  
    cout << "After the name Encryption :";  
    for (i=0;name[i]!='\0';i++)  
    {  
       cout << name_num[i]<<" ";  
    }  
    cout << endl; 
    return 0;  
}  
//输入音标姓名,先将每个字母转换为ASCII码,再与输入的数做异或运算,最后输出加密后的结果

六:


  1. #include<iostream>
  2. #include<bitset>
  3. using namespace std;
  4. int main()
  5. {
  6. bitset<16> ctrl_1;
  7. bitset<16> ctrl_2 = 1000000011111111;
  8. while (1)
  9. {
  10. cout << "请输入16位的二进制指令:";
  11. cin >>ctrl_1;
  12. ctrl_1=ctrl_1&ctrl_2;
  13. if(ctrl_1==0||ctrl_1==1000000000000000){
  14. if(ctrl_1==1000000000000000){
  15. cout << '\a'<<endl;
  16. break;
  17. }
  18. if(ctrl_1==0)break;
  19. }
  20. }
  21. return 0;
  22. }
#include<iostream>
#include<bitset>
using namespace std;
int main()
{
    bitset<16> ctrl_1;
    bitset<16> ctrl_2 = 1000000011111111;
    while (1)
    {
        cout << "请输入16位的二进制指令:";
        cin >>ctrl_1;
        ctrl_1=ctrl_1&ctrl_2;
        if(ctrl_1==0||ctrl_1==1000000000000000){
             if(ctrl_1==1000000000000000){
                cout << '\a'<<endl;
                break;
             }
             if(ctrl_1==0)break;
        }
    }

    return 0;
}

错误:未出现。

问题:不明白第六题的要求是什么。只能照自己理解去编程。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值