问几个问题
(1)
#define MaxLen 20
#include<iostream>
#include<string>
using namespace std;
typedef struct
{
char ch[MaxLen];
int len;
}strtype;
void create(strtype *s,char str[]) //将普通字符串赋给串
{
strcpy(s->ch,str);
s->len=strlen(str);
}
int length(strtype *s) //求串的长度
{
return s->len;
}
void copy(strtype *s1,strtype *s2) //串的复制
{
int i;
for (i=0;i<s1->len;i++)
s2->ch[i]=s1->ch[i];
s2->len=s1->len;
s2->ch[s2->len]='\0'; //添加字符串结束符
}
strtype subs(strtype *s,int pos,int n) //求子串
{
int i;
strtype sub;
if(pos+n-1>length(s)) //参数不正确
sub.len=0;
else
{
for(i=pos-1;i<pos+n-1;i++)
sub.ch[i-pos+1]=s->ch[i];
sub.len=n;
sub.ch[sub.len]='\0';
}
return sub;
}
int concat(strtype *s,strtype *t) //连接两个串
{
int i;
if (s->len+t->len>MaxLen)
return 0;
for (i=0;i<t->len;i++)
s->ch[i+s->len]=t->ch[i];
s->len=s->len+t->len;
s->ch[s->len]='0';
return 1;
}
int ins(strtype *s,strtype *t,int i) //插入一个字串
{
int j;
if (s->len+t->len>MaxLen)
return 0;
for (j=s->len-1;j>i-1;j--) //i之后的所有元素后移t->len个位置
s->ch[j+t->len]=s->ch[j];
for (j=0;j<t->len;j++)
s->ch[j+i-1]=t->ch[j];
s->len=s->len+t->len;
s->ch[s->len]='\0';
return 1;
}
int del(strtype *s,int pos,int n) //删除一个字串
{
int i;
if (pos+n>s->len)
return 0;
for (i=pos+n-1;i<s->len;i++)
s->ch[i-n]=s->ch[i];
s->len=s->len-n;
s->ch[s->len]='\0';
return 1;
}
void disp(strtype *s) //输出串
{
if(s->len==0)
cout<<"空串"<<endl;
else
cout<<s->ch<<endl;
}
--------------------Configuration: String - Win32 Debug--------------------
Compiling...
String.cpp
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/String.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
String.exe - 2 error(s), 0 warning(s)
这个是数据结构中的串操作,但是我不知道错在那里了
(2)
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Line 1:Enter a string:";
cin.get(ch);
cout<<"ch="<<ch;
}
和
#include<iostream>
using namespace std;
void main()
{
char ch;
cout<<"Line 1:Enter a string:";
cin.get(ch);
cout<<"ch="<<ch;
}
这两个函数返回值不同为何所取得的值是相同的?例如ch=abcd,所得都是a
(3)
#include<iostream>
using namespace std;
char main()
{
char ch;
cin>>ch;
cin.get(ch);
cout<<"ch="<<ch;
}
如果ch=abcd 为何所得ch=b?get不是只取第一个字符吗?