第二大题 改错题
最后大题40分 程序填空题
- 控制台画图
```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() {
//—————————————————————sec=0;
x=30;y=10;
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<
//————————————————————— 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; }
2. 运算符重载
重载"+"运算符,两个线段的顶点坐标分别相加;重载“<<”运算符,输出对象中的数据
```cpp
#include <iostream>
using namespace std;
class point
{
protected:
int x1,y1;
public:
point(int xx1,int yy1)
{
x1=xx1;y1=yy1;
}
};
class line:public point
{
protected:
int x2,y2;
public:
line(int xx1,int yy1,int xx2,int yy2):point(xx1,yy1)
{
x2=xx2;y2=yy2;
}
void display()
{
cout<<"x1="<<x1<<endl;
cout<<"y1="<<y1<<endl;
cout<<"x2="<<x2<<endl;
cout<<"y2="<<y2<<endl;
}
line operator +(line L2)//坐标相加
{
//------------------------------------------
//------------------------------------------
temp.x1=x1+L2.x1;
temp.y1=y1+L2.y1;
temp.x2=x2+L2.x2;
temp.y2=y2+L2.y2;
return temp;
}
friend ostream& operator<<(ostream& os,line a)
{
//------------------------------------------
//------------------------------------------
return os;
}
};
int main()
{
line L1(1,2,3,4);
line L2(5,6,7,8);
cout<<L1+L2;
return 0;
}
- 从命令行读入命令
输入三次密码,输入密码时显示为“*”。(提示:用getch()函数无回显输入单个字符)
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class Password
{
private:
string password;
int times;
public:
Password(string ss,int nn)
{
}
string input1()
{
string temp;
char c;
cout<<"input password:";
while(true)
{
}
cout<<endl;
return temp;
}
void input2()
{
string temp;
while(true)
{
temp=input1();
if(temp==password)
{
cout<<"密码正确!"<<endl;
//此处进行正常操作
break;
}
else
{
cout<<"密码不正确!"<<endl;
--times;
if(times<=0)
{
cout<<"输入错误,系统已退出!";
break;
}
}
}
}
};
int main()
{
Password p("123456",3);
p.input2();
return 0;
}
- 文件操作
文本文件”04-1.txt”每一行的格式为“学号 姓名 成绩”。将文本文件的每一行作为单个元素压入容器。从键盘输入姓名,从容器中查找并显示该姓名的学生信息(学号 姓名 成绩)
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
struct A
{
string s;
bool operator==(string name)
{
}
};
class Score
{
private:
vector<A>a;
vector<A>::iterator p;
public:
Score()
{
string s;
A temp;
ifstream f("04-1.txt",ios::in);
if(f)
{
while(!f.eof())
{
getline(f,s);
temp.s=s;
a.push_back(temp);
}
}
}
void find_v()
{
string name;
cout<<"input name:";
cin>>name;
p=find(a.begin(),a.end(),name);
if(p!=a.end())
cout<<p->s<<endl;
}
};
int main()
{
Score s;
s.find_v();
return 0;
}
- 文件操作
文本文件”04-2.txt”每行的格式为“学号-姓名-成绩”。将每行作为容器的一个元素(结构体类型)读入vector容器中。将所有记录按照成绩由高到低的顺序进行排序,显示所有记录。按学号查找一个记录并显示。
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
struct A
{
string number;
string name;
string score;
bool operator==(string s)
{
}
};
class Score
{
private:
vector<A>a;
vector<A>::iterator p;
public:
Score()
{
string s,s1,s2,s3;
int n1,n2;
A temp;
ifstream f("04-2.txt",ios::in);
if(f)
{
while(!f.eof())
{
getline(f,s);
temp.number=s1;
temp.score=s3;
temp.name=s2;
temp.score=s3;
a.push_back(temp);
}
}
}
static bool sort1(A a1,A a2)
{
return a1.score>a2.score;
}
void browse()
{
for(p=a.begin();p!=a.end();++p)
{
cout<<p->number<<"-"
<<p->name<<"-"
<<p->score<<endl;
}
}
void sort_v()
{
sort(a.begin(),a.end(),sort1);
}
void find_v()
{
string number;
cout<<"input number:";
cin>>number;
p=find(a.begin(),a.end(),number);
if(p!=a.end())
{
cout<<p->number<<"-"
<<p->name<<"-"
<<p->score<<endl;
}
}
};
int main()
{
Score s;
s.sort_v();
s.browse();
s.find_v();
return 0;
}