第二大题 改错题

C++1.png

最后大题40分 程序填空题

  1. 控制台画图 ```cpp

    include

    include

    include

    include

    using namespace std; class Timer { private: int sec; int x,y;//move int c;//key clock_t t1,t2,t3; public: Timer() {
    1. sec=0;
    2. x=30;y=10;
    3. c=0;
    //—————————————————————

//————————————————————— } void gotoxy(int x,int y) { HANDLE h;//句柄,对象的索引 COORD c;//结构体,坐标值 c.X=x; c.Y=y; h=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(h,c); } void draw() { gotoxy(x,y); cout<1000) { //—————————————————————

//————————————————————— t1=clock(); } } void key() { if(clock()-t2>50) { if(GetAsyncKeyState(VK_ESCAPE)) exit(0); if(GetAsyncKeyState(VK_UP)) c=1; if(GetAsyncKeyState(VK_DOWN)) c=2; if(GetAsyncKeyState(VK_LEFT)) c=3; if(GetAsyncKeyState(VK_RIGHT)) c=4; if(GetAsyncKeyState(VK_SPACE)) c=5; t2=clock(); } } void move1() { if(clock()-t3>50) { erase(); switch(c) { case 1:y=y<0?0:y-1;break; case 2:y=y>24?24:y+1;break; case 3:x=x<0?0:x-1;break; case 4:x=x>78?78:x+1;break; case 5:sec=0;break; } draw(); c=0; t3=clock(); } } void move() { while(true) { timer(); key(); move1(); } } };

int main() { Timer t; t.move(); return 0; }

  1. 2. 运算符重载
  2. 重载"+"运算符,两个线段的顶点坐标分别相加;重载“<<”运算符,输出对象中的数据
  3. ```cpp
  4. #include <iostream>
  5. using namespace std;
  6. class point
  7. {
  8. protected:
  9. int x1,y1;
  10. public:
  11. point(int xx1,int yy1)
  12. {
  13. x1=xx1;y1=yy1;
  14. }
  15. };
  16. class line:public point
  17. {
  18. protected:
  19. int x2,y2;
  20. public:
  21. line(int xx1,int yy1,int xx2,int yy2):point(xx1,yy1)
  22. {
  23. x2=xx2;y2=yy2;
  24. }
  25. void display()
  26. {
  27. cout<<"x1="<<x1<<endl;
  28. cout<<"y1="<<y1<<endl;
  29. cout<<"x2="<<x2<<endl;
  30. cout<<"y2="<<y2<<endl;
  31. }
  32. line operator +(line L2)//坐标相加
  33. {
  34. //------------------------------------------
  35. //------------------------------------------
  36. temp.x1=x1+L2.x1;
  37. temp.y1=y1+L2.y1;
  38. temp.x2=x2+L2.x2;
  39. temp.y2=y2+L2.y2;
  40. return temp;
  41. }
  42. friend ostream& operator<<(ostream& os,line a)
  43. {
  44. //------------------------------------------
  45. //------------------------------------------
  46. return os;
  47. }
  48. };
  49. int main()
  50. {
  51. line L1(1,2,3,4);
  52. line L2(5,6,7,8);
  53. cout<<L1+L2;
  54. return 0;
  55. }
  1. 从命令行读入命令

输入三次密码,输入密码时显示为“*”。(提示:用getch()函数无回显输入单个字符)

  1. #include <iostream>
  2. #include <string>
  3. #include <conio.h>
  4. using namespace std;
  5. class Password
  6. {
  7. private:
  8. string password;
  9. int times;
  10. public:
  11. Password(string ss,int nn)
  12. {
  13. }
  14. string input1()
  15. {
  16. string temp;
  17. char c;
  18. cout<<"input password:";
  19. while(true)
  20. {
  21. }
  22. cout<<endl;
  23. return temp;
  24. }
  25. void input2()
  26. {
  27. string temp;
  28. while(true)
  29. {
  30. temp=input1();
  31. if(temp==password)
  32. {
  33. cout<<"密码正确!"<<endl;
  34. //此处进行正常操作
  35. break;
  36. }
  37. else
  38. {
  39. cout<<"密码不正确!"<<endl;
  40. --times;
  41. if(times<=0)
  42. {
  43. cout<<"输入错误,系统已退出!";
  44. break;
  45. }
  46. }
  47. }
  48. }
  49. };
  50. int main()
  51. {
  52. Password p("123456",3);
  53. p.input2();
  54. return 0;
  55. }
  1. 文件操作

