#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<bitset>
#include<fstream>
#include<sstream>
using namespace std;
/*
程序设计题。动态数组。
有未知长度的字符串,有大小为N的空间,用户输入字符串并将字符串存入空间中。
第一次存入编号为1,第二次存入编号为2,以此类推。
要求
1)写出存储字符串的接口,返回字符串编号;
2)写出根据字符串编号读取字符串的接口。
*/
#define N 10000
char space[N];
int current_index;
vector<int> index;
int StoreAndGetStringNumber(char* ch) {
int count = 0;
while (*ch != '\0') {
space[current_index++] = *ch++;
count++;
}
index.push_back(count);
return index.size() - 1;
}
char* GetString(int number) {
int offset = 0;
for (int i = 0; i != number; i++)
offset += index[i];
char* ch = new char[index[number] + 1];
ch[index[number]] = '\0';
for (int i = 0; i != index[number]; i++)
//cout << space[i];
ch[i] = space[i + offset];
return ch;
}
int main()
{
char p1[] = "111111111111";
char p2[] = "222222";
char p3[] = "333";
StoreAndGetStringNumber(p1);
StoreAndGetStringNumber(p2);
StoreAndGetStringNumber(p3);
char* c = GetString(2);//此处选择下标
for (int i = 0; c[i] != '\0'; i++)
cout << c[i];
}
1)写出存储字符串的接口,返回字符串编号; 2)写出根据字符串编号读取字符串的接口。
最新推荐文章于 2022-07-01 20:55:59 发布