本程序使用了链表的知识,目的是根据词性翻译英文意思。代码需要大幅度优化,但是本人能力有限,还请各位多多指教。
先看效果,在上代码。开发环境(vs2017)。(我老大布置的作业)可以从这里下载https://download.csdn.net/my
词库
主文件
/********************************************************************
** 文 件 名:翻译
** 创 建 人:yk
** 最后修改时间:2018年9月10日
*********************************************************************/
#include "stdafx.h"
#include"line.h"
#include"openfile.h"
#include "Translate.h"
#include<iostream>
void input(WORDS *str, words *& daTe); //将键入信息存入结构体并翻译
int main()
{
date *DaT = NULL, *DaT1 = NULL, *DaT2 = NULL;
FiLe fILE;
lines LINE,LINe;
WORDS str[200] = {0};
words* daTe = NULL,*dae=NULL,*again=NULL,*jkl=NULL;
coloR(2);
std::cout << "Loading................0%" << std::endl;
coloR(16);
DaT = fILE.Fileread(1);
DaT1 = fILE.Fileread(2);
DaT2 = fILE.Fileread(3); //读取数据
coloR(2);
std::cout << "Loading................50%" << std::endl;
coloR(16);
LINE.LOOKUP(daTe, DaT2);//向链表中存数据
coloR(2);
std::cout << "Loading................80%" << std::endl;
coloR(16);
LINE.AddSon(daTe, DaT2, DaT,DaT1);
coloR(2);
std::cout << "Data read completion................100%" << std::endl;
coloR(16);
while(1)
{
coloR(5);
std::cout << "[Please input the content you want to translate.]" << std::endl;
coloR(16);
scanf("%[^\n]", str);
setbuf(stdin, NULL);//清空缓冲区
coloR(1);
input(str, daTe);//翻译
coloR(16);
}
system("pause");
return 0;
}
void input(WORDS * str,words *& daTe)
{
Trancelate english;
date *Trance = NULL, *NOW = NULL, *Head = NULL;
Trance= (date *)malloc(sizeof(date));
Head = Trance;
for (NUMBERS Position = 0, i = 0;; i++)
{
if (str[i] != ' ' && str[i] != '\0' && str[i] != '\n')
{
Position++;
}
else if (Position != 0 && (str[i] == ' ' || str[i] == '\0' || str[i] == '\n'))
{
NUMBERS NOWPOsition = 0;
Trance->wordss = (WORDS *)malloc(sizeof(WORDS)*(Position + 1));
for (NUMBERS ADposition = i - Position; ADposition < i; ADposition++)
{
Trance->wordss[NOWPOsition] = str[ADposition];
NOWPOsition++;
}
Trance->wordss[NOWPOsition] = '\0';
Trance->next = NULL;
NOW = Trance;
Trance = (struct date *)malloc(sizeof(struct date));
NOW->next = Trance;
Position = 0;
}
else if (str[i] == '\0' || str[i] == '\n')
{
NOW->next = NULL;
break;
}
}
english.Enginsh_TO_Chinese(daTe, Head);
}
line.h
#ifndef LINE_H
#define LINE_H
#include"stdafx.h"
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
struct words
{
WORDS * characteristic;
struct words* next;
struct words *as=NULL;
};
class lines
{
public:
NUMBERS line_push_back(struct words *& head, WORDS *str); //在末尾添加数据
NUMBERS print_line(words *& head); //打印数据
NUMBERS OR(WORDS* p, WORDS* q); //判断字符串,相同返回0,不相同返回非0
NUMBERS lineOR(words *&head, WORDS *q); //寻找相同内容,相同返回1,不相同返回0
EMPTY Creadteline(struct words *& head, WORDS *STr); //创建一个空链表
NUMBERS LOOKUP(struct words *& daTe, date *DaTE); //初次存入中链表
NUMBERS AddSon(words *& daTe, date * DaTE, date * DAT, date * DAT1);//添加子链的内容
NUMBERS sIze=0;
private:
struct words *Begin = NULL, *End = NULL;
NUMBERS line_push_back_double(struct words *& head, WORDS *str);
NUMBERS ADDlinemomchar(words *& liNE1, WORDS *sTR, WORDS *Str);
};
#endif // !LINE_H
line.cpp
#include"stdafx.h"
#include "line.h"
NUMBERS lines::OR(WORDS* p, WORDS* q)//判断字符串,相同返回0,不相同返回非0
{
NUMBERS xc = 0;
while ((!(xc = *p - *q)) && *p)
{
*q++;
*p++;
}
return xc;
}
NUMBERS lines::lineOR(words *& head, WORDS* q)//寻找相同内容,相同返回1,不相同返回0
{
words *Guochen = head;
while (Guochen != NULL)
{
if (!OR(Guochen->characteristic, q))
{
return 1;
}
Guochen = Guochen->next;
}
return 0;
}
EMPTY lines::Creadteline(struct words *& head, WORDS *STr)
{
head = (struct words *)malloc(sizeof(struct words));
head->characteristic = STr;
sIze++;
head->next = NULL;
head->as = NULL;
if (Begin == NULL)
{
Begin = head;
End = head;
}
}
NUMBERS lines::line_push_back(words *& head, WORDS *STr) //在末尾添加数据
{
if (*STr == '\0'||*STr=='\n')
{
return 1;
}
if (head == NULL)
{
Creadteline(head, STr);
}
else
{
struct words *guochen = head, *TO = NULL, *Guochen = NULL;
guochen = End;
TO= (struct words *)malloc(sizeof(struct words));
if (TO == NULL)return 1;
TO->characteristic = STr;
TO->next=NULL;
TO->as = NULL;
sIze++;
guochen->next = TO;
End = TO;
}
return 0;
}
NUMBERS lines::line_push_back_double(words *& head, WORDS * STr)
{
if (*STr == '\0')
{
return 1;
}
if (head == NULL)
{
Creadteline(head, STr);
}
else
{
struct words *guochen = head, *TO = NULL, *Guochen = NULL;
while(guochen->next!=NULL)
{
guochen = guochen->next;
}
TO = (struct words *)malloc(sizeof(struct words));
if (TO == NULL)return 1;
TO->characteristic = STr;
TO->next = NULL;
TO->as = NULL;
guochen->next = TO;
}
return 0;
}
NUMBERS lines::print_line(words *& head)
{
words *Guochen = head;
while (Guochen != NULL)
{
std::cout << ">>" << Guochen->characteristic << ">>";
Guochen = Guochen->next;
}
printf("\n");
return 0;
}
NUMBERS lines::LOOKUP(struct words *& daTe, date *DaTE)
{
for (NUMBERS i = 0;; i++)
{
if (DaTE->next != NULL)
{
if (daTe != NULL)
{
if (!lineOR(daTe, DaTE->wordss))//避免存入词的重复
{
line_push_back(daTe, DaTE->wordss);
}
DaTE = DaTE->next;
}
else
{
line_push_back(daTe, DaTE->wordss);
DaTE = DaTE->next;
}
}
else
{
break;
}
}
return 0;
}
NUMBERS lines::ADDlinemomchar(words *& liNE1,WORDS *sTR, WORDS *Str)
{
struct words* Add = liNE1;
while(Add!=NULL)
{
if(!OR(Add->characteristic,sTR))
{
if(!lineOR(Add->as, Str))
{
line_push_back_double(Add->as, Str);
return 0;
}
}
Add = Add->next;
}
return 1;
}
NUMBERS lines::AddSon(words *& daTe, date * DaTE, date * DAT, date * DAT1) //此处需优化!!!
{
date *Threesline = DAT1, *Twoline = DAT,*oneline=DaTE, *oneline1 = DaTE;
words * daTe1 = daTe;
words * daTe2 = daTe;
for (;;)
{
if (oneline->next != NULL)
{
ADDlinemomchar(daTe, oneline->wordss, Twoline->wordss);
oneline = oneline->next;
Twoline = Twoline->next;
}
else
{
break;
}
}
Twoline = DAT;
Threesline = DAT1;
for (;;)
{
if (daTe2 != NULL)
{
while (Threesline->next != NULL)
{
ADDlinemomchar(daTe2->as, Twoline->wordss, Threesline->wordss);
Twoline = Twoline->next;
Threesline = Threesline->next;
}
daTe2 = daTe2->next;
Threesline = DAT1;
Twoline = DAT;
}
else
{
break;
}
}
words * daTe7 = NULL;
words * daTemain = daTe;
words * daTe4 = daTemain->as;
words * daTe5 = daTe4->as;
words * daTemain1 = daTe;
Threesline = DAT1;
Twoline = DAT;
oneline = DaTE;
for (;;)
{
if (!OR(daTe4->characteristic, Twoline->wordss))
{
daTe5 = daTe4->as;
while (daTe5 != NULL)
{
if (!OR(daTe5->characteristic, Threesline->wordss))
{
oneline = oneline->next;
if(*oneline->wordss!='\n')
{
while (daTemain1 != NULL)
{
if (!OR(daTemain1->characteristic, oneline->wordss))
{
daTe5->as = daTemain1;
Threesline = DAT1;
Twoline = DAT;
oneline = DaTE;
if (daTe4 != NULL)
daTe4 = daTe4->next;
daTemain1 = daTe;
goto xc;
}
else
{
daTemain1 = daTemain1->next;
}
}
}
else
{
daTe5->as = NULL;
Threesline = DAT1;
Twoline = DAT;
oneline = DaTE;
daTe4 = daTe4->next;
daTemain1 = daTe;
break;
}
xc:daTe5 = daTe5->next;
}
else
{
if (oneline == NULL)
{
Threesline = DAT1;
Twoline = DAT;
oneline = DaTE;
}
Threesline = Threesline->next;
oneline = oneline->next;
}
}
}
else
{
if (oneline == NULL)
{
Threesline = DAT1;
Twoline = DAT;
oneline = DaTE;
}
oneline = oneline->next;
Twoline = Twoline->next;
Threesline = Threesline->next;
}
if (daTe4 == NULL)
{
if (daTemain->next == NULL)break;
daTemain = daTemain->next;
daTe4= daTemain->as;
}
}
return 0;
}
openfile.h
#pragma once
#include"stdafx.h"
#include<iostream>
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
#define MAX_NUMBEr 1024
class FiLe
{
public:
NUMBERS Filestart(FILE * &fp); //初始化文件读取功能
NUMBERS Filestop(FILE * &fp); //结束文件读取
date * Fileread(NUMBERS FLog); //读取文件
private:
};
openfile.cpp
#include"stdafx.h"
#include"openfile.h"
NUMBERS FiLe::Filestart(FILE * &fp)
{
fp = fopen("G:\\yk\\Desktop\\as.txt", "r");
if (fp == NULL)
{
return 1;
}
return 0;
}
NUMBERS FiLe::Filestop(FILE * &fp)
{
if (!(fclose(fp) ^ 0))
{
return 0;
}
return 1;
}
date * FiLe::Fileread(NUMBERS FLog)//获取数据
{
WORDS STR[1024], c[1024];
NUMBERS i = 0, l = 0, position = 0;
date *DaTE = NULL, *HEad = NULL, *NOW;
FILE *fp;
FiLe fIlE;
if (fIlE.Filestart(fp)) //打开文件
{
std::cout << "open faile" << std::endl;
system("pause");
exit(0);
}
DaTE = (date *)malloc(sizeof(date));
HEad = DaTE;
while (fgets(c, MAX_NUMBEr, fp)) {
i++;
if (i == 4)i = 1;
if (i== FLog)//只读FLog行的数据
{
strcpy(STR, c);//遇到空格换节点,遇到换行就读下个FLog行数据
for (NUMBERS k = 0; ; k++)
{
if (STR[k] != ' ' && STR[k] != '\n'&&STR[k] != '\0')
{
position++;
}
else if (position != 0 && (STR[k] == ' ' || STR[k] == '\0' || STR[k] == '\n'))//按需给予内存
{
NUMBERS NOWPOsition = 0;
DaTE->wordss = (WORDS *)malloc(sizeof(WORDS)*(position + 1));
for (NUMBERS ADposition = k - position; ADposition < k; ADposition++)
{
DaTE->wordss[NOWPOsition] = STR[ADposition];
NOWPOsition++;
}
DaTE->wordss[NOWPOsition] = '\0';
DaTE->next = NULL;
NOW = DaTE;
DaTE = (struct date *)malloc(sizeof(struct date));
NOW->next = DaTE;
position = 0;
}
else if (STR[k] == '\0' || STR[k] == '\n')
{
break;
}
}
DaTE->wordss = (WORDS *)malloc(sizeof(WORDS)*(2));
DaTE->wordss[0] ='\n';
DaTE->wordss[1] = '\0';
DaTE->next = NULL;
NOW = DaTE;
DaTE = (struct date *)malloc(sizeof(struct date));
NOW->next = DaTE;
DaTE->next = NULL;
}
}
if (fIlE.Filestop(fp))//关闭文件
{
std::cout << "open faile" << std::endl;
system("pause");
exit(0);
}
return HEad;
}
//NUMBERS FiLe::Fileread(date *DaTE,NUMBERS FLog)//获取数据
//{
//
//
//
//
//
// WORDS STR[1024], c[1024];
// NUMBERS i = 0, z = 0, l = 0;
// FILE *fp;
// if (Filestart(fp))
// {
// std::cout << "open faile" << std::endl;
// system("pause");
// exit(0);
// }
// while (fgets(c, MAX_NUMBEr, fp)) {
// i++;
//
// if (!(i % FLog))
// {
// strcpy(STR, c);
// for (NUMBERS j = 0;; j++)
// {
// if (STR[j] != ' ' && STR[j] != '\n')
// {
// DaTE[z].wordss[l] = STR[j];
// l++;
//
// }
// else if (STR[j] == ' ')
// {
// l = 0;
// z++;
//
// }
// else if (STR[j] == '\n')
// {
// l=0;
// break;
// }
// }
// }
//
//
// }
// if (Filestop(fp))
// {
// std::cout << "open faile" << std::endl;
// system("pause");
// exit(0);
// }
// return 0;
//}
translate.h
#pragma once
#include"stdafx.h"
#include"line.h"
class Trancelate
{
public:
EMPTY Chinese_TO_English(words *&linE,WORDS *str);
EMPTY Enginsh_TO_Chinese(words *&linE, date *&str); //翻译
private:
};
translate.cpp(没写汉译英)
#include "stdafx.h"
#include "Translate.h"
EMPTY Trancelate::Chinese_TO_English(words *& linE, WORDS * str)
{
words * linEstrat = linE;
return EMPTY();
}
EMPTY Trancelate::Enginsh_TO_Chinese(words *& linE,date *& str)
{
words * linEstrat = linE;
lines line_or;
words * linEguochen = linEstrat->as;
words * linEGuochen = linEguochen->as;
words * linEGUochen = linEGuochen->as;
date *ddF = str;
date *dDF = str;
if (str->wordss[0] == '\0')
{
coloR(4);
std::cout << "Error!!!" << std::endl;
coloR(16);
return;
}
while(linEstrat!=NULL)
{
linEguochen = linEstrat->as;
A:while (linEguochen != NULL)
{
if (!line_or.OR(linEguochen->characteristic, ddF->wordss))
{
linEGuochen = linEguochen->as;
C:if (linEGuochen->next == NULL)
{
B:printf("[%s]", linEGuochen->characteristic);
ddF = ddF->next;
if (linEGuochen->as == NULL || ddF == NULL)
{
printf("\n");
return;
}
linEguochen = linEGuochen->as->as;
goto A;
}
else
{
linEGUochen = linEGuochen->as->as;
dDF = ddF->next;
while (linEGUochen != NULL)
{
if (!line_or.OR(linEGUochen->characteristic, dDF->wordss))
{
goto B;
}
linEGUochen = linEGUochen->next;
}
linEGuochen = linEGuochen->next;
goto C;
}
}
linEguochen = linEguochen->next;
}
linEstrat = linEstrat->next;
}
coloR(4);
std::cout << "Error!!!" << std::endl;
coloR(16);
return;
}
stdax.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include"malloc.h"
#include <stdio.h>
#include <tchar.h>
#include"windows.h"
#include<string>
#include<iostream>
typedef char WORDS;
typedef int NUMBERS;
typedef bool JUDGE;
typedef void EMPTY;
void coloR(short x);
struct date
{
WORDS *wordss;
struct date *next;
};
// TODO: 在此处引用程序需要的其他头文件
stdafx.cpp
// stdafx.cpp : 只包括标准包含文件的源文件
// ConsoleApplication5.pch 将作为预编译标头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
void coloR(short x) //自定义函根据参数改变颜色
{
if (x >= 0 && x <= 15)//参数在0-15的范围颜色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); //只有一个参数,改变字体颜色
else//默认的颜色白色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
// TODO: 在 STDAFX.H 中引用任何所需的附加头文件,
//而不是在此文件中引用