zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++简单笔试题8道

C++ 简单 笔试
2023-09-14 09:01:05 时间

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;


}

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;

 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;

}