文本文件”04-1.txt”每一行的格式为“学号 姓名 成绩”。将文本文件的每一行作为单个元素压入容器。从键盘输入姓名,从容器中查找并显示该姓名的学生信息(学号 姓名 成绩)

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <algorithm>
  6. using namespace std;
  7. struct A
  8. {
  9. string s;
  10. bool operator==(string name)
  11. {
  12. }
  13. };
  14. class Score
  15. {
  16. private:
  17. vector<A>a;
  18. vector<A>::iterator p;
  19. public:
  20. Score()
  21. {
  22. string s;
  23. A temp;
  24. ifstream f("04-1.txt",ios::in);
  25. if(f)
  26. {
  27. while(!f.eof())
  28. {
  29. getline(f,s);
  30. temp.s=s;
  31. a.push_back(temp);
  32. }
  33. }
  34. }
  35. void find_v()
  36. {
  37. string name;
  38. cout<<"input name:";
  39. cin>>name;
  40. p=find(a.begin(),a.end(),name);
  41. if(p!=a.end())
  42. cout<<p->s<<endl;
  43. }
  44. };
  45. int main()
  46. {
  47. Score s;
  48. s.find_v();
  49. return 0;
  50. }
  1. 文件操作

文本文件”04-2.txt”每行的格式为“学号-姓名-成绩”。将每行作为容器的一个元素(结构体类型)读入vector容器中。将所有记录按照成绩由高到低的顺序进行排序,显示所有记录。按学号查找一个记录并显示。

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <algorithm>
  6. using namespace std;
  7. struct A
  8. {
  9. string number;
  10. string name;
  11. string score;
  12. bool operator==(string s)
  13. {
  14. }
  15. };
  16. class Score
  17. {
  18. private:
  19. vector<A>a;
  20. vector<A>::iterator p;
  21. public:
  22. Score()
  23. {
  24. string s,s1,s2,s3;
  25. int n1,n2;
  26. A temp;
  27. ifstream f("04-2.txt",ios::in);
  28. if(f)
  29. {
  30. while(!f.eof())
  31. {
  32. getline(f,s);
  33. temp.number=s1;
  34. temp.score=s3;
  35. temp.name=s2;
  36. temp.score=s3;
  37. a.push_back(temp);
  38. }
  39. }
  40. }
  41. static bool sort1(A a1,A a2)
  42. {
  43. return a1.score>a2.score;
  44. }
  45. void browse()
  46. {
  47. for(p=a.begin();p!=a.end();++p)
  48. {
  49. cout<<p->number<<"-"
  50. <<p->name<<"-"
  51. <<p->score<<endl;
  52. }
  53. }
  54. void sort_v()
  55. {
  56. sort(a.begin(),a.end(),sort1);
  57. }
  58. void find_v()
  59. {
  60. string number;
  61. cout<<"input number:";
  62. cin>>number;
  63. p=find(a.begin(),a.end(),number);
  64. if(p!=a.end())
  65. {
  66. cout<<p->number<<"-"
  67. <<p->name<<"-"
  68. <<p->score<<endl;
  69. }
  70. }
  71. };
  72. int main()
  73. {
  74. Score s;
  75. s.sort_v();
  76. s.browse();
  77. s.find_v();
  78. return 0;
  79. }