问几个问题

fengzhizi715 2004-11-18 01:34:15
(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不是只取第一个字符吗?

...全文
120 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangrong 2004-11-20
  • 打赏
  • 举报
回复
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;
}

你得为sub动态分配空间吧
pandengzhe 2004-11-20
  • 打赏
  • 举报
回复
很多基础的东西需要先弄明白
lxjlz 2004-11-20
  • 打赏
  • 举报
回复
没有main函数
greenteanet 2004-11-20
  • 打赏
  • 举报
回复
1、没有main函数
2、main()返回值与你的cout无关,那只是标准与非标准写法的区别,你的输出都是char,当然输出结果一样啦。
3、我觉得楼主的程序这样子写就很明显了:
#include<iostream>
using namespace std;

int main()
{
char ch;
cin>>ch;
cout<<ch<<endl;
cin.get(ch);
cout<<"ch="<<ch;
return 0;
}
第一个输出是a,第二个输出是b
也就是说第一个读取a,第二个读取的是b.
aa1298 2004-11-19
  • 打赏
  • 举报
回复
没有main函数
fancard 2004-11-19
  • 打赏
  • 举报
回复
2)返回值不同是main不同,ch都是char型啊,当然输出一样
3)不明白什么意思。。。
铖邑 2004-11-19
  • 打赏
  • 举报
回复
1没有main函数
2 类型为字符,只取第一个字符
3
cin>>ch; // 取a
cin.get(ch); // 取b
fancard 2004-11-19
  • 打赏
  • 举报
回复
main函数在哪里呢?
fengzhizi715 2004-11-19
  • 打赏
  • 举报
回复
顶阿
大家帮帮忙啊。。。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