#include<iostream>
#include<string>
using namespace std;
struct ChainNode
{
char value;
ChainNode* next;
ChainNode(char v){
value = v;
next = NULL;
}
};
class ChainQueue
{
public:
ChainQueue(){
head = NULL;
size = 0;
}
void push(char value){
if (head == NULL){
size++;
head = new ChainNode(value);
return;
}
ChainNode* ptr = head;
size++;
while (ptr != NULL && ptr->next != NULL)ptr = ptr->next;
ptr->next = new ChainNode(value);
}
char front()const{
return head->value;
}
void pop(){
if(size == 0)return;
ChainNode* temp = head->next;
delete head;
head = temp;
size--;
}
int getsize(){
return size;
}
~ChainQueue(){
ChainNode* cur = head;
while (cur != NULL)
{
ChainNode* temp = head->next;
delete cur;
cur = temp;
}
}
private:
ChainNode* head;
int size;
};
int main(){
string str;
string number = "";
string letter = "";
ChainQueue queue;
cout << "输入一串包括字母和数字的字符串:" << endl;
cin >> str;
cout << "输入的混合字符串为:" << str << endl;
for (int i = 0; i < str.length(); i++)
{
queue.push(str[i]);
}
for (int i = 0; queue.getsize() != 0; i++)
{
if(queue.front() >= '0' && queue.front() <= '9')
number += queue.front();
else
letter += queue.front();
queue.pop();
}
cout << "数字字符串为:" << number << endl;
cout << "字母字符串为:" << letter << endl;
}
2024-09-02 广告