C++二级总结优质大题

【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;
}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值