1.定义一个”数据类型” datatype类,能处理包含字符型、整型、浮点型三种类型的数据,给出其构造函数。
#include <iostream>
using namespace std;
class datatype{
enum{
character,
integer,
floating_point
} vartype;
union
{
char c;
int i;
float f;
};
public:
datatype(char ch) {
vartype = character;
c = ch;
}
datatype(int ii) {
vartype = integer;
i = ii;
}
datatype(float ff) {
vartype = floating_point;
f = ff;
}
void print();
};
void datatype::print() {
switch (vartype) {
case character:
cout << "字符型: " << c << endl;
break;
case integer:
cout << "整型: " << i << endl;
break;
case floating_point:
cout << "浮点型: " << f << endl;
break;
}
}
void test() {
datatype A('c'), B(12), C(1.44F);
A.print();
B.print();
C.print();
}
知识点:联合体、枚举类型的使用。
联合体公用一个存储空间值得至于的是sizeof(union);枚举类型则要注意枚举类型的值默认从0开始,但是如果前面指定了值,后面没有指定的要挨个比前面大1。
2.用穷举法找出1~100间的质数,显示出来
质数是除了一和它本身之外,不能被其他数整除的正整数,又称素数。100以内的质数有:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
合数是除了质数以外的数,即除了一和它本身以外,还有其他的因数的正整数。
区别在于因数的个数,质数只有2个因数,合数有多于2个因数。
1既不是质数,也不是合数。
//n是否为素数
bool is_prime(int n)
{
if (n < 2) // 1既不是质数,也不是合数。
return false;
for (int i = 2; i*i <= n; i++)
if (n % i == 0)
return false;
return true;
}
void test()
{
int cnt = 0;
cout << "质数如下:" << endl;
for (int i = 2; i <= 100; i++){
if (is_prime(i)){
cnt++;
cout << i << "\t";
}
if (cnt % 10 == 0) cout << endl;
}
cout << endl;
}
结果:
质数如下:
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
请按任意键继续. . .
3.在程序中定义一个整型变量,赋以1~100的值,要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。
#include <ctime>
#include <iostream>
using namespace std;
void test(){
srand(time(NULL));
int ans = rand() % 100 + 1;
cout << "随机产生了一个数字 : " << ans << endl;
int input=-1;
while (ans != input){
cout << endl << "Now, Input a num : ";
cin >> input;
if (input < ans) cout << "你输入的数字小了点!" << endl;
if (input > ans) cout << "你输入的数字大了点!" << endl;
}
cout << endl << "恭喜你,猜对了!" << endl;
}
4.编写函数判断一个数是否是质数,在主程序中实现输入、输出。
同第(2)题;
5.编写函数求两个整数的最大公约数和最小公倍数。
这里利用辗转相除法(欧几里德算法 )求解,他有一个下面的定理:
定理:两个整数的最大公约数等于其中较小的那个数和两数相除余数的最大公约数。最大公约数(greatest common divisor)缩写为gcd。
gcd(a,b)=gcd(b,a%b) (a>b)证明:
若 a>b ,则a可以写作b的线性表示:
a=kb+r,r=a%b
也就是,
r=a−kb
若d是a和b的最大公约数,很显然 rd=a−kbd=m ,m是一个整数,也就是余数r也是可以被这个最大公约数所整除的,证毕。
好了,最小公倍数就是两数相乘再除以最大公约数,实现如下:
// 求a,b的最大公约数
int gcd(int a, int b){
// 保证a>b
if (a < b){
int temp = a;
a = b;
b = temp;
}
// 递归终止
if (b == 0) return a;
// 递归过程:辗转相除
return gcd(b, a%b);
}
void test(){
int m = 50, n = 135;
// 最大公约数
cout << gcd(m, n) << endl;
// 最小公倍数
cout << (m*n / gcd(m, n)) << endl;
}
6.编写递归函数计算x的y次幂, 在主程序中实现输入输出。
double myPow(double x, int n){
// 递归终止
if (n == 0 || x == 1.0) return 1.0;
if (n == 1) return x;
// 解决溢出
if (n == INT_MIN) return myPow(x, INT_MAX)*x;
// 处理为负的情况
if (n < 0) return myPow(x, -n);
// n>0,递归过程
double half = myPow(x, n / 2);
return half*half*myPow(x, n % 2);
}
注意:这里递归终止条件必须考虑0和1的情况,至于x=1.0的时候是为了在这种情况下能够有所加速。
8.编写一个函数,统计一个英文句子中字母的个数。
int numOfLetter(char *str){
int res = 0;
while (*str){
if (*str >= 'a' && *str <= 'z' || *str >= 'A' && *str <= 'Z')
res++;
str++;
}
return res;
}
10.编写函数int index(char *s, char *t),返回字符串t 在字符串s中出现的最左边的位置,如果在s中没有与t匹配的子串,就返回-1。
int index(char *s, char *t){
int loc = -1;
int i = 0;
// s中查找与t匹配的位置
while (s[i]){
int k = 0;
for (int j = i; s[j] && t[k] && s[j] == t[k]; j++, k++);
// 找到了
if (t[k] == '\0') {
loc = i;
break;
}
i++;
}
return loc;
}
void test(){
char s[100] = "wasscgas";
char t[10] = "ssc";
cout << index(s,t) << endl;
}
11.编写函数reverse(char *s)的倒序递归程序,使字符串s倒序。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void reverseStr(string &str){
for (int i = 0; i < str.length() / 2; i++){
swap(str[i], str[str.length() - i - 1]);
}
}
void reverse(char *s, char *t){
// 递归终止:只有一个字符,或者超过
if (s >= t)
return;
// 递归过程:先交换首尾,在向中间交换
char c = *s;
*s = *t;
*t = c;
reverse(++s, --t);
}
// 反转一个字符串
void reverse(char *s){
reverse(s, s + strlen(s) - 1);
}
void test(){
char s[10] = "aswfasdw";
reverse(s);
string str = "wsdak";
reverseStr(str);
cout << s << endl;
cout << str << endl;
}
结果:
wdsafwsa
kadsw
请按任意键继续. . .