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; }
相关文章
- C++学习之路——函数重载和运算符重载
- c++中map遍历_怎么遍历map集合
- EasyC++16,指针初探(二)
- EasyC++48,内部链接性和无链接性
- c++语言截取字符串,详解C++ string常用截取字符串方法
- c++实现简单的web服务器搭建
- c++获取子类窗口句柄位置_C++中各种获取窗口句柄的方法「建议收藏」
- C++简单有趣的恶搞问答关机程序
- LeetCode 704 二分查找 C++ 解法
- 深入理解C++11_c++ string char
- C++构造函数的作用_c++什么是构造函数
- C++ map遍历(简单易记忆)[通俗易懂]
- C++内存模型,我们常说的堆栈究竟指什么?
- C/C++ Qt 数据库与ComBox多级联动
- 【C++初阶】函数模板与类模板
- C++ 中文周刊 第100期
- c++基础篇之C++ 模板
- 一个简单的C++编写的u盘病毒代码详解编程语言
- C/C++中substr函数的应用(简单讲解)详解编程语言
- C++ lower_bound(STL lower_bound)二分查找算法详解
- 二分搜索算法(C++详解版)
- 编写一个简单的游戏来练习用 C++ 编程
- C与MySQL快速配置指南(c++ mysql 配置)
- 解决C++中重定义的方法总结
- C++之友元:友元函数和友元类详解
- c++冒泡排序示例分享
- C++实现顺序排序算法简单示例代码