icoding的数据结构并没有一个测试代码,其都是直接编写一个函数的形式,因此很难知道自己的实际输出是什么。针对部分题目,我编写了一系列测试代码以供大家进行数据输出的测试。
请先将你的代码复制到
bool blstr_substr(Graph *G, VertexType v){ //TODO }
里面
到函数中然后修改main函数完成测试样例的输入
在main代码中间有个修改提示,请按照提示操作修改即可
部分代码来自白zj老师
main函数中一共有一个区域可以进行修改
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define BLOCK_SIZE 4 // 可由用户定义的块大小
#define BLS_BLANK '#' // 用于空白处的补齐字符
typedef struct _block {
char ch[BLOCK_SIZE]; //块的数据域
struct _block *next; //块的指针域
} Block;
typedef struct {
Block *head; // 串的头指针
Block *tail; // 串的尾指针
int len; // 串的当前长度
} BLString;
//字符串初始化函数:
void blstr_init(BLString *T) {
T->len = 0;
T->head = NULL;
T->tail = NULL;
}
bool blstr_substr(BLString src, int pos, int len, BLString* sub)
{
//TODO
}
//将字符串转化为块链串
void blstr_to_blstr(BLString *T, char *str) {
int i, j, len = strlen(str);
Block *p, *q;
p = (Block*)malloc(sizeof(Block));
p->next = NULL;
q = p;
for (i = 0; i < len; i++) {
if (i % BLOCK_SIZE == 0) {
q->next = (Block*)malloc(sizeof(Block));
q = q->next;
}
q->ch[i % BLOCK_SIZE] = str[i];
}
while (i % BLOCK_SIZE != 0) {
q->ch[i % BLOCK_SIZE] = BLS_BLANK;
i++;
}
T->head = p;
T->tail = q;
T->len = i;
}
//打印块链串
void blstr_print(BLString T) {
Block *p;
p = T.head;
if(p==NULL){
printf("块链串为空\n");
return;
}
while (p != NULL) {
for (int i = 0; i < BLOCK_SIZE; i++) {
printf("%c", p->ch[i]);
}
printf(" | ");
p = p->next;
}
printf("\n");
}
int main(){
//----------以下内容可以修改------------
char str[100]="Hello world";//原始的字符串(程序会自动将其转化为块链串)
//允许使用空格。空格会在后方自动转化为’#‘
int pos=1;//子串的起始位置
int len=5;//子串的长度
//在这个icoding中,我们一般认为head即开始存储了字符串的第一个字符。即head有值
bool choice=true;
//是否进行子串的查找并存储到sub(即运行blstr_substr函数)? true:进行 false:不进行,只打印初始块链串
//----------以上内容可以修改------------
//请勿修改以下内容
//请勿修改以下内容
for(int i=0;i<strlen(str);i++){
if(str[i]==' '){
str[i]='#';
}
}
BLString sub;
BLString src;
blstr_init(&src);
blstr_init(&sub);
bool flag;
printf("---icoding---\n3-3块链串\n");
pos=pos+BLOCK_SIZE;//让pos指向第二个块,因为编写的测试代码的第一个块为head为空。
//为了满足icoding第一个块即有值的要求,我们把pos向后移动一个块,以便让pos跳过head空块。
blstr_to_blstr(&src, str);
printf(">>打印src原始串\n");
blstr_print(src);
printf("\n说明:| *** |所围成的区域为一个块(Block),***为其内容\n");
if(choice){
printf(">>块链子串\n");
flag=blstr_substr(src, pos, len, &sub);
if(flag==true){
printf("函数返回true\n·子串要求·\t子串开始的下标:%d\t长度:%d\n",pos-BLOCK_SIZE,len);
printf(" | ");
blstr_print(sub);
}
else{
printf("函数返回false,不进行打印!\n·子串要求·\t子串开始的下标:%d\t长度:%d\n",pos-BLOCK_SIZE,len);
}
}
else{
printf("\n您未进行子串的查找并存储函数(blstr_substr)的运行,若需要请修改main函数的choice值\n");
}
if(choice==true){
printf("\n\n子串要求是根据题目条件设置的要求值,可能不是实际生成的sub的信息.\n");
}
else{
putchar('\n');
}
printf("本代码仅提供输出结果,不进行对错的判断\n");
printf("----测试结束----\n");
return 0;
}
如果不更改main函数内的内置测试样例
那么您将得到的正确的输出结果为: