|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 白砂糖 于 2020-11-12 18:19 编辑
1.定义任意两个字符串,完成以下操作:
(1)编写求字符串长度函数,显示该字符串长度;
(2)编写字符串拼接函数,将该字符串与键盘输入的另一字符串拼接,并显示拼接结果;
(3)编写比较大小函数,比较两字符串大小,并显示比较结果。
#include<iostream>
#include<string>
using namespace std;
class linklist
{
public:
Str();
Str(T a[], int n);
~Str();
int Strlength();
Strcat();
StrCmp();
void PrintList();
int Empty();
private:
Node<T>*first;
};
template<typename T>
linklist<T>::Str()
{
first = nem Node<T>;
first->next = nullptr;
}
template<typename T>
linklist<T>::Str(T a[], int n)
{
first = new Node<T>;
first->next = nullptr;
for (i = 0; i < n; i++)
{
Node<T>*s = nullptr;
s = new Node<T>;
s->data = a[i];
s->next = first->next;
first->next = s;
}
}
template<typename T>
linklist<T>::~Str()
{
Node<T>*p = first;
while (first != nullptr)
{
first = first->next;
delete p;
p = first;
}
}
template<typename T>
int linklist<T>::Strlength()
{
Node<T>*p = first->next;
int count = 0;
while (p != nullptr)
{
p = p->next;
count++;
}
return count;
}
template<typename T>
linklist::Strcat()
{
}
template<typename T>
linklist::StrCmp()
{
}
template<typename T>
void PrintList()
{
Node<T>*p = first->next;
while (p != nullptr)
{
cout << p->data<<"\t";
p = p->next;
}
cout << endl;
}
template<typename T>
int linklist::Empty()
{
Node<T>*p = first->next;
while (p = nullptr)
{
cout << "表为空。";
}
cout << endl;
}
int main()
{
int r[], m[],x,y,i,j;
for (i = 0; i < 10;i++)
{
cin >> x;
r[i] = x;
}
linklist L{ r,10};
cout << "当前线性表r的数据为:";
L.PrintList();
cout << "当前字符串的长度为:" << L.Strlength() << endl;
for (j = 0; j < 10;j++)
{
cin >> y;
m[j] = y;
}
linklist L{ m,10 };
cout << "当前线性表m的数据为:";
L.PrintList();
cout << "当前字符串的长度为:" << L.Strlength() << endl;
cout << "两个字符串链接后的数据为:" << L.Strcat() << endl;
cout << "比较两个字符串的结果:" << L.StrCmp() << endl;
return 0;
} |
|