def strQ2B(ustring): rstring = "" for uchar in ustring: inside_code = ord(uchar) if inside_code == 12288: inside_code = 32 elif 65281 <= inside_code <= 65374: inside_code -= 65248 rstring += chr(inside_code) return rstring
时间: 2024-02-14 22:35:42 浏览: 28
这段代码是用来将字符串中的全角字符转换为半角字符的。其中,全角字符指的是中文字符、全角标点符号等,半角字符则是英文字符、半角标点符号等。一般来说,全角字符的 Unicode 编码的范围是 65281 ~ 65374,其中包括了中文字符和全角标点符号的编码。而半角字符的 Unicode 编码的范围是 33 ~ 126,其中包括了英文字符和半角标点符号的编码。这段代码的主要思路是遍历字符串中的每一个字符,如果是全角字符,则将其 Unicode 编码减去 65248,即可得到对应的半角字符的编码,然后将其转换为字符,加到新的字符串中。如果是空格的 Unicode 编码(12288),则直接将其转换为半角空格的 Unicode 编码(32)即可。最后,返回转换后的字符串。
相关问题
python怎么将字符串中的全角自动转换成半角
可以使用Python的unicodedata模块中的normalize方法将全角字符转换成半角字符。具体操作如下:
```python
import unicodedata
def strQ2B(ustring):
"""将字符串中的全角字符转换成半角字符"""
rstring = ""
for uchar in ustring:
inside_code = ord(uchar)
# 全角空格直接转换
if inside_code == 0x3000:
inside_code = 0x0020
# 全角字符(除空格)根据关系转化
elif 0xFF01 <= inside_code <= 0xFF5E:
inside_code -= 0xfee0
rstring += chr(inside_code)
return rstring
s = "Python全角到半角转换!1234567890"
print(strQ2B(s))
```
输出结果为:`Python全角到半角转换!1234567890`
要求如下:编写一个能为栈也能为队列的线性表,根据要求完善下列代码: #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 函数中,分别使用了栈和队列的实现类,演示了它们的基本操作。