第三周作业

来源:互联网 发布:剑灵灵族男捏脸数据吧 编辑:程序博客网 时间:2024/06/01 20:26

一:书中例题

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(longlong)<< endl;
  9. cout<<"numerb of bytes in float int is " <<sizeof(float) << endl;
  10. return 0;
  11. }

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. }

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. }

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. }

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. }

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. }


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. }





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

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
一:书中例题 1. [cpp] view plaincopy01.#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 plaincopy01.#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 plaincopy01.#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 plaincopy01.#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 plaincopy01.#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 plaincopy01.#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 plaincopy01.#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 plaincopy01.#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 plaincopy01.#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 plaincopy01.//e+f等于5,再处于g,由于是整型,得到结果为2  [cpp] view plaincopy01.//n开平方再乘以1.2才除以2再加上m再加上前面的结果,得到最后结果为13.7,根据类型转换规则,得到的类型为double型   例题3_2: [cpp] view plaincopy01.#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 plaincopy01.//a%3结果为1,int(x+y)%2结果也为1,两者相乘结果为1且为int型,除以4之后得到结果为0,最后加上x,所以最后结果为2.5   四:求一元二次方程的解 [cpp] view plaincopy01.#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 plaincopy01.#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 plaincopy01.#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型,结果直接输出,不另用变量储存。

三:例题3_1

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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. }
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //e+f等于5,再处于g,由于是整型,得到结果为2
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //n开平方再乘以1.2才除以2再加上m再加上前面的结果,得到最后结果为13.7,根据类型转换规则,得到的类型为double型

例题3_2:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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. }
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //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. }



五:加密算法


[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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码,再与输入的数做异或运算,最后输出加密后的结果

六:


[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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. }

错误:未出现。

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

0 0
原创粉丝点击