C语言作业
1、顺序表
#include
#include
#include
#define MaxSize 20
typedef struct{
char name[10];
int age;
char skill[10];
}Person;
typedef struct{
Person per[MaxSize];
int length;
}SqList;
void InitList(SqList *L) {
int i;
for (i = 0; i < MaxSize; i ++) {
strcpy(L->per[i].name, "佚名");
L->per[i].age = 0;
strcpy(L->per[i].skill, "无");
}
L->length = 0;
}
void CreateList(SqList *L) {
int i = 0;
int flag = 1;
printf("---------------创建人物信息表--------------------\n");
while(flag == 1) {
printf("请输入第 %d 个人物的姓名:", i + 1);
scanf("%s", L->per[i].name);
printf("请输入第 %d 个人物的年龄:", i + 1);
scanf("%d", &(L->per[i].age));
printf("请输入第 %d 个人物的技能:", i + 1);
scanf("%s", L->per[i].skill);
printf("请问是否还要继续输入? 1-是 0-否\t");
scanf("%d", &flag);
i ++;
}
L->length = i;
}
void PrintList(SqList L) {
printf("---------------打印人物信息----------------------\n");
int i;
for (i = 0; i < L.length; i ++) {
printf("第 %d 个人的姓名为:%s\n", i + 1, L.per[i].name);
printf("第 %d 个人的年龄为:%d\n", i + 1, L.per[i].age);
printf("第 %d 个人的技能为:%s\n", i + 1, L.per[i].skill);
printf("\n");
}
}
int SearchList(SqList L, char keyName[10]) {
int i = 0;
for (i = 0;i<L.length;i++){
if (!strcmp(L.per[i].name,keyName)){
return i +1;
}
}
return 0;
}
void PrintListOne(SqList L,int num) {
printf("---------------打印人物信息----------------------\n");
printf("第 %d 个人的姓名为:%s\n", num, L.per[num-1].name);
printf("第 %d 个人的年龄为:%d\n", num, L.per[num-1].age);
printf("第 %d 个人的技能为:%s\n", num, L.per[num-1].skill);
}
bool DeleteList(SqList *L, char keyName[10]) {
if (L->length == 0){
return false;
}
int num = SearchList(*L,keyName);
if (num == 0){
return false;
} else{
for (int i = num;i<L->length;i++){
L->per[i-1] = L->per[i];
}
L->length--;
return true;
}
}
bool InsertList(SqList *L, Person newPer) {
if (L->length >= MaxSize){
return false;
}
printf("---------------插入人物信息----------------------\n");
int num = L->length + 1;
printf("请输入人物的姓名:");
scanf("%s", newPer.name);
printf("请输入人物的年龄:");
scanf("%d", &(newPer.age));
printf("请输入人物的技能:");
scanf("%s", newPer.skill);
strcpy(L->per[num - 1].name,newPer.name);
L->per[num-1].age = newPer.age;
strcpy(L->per[num-1].skill,newPer.skill);
L->length++;
PrintList(*L);
return true;
}
int main() {
SqList L;
InitList(&L);
CreateList(&L);
PrintList(L);
char name[10] ={0};
printf("请输入你要查找人的姓名:");
scanf("%s",name);
int num = SearchList(L,name);
if (num == 0){
printf("未找到此人\n");
} else{
PrintListOne(L,num);
}
char name1[10] ={0};
printf("请输入你要删除的人物姓名:");
scanf("%s",name1);
bool flag = DeleteList(&L,name1);
if (flag == true){
printf("%s的信息删除成功\n",name1);
PrintList(L);
} else{
printf("未找到此人\n");
}
Person newPer;
InsertList(&L,newPer);
return 0;
}
2、串的基本操作
#include
#include
#include
#include
#define MAX_LENGTH 50
typedef struct {
char *str;
int maxlength;
int length;
} DString;
void StrInit(DString *s) {
s->str = (char *)malloc(MAX_LENGTH);
s->maxlength = MAX_LENGTH;
s->length = 0;
}
int StrLength(DString s) {
return s.length;
}
int StrMaxLength(DString s) {
return s.maxlength;
}
bool StrEmpty(DString s){
if (s.length == 0){
return true;
} else {
return false;
}
}
void StrAssign(DString *s, char *chars) {
int maxLength = s->maxlength;
int charsLength = strlen(chars);
if (charsLength > maxLength){
maxLength = charsLength;
}
s->str = (char *) malloc(sizeof(char) * maxLength);
s->length = strlen(chars);
s->maxlength = maxLength;
for (int i = 0; i < s->length; i ++) {
s->str[i] = chars[i];
}
}
void StrDestory(DString *s) {
free(s->str);
s->str = NULL;
s->maxlength = 0;
s->length = 0;
}
void StrPrint(DString s) {
if (s.length == 0){
printf("串为空\n");
return;
}
for (int i=0; i < s.length; i++){
printf("%c",s.str[i]);
}
printf("\n");
}
int StrCompare(DString s, DString t)
{
for (int i = 0; i < s.length && i < t.length; i ++){
if (s.str[i] != t.str[i]) {
return s.str[i] - t.str[i];
}
}
return s.length - t.length;
}
bool StrInsert(DString *s, DString t, int p) {
if (p <= 0 || p >s->length){
return false;
}
int num = 0;
int tLength = t.length;
if (tLength < s->maxlength - p+1){
for (int i = s->length+tLength-1;i>=p+tLength-1;i--){
s->str[i] = s->str[i-tLength];
}
s->length +=tLength;
for (int i = p-1;i<p + tLength-1;i++){
s->str[i] = t.str[num];
num++;
}
return true;
}
if (tLength >= s->maxlength - p+1){
for (int i = p-1;i<s->maxlength;i++){
s->str[i] = t.str[num];
num++;
}
s->length = s->maxlength;
return true;
}
}
bool StrDelete(DString *s, int p, int len) {
if (p <= 0 || p >s->length){
return false;
}
if (p + len >= s->maxlength){
s->length = p-1;
return true;
}
int num = p + len -1;
for (int i = num;i<s->length;i++){
s->str[i-len] = s->str[i] ;
}
s->length -=len;
return true;
}
int BFIndex (DString *s, DString t, int p) {
int i = p, j = 0;
while ((i < s->length && j< t.length)) {
if (s->str[i] == t.str[j]){
i++, j++;
}else{
i = i - j +1, j = 0;
}
}
if (j >= t.length) {
return i - t.length;
}else{
return -1;
}
}
bool StrReplace(DString *s , int p, DString t, DString v) {
int i, j, k;
k = 0;
for (i=p; i <= s->length - t.length; i++) {
j = BFIndex(s, t, i);
if (j == -1){
if (k==0) {
printf("没有找到要找的字符串! \n");
return false;
}else{
printf("完成了%d次替换! \n", k);
return true;
}
} else{
if (StrDelete(s, j, t.length)){
if (StrInsert(s,v,j)){
k+=1;
}
}
}
}
}
void main(){
DString s, t, v;
int start=0;
StrInit(&s);
StrInit(&t);
StrInit(&v);
char schar[10000];
char tchar[100];
int num = 0;
int strNum = 0;
printf("请输入主串: \n");
scanf("%s", schar);
StrAssign(&s,schar);
printf("请输入要插入的子串: \n");
scanf("%s", tchar);
StrAssign(&t,tchar);
printf("请输入要插入的位置: \n");
scanf("%d", &num);
bool flag = StrInsert(&s,t,num);
if (flag == true){
printf("插入成功此时主串为:");
StrPrint(s);
} else{
printf("插入失败");
}
printf("请输入要删除的起始的位置: \n");
scanf("%d", &num);
printf("请输入要删除的字符个数: \n");
scanf("%d", &strNum);
StrDelete(&s,num,strNum);
StrPrint(s);
}