1. Read the codes carefully on page 230 and page 248 of the book, or on page 247 and page 266 of the e-book, then answer the questions as below:
1.1 Describe the main data structure of Stack;
1.2 Describe the differences in the data storage of Stash and Stack.
the words number in that file.
NOTE: I) Words are seperated by spaces(one or more spaces).
3.1 Define two member variables, X and Y, as coordinate, and the other member variable R is radius;
3.2 Define a member function, Distace, to calculate the distance between two points.
3.3 Define a member function, Relationship, to judge the relationship such as seperation, intersection, tangent(intenally or externally tangent) between two circles.
1.1 Describe the main data structure of Stack;
1.2 Describe the differences in the data storage of Stash and Stack.
Notice: The answer can be written in English or Chinese.
1.1
Stack只能在表尾进行操作。
Stack满足后进先出的储存原则,通过栈顶指针Top进行控制。如果栈空,则Top指向栈底,否则Top指向当前栈顶元素的下一位。
Stack的元素是以结点的形式储存的,其中Date保存的是数据的地址而不是数据的实体。
Stack元素的内存空间并不一定连续,Stack是一种链栈。
Stack的基本操作:
1.插入。Stack只能在栈顶位置插入,即表尾,将元素存入当前Top指针指向的位置,然后Top++;
2.删除。Stack的删除只能弹出栈顶的元素。
3.判断是否为空。当Top==Low(栈底)时,Stack为空。
等等
1.2
①.Stash有inflate方法用作膨胀空间,Stack中没有相关操作
②.Stack保存元素是是产生结点的模式,Stack的Date并不储存数据实体,而是储存数据的地址
③.Stack的元素储存位置并不一定连续,是一种链栈
2. Create a program that opens a text file in English and counts
the words number in that file.
NOTE: I) Words are seperated by spaces(one or more spaces).
II) A number or continuous numbers are not WORD.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
const int MAX = 50;
int Count() {
int cou = 0, flag = 0;
char ch;
ifstream text;
text.open("Question 2.txt", ios::in);//打开.txt文件
while (!text.eof()) {//逐字符读取
ch = text.get();
if ((ch >= 'A'&&ch <= 'Z') || (ch >= 'a'&&ch <= 'z')) {//如果是字母则flag置1
flag = 1;
}
else
{
if (flag) {//如果遇到非字母则表示已经读取了一个单词,则cou++,同时flag置0
cou++;
flag = 0;
}
}
}
if (flag)cou++;//最后一个单词可能没有停止记录的机会
return cou;
}
int main()
{
int res;
res = Count();
cout << "该文本共有" << res << "个单词" << endl;
return 0;
}
3. Create a class Point, and finish the following functions as below:
3.1 Define two member variables, X and Y, as coordinate, and the other member variable R is radius;
3.2 Define a member function, Distace, to calculate the distance between two points.
3.3 Define a member function, Relationship, to judge the relationship such as seperation, intersection, tangent(intenally or externally tangent) between two circles.
Note: You can define appropriate parameters according to your member functions.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<math.h>
using namespace std;
class Circle {
public:
void setCircle(int x, int y, int r) {//设置
X = x;
Y = y;
R = r;
}
int Distance(Circle b) {//4.2 计算两个圆的距离
int dis, disx, disy;
disx = X - b.X;//x方向上的距离
disy = Y - b.Y;//y方向上的距离
dis = sqrt(pow(disx, 2) + pow(disy, 2));
return dis;
}
void Relationship(Circle b) {//4.3 判断两个圆的关系
if (this->Distance(b) < (R + b.R))cout << "圆a与圆b相交" << endl;
else {
if (this->Distance(b) == (R + b.R))cout << "圆a与圆b外切" << endl;
else
{
if (this->Distance(b) == abs(R - b.R))cout << "圆a与圆b内切" << endl;
else
{
if(this->Distance(b) > (R + b.R))cout << "圆a与圆b外离" << endl;
else
{
cout << "圆a与圆b内离" << endl;
}
}
}
}
}
private:
int X;
int Y;
int R;
};
int main()
{
Circle a, b;
int ax, ay, ar;
int bx, by, br;
cout << "请输入圆a的x坐标"; cin >> ax;
cout << "请输入圆a的y坐标"; cin >> ay;
cout << "请输入圆a的半径"; cin >> ar;
cout << "请输入圆b的x坐标"; cin >> bx;
cout << "请输入圆b的y坐标"; cin >> by;
cout << "请输入圆b的半径"; cin >> br;
a.setCircle(ax, ay, ar);
b.setCircle(bx, by, br);
cout << "两个圆心的距离是" << a.Distance(b) << endl;//4.2
a.Relationship(b);//4.3
}