C++语句解释
#include <iostream>#include <string>
#include <malloc.h>
using namespace std;
#define N 5
struct List
{
string line;
struct List *next;
}list;
struct List * creat(struct List *head)
{
head = NULL;
return head;
}
struct List *Insert(struct List *head,string str)
{
struct List *p;
struct List *q;
q = head;
p = new struct List;
p->line = str;
p->next = NULL;
if (head == NULL)
{
head = p;
}
else
{
while (q->next != NULL)
{
q = q->next;
}
q->next = p;
}
return head;
}
void Count(struct List *head,int &alph,int &digital,int &space,int &total)
{
struct List *p = head;
int i;
if (head == NULL)
{
return ;
}
else
{
while (p != NULL)
{
total += p->line.length();
for (i = 0; i < p->line.length(); ++i)
{
if (isalpha(p->line[i]))
{
++alph;
}
else if (isdigit(p->line[i]))
{
++digital;
}
else
{
if (isspace(p->line[i]))
{
++space;
}
}
}
p = p->next;
}
}
}
int CountStr(struct List *head, string str)
{
int sum = 0;
struct List *p = head;
int pos;
if (head == NULL)
{
return -1;
}
while (p != NULL)
{
pos = p->line.find(str);
while (pos != -1)
{
++sum;
pos = p->line.find(str,pos+str.length());
}
p = p->next;
}
return sum;
}
struct List *Del(struct List *head,string str)
{
struct List *p = head;
int pos;
if (head == NULL)
{
return NULL;
}
else
{
while (p != NULL)
{
pos = p->line.find(str);
while (pos != -1)
{
p->line = p->line.erase(pos,str.length());
pos = p->line.find(str);
}
p = p->next;
}
}
return head;
}
void Print(struct List *head)
{
struct List *p,*q;
p = head;
while (p != NULL)
{
cout<<p->line<<endl;
q = p->next;
p = q;
}
}
void Free(struct List *head)
{
struct List *p = head;
struct List *q;
while (p != NULL)
{
q = p->next;
delete p;
p = q;
}
}
int main()
{
int i;
int alph = 0;
int space = 0;
int total = 0;
int digital = 0;
int count;
string str;
string ss = "aa";
struct List *head;
head = creat(head);
for (i = 0; i < N; ++i)
{
cout<<"please input a string:\n";
fflush(stdin);
getline(cin,str);
head = Insert(head,str);
}
Print(head);
cout<<"------------------------------------------------\n";
Count(head,alph,digital,space,total);
cout<<"全部字母数: "<<alph<<endl;
cout<<"数字个数: "<<digital<<endl;
cout<<"空格个数: "<<space<<endl;
cout<<"文章总字数: "<<total<<endl;
cout<<"------------------------------------------------\n";
count = CountStr(head,ss);
cout<<ss<<"次数:"<<count<<endl;
cout<<"------------------------------------------------\n";
head = Del(head,ss);
Print(head);
Free(head);
return 0;
}