题目是设计文章编辑的程序
输入的英文输出后全部乱码,应该是编码的问题,但是不知道怎么解决
下面是全部代码,求指正
#include <iostream>
#include <string>
using namespace std;
typedef struct line
{
char *data; //字符串指针需要时动态分配内存
struct line* next;
}LINE;
int free_link(LINE*& head)
{
LINE* p = head;
do
{
free(p->data);
p->data = NULL;
} while ((p = p->next) != NULL);
head->next = NULL;
return 0;
}
void CreateTXT(LINE*& head)
{
LINE* p = new LINE; //首先为链表 建立一个附加表头结点
head = p; //将p付给表头指针
char tmp[80]{};
while (1)
{
cin>>tmp; //输入字符串
if (tmp[0] == 5)break; //如果发现输入 ^E,则退出输入
p->data = new char[strlen(tmp) + 1];//为结点分配空间
size_t (p->data) ;
if (tmp[strlen(tmp) - 1] == 5) { //除去最后一个控制符 ^E
p->data[strlen(tmp) - 1] = '\0';
break;
}
}
p->next = NULL; //是最后的一个指针为空。
}
int Count_Space(LINE*& head)
{ //统计空格数
LINE* p = head;
int asc_space = 32; //空格的ASCIC 码值
int count = 0;
do
{
int Len = strlen(p->data); //计算当前 data 里的数据元素的个数
for (int i = 0; i < Len; i++)
if (p->data[i] == asc_space)count++; //计算空格数
} while ((p = p->next) != NULL); //遍历链表
return count;
}
int Count_Num(LINE*& head)
{ //统计数字数
LINE* p = head;
int count = 0;
do
{
int Len = strlen(p->data); //计算当前 data 里的数据元素的个数
for (int i = 0; i < Len; i++)
if (p->data[i] >= 48 && p->data[i] <= 57)count++; //计算数字数
} while ((p = p->next) != NULL); //遍历链表
return count;
}
int Count_All_Word(LINE*& head)
{ //统计文章的总字数
LINE* p = head; //保存链表的首地址
int count = 0; //总字母数
do
{
count += strlen(p->data);
} //计算当前行内的字符数
while ((p = p->next) != NULL); //遍历链表
return count;
}
int Count_ZM(LINE*& head)
{ // 统计字母数
int count = Count_All_Word(head); //总的字符数,包含空格
int space_count = Count_Space(head); //空格数
int num_count = Count_Num(head);//数字数
return count - space_count - num_count; //返回文章的字母总数
}
int Find_Word(LINE*& head, char* sch)
{ //统计 sch 在文章中出现的次数
LINE* p = head;
int count = 0;
int len1 = 0; //保存当前行的总字符数
int len2 = strlen(sch); //待统计字符串的长度
int i, j, k;
do
{
len1 = strlen(p->data);//当前行的字符数
for (i = 0; i < len1; i++)
{
if (p->data[i] == sch[0])
{
k = 0;
for (j = 0; j <= len2 - 1; j++)
if (p->data[i + j] == sch[j])k = k + 1;
if (k == len2) { count++; i = i + k - 1; }
}
}
} while ((p = p->next) != NULL); //遍历链表
return count;
}
void del_string_word(char* s, char* sch)
{
char* p = strstr(s, sch); //查询结果
char tmp[80]{};
int len = strlen(s);
int i = len - strlen(p);
p __notnull;
int j = i + strlen(sch);
int count = 0;
for (int k = 0; k < i; k++)tmp[count++] = s[k];
for (int kk = j; kk < len; kk++)tmp[count++] = s[kk];
tmp[count] = { 0 };
string(s,tmp);//返回新的字符串
}
void Del_String(LINE*& head, char* sch)
{ //删除指定的字符串
LINE* p = head;
do
{
while (strstr(p->data, sch) != NULL)del_string_word(p->data, sch);
} while ((p = p->next) != NULL); //遍历链表
}
void OutPutTxt(LINE*& head)
{ //向屏幕输出文章
LINE* p = head;
do
{
cout << p->data << endl;
}
while ((p = p->next) != NULL); //遍历 链表
}
void Tj(LINE*& head)
{ //统计
cout << "文章统计信息结果如下:" << endl;
cout << "英文字母数:" << Count_ZM(head) << endl;
cout << "空格数: " << Count_Space(head) << endl;
cout << "文章中共出现数字:" << Count_Num(head) << endl;
cout << "统计文章的总字数: " << Count_All_Word(head) << endl;
}
int main()
{
LINE* head = (LINE*)malloc(sizeof(LINE)); ////////////////////////////
int opt;
cout << "\n ***************************************请选择操作**************************" << endl;
cout << " 1、新建 ";
cout << "2、浏览 ";
cout << "3、统计 ";
cout << "4、串统计 ";
cout << "5、删除 ";
cout << "6、退出 " << endl;
cin >> opt;
while (1) {//////////////////////
if (opt != 1)
{
cout << "第一次请务必选择第1项,以输入文本以便操作" << endl;
opt = 0;
}
else
{
while (opt != 6) {
switch (opt) {
case 0: {
cout << "\n ***************************************请选择操作**************************" << endl;
cout << " 1、新建 ";
cout << "2、浏览 ";
cout << "3、统计 ";
cout << "4、串统计 ";
cout << "5、删除 ";
cout << "6、退出 " << endl;
cin >> opt;
if (opt != 1 && opt != 2 && opt != 3 && opt != 4 && opt != 5 && opt != 6)
{
cout << "error!不可识别选项!" << endl; exit(0);
}
break; }
case 1: {cout << "--------新建文本---------------" << endl;
cout << "请输入文本,每行最多输入80字符!(结束请按 Ctrl + E)" << endl;
CreateTXT(head);
cout << endl << "-------------------------------" << endl;
cout << "按回车调出主菜单……" << endl;
int n = getchar();
opt = 0; break; }
case 2: {cout << "--------浏览输入文本------------" << endl;
OutPutTxt(head);
cout << endl << "-------------------------------" << endl;
cout << "按回车调出主菜单……" << endl;
int n = getchar();
opt = 0; break; }
case 3: {cout << "-----------文本统计-------------" << endl;
Tj(head);
cout << endl << "-------------------------------" << endl;
cout << "按回车调出主菜单……" << endl;
int n = getchar();
opt = 0; break; }
case 4: {cout << "----请输入要统计的字符串-----" << endl;
char sch[20]{};
cin>>sch;
cout << endl;
cout << sch << "出现的次数为:" << Find_Word(head, sch) << endl;
cout << "-------------------------------" << endl;
cout << "按回车调出主菜单……" << endl;
int n = getchar(); opt = 0; break; }
case 5: {cout << "------字符串删除------------" << endl;
cout << "请输入要删除的某一字符串:" << endl;
char tmp_sch[20]{};
cin>>tmp_sch;
Del_String(head, tmp_sch); //删除指定字符
cout << "--------字符串 " << tmp_sch << " 被删除后---------" << endl;
OutPutTxt(head); //向屏幕输出 文章
cout << endl << "-------------------------------" << endl;
cout << "按回车调出主菜单……" << endl;
int n = getchar(); opt = 0; break; }
case 6:exit(0);
default: {
cout << "请输入1~6之间的数字!" << endl;
cout << "按回车调出主菜单……" << endl;
int n = getchar(); opt = 0; break; }
}
}
}
}
}
其余的代码还没细看,现在是能正常输出你需要的字符,原来不是输出乱码,你输出的是位置的内存数据
原来的问题,主要是你没有执行数据拷贝操作,没用的那行代码已经备注了,你看下就明白了
// 先尝试理解你的代码,设计 line 数据结构,是想每次存储一行?如果继续输入新的行,就换行保存
void CreateTXT(LINE* head)
{
// 代码的 main 入口已经给 head 分配了空间,还没使用呢,不需要重新分配
LINE* p = head;
char tmp[80];
while (1)
{
// 输入字符串,每次读入一行数据,你这个你可以试试随便输入,只要结尾不是 ctrl E 你的代码会一直死循环在这里
cin >> tmp;
if (tmp[0] == 5)break; //如果发现输入 ^E,则退出输入
p->data = new char[strlen(tmp) + 1];//为结点分配空间
// 这行代码?实际没用诶
size_t(p->data);
// 如果你想要拷贝数据
memcpy(p->data, tmp, strlen(tmp));
if (tmp[strlen(tmp) - 1] == 5) { //除去最后一个控制符 ^E
p->data[strlen(tmp) - 1] = '\0';
break;
}
}
p->next = NULL; //是最后的一个指针为空。
}
什么编译器编写的,我用codeblocks和dev都不能通过运行。。。