64,683
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include "iostream"
#include "fstream"
using namespace std;
#define INITSIZE 100
#define LISTINCREMENT 100
#define MIDSTRLEN 30
//typedef char ElemType
struct sqList
{
int length;
char* elem;
int listsize;
};
void InitList(sqList* L)
{
L->elem = (char*)malloc(sizeof(char)*INITSIZE);
memset(L->elem, 0, INITSIZE);
L->length = 0;
L->listsize = INITSIZE;
}
void GetLen(sqList* L)
{
for (int i = 0; i < (L->listsize); i++)
{
if (*(L->elem + i) != '\0')
{
++(L->length);
}
}
}
char GetElem(sqList L, int i)
{
char e;
e = *(L.elem + i - 1);
return e;
}
void ListInsert(sqList* L, char e)
{
if (L->length >= L->listsize)
{
char* newbase;
newbase = (char*)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(char));
if (newbase == NULL)
{
cout <<"Error when alloting memory!" <<endl;
exit(0);
}
L->elem = newbase;
L->listsize = L->listsize + LISTINCREMENT;
}
char* p;
p = L->elem + L->length;
*p = e;
++(L->length);
}
void main (int argc, char* argv[])
{
sqList la, lb;
char str1[MIDSTRLEN];
char str2[MIDSTRLEN];
InitList(&la);
InitList(&lb);
ifstream infile("input.txt");
if (!infile)
{
cout <<"input.txt can't be opened!" <<endl;
system("pause");
exit(0);
}
infile.getline(str1, sizeof(str1));
infile.getline(str2, sizeof(str2));
char* pElem =la.elem;
for (int i = 0; i <= sizeof(str1); i++)
{
if(str1[i] != ' ' && str1[i] != '\0')
{
// *(la.elem) = str1[i] - 48;
// ++(la.elem);
*pElem = str1[i] - 48;
pElem++;
}
}
char* pLb_elem = lb.elem;
for (int m = 0; m <= sizeof(str2); m++)
{
//if(str2[m] != '\32' || str2[m] != '\0')
if(str1[m] != ' ' && str1[m] != '\0')
{
//*(lb.elem) = str2[m];
//++(lb.elem);
*pLb_elem = str2[m] - 48;
pLb_elem++;
}
}
infile.close();
GetLen(&la);
GetLen(&lb);
for (int n = 1; n < lb.length; n++)
{
char e;
e = GetElem(lb, n);
for (int j = 0; j < la.length; j++)
{
if (*(la.elem + j) == e)
{
ListInsert(&la, e);
}
}
}
ofstream outfile("output.txt");
for (int k = 0; k < 2*(la.length); k++)
{
outfile <<(*(la.elem + k)) << " " << endl;
}
outfile.close();
system("pause");
}