资源描述
程序设计与算法语言Ⅱ(电类) 2010 级机试(A 卷)
(考试时间 80 分钟)
说明:首先在网络 Z 盘建立一个以自己的学号+姓名命名的文件夹,在考试结束前
根据机房要求,将要上交的源文件“学号-fa1.CPP”和“学号-fa2.CPP”复制到该
文件夹中。
注意:请在本机的 D 盘根目录上建立一个以自己学号命名的文件夹,将本次机试的
两题所用的工程目录及文件均建立在此文件夹中。
一、改错题 (50 分)
【要求】调试程序,修改其中的语法错误及少量逻辑错误。只能修改、不能增加或
删除整条语句,但可增加少量说明语句和编译预处理指令。
★ 请在修改的语句后依次加上://错误 1、//错误 2、……。
【注意】源程序以“学号-fa1.cpp”命名,存入自己学号文件夹,然后在“学号-fa1.cpp”
源文件中改错。请不要直接在此 WORD 文档上修改。
【题目】以下程序实现了对字符串的选择排序,初始字符串为“Hello World!”,排
序后输出字符串为“roollledWH!”。
【含错误的源程序】
#include <iostream>
using namespace std;
void SelectSort( char [] )
int main()
{
int n=12;
char list[n]="Hello World!";
cout>>"未排序字符串:"<<list<<endl;
SelectSort( list[n] );
cout<<"已排序字符串:"<<list[n]<<endl;
return 0;
}
void SelectSort( char slist[n] )
{
int i,j,k;
char temp;
for(i=0;i<n;i++)
{
k=i;
temp=slist[i];
for(j=0;j<=n;j++)
if(slist[j]>temp)
{
k=j;
temp=slist[j];
}
if(k!=i)
{
temp=slist[i];
slist[i]=slist[k];
slist[k]=temp;
}
}
}
我编的:
#include <iostream>
using namespace std;
void SelectSort( char [],int );
int main()
{
int n=12;
char list[]="Hello World!";
cout<<"未排序字符串:"<<list<<endl;
SelectSort( list,n );
cout<<"已排序字符串:"<<list<<endl;
return 0;
}
void SelectSort( char slist[],int n)
{
int i,j,k;
char temp;
for(i=0;i<n;i++)
{
k=i;
temp=slist[i];
for(j=i;j<=n;j++)
if(slist[j]>temp)
{
k=j;
temp=slist[j];
}
if(k!=i)
{
temp=slist[i];
slist[i]=slist[k];
slist[k]=temp;
}
}
}
二、编程题(50 分)
【注意】源程序以“学号-fa2.cpp”命名,存入自己学号文件夹。
【题目】以下程序定义了一个链表类 List,其元素为整型数据结点。链表可以通过
流运算符从当前目录中的文件“ListA.txt”中读取数据,再向控制台输出。
【说明】本程序的执行流程是,创建链表对象并通过文件设置链表初值,然后向链
表中添加一些数据。请按以上说明和要求将下面程序补充完整,并调试运行。
//此处添加代码
class List;
ostream& operator<<(ostream &os,List &a);
istream& operator>>(istream &, List &);
class Node
{
public:
int info;//数据域
Node *link;//指针域
Node( const int data=0 ) { info=data; link=NULL; }
};
class List
{
Node *head, *tail;
public:
List();
~List();
void Empty(); //清空整个链表
List &operator+=(const Node &a);
//在当前表的最后添加一个元素
//用于直接输出链表对象
//用于从文件输入链表对象
friend ostream& operator<<(ostream &, List &);
friend istream& operator>>(istream &, List &);
};
List::List()
{
//此处添加代码
}
List::~List()
{
Empty();
delete head;
}
void List::Empty()
{
//此处添加代码
}
List& List::operator+=(const Node &a)
{
//此处添加代码
}
ostream& operator<<(ostream &os, List &a)
{
//此处添加代码
}
istream& operator>>(istream &fs,List &a)
{
//此处添加代码
}
int main()
{
List list;
fstream file;
//创建链表
file.open("ListA.txt", ios::in);
if( !file )
{
cout << "Can not open input file!\n" << endl;
return 0;
}
file>>list;
file.close();
file.clear();
cout<<list;
for(int i=0;i<3;i++)
{
Node node(i);
list += node;
}
cout<<"当前链表内容:"<<endl;
cout<<list;
return 0;
}
//向链表中添加 3 个结点
我编的:
//此处添加代码
#include<iostream>
#include<fstream>
using namespace std;
class List;
ostream& operator<<(ostream &os,List &a);
istream& operator>>(istream &, List &);
class Node
{
public:
int info;
//数据域
Node *link;//指针域
Node( const int data=0 ) { info=data; link=NULL; }
};
class List
{
Node *head, *tail;
public:
List();
~List();
void Empty(); //清空整个链表
List &operator+=(const Node &a); //在当前表的最后添加一个元素
friend ostream& operator<<(ostream &, List &);//用于直接输出链表对象
friend istream& operator>>(istream &, List &);//用于从文件输入链表对象
};
List::List()
{
//此处添加代码
head=tail=new Node();
}
List::~List()
{
Empty();
delete head;
}
void List::Empty()
{
//此处添加代码
Node* temp;
while(head->link!=NULL){
temp = head->link;
head->link = temp->link;
delete temp;
}
tail=head;
}
List& List::operator+=(const Node &a)
{
Node* b = new Node(a.info);
tail->link = b;
tail = b;//此处添加代码
return *this;
}
ostream& operator<<(ostream &os, List &a)
{
//此处添加代码
Node* temp=(a.head)->link;
while(temp){
os<<temp->info<<'\t'<<endl;
temp=temp->link;
}
return os;
}
istream& operator>>(istream &fs,List &a)
{
//此处添加代码
int aa;
fs>>aa;
//cout<<aa;
while(1){
Node* temp;
temp = new Node(aa);
a+=*temp;
//cout<<aa;
if(fs.eof()!=0)break;
fs>>aa;
}
return fs;
}
int main()
{
List list;
fstream file;
//int y;
//创建链表
file.open("ListA.txt", ios::in);
if( !file )
{
cout << "Can not open input file!\n" << endl;
return 0;
}
//cout<<"############"<<endl;
//file>>y;cout<<y;
file>>list;
file.close();
file.clear();
cout<<list;
for(int i=0;i<3;i++)//向链表中添加 3 个结点
{
Node node(i);
list += node;
}
cout<<"当前链表内容:"<<endl;
cout<<list;
return 0;
}
展开阅读全文