成都工业学院数据结构与算法(DSA)实验一:顺序表

写在前面

1、基于2021级计算机类实验指导书

2、代码仅提供参考

3、如果代码不满足你的要求,请寻求其他的途径

运行环境

window11家庭版

CLion 2023.2.2

实验要求、源代码和运行结果

1、建立一个顺序表,随机产生10个100以内的整数,并按要求完成:

(1)在屏幕上显示顺序表中的10个整数;

(2)把值为b的元素插入到值为a的元素后面,若不存在a,则把b插入到表尾,显示更新后的顺序表;

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

// 自定义顺序表结构体
struct SeqList {
    vector<int> data; // 存放整数的数组
    int length; // 当前长度

    // 构造函数
    SeqList() : data(vector<int>(10)), length(0) {}
};

// 在屏幕上显示顺序表中的整数
void displaySeqList(const SeqList& seqList) {
    for (int i = 0; i < seqList.length; i++) {
        cout << seqList.data[i] << " ";
    }
    cout << endl;
}

// 将值为b的元素插入到值为a的元素后面或表尾
void insertElement(SeqList& seqList, int a, int b) {
    int insertIndex = -1;
    for (int i = 0; i < seqList.length; i++) {
        if (seqList.data[i] == a) {
            insertIndex = i + 1; // 插入位置在元素a的后面
            break;
        }
    }

    if (insertIndex == -1) {
        insertIndex = seqList.length; // 若不存在元素a,则插入到表尾
    }

    seqList.data.insert(seqList.data.begin() + insertIndex, b); // 插入元素b
    seqList.length++; // 更新长度
}

int main() {
    srand(static_cast<unsigned int>(time(0))); // 设置随机种子

    SeqList seqList;

    // 随机产生10个100以内的整数并存入顺序表
    for (int i = 0; i < 10; i++) {
        int randomNum = rand() % 100 + 1; // 生成1到100之间的随机整数
        seqList.data[i] = randomNum;
        seqList.length++;
    }

    // 显示顺序表中的整数
    cout<<"更新前的顺序表:"<<endl;
    displaySeqList(seqList);

    int a, b;
    cout << "请输入需要插入位置前的数字:"<<endl;
    cin >> a;
    cout << "请输入需要插入的数据:"<<endl;
    cin >> b;

    // 将值为b的元素插入到值为a的元素后面或表尾
    insertElement(seqList, a, b);

    // 显示更新后的顺序表
    cout << "更新后的顺序表:"<<endl;
    displaySeqList(seqList);

    return 0;
}

2、已知A、B分别是m、n位(位数为小于30的随机正整数)的随机正整数,编写程序给出这两个正整数相加的结果。如:A=47858628539074,B= 212821064467,C=A+B=48071449603541

#include <iostream>
#include <vector>

using namespace std;

// 将大整数按位存储到顺序表中
vector<int> convertToSeq(string num)
{
    vector<int> seq;
    for (int i = num.length() - 1; i >= 0; i--) {
        seq.push_back(num[i] - '0');
    }
    return seq;
}

// 大整数相加
string addLargeIntegers(vector<int>& seqA, vector<int>& seqB)
{
    int carry = 0;
    int lenA = seqA.size();
    int lenB = seqB.size();
    int lenC = max(lenA, lenB);

    vector<int> seqC(lenC, 0);

    for (int i = 0; i < lenC; i++) {
        int sum = carry;
        if (i < lenA) {
            sum += seqA[i];
        }
        if (i < lenB) {
            sum += seqB[i];
        }

        seqC[i] = sum % 10;
        carry = sum / 10;
    }

    if (carry > 0) {
        seqC.push_back(carry);
    }

    string result;
    for (int i = seqC.size() - 1; i >= 0; i--) {
        result += to_string(seqC[i]);
    }

    return result;
}

int main()
{
    string strA, strB;
    cout << "请输入A的值:"<<endl;
    cin >> strA;
    cout << "请输入B的值:"<<endl;
    cin >> strB;

    vector<int> seqA = convertToSeq(strA);
    vector<int> seqB = convertToSeq(strB);

    string result = addLargeIntegers(seqA, seqB);
    cout <<"运算结果为:"<<endl;
    cout <<"C=A+B="<< result << endl;

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值