c语言面试题2道(华为)

王朝c/c++·作者佚名  2006-01-27
窄屏简体版  字體: |||超大  

1、一个学生的信息是:姓名,学号,性别,年龄等信息,用一个链表,把这些学生信息连在一起, 给出一个age, 在些链表中删除学生年龄等于age的学生信息。

#include "stdio.h"

#include "conio.h"

struct stu{

char name[20];

char sex;

int no;

int age;

struct stu * next;

}*linklist;

struct stu *creatlist(int n)

{

int i;

//h为头结点,p为前一结点,s为当前结点

struct stu *h,*p,*s;

h = (struct stu *)malloc(sizeof(struct stu));

h->next = NULL;

p=h;

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

{

s = (struct stu *)malloc(sizeof(struct stu));

p->next = s;

printf("Please input the information of the student: name sex no age \n");

scanf("%s %c %d %d",s->name,&s->sex,&s->no,&s->age);

s->next = NULL;

p = s;

}

printf("Create successful!");

return(h);

}

void deletelist(struct stu *s,int a)

{

struct stu *p;

while(s->age!=a)

{

p = s;

s = s->next;

}

if(s==NULL)

printf("The record is not exist.");

else

{

p->next = s->next;

printf("Delete successful!");

}

}

void display(struct stu *s)

{

s = s->next;

while(s!=NULL)

{

printf("%s %c %d %d\n",s->name,s->sex,s->no,s->age);

s = s->next;

}

}

int main()

{

struct stu *s;

int n,age;

printf("Please input the length of seqlist:\n");

scanf("%d",&n);

s = creatlist(n);

display(s);

printf("Please input the age:\n");

scanf("%d",&age);

deletelist(s,age);

display(s);

return 0;

}

2、实现一个函数,把一个字符串中的字符从小写转为大写。

#include "stdio.h"

#include "conio.h"

void uppers(char *s,char *us)

{

for(;*s!='\0';s++,us++)

{

if(*s>='a'&&*s<='z')

*us = *s-32;

else

*us = *s;

}

*us = '\0';

}

int main()

{

char *s,*us;

char ss[20];

printf("Please input a string:\n");

scanf("%s",ss);

s = ss;

uppers(s,us);

printf("The result is:\n%s\n",us);

getch();

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航