当前位置:新励学网 > 秒知问答 > C语言,求帮忙

C语言,求帮忙

发表时间:2024-07-21 01:24:37 来源:网友投稿

问题共分三题:

1、判断回文:用两个指针指向字符串两头,往中间汇合,同时比较值,全部相等为回文。

2、猜数字:用随机数函数即可,但题目强调用指针,所以代码用递归写,指针传址。

3、数据结构:这种上限不定,且需要删除功能,用结构链表写更好,但考虑你学习范围,改用结构数组。

下面是代码:

//----------题1

#include

intplalindrome(charstr[]);//判断回文,是返回1,否返回0

intmain()

{

charstr[50];

while(1)

{

printf(请输入要判断的字符串:\n);

scanf(%s,str);

printf(判断结果:%s\n,plalindrome(str)?Yes:No);

}

return0;

}

intplalindrome(charstr[])

{

char*p0=str,*p1=str;

while(*p1!=0)p1++;

p1--;

while(p0<p1)

{

if(*p0!=*p1)return0;

p0++,p1--;

}

return1;

}

//----------题2

#include

#include

#include

intguess(int*cnt,int*n);

intmain()

{

srand(time(NULL));

printf(cnt-%d\n,guess(NULL,NULL));

return0;

}

intguess(int*cnt,int*n)

{

intnum,t,rn;

if(cnt==NULL)cnt=&t,t=0;

if(n==NULL)n=&rn,rn=rand()%100+1;

printf(pleaseinputanumber:),scanf(%d,&num);

(*cnt)++;

if(num<*n)printf(inputabigone\n);

if(num>*n)printf(inputasmallone\n);

if(num!=*n)guess(cnt,n);

return*cnt;

}

//----------题3

#include

#include

#include

#include

#defineMS30//学生人数上限

typedefstructstudent

{

charsid[20];

charsname[20];

charpnum[20];

charqq[20];

}STU;

intsn=0;//当前学生人数

voidshowStu(STUstus[]);

intinsertInfo(STUstus[]);

intdelInfo(STUstus[]);

voidrefcon();

intmain()

{

intn;

STUstus[MS];

while(1)

{

printf(----班级通讯录----\n);

printf(1、显示所有信息\n);

printf(2、增加一条信息\n);

printf(3、删除一条信息\n);

printf(请输入序号:),scanf(%d,&n);

switch(n)

{

case1:showStu(stus);refcon();break;

case2:insertInfo(stus);refcon();break;

case3:delInfo(stus);refcon();break;

}

}

return0;

}

voidrefcon()

{

printf(按任意键继续。。。。。。。),getch();

system(cls);

}

voidshowStu(STUstus[])

{

inti;

if(sn>0)

{

printf(%20s%20s%20s%20s\n,学号,姓名,手机,QQ号);

for(i=0;i<sn;i++)

printf(%20s%20s%20s%20s\n,stus[i].sid,stus[i].sname,stus[i].pnum,stus[i].qq);

}

}

intinsertInfo(STUstus[])

{

if(sn==MS)

{

printf(班级人数已达上限,请修改上限!\n);

return0;

}

printf(请输入学号:),scanf(%s,stus[sn].sid);

printf(请输入姓名:),scanf(%s,stus[sn].sname);

printf(请输入手机:),scanf(%s,stus[sn].pnum);

printf(请输入QQ号:),scanf(%s,stus[sn].qq);

sn++;

fflush(stdin);

return1;

}

intdelInfo(STUstus[])

{

inti,j;

charsid[20];

if(!sn)return0;

showStu(stus);

printf(请输入要删除学生的学号:),scanf(%s,sid);

for(i=0;i<sn;i++)

if(strcmp(stus[i].sid,sid)==0)

{

for(j=i;j<sn;j++)

stus[j]=stus[j+1];

printf(删除成功!\n);

sn--;

return1;

}

printf(未找到对应学号!\n);

fflush(stdin);

return0;

}

免责声明:本站发布的教育资讯(图片、视频和文字)以本站原创、转载和分享为主,文章观点不代表本网站立场。

如果本文侵犯了您的权益,请联系底部站长邮箱进行举报反馈,一经查实,我们将在第一时间处理,感谢您对本站的关注!