C++简单笔试题8道

1.定义一个”数据类型” datatype类,能处理包含字符型、整型、浮点型三种类型的数据,给出其构造函数。

实现方式:

DataType.h

#pragma once
class DataType
{
public:
	enum
	{
		character,
		integer,
		float_point
	} vartype;

	union
	{
		char c;
		int i;
		float f;
	};

	DataType(char ch);
	DataType(int i);
	DataType(float f);
	~DataType(void);

	void print();
};

DataType.cpp

#include "DataType.h"
#include <iostream>
using namespace std;

DataType::DataType(char ch)
{
	vartype = character;
	c = ch;
}

DataType::DataType(int ii)
{
	vartype = integer;
	i = ii;
}

DataType::DataType(float ff)
{
	vartype = float_point;
	f = ff;
}


DataType::~DataType(void)
{
}

void DataType::print()
{
	switch (vartype)
	{
	case character:
		cout<<"字符型:"<<c<<endl;
		break;
	case integer:
		cout<<"整形:"<<i<<endl;
		break;
	case float_point:
		cout<<"浮点型:"<<f<<endl;
		break;
	}
}

main.cpp

#include <iostream>
#include "DataType.h"
using namespace std;

void main()
{
	DataType A('a'),B(1),C(2.0f);
	A.print();
	B.print();
	C.print();
}

2.用穷举法找出1~100间的质数,显示出来

#include <iostream>
#include <cmath>
using namespace std;

void main()
{
	int i = 1;
	while (i++ < 100)
	{
		int flag = 1;
		int k = sqrt(float(i));
		for (int j = 2; j <= k; j++)
		{
			if (i % j == 0)
			{
				flag = 0;
				break;
			}
		}

		if (flag)
		{
			cout<<i<<"是质数"<<endl;
		}
	}
}

3.在程序中定义一个整型变量,赋以1~100的值,要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。

第一种写法:

#include <iostream>
#include <cmath>
using namespace std;

void main()
{
	int n = 55;
	int m;
	cout<<"请输入你猜的数:";
	cin>>m;
	while (n != m)
	{
		if (n > m)
		{
			cout<<"你猜的数太小了"<<endl;
		}
		else if (n < m)
		{
			cout<<"你猜的数太大了"<<endl;
		}
		cout<<"请输入你猜的数:";
		cin>>m;
	}
	cout<<"恭喜你,猜对了!"<<endl;
}

第二种写法:

#include <iostream>
#include <cmath>
using namespace std;

void main()
{
	int n = 55;
	int m;
	do 
	{
		cout<<"请输入你猜的数:";
		cin>>m;
		if (n > m)
		{
			cout<<"您猜的数太小了"<<endl;
		}
		else if (n < m)
		{
			cout<<"您猜的数太大了"<<endl;
		}
		else
		{
			cout<<"恭喜你,猜对了!"<<endl;
		}
	} while (m != n);
}

4.编写函数求两个整数的最大公约数和最小公倍数。

#include <iostream>
#include <cmath>
using namespace std;

void main()
{
	int m,n;
	cout<<"请输入两个正整数:";
	cin>>m>>n;
	int A = 1;
	int B;
	int min = m < n ? m : n;
	for (int i = 2; i <= min; i++)
	{
		if (m % i == 0 && n % i == 0)
		{
			A = i;
		}
	}
	B = m * n / A;
	cout<<m<<"和"<<n<<"的最大公因子是"<<A<<",最小公倍数是"<<B<<endl;
}

5.编写递归函数GetPower(int x, int y)计算x的y次幂, 在主程序中实现输入输出。

#include <iostream>
#include <cmath>
using namespace std;

long GetPower(int x, int y);
void main()
{
	cout<<GetPower(2,7)<<endl;
}

long GetPower(int x, int y)
{
	if (y == 0)
	{
		return 1;
	}
	else
	{
		return x * GetPower(x, y-1);
	}
}

6 .定义一个矩形类,有长、宽两个属性,有成员函数计算矩形的面积

Rect.h

#pragma once
class Rect
{
private:
	float length;
	float width;
public:
	Rect(void);
	Rect(float len, float wid);
	float GetArea();
	float GetLength();
	float GetWidth();
	~Rect(void);
};

Rect.cpp

#include "Rect.h"
Rect::Rect(void)
{
}

Rect::Rect(float len, float wid)
{
	length = len;
	width = wid;
}

float Rect::GetArea()
{
	return length * width;
}

float Rect::GetLength()
{
	return length;
}

float Rect::GetWidth()
{
	return width;
}

Rect::~Rect(void)
{
}

main.cpp

#include <iostream>
#include "Rect.h"
using namespace std;

void main()
{
	Rect r(1.2,1.5);
	cout<<r.GetArea()<<endl;
}

7.编写一个函数,统计一个英文句子中字母的个数,在主程序中实现输入输出

#include <iostream>
using namespace std;

void main()
{
	char ch[100];
	cin.getline(ch,100);
	int count = 0;
	for (int i = 0; i < strlen(ch); i++)
	{
		if ((ch[i] >= 'A' && ch[i] <= 'Z') || (ch[i] >= 'a' && ch[i] <= 'z'))
		{
			count++;
		}
	}
	cout<<"输入的句子中含有字母的个数为:"<<count<<endl;
}


8.编写函数int index(char *s, char *t),返回字符串t 在字符串s中出现的最左边的位置,如果在s中没有与t匹配的子串,就返回-1。

int index(char* s, char* t)
{
	int i,j,k;
	for (i = 0; s[i] != '\0'; i++)
	{
		for (j = 0, k = i; t[j] != '\0' && s[k] == t[j]; k++,j++);
		if (t[j] == '\0')
		{
			return i;
		}
	}
	return -1;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值