C++笔试测试用例构造


前言

提示:这里可以添加本文要记录的大概内容:

此文用来记录C++笔记时常见的测试用例的输入


提示:以下是本篇文章正文内容,下面案例可供参考

一、从键盘输入一行数据并存储到数组中

1、使用cin

提示:用空格隔开,或者用逗号(,)隔开都可以
比如:输入:1 2 3 4 5, 输出:1 2 3 4 5
比如:输入:1,2,3,4,5, 输出:1 2 3 4 5

代码如下(示例):

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int num;
    vector<int> nums;
    while(cin >> num){
        nums.push_back(num);
        if(getchar()=='\n') break;
    }
    for(auto n : nums){
        cout<<n<<" ";
    }
    cout<<endl;
    
    return 0;
}

2、使用stringstream

提示:用空格隔开
比如:输入:1 2 3 4 5, 输出:1 2 3 4 5
代码如下(示例):

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string str;
    getline(cin,str);
    istringstream in_str(str);
    vector<int> nums;
    int num;
    while(in_str>>num){
        nums.push_back(num);
    }
    for(int i = 0; i < nums.size(); i++){
        cout<<nums[i]<<" ";
    }
    cout<<endl;
    
    return 0;

提示:用逗号(,)隔开
比如:输入:1, 2, 3, 4, 5, 输出:1 2 3 4 5
代码如下(示例):

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string str;
    getline(cin, str);
    stringstream ss(str);

    vector<int> nums;
    char del = ',';// 分割符号为,
    string tmp;
    while (getline(ss, tmp, del)) {
        nums.push_back(stoi(tmp));
    }
    for(int i = 0; i < nums.size(); i++){
        cout<<nums[i]<<" ";
    }
    cout<<endl;
    
    return 0;

3、注意事项

1、cin.getline()属于istream流,而getline()属于string流,是不一样的两个函数
2、当同时使用cin>>,getline()时,需要注意的是,在cin>>输入流完成之后,getline()之前,需要通过

str="\n";
getline(cin,str);

的方式将回车符作为输入流cin以清除缓存,如果不这样做的话,在控制台上就不会出现getline()的输入提示,而直接跳过,因为程序默认地将之前的变量作为输入流。

3、cin.getline()实际上有三个参数,cin.getline(接收字符串的变量,接收字符个数,结束字符). 当第三个参数省略时,系统默认为’\0’。 例如cin.getline(ss, 5, ‘a’);当输入ccdadv时,输出ccd


二、用数组构建链表,二叉树

1、构建链表

1、从键盘输入,得到数组;
2、用数组构建链表。

代码如下(示例):

#include <bits/stdc++.h>
using namespace std;

struct ListNode{
    int val;
    ListNode* next;
    ListNode() : val(0), next(NULL) {}
    ListNode(int x) : val(x), next(NULL) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}

};

// 数组转换为链表,返回链表的头节点
ListNode* creatList(vector<int>& nums){
    if(nums.size() == 0) return NULL;

    ListNode* head = new ListNode(nums[0]);
    ListNode* curNode = head;

    for(int i = 1; i < nums.size(); i++){
        curNode->next = new ListNode(nums[i]);
        curNode = curNode->next;
    }
    return head;
}

int main()
{
    vector<int> nums;
    int num;
    while(cin >> num){
        nums.push_back(num);
        if(cin.get() == '\n') break;
    }
    ListNode* head = creatList(nums);

	/*处理链表的其他逻辑*/

	//输出链表
    while(head){
        cout<<head->val<<"->";
        head1 = head1->next;
    }
    return 0;
}

2、构建二叉树

1、从键盘输入,得到数组;
2、用数组构建二叉树。
代码如下(示例):

#include <bits/stdc++.h>
using namespace std;

struct TreeNode {
    int val; 
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

// 根据数组构造二叉树,返回根节点
TreeNode* construct_binary_tree(const vector<int>& vec) {
    vector<TreeNode*> vecTree (vec.size(), NULL);
    TreeNode* root = NULL;
    for (int i = 0; i < vec.size(); i++) {
        TreeNode* node = NULL;
        if (vec[i] != -1) node = new TreeNode(vec[i]);
        vecTree[i] = node;
        if (i == 0) root = node;
    }
    for (int i = 0; i * 2 + 2 < vec.size(); i++) {
        if (vecTree[i] != NULL) {
            vecTree[i]->left = vecTree[i * 2 + 1];
            vecTree[i]->right = vecTree[i * 2 + 2];
        }
    }
    return root;
}

// 层序打印打印二叉树
void print_binary_tree(TreeNode* root) {
    queue<TreeNode*> que;
    if (root != NULL) que.push(root);
    vector<vector<int> > result;
    while (!que.empty()) {
        int size = que.size();
        vector<int> vec;
        for (int i = 0; i < size; i++) {
            TreeNode* node = que.front();
            que.pop();
            if (node != NULL) {
                vec.push_back(node->val);
                que.push(node->left);
                que.push(node->right);
            }
            // 这里的处理逻辑是为了把null节点打印出来,用-1 表示null
            else vec.push_back(-1);
        }
        result.push_back(vec);
    }
    for (int i = 0; i < result.size(); i++) {
        for (int j = 0; j < result[i].size(); j++) {
            cout << result[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    // 注意本代码没有考虑输入异常数据的情况
    // 用 -1 来表示null
  
    vector<int> vec;
    int num;
    while(cin >> num){
        vec.push_back(num);
        if(cin.get() == '\n') break;
    }
    
    TreeNode* root = construct_binary_tree(vec);
    print_binary_tree(root);
}

构建二叉树的代码参考代码随想录


三 、构造m*n的二维矩阵

1、从键盘输入,构造二维矩阵

提示:用空格隔开
代码如下(示例):

#include <bits/stdc++.h>
using namespace std;

int main(){
    // 输入一个m*n的矩阵
    int m, n, temp;
    cin>>m;
    cin>>n;
    vector<vector<int> > matrix(m, vector<int>(n));
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cin >> temp;
            matrix[i][j] = temp;
        }
    }
    // 输出m*n的矩阵
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout<<matrix[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
测试用例模板是指在进行软件测试时为了达到特定测试目标而编的一套规范化的模板,通常包含了测试用例的标题、前提条件、步骤描述、期望结果以及实际结果等内容。测试用例模板可以帮助测试人员系统地规划和编测试用例,确保测试工作的准确性和完整性。 测试用例模板一般包括以下几个部分: 1. 标题:用于简要描述测试用例的目标或功能。 2. 前提条件:指测试用例执行前需要满足的条件或假设,用于保证测试用例执行的准确性和可靠性。 3. 测试步骤:具体描述每个测试用例的执行步骤,包括输入数据、操作行为等。 4. 期望结果:描述在执行测试步骤后预期的结果是什么。 5. 实际结果:记录测试人员在执行测试步骤后观察到的实际结果。 通过使用测试用例模板,测试人员能够更加系统地规划和编测试用例,从而确保测试工作的全面性和准确性。测试用例模板可以帮助测试人员更好地组织和管理测试用例,提高测试工作的效率和质量。 例如,假设我们要测试一个登录功能,测试用例模板可以如下所示: 标题:登录功能测试 前提条件:已部署好登录页面,并录入测试账号和密码。 测试步骤: 1. 打开登录页面。 2. 输入有效的账号和密码。 3. 点击登录按钮。 期望结果:成功登录到系统首页。 实际结果:登录成功,页面跳转到系统首页。 通过编和执行测试用例模板,我们能够更加全面地验证软件功能的正确性和合理性,找出潜在的问题和改进空间,从而提升软件质量和用户体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值