C语言 算法与数据结构 串和数组 基本操作和案例
程序员文章站
2022-03-15 22:04:36
...
C语言 算法与数据结构 串和数组 基本操作和案例
实验要求:
1.定义顺序串,实现顺序串的输入、串复制、串插入、串删除、串输出运算,并编写主程序测试;
2.寻找矩阵的鞍点(在所在行中最大,所在列中最小);(注:矩阵的大小由键盘输入,矩阵数据由随机数产生)
//数组和串的操作
//预处理区
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXSIZE 1024
//数据设计区
typedef struct
{
char data[MAXSIZE];
} String;
typedef struct
{
int lines;
int cols;
int data;
} point;
//函数申明区
//实验1函数区
String * StringInit();
void StringInput(String * string);
int StringCopy(String * string_item,String * string_source);
int StringInsert(String * string_item,String * string_source,int space); //wrong
int StringLen(String * string);
void StringDel(String * string,int space_front,int space_behind); //wrong
int StringSubstr(String * string_item,String * string_source,int space_front,int space_behind);
//实验2函数区
int IsAnPoint(int * num[],int lines,int cols);
void RandNumFull(int * num[],int lines,int cols);
//主函数区
int main()
{
String * string=StringInit();
printf("Please input string:\n>/ ");
StringInput(string);
printf("your input is :\n>> ");
puts(string);
String * string_item=StringInit();
StringCopy(string_item,string);
printf("copy result is :\n>> ");
puts(string_item);
printf("String length: %d\n",StringLen(string));
StringInsert(string,string_item,2);
printf("your insert result is: %s\n",string);
StringDel(string,2,4);
printf("your delete result is: %s\n",string);
return 0;
}
//函数实现区
String * StringInit()
{
String * string=(String *)malloc(sizeof(String));
return string;
}
void StringInput(String * string)
{
gets(string->data);
}
int StringCopy(String * string_item,String * string_source)
{
int i=0;
while(string_source->data[i])
{
string_item->data[i]=string_source->data[i];
i++;
}
string_item->data[i]='\0';
return 1;
}
int StringInsert(String * string_item,String * string_source,int space)
{
if(MAXSIZE<StringLen(string_item)+StringLen(string_source)+1)
return 0;
if (space>StringLen(string_item))
space=StringLen(string_item);
int len=StringLen(string_source);
string_item->data[space+len+1]='\0';
for(int i=0;i<len;i++)
{
string_item->data[space+len-i]=string_item->data[space+i];
}
int i=0;
while(string_source->data[i])
{
string_item->data[i+space]=string_source->data[i];
i++;
}
return 1;
}
int StringLen(String * string)
{
int i=0;
while(string->data[i])
i++;
return i;
}
void StringDel(String * string,int space_front,int space_behind)
{
if(space_behind==0)
string->data[space_front]='\0';
else
{
int i=0;
while(string->data[space_behind])
{
string->data[space_front+i]=string->data[space_behind+i];
i++;
}
string->data[space_front+i]='\0';
}
}
int StringSubstr(String * string_item,String * string_source,int space_front,int space_behind)
{
int i;
for(i=0;i<space_behind-space_front;i++)
{
string_item->data[i]=string_source->data[space_front+i];
}
string_item->data[i+1]='\0';
}
void RandNumFull(int * num[],int lines,int cols)
{
int i=0;
int j=0;
srand((unsigned)time(NULL));
for(;i<lines;i++)
for(;j<cols;j++)
{
num[i][j]=rand()%100;
}
}
int IsAnPoint(int * num[],int lines,int cols)
{
int i,j,k,max,min,lmindex,cmindex,llindex;
for(i=0;i<lines;i++)
{
max=num[i][0];
lmindex=i,cmindex=0;
for(j=1;j<cols;j++)
{
if(num[i][j]>max)
{
max=num[i][j];
lmindex=i,cmindex=j;
}
}
min=num[0][cmindex];
llindex=0;
for(k=1;k<lines;k++)
{
if(num[k][cmindex]<min)
{
min=num[k][cmindex];
llindex=k;
}
}
if(lmindex==llindex)
{
printf("An Point is: %d in: %d %d\n",max,lmindex,cmindex);
break;
}
}
printf("Not Found An Point\n");
}