📄 main.cpp
字号:
#include "iostream.h"
#include "Checker.h"
#include "NumericChecker.h"
#include "NameChecker.h"
#include "CNoChecker.h"
#include "Product.h"
#include "math.h"
#include "string.h"
#include "fstream.h"
int strchr(const char *charSet,const char ch);
int atoi(const char *str);
void main()
{
ProductNode pn[1024];//该数组用于按顺序存储商品结点
char name[30];char num[30];char count[30];
int i=0,j=0;
/*以下是文件读取并对相关信息予以处理*/
ifstream fin;
fin.open("data.txt");
while(fin.good() && !fin.eof())
{
fin>>name>>num>>count;//分别从文件中读取相关信息,赋值给商品名称,编号和数量
ProductNode pntemp(name,num,count,i);
pn[i]=pntemp;
i++;
}
int pncount=i;
fin.close();
/*以下是对相关信息进行的检查*/
CCheck *pBase;
CNameChecker Name_checker;
CNoChecker No_checker;
CNumericChecker Num_checker;
int wrongPos[1024];//该数组用于记录出现错误信息的结点的位置
int a,b,c;
for(i=0,j=0;i<pncount;i++)
{
pBase=&Name_checker;
a=pBase->StringValid(pn[i].Name);//体现了多态
pBase=&No_checker;
b=pBase->StringValid(pn[i].No);
pBase=&Num_checker;
c=pBase->StringValid(pn[i].Numeric);
if((!a)||(!b)||(!c))
{wrongPos[j]=i;j++;}
}
int wrongCnt=j;//计算出错误的商品信息个数
/*以下是对"out.txt"文件的输出并在屏幕上打印出出错信息的具体详情*/
ofstream fout;
fout.open("out.txt");
if(fout.is_open())
{ cout<<"错误的商品信息如下:"<<endl;
for(i=0;i<=wrongCnt;i++)
{
fout<<pn[wrongPos[i]].GetName()<<' '
<<pn[wrongPos[i]].GetNo()<<' '
<<pn[wrongPos[i]].GetNumeric()<<endl;
cout<<"------------第"<<wrongPos[i]+1<<"条------------"<<endl;
cout<<pn[wrongPos[i]].GetName()<<' '
<<pn[wrongPos[i]].GetNo()<<' '
<<pn[wrongPos[i]].GetNumeric()<<endl;
pBase=&Name_checker;//体现了多态
a=pBase->StringValid(pn[wrongPos[i]].GetName());
pBase=&No_checker;
b=pBase->StringValid(pn[wrongPos[i]].GetNo());
pBase=&Num_checker;
c=pBase->StringValid(pn[wrongPos[i]].GetNumeric());
if(!a)cout<<"商品名称错误!"<<endl;
if(!b)cout<<"商品编号错误!"<<endl;
if(!c)cout<<"商品数量错误!"<<endl;
}
fout.close();
}
else
{
cout<<"不能打开文件!"<<endl;
}
}
// 检查输入字符的有效性
int strchr(const char *charSet,const char ch)
{
for(int i=0;*(charSet+i)!='\0';i++)
if(*(charSet+i)==ch) return 1;
return 0;
}
// 将数字字符转换成相应的十进制数.
int atoi(const char *str)
{
int length=strlen(str);
int value=0;
for(int i=0; *(str+i)!='\0';i++)
value=value+(*(str+i)-'0')*(int)pow(10,length-1-i);
return value;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -