JAVA 版跑过来拜师学艺 , 请教几个基础一点的 C 问题. 给 60 大分 ^_^

vino_beer 2005-01-19 09:56:19
请完成以下题目。注意,请勿直接调用 ANSI C 函数库中的函数实现。

a)请编写一个 C 函数,该函数给出一个字节中被置 1 的位的个数,并请
给出该题的至少一个不同解法。
b)请编写一个 C 函数,该函数将给定的一个字符串转换成整数。
c)请编写一个 C 函数,该函数将给定的一个整数转换成字符串。
d)请编写一个 C 函数,该函数将一个字符串逆序。
e)请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回
该字符所在位置索引值。
f)请编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,
该字符串是由同一字符组成的。
...全文
146 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
idler 2005-01-20
  • 打赏
  • 举报
回复
e)请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回
该字符所在位置索引值。
---
int func_e(const char *base, int size, const char ch)
{
const char *p = base;
int i;

for (i = 0; i < size; i++) {
if (*p == ch) return i;
p++;
}

return -1; /* Not found */
}
qybao 2005-01-20
  • 打赏
  • 举报
回复
小弟不才,斗胆献个丑

#include <iostream.h>

//a
int countone(char src)
{
int count = 0;
while (src != 0)
{
count += (src<0)?1:0;
src <<= 1;
}
return count;
}

//b
int strtoint(const char* src)
{
//validity check
if (*src!='\0' && *src!='-')
{
if (*src<'0' || *src>'9')
{
return 0;
}
}

int result=0, flag=1;
if (*src == '-')
{
flag = -1;
src++;
}

while (*src != '\0')
{
result = result*10 + (*(src++)-'0');
}

return (result*flag);
}

//c
char* inttostr(int src, char* dest)
{
int i=0;
char t[16];
char *p = t;
if (src < 0)
{
*(dest+(i++)) = '-';
src *= -1;
}
while (src > 0)
{
*(p++) = (char)(src%10 + '0');
src /= 10;
}
while (p != t)
{
*(dest+(i++)) = *(--p);
}
*(dest+i) = '\0';

return dest;
}

//d
char* reverse(char *src)
{
int count = 0;
char t;
while (*(src+(count++))!= '\0');

for (int i=0; i<=(--count)/2; i++)
{
t = *(src+i);
*(src+i) = *(src+count-1);
*(src+count-1) = t;
}

return src;
}

//e
int indexof (const char* src, char c)
{
if (*src == '\0')
{
return -1;
}
int index=-1;
while (*src != '\0')
{
index++;
if (*src == c)
{
break;
}
src++;
}

return (*src=='\0'?-1:index);
}

//f
char* findmaxstr(const char* src, char* dest)
{
char c='\0';
int count=0, max=0;
const char *p=src, *q=src;
while (*src != '\0')
{
if (c != *src)
{
if (count > max)
{
max = count;
p = q;
}
q = src;
count = 0;
c = *src;
}
else
{
count++;
}
src++;
}
if (count > max)
{
max = count;
p = q;
}
for(count=0; count<=max; count++)
{
*(dest+count) = *(p++);
}
*(dest+count) = '\0';

return dest;
}


//test
void main(void)
{
char a = -2;
cout << "countone(" << (int)a << ")=" << countone(a) << endl;

char b[] = "-120";
cout << "strtoint(" << b << ")=" << strtoint(b) << endl;

int c = -300;
char d[16];
cout << "inttostr(" << c << ")=" << inttostr(c, d) << endl;

char e[] = "abcde";
cout << "reverse(" << e << ")=" << reverse(e) << endl;

char f[] = "test";
char g = 'a';
cout << "indexof(" << f << "," << g << ")=" << indexof(f, g) << endl;

char h[] = "aaabbbbcccccddddeee";
char s[10];
cout << "findmaxstr(" << h << ")=" << findmaxstr(h, s) << endl;

}
idler 2005-01-20
  • 打赏
  • 举报
回复
厄。。。笔误,j是多余的。
liujingfu123 2005-01-20
  • 打赏
  • 举报
回复
d)请编写一个 C 函数,该函数将一个字符串逆序。
-------------------------------------------------
/*******************************************************
方法一: Resource为源字符串
Dest为逆序后的字符串
*******************************************************/

void ReverseStr(char *Dest, char* Resource)
{
int front = 0, rear = 0;
assert(Resource != NULL);
while( (Resource[rear]) != '\0')
rear++;
rear --;
while(rear >= 0 )
{
Dest[front++] = Resource[rear--];
}
Dest[front] = '\0';
}

