【1】此工程有一个源程序,函数char *GetNum(char *src, char *buf)从src开始扫描下一个数字字符系列,并将其作为一个字符串取出放入字符串空间buf 中。函数返回扫描的终止位置,如果返回NULL表示没有扫到数字字符序列。
例如输入:1R7Y8P9O66时,输出的数表示为Digit string1 is 1;Digit string2 is 7;Digit string3 is 8;等
代码如下:
//proj2.cpp
#include<iostream>
using namespace std;
char *GetNum(char *src, char *buf)
{
while(*src!='\0')
{
if(isdigit(*src)) break;
src++;
}
if(*src=='\0')
//********found********
return NULL;
while(*src!='\0' && isdigit(*src))
{
//********found********
*buf=*src;
buf++;
src++;
}
*buf='\0';
return src;
}
int main()
{
char str[100], digits[20];
cin.getline(str,100);
char *p=str;
int i=1;
while((p=GetNum(p, digits))!=NULL)
{
cout<<"Digit string "<<i<<" is "<<digits<<endl;
//********found********
i++;
}
while(1);
return 0;
}
[2]工程中含有论文main.cpp,其中类Graphics("图形"),Squares(“正方形”)、Diamods(“菱形”)的定义和主函数main的定义。当输入数值为3时,就会出现自定义的菱形和正方形。
代码如下:
#include <iostream>
#include <iomanip>
using namespace std;
class Graphics //图形类
{
public:
Graphics(int e):edges(e) { }
//**********found**********
virtual void Draw()=0;
protected:
int edges;
};
class Squares : public Graphics //正方形类
{
public:
Squares(int x):Graphics(x) { }
void Draw();
};
void Squares::Draw()
{
int i,j;
if(edges<=0)
cout<<"errors"<<endl;
if(edges>0)
{
for(i=0;i<edges;i++)
{
for(j=0;j<edges;j++)
cout<<setw(2)<<'*';
cout<<endl;
}
}
}
//**********found**********
class Diamonds : public Graphics //菱形类
{
public:
Diamonds(int x):Graphics(x) { }
void Draw();
};
void Diamonds::Draw()
{
int i,j;
if(edges<=0)
cout<<"errors"<<endl;
if(edges>0)
{
for(i=0;i<edges;i++) //输出菱形的上半部分
{
cout<<setw(edges-i);
//**********found**********
for(j=0;j<2*i+1;j++)
cout<<'*';
cout<<endl;
}
//**********found********** //输出菱形的下半部分
for (i = edges; i > 0;i--)
{
cout<<setw(edges-i+1);
for(j=0;j<=2*(i-1);j++)
cout<<'*';
cout<<endl;
}
}
}
int main()
{
int e;
cout<<"请输入表示边长的整数:";
cin>>e;
Graphics *objs[2];
objs[0]=new Diamonds(e);
objs[1]=new Squares(e);
for(int i=0;i<2;i++)
objs[i]->Draw();
delete objs[0];
delete objs[1];
return 0;
}