for (int i=0;i<=top;++i) if (s[i]>='0'&&s[i]<='9') a[top-i]=s[i]-'0'; else a[top-i]=s[i]-55;
时间: 2024-04-12 18:34:43 浏览: 16
这段代码是将字符串s中的字符转换为对应的十六进制数,并将其保存在数组a中。通过循环遍历字符串s中的每个字符,从最低位开始处理。
在循环中,首先通过条件判断语句检查当前字符s[i]是否为数字('0'到'9'之间的字符)。如果是数字,则将其转换为对应的十进制数值(s[i]-'0'),并将结果存储在数组a中的正确位置(a[top-i])。
如果当前字符s[i]不是数字,即为字母(A到F之间的大写字母),则将其转换为对应的十进制数值。这里使用了ASCII码的差值来实现转换(s[i]-55),其中'A'的ASCII码为65,所以减去55可以得到对应的十进制数值。
最后,将转换后的数值存储在数组a中的正确位置。整个过程将字符串s中的字符逆序处理并保存在数组a中。
相关问题
要求如下:编写一个能为栈也能为队列的线性表,根据要求完善下列代码: #include <iostream> using namespace std; #include<string> #include"math.h" template<class T> class vlist//线性表抽象类 { public: virtual void in(T &e)=0; virtual T out()=0; }; template<class T> class Element//线性表存储元素 { public: T data; Element<T> next; Element(){} }; //在beign和end之间完成SStack和Quene类的定义及实现,其继承vlist /begin/ /end***/ int main() { vlist<int> *intp; vlist<string> *strp; SStack<int> ints; Quene<int> intq; SStack<string> strs; Quene<string> strq; intp=&ints; int t=2; intp->in(t); t=3; intp->in(t); t=4; intp->in(t); for(int i=0;i<3;i++) cout<<intp->out(); intp=&intq; t=2; intp->in(t); t=3; intp->in(t); t=4; intp->in(t); for(int i=0;i<3;i++) cout<<intp->out(); strp=&strs; string str; cout<<endl; for(int i=0;i<3;i++) { cin>>str; strp->in(str); } for(int i=0;i<3;i++) cout<<strp->out()<<" "; cout<<endl; strp=&strq; for(int i=0;i<3;i++) { cin>>str; strp->in(str); } for(int i=0;i<3;i++) cout<<strp->out()<<" "; }
代码如下:
```
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
template<class T>
class vlist // 线性表抽象类
{
public:
virtual void in(T &e) = 0;
virtual T out() = 0;
};
template<class T>
class Element // 线性表存储元素
{
public:
T data;
Element<T>* next;
Element() { next = NULL; }
};
template<class T>
class SStack : public vlist<T> // 栈的实现类
{
private:
Element<T>* top; // 栈顶元素指针
public:
SStack() { top = NULL; }
void in(T &e) // 入栈操作
{
Element<T>* p = new Element<T>;
p->data = e;
p->next = top;
top = p;
}
T out() // 出栈操作
{
if (top == NULL) // 栈空,抛出异常
throw "Stack is empty!";
Element<T>* p = top;
top = top->next;
T temp = p->data;
delete p;
return temp;
}
};
template<class T>
class Quene : public vlist<T> // 队列的实现类
{
private:
Element<T>* front; // 队头元素指针
Element<T>* rear; // 队尾元素指针
public:
Quene() { front = rear = NULL; }
void in(T &e) // 入队列操作
{
Element<T>* p = new Element<T>;
p->data = e;
p->next = NULL;
if (front == NULL)
front = rear = p;
else
{
rear->next = p;
rear = p;
}
}
T out() // 出队列操作
{
if (front == NULL) // 队空,抛出异常
throw "Quene is empty!";
Element<T>* p = front;
front = front->next;
T temp = p->data;
delete p;
return temp;
}
};
int main() {
vlist<int>* intp;
vlist<string>* strp;
SStack<int> ints;
Quene<int> intq;
SStack<string> strs;
Quene<string> strq;
intp = &ints;
int t = 2;
intp->in(t);
t = 3;
intp->in(t);
t = 4;
intp->in(t);
for (int i = 0; i < 3; i++)
cout << intp->out() << " ";
intp = &intq;
t = 2;
intp->in(t);
t = 3;
intp->in(t);
t = 4;
intp->in(t);
for (int i = 0; i < 3; i++)
cout << intp->out() << " ";
strp = &strs;
string str;
cout << endl;
for (int i = 0; i < 3; i++) {
cin >> str;
strp->in(str);
}
for (int i = 0; i < 3; i++)
cout << strp->out() << " ";
cout << endl;
strp = &strq;
for (int i = 0; i < 3; i++) {
cin >> str;
strp->in(str);
}
for (int i = 0; i < 3; i++)
cout << strp->out() << " ";
}
```
这段代码定义了一个 vlist 抽象类,并且定义了两个线性表的实现类:SStack 和 Quene,分别实现了栈和队列的基本操作。在 main 函数中,分别使用了栈和队列的实现类,演示了它们的基本操作。
#include <iostream>#include <vector>#include <stack>using namespace std;void dfs(int u, vector<vector<int>>& graph, vector<bool>& visited, vector<int>& farthest) { stack<int> s; s.push(u); visited[u] = true; while (!s.empty()) { int v = s.top(); s.pop(); for (int i = 0; i < graph[v].size(); i++) { int w = graph[v][i]; if (!visited[w]) { visited[w] = true; s.push(w); farthest[w] = farthest[v] + 1; } } }}int main() { int n, m; cin >> n >> m; vector<vector<int>> graph(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; graph[u].push_back(v); } vector<int> farthest(n, 0); for (int i = 0; i < n; i++) { vector<bool> visited(n, false); dfs(i, graph, visited, farthest); } for (int i = 0; i < n; i++) { cout << "Node " << i << " can reach farthest node " << farthest[i] << endl; } return 0;}不用STL实现这个代码
不使用STL实现该代码,可以手动实现栈和vector。
下面是C++实现:
```c++
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 10005;
int stack[MAXN], top = -1;
void push(int x) {
stack[++top] = x;
}
int pop() {
return stack[top--];
}
bool empty() {
return top == -1;
}
void dfs(int u, int graph[][MAXN], bool visited[], int farthest[]) {
push(u);
visited[u] = true;
while (!empty()) {
int v = pop();
for (int i = 0; i < MAXN; i++) {
if (graph[v][i] && !visited[i]) {
visited[i] = true;
push(i);
farthest[i] = farthest[v] + 1;
}
}
}
}
int main() {
int n, m;
cin >> n >> m;
int graph[MAXN][MAXN] = {0};
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
graph[u][v] = 1;
}
int farthest[MAXN] = {0};
for (int i = 0; i < n; i++) {
bool visited[MAXN] = {false};
dfs(i, graph, visited, farthest);
}
for (int i = 0; i < n; i++) {
cout << "Node " << i << " can reach farthest node " << farthest[i] << endl;
}
return 0;
}
```
在这个实现中,我们手动实现了栈,并使用数组来代替vector。同时,我们使用二维数组来表示邻接矩阵,而不是vector<vector<int>>来表示邻接表。
其他部分的实现与使用STL的版本相同。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)