/*******************************************************
方法二: 在原来字符串的基础上逆序
*******************************************************/
void Revserse(char *Str)
{
int i=0,j=0;
char TempChar = 0;
assert(Str != NULL);
while(Str[i] != '\0')
i++;
i--;
while(j <= i)
{
TempChar = Str[j];
Str[j] = Str[i];
Str[i] = TempChar;
j++;
i--;
}
}
idler 2005-01-20
  • 打赏
  • 举报
回复
f)请编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,
该字符串是由同一字符组成的。
---
int func_f(const char *str)
{
int max = 0;
int f_max[2][2];

int i, j, len;

/* Do not use strlen function here */
len = 0;
while (str[len] != '\0') len++;

max = 1;
f_max[0][0] = 1; f_max[0][1] = 1;
for (i = 1; i < len; i++) {
f_max[1][0] = (f_max[0][0] > f_max[0][1]) ? f_max[0][0] : f_max[0][1];
if (str[i] == str[i - 1]) {
f_max[1][1] = f_max[0][1] + 1;
}
else {
f_max[1][1] = 1;
}

f_max[0][0] = f_max[1][0];
f_max[0][1] = f_max[1][1];
}

max = (f_max[1][0] > f_max[1][1]) ? f_max[1][0] : f_max[1][1];

return max;
}
liujingfu123 2005-01-20
  • 打赏
  • 举报
回复
c)请编写一个 C 函数,该函数将给定的一个整数转换成字符串。
-----------------------------------------------------

void MyIntToStr(char *Str,const int Num)
{
int i=0,j=0;
int Value=0,Res=0;
char TempArr[20]; //用于存放转换后的逆序临时字符串,应该不会超过20位吧? :)
memset(TempArr,0,sizeof(TempArr));
Value = Num;
while(Value !=0)
{
Res = Value % 10; //得到整数的最后一位(个位)数字
Value = Value / 10; //得到商
TempArr[i++] = (char)(Res + '0'); //将余数转换成字符
} //循环完毕时得到TempArr为逆序的字符串
i = 0;
while(TempArr[i] != 0)
{ i++; } //求得字符串长度
i--; //i减1得到逆序字符串的最后一个位置
while(i >= 0) //将得到的逆序字符串颠倒过来
{
Str[j++] = TempArr[i--];
}
Str[j] = '\0'; //将字符串置上结束标志
}
xuelong_zl 2005-01-20
  • 打赏
  • 举报
回复
好贴
wkoji 2005-01-20
  • 打赏
  • 举报
回复
欢迎JAVAER弃暗投明啊
idler 2005-01-20
  • 打赏
  • 举报
回复
hoho,楼上犯规了哦,printf用在main里面输出不论,memset是8可以用滴。。。
liujingfu123 2005-01-20
  • 打赏
  • 举报
回复
所有问题都解决了,恭喜!

本人以上函数均在VC6.0上面通过,记得加上头文件
#include "stdafx.h" // for VC project
#include <stdio.h> // for standard I/O
#include <string.h> // for memset()
#include <assert.h> //for assert()

睡觉去了,希望你能给点分…… 呵呵:)
idler 2005-01-20
  • 打赏
  • 举报
回复
这下60分就不够分咯。。。
liujingfu123 2005-01-19
  • 打赏
  • 举报
回复
b)请编写一个 C 函数,该函数将给定的一个字符串转换成整数。
--------------------------------------------------------
/*******************************************************
没有考虑转换后的整数是否越界,你自己去考虑吧,加上一些判断条件
*******************************************************/
int MyStrToInt( const char * const str)
{
int i;
int RetValue = 0;

for ( i = 0; (str[i] >= '0' && str[i] <= '9'); i++)
NULL;
if ( str[i] != '\0')
{
printf("String Error!!!!!!!!!!!\n");
return RetValue;
}
for ( i = 0; ( str[i] != '\0'); i++ )
{
RetValue = RetValue*10 + str[i] - '0';
}

return RetValue;
}
liujingfu123 2005-01-19
  • 打赏
  • 举报
回复
a)请编写一个 C 函数,该函数给出一个字节中被置 1 的位的个数,并请
给出该题的至少一个不同解法。
----------------------------------------------------------------
int NumberOf1(unsigned char bits)
{
int i = 0,num = 0;
unsigned char check = 0x01;
for(i = 0; i < 8; i++)
{
if(bits & check )
{ num++; }
check <<= 1;
}
return num;
}

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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