debug醒目 ssd5醒目

DraculaW 2004-11-21 08:45:08
//main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

#include "course.h"
#include "safearray.h"

void init_courses(safearray<course>);
void display_menu(safearray<course>);

const int NUM_COURSES = 10;
const int QUIT = 99;

int main(int argc, char* argv[])
{

safearray<course> courses(NUM_COURSES);
init_courses(courses);

int choice = 0;
while (choice != QUIT)
{

display_menu(courses);
cout << "Enter number of course to see more information on\n";
cin >> choice;
cout << "\n";

if (choice >= 1 && choice <= NUM_COURSES)
{
cout << courses[choice - 1] << "\n\n";
}
cout<<"a"<<endl;
}

return EXIT_SUCCESS;
}

void display_menu(safearray<course> courses)
{

for (int i = 1; i <= NUM_COURSES; i++)
{
cout << i << ". " << courses[i - 1].name << "\n";
}
cout << "99. Quit\n";
}

void init_courses(safearray<course> courses)
{

ifstream inf("courses.txt");
if (! inf)
{
cerr << "Could not open courses.txt" << endl;
exit(EXIT_FAILURE);
}

for (int i = 0; i < NUM_COURSES; i++)
{
inf >> courses[i];
}

inf.close();
}

//safearray.h

#ifndef SAFEARRAY_H
#define SAFEARRAY_H

template <class T>
class safearray {

private:
T *storage;
int capacity;

public:
safearray() : storage(NULL), capacity(0) {} // default constructor
safearray(int size)
{
storage= new T[size];
capacity=size;
}
~safearray(void){delete []storage ;}

T& operator[] (int i) throw(out_of_range)
{
if(i<0)
throw(out_of_range)("err: input < 0");
else if(i>capacity)
throw(out_of_range)("err: input > max");
else
return *(storage+i);
}

};


#endif

//course.h

#ifndef COURSE_H
#define COURSE_H

int const MAX_LINES = 10;

class course {

public:
string name;
string title;
string description[MAX_LINES];

course() : name(""), title("") {}
course(string name, string title) :
name(name), title(title) {}

friend ostream& operator<<(ostream&, const course&);
friend istream& operator>>(istream&, course&);
};

ostream& operator<<(ostream& out, const course& c) {

out << c.name << ": " << c.title << "\n";

int index = 0;
while (c.description[index] != "") {
out << c.description[index++] << "\n";
}

return out;
}

istream& operator>>(istream& in, course& c) {

getline(in, c.name);
getline(in, c.title);

string line;
getline(in, line);
int number = 0;
while (line != "") {
c.description[number++] = line;
getline(in, line);
}

return in;
}

#endif


...全文
122 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
DraculaW 2004-11-22
  • 打赏
  • 举报
回复
我写出来了

//main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

#include "course.h"
#include "safearray.h"

void init_courses(safearray<course>&);
void display_menu(safearray<course>&);

const int NUM_COURSES = 10;
const int QUIT = 99;

int main(int argc, char* argv[])
{

safearray<course> courses(NUM_COURSES);
init_courses(courses);

int choice = 0;
while (choice != QUIT)
{

display_menu(courses);
cout << "Enter number of course to see more information on\n";
cin >> choice;
cout << "\n";
if (choice==99)
{
cout<<"program endl;"<<endl;
break;
}
try
{
cout << courses[choice - 1] << "\n\n";
if (choice<=0)
{
throw out_of_range("out of range(too little)");
}
else if(choice>=11)
{
throw out_of_range("out of range(too big)");
}
}
catch(out_of_range err)
{
cerr<<err.what()<<endl;
}
}
return EXIT_SUCCESS;
}

void display_menu(safearray<course> &courses)
{

for (int i = 1; i <= NUM_COURSES; i++)
{
cout << i << ". " << courses[i - 1].name << "\n";
}
cout << "99. Quit\n";
}

void init_courses(safearray<course> &courses)
{

ifstream inf("courses.txt");
if (! inf)
{
cerr << "Could not open courses.txt" << endl;
exit(EXIT_FAILURE);
}

for (int i = 0; i < NUM_COURSES; i++)
{
inf >> courses[i];
}

inf.close();
}

//safearray.h

#ifndef SAFEARRAY_H
#define SAFEARRAY_H

template <class T>
class safearray {

private:
T *storage;
int capacity;

public:
safearray() : storage(NULL), capacity(0) {} // default constructor
safearray(int size)
{
storage= new T[size];
capacity=size;
}
~safearray(void){delete []storage ;}

T& operator[] (int i) throw(out_of_range)
{
if(i==98)
throw out_of_range("quit program");
else if(i<0)
throw(out_of_range)("err: input < 0");
else if(i>capacity-1)
throw(out_of_range)("err: input > max");
else

return *(storage+i);
}

};


#endif
xuzheng318 2004-11-22
  • 打赏
  • 举报
回复
帮你顶了
DraculaW 2004-11-21
  • 打赏
  • 举报
回复
在同一个文件夹下有这个文件


//courses.txt


SSD1
Introduction to Information Systems
This course introduces students to fundamental concepts
and skills of software development. Students learn how
software is written, what its fundamental building blocks
are, and how multiple pieces of software cooperate to make
the Web work. Students learn to create web pages in HTML
and applets in Java.

SSD2
Introduction to Computer Systems
This course introduces students to the fundamentals of
using and maintaining computer systems in an Internet
environment. The basic components and functions of the
computer and the network are introduced, along with tools
and procedures for their operation and maintenance.

SSD3
Object-Oriented Programming and Design
This course introduces students to problem solving by
means of object-oriented design and implementation.
Emphasis is on problem analysis and solution design,
documentation and implementation. Students use commercial
software libraries, and create software projects.
Programming assignments are carried out in Java.

SSD4
User-Centered Design and Testing
This course focuses on human-computer interaction teaching
basic skills in designing, creating, and evaluating user
interfaces. Students learn how to design usable,
human-friendly user interfaces using a rapid-prototyping
programming language. They also learn how to evaluate
interfaces empirically with two usability tools. Visual
Basic is used in programming assignments.

SSD5
Data Structures and Algorithms
This course focuses on understanding the dependence of
execution time, bandwidth and memory requirements on
the data structures and algorithms chosen. Students
learn to reason informally about algorithm and data
structure correctness and complexity. Primary emphasis
is given to intelligent selection of algorithms and
representations. Programming assignments use C++ and the
Standard Template Library.

SSD6
System-Level Programming
This course provides students with a programmer's view
of processors, memory, and operating systems. Students
learn explicitly about memory organization and hierarchies,
about context-switching and threads, and about the
transformations that a high-level program undergoes before
it is executed on actual hardware. These enable students
to eliminate obscure "bugs" and to measure and optimize
program performance. Programming assignments use the C
programming language.

SSD7
Database Systems
This course introduces students to database concepts
including database design. Relational data models are
emphasized. Students develop client-server applications
in Java and/or Visual Basic, using commercial database
management systems. Example applications include e-commerce
systems.

SSD8
Networks and Distributed Computing
This course focuses on principles and practices of
network-based computing. It begins with an overview of
networking technology in support of data and multimedia
communication. It continues with application-oriented
protocols and approaches to distributed object-oriented
programming using Java.

SSD9
Software Specification, Testing, and Maintenance
This course focuses on the principles of development of
software systems following software engineering practices.
Students work individually or in multi-person teams on
Internet-based software projects of significant scale,
acquiring experience in all phases of the software product
lifecycle.

SSD10
Software Project Organization and Management
This course focuses on the organizational and management
aspects of software projects. Students learn techniques of
project planning, scheduling, costing and organization, from
an iterative perspective, and examine and critique various kinds
of planning and management artifacts. A key emphasis is management
of client issues.

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