好的调试手段,而这里提到的就是一种有用的方法。
一个学生的信息是:姓名,学号,性别,年龄等信息,用一个链表,把这些学生信息连在一起, 给出一个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();
}
随机输入一个数,判断它是不是对称数(回文数)(如3,121,12321,45254)。不能用字符串库函数
/***************************************************************
1.
函数名称:Symmetry
功能: 判断一个数时候为回文数(121,35653)
输入: 长整型的数
输出: 若为回文数返回值为1 esle 0
******************************************************************/
unsigned char Symmetry (long n)
{
long i,temp;
i=n; temp=0;
while(i) //不用出现长度问题,将数按高低位掉换
{
temp=temp*10+i%10;
i/=10;
}
return(temp==n);
}
方法一
/* -------------------------------------------------------
--------------------
功能:
判断字符串是否为回文数字
实现:
先将字符串转换为正整数,再将正整数逆序组合为新的正整数,两数相同则为回文数字
输入:
char *s:待判断的字符串
输出:
无
返回:
0:正确;1:待判断的字符
华为 C 笔试
串为空;2:待判断的字符串不为数字;
3:字符串不为回文数字;4:待判断的字符串溢出
---------------------------------------------------------------------------- */
unsigned IsSymmetry(char *s)
{
char *p = s;
long nNumber = 0;
long n = 0;
long nTemp = 0;
/*判断输入是否为空*/
if (*s == \'\\0\')
return 1;
/*将字符串转换为正整数*/
while (*p != \'\\0\')
{
/*判断字符是否为数字*/
if (*p<\'0\' || *p>\'9\')
return 2;
/*判断正整数是否溢出*/
if ((*p-\'0\') > (4294967295-(nNumber*10)))
return 4;
nNumber = (*p-\'0\') + (nNumber * 10);
p++;
}
/*将数字逆序组合,直接抄楼上高手的代码,莫怪,呵呵*/
n = nNumber;
while(n)
{
/*判断正整数是否溢出*/
if ((n%10) > (4294967295-(nTemp*10)))
return 3;
nTemp = nTemp*10 + n%10;
n /= 10;
}
/*比较逆序数和原序数是否相等*/
if (nNumber != nTemp)
return 3;
return 0;
}
方法二
/* ---------------------------------------------------------------------------
功能:
判断字符串是否为回文数字
实现:
先得到字符串的长度,再依次比较字符串的对应位字符是否相同
输入:
char *s:待判断的字符串
输出:
无
返回:
0:正确;1:待判断的字符串为空;2:待判断的字符串不为数字;
3:字符串不为回文数字
---------------------------------------------------------------------------- */
unsigned IsSymmetry_2(char *s)
{
char *p = s;
int nLen = 0;
int i = 0;