请编写能直接实现int atoi(const char * pstr)函数功能的代码。
第1题:
请帮忙给出正确答案和分析,谢谢
答案:
void select_sort(int Array[], int n)//n为数组a的元素个数
{
for (int i = 0; i<n - 1; i++)//进行N-1轮选择
{
int min_index = i;
for (int j = i + 1; j<n; j++)//找出第i小的数所在的位置
{
if (Array[j] > Array[min_index])
{
min_index = j;
}
}
//将第i小的数,放在第i个位置;如果刚好,就不用交换
if (i != min_index)
{
int temp = Array[i];
Array[i] = Array[min_index];
Array[min_index] = temp;
}
}
}
int main()
{
int num[7] = { 1, 2, 3, 4, 5, 6, 7};
select_sort(num, 7);
printf("\n结果如下:\n");
for(int i=0; i<7; i++)
{
printf("\n%d\n ", num[i]);
}
printf("\n");
}
测试结果:
解析:
选择排序(从小到大)的基本思想是,首先,选出最小的数,放在第一个位置;然后,选出第二小的数,放在第二个位置;以此类推,直到所有的数从小到大排序。
第2题:
第3题:
阅读下列函数说明和C函数,将应填入(n)处的字句写在对应栏内。
[函数2.1说明]
函数strcpy的功能是将字符串str2的内容复制到字符申str1。
[函数2.1]
(1) strcpy (char *slr1, const char *str2)
{ char * temp;
while( * str2!='\0') *cp++ =(2);
(3)='\0';
return str1;
}
[函数2.2说明]
函数int strcmp(const char *str1, const char *str2)的功能是按字典序比较两个字符串str1和str2的大小。当str1<str2时返回-1,当str1>str2时返回1,否则返回0。
[函数2.2]
int strcmp(const char *str1, const char *str2)
{ while( *str1= =* str2) {
if(* s1= =(4)) return 0;
s1++;
(5);
}
if( *str1<*str2) return -1;
return 1;
}
第4题:
请编写一个函数 int find(char s[],char t[]), 该函数在字符串s中查找字符串t,如果找到,则返回字符串t在字符串s中的位置(整数值):否则返回-1。本题要求:用数组方式及两重循环来实现该函数。
注意:部分源程序已存在考生文件夹的文件PROC1.cpp中。
请勿修改主函数和其他函数中的任何内容,仅在函数find()的花括号中填写若干语句。
文件PROC1.cpp的内容如下:
//PROC1.cpp
include<iostream>
using namespace std;
int find(char s[],char t[]);
const int MAXLINE = 256;
int main()
{
char source[MAXLINE],target[MAXLINE];
cout<<"Please input a string for searching:\n";
cin.getline(source,MAXLINE);
cout<<"Please input a string you want to find:\n";
cin.getline(target,MAXLINE);
int intPos=find(source,target);
if(intPos>=0)
cout<<"Finding it,The target string is at index"
<<intPos<<"of the source string\n";
else
cout<<"Not finding it \n";
return 0;
}
int find(char s[],char t[])
{
//********
}
第5题:
阅读下列程序说明和C++程序,把应填入其中(n)处的字句,写对应栏内。
【说明】
下面的程序实现了类String的构造函数、析构函数和赋值函数。
已知类String的原型为:
class String
{
public:
String(coust char * str = NULL); //普通构造函数
String( const String &other); //拷贝构造函数
~String(void); //析构函数
String & perate =(const String &other); //赋值函数
private:
char * m_data; // 用于保存字符串
};
//String 的析构函数
String:: ~String (void)
{
(1);
}
//String 的普通构造函数
String: :String( const char * str)
{
if (2)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new ehar[ length + 1 ];
strepy(m_data, str);
}
}
//拷贝的构造函数
String:: String( const String &other)
{ int length = strlen(other. m_data);
m_data = new char[ length + 1 ];
strepy(m_data, other, m_data); //赋值函数
String & String::operate = (eonst String &other) //
{
if (3)
return * this;
delete [] m_clara; //释放原有的内存资源
int length = strlen( other, m_data);
m_data = new chart length + 1 ];
(4);
return (5);
}
第6题:
如何为函数int atoi(const char * pstr)编写测试向量?
第7题:
请编写实现void * malloc(int)内存分配函数功能一样的代码。
第8题:
输入二个 64 位的十进制数,计算相乘之后的乘积
已知 strcpy 函数的原型是:
char * strcpy(char * strDest,const char * strSrc);
1.不调用库函数,实现strcpy 函数。
2.解释为什么要返回char *。
第9题:
已知String类定义如下:
class String
{
public:
String(const char *str = NULL); // 通用构造函数
String(const String &another); // 拷贝构造函数
~ String(); // 析构函数
String & perater =(const String &rhs); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
尝试写出类的成员函数实现。
String::String(const char *str)
{
if ( str == NULL ) //strlen在参数为NULL时会抛
异常才会有这步判断
{
m_data = new char[1] ;
m_data[0] = '\0' ;
}
else
{
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}
}
String::String(const String &another)
{
m_data = new char[strlen(another.m_data) + 1];
strcpy(m_data,other.m_data);
}
String& String::operator =(const String &rhs)
{
if ( this == &rhs)
return *this ;
delete []m_data; //删除原来的数据,新开一块内
存
m_data = new char[strlen(rhs.m_data) + 1];
strcpy(m_data,rhs.m_data);
return *this ;
}
String::~String()
{
delete []m_data ;
}
第10题:
编写 strcpy函数
已知 strcpy函数的原型是
char *strcpy(char *strDest, const char *strSrc);
其中 strDest 是目的字符串,strSrc 是源字符串。
(1)不调用 C++/C 的字符串库函数,请编写函数 strcpy
(2)strcpy能把 strSrc 的内容复制到 strDest,为什么还要 char * 类型的返回值?
第11题:
下列给定程序中,函数fun()的功能是:用冒泡法对6个字符串按由大到小的顺序进行排序。
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <conio.h>
include <stdio.h>
define MAXLINE 20
void fun(char *pstr[6])
{
int i,j;
char *p;
for(i=0;i<5;i++)
for(j=i+1;j<6;j++)
/*************found*************/
if(strcmp((pstr+i),(pstr+j))<0)
{
p=*(pstr+i);
*(pstr+i)=*(pstr+j);
/*************found*************/
*(pstr+j)=*p;
}
}
main()
{
int i;
char*pstr[6],str[6][MAXLINE];
clrscr();
for(i=0;i<6;i++)
pstr[i]=str[i];
printf("/nEnter 6 string(1 string at each line):\n");
for(i=0;i<6;i++)
scanf("%s",pstr[i]);
fun(pstr);
printf("The strings after sorting:\n");
for(i=0;i<6;i++)
printf("%s\n",pstr[i]);
}
第12题:
第13题:
试题二(共 15 分)
阅读以下说明和 C 程序,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
下面的程序按照以下规则输出给定名词的复数形式:
a. 若名词以“y”结尾,则删除 y 并添加“ies” ;
b. 若名词以“s” 、 “ch”或“sh”结尾,则添加“es” ;
c. 其他所有情况,直接添加“s” 。
[C 程序]
#include <stdio.h>
#include <string.h>
char *plural(char *word)
{
int n;
char *pstr;
n = strlen(word); /*求给定单词的长度*/
pstr = (char *)malloc(n+3); /*申请给定单词的复数形式存储空间*/
if (!pstr || n < 2)
return NULL;
strcpy(pstr,word); /*复制给定单词*/
if ( (1) )
{
pstr[n-1] = 'i'; pstr[n] = 'e'; pstr[n+1] = 's'; (2) ;
}
else
if(pstr[n-1]=='s'||pstr[n-1]== 'h' && ( (3) ))
{
pstr[n] = 'e'; pstr[n+1] = 's'; pstr[n+2] = '\0';
}
else
{ pstr[n] = 's'; pstr[n+1] = '\0'; }
(4) ;
}
main( )
{ int i; char *ps;
char wc[9][10] =
{"chair","dairy","boss","circus","fly","dog","church","clue","dish"}
for(i = 0; i < 9; i++) {
ps = (5) ;
printf("%s: %s\n",wc[i],ps); /*输出单词及其复数形式*/
free(ps); /*释放空间*/
}
system("pause");
}
第14题:
请编写一个函数int stringLen(char*ps),该函数能计算出字符串ps的长度,函数返回值就是字符串的长度(不包括字符串结束标识号'\0')。本题要求:用指针方式及循环来实现该函数。
注意;部分源程序已存在考生文件夹下的文件PROC6,cpp中。
请勿修改主函数和其他函数中的任何内容,仅在函数stringLen()的花括号中填写若干语句。
文件PROC6.cpp的内容如下:
//PROC6.cpp
include<iostream>
using namespace std;
int stringLen(char *);
int main()
{
char str[100],*p;
cout<<"Input your string please!\n";
cin>>str;
p=str;
cout<<"The lenth of your string is "<<stringLen(p)<<end1;
return 0;
}
int stringLen(char *ps)
{
// * * * * *
}
第15题:
请编写函数fun(),它的功能是:求出ss所指字符串中指定字符的个数,并返回此值。
例如,若输入字符串123412132,输入字符1,则输出3。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include<coio.h>
include<stdio.h>
define M 81
int fun(char *ss,char c)
{
}
main()
{ char a[M],ch;
clrscr();
printf("\nPlease enter a string:");
gets(a);
printf("\nPlease enter a char:");
ch=getchar();
printf("\nThe number of the char is:%d \n",fun(a,ch));
}
第16题:
请编写一个函数char *fun(char *s,int n)。函数fun()的功能是将字符串s中的字符“循环左移”n位。例如,输入“ABCDE”,则循环左移2位应输出“CDEAB”,输入“1234567”,循环左移3位应输出“4567123”。
注意:部分源程序已存在文件PROC13.cpp中。
请勿修改主函数和其他函数中的任何内容,仅在函数fun()的花括号中填写若干语句。
文件PROC13.cpp的内容如下:
//PROC13.cpp
include <iostream>
include <string>
using namespace std;
char *fun(char *s,int n);
int main()
{
char str[81];
int n;
cout<<"Enter a string(less than 80 char)\n”;
cin>>str;
cout<<"\n Enter n:";
cin>>n;
if(n>strlen(str))
{
cout<<"\n Data overflow";
return 0;
}
cout<<"The result is: "<<fun(str,n)<<end1;
return 0;
}
char *fun(char*s,int n)
{
//* * * * * *
}
第17题:
有如下类定义:
class Bag {
public:
Bag(int p,char s='M'):price(p),size(s) { count++; }
~Bag() { count--; }
int GetPrice() { return price; }
int GetPrice() const { return price; }
void SetPrice(int val) const { price=val; }
private:
int price;
char size;
static int count;
};
下列关于类成员的叙述中,正确的是( )。
A. 成员函数GetPrice存在重定义
B.构造函数的参数不能设置默认值
C.析构函数中不能访问静态数据成员
D.成员函数SetPrice不能定义为常函数
第18题:
请编写能直接实现char * strcpy(char * pstrDest,const char * pstrSource)函数功能的代码。
4.
char * strcpy(char * pstrDest,const char * pstrSource)
{
assert((pstrDest!=NULL)&&(pstrSource!=NULL));
char * pstr=pstrDest;
while((*(pstrDest++)=*(pstrSource++))!='\0');
return pstr;
}
第19题:
不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1;
第20题:
已知strcpy 的函数原型:char *strcpy(char
*strDest, const char *strSrc)其中strDest 是目的字符
串,strSrc 是源字符串。不调用C++/C 的字符串库
函数,请编写函数strcpy。
char *strcpy(char *strDest, const char *strSrc)
{
if ( strDest == NULL || strSrc == NULL)
return NULL ;
if ( strDest == strSrc)
return strDest ;
char *tempptr = strDest ;
while( (*strDest++ = *strSrc++) != ‘\0’)
;
return tempptr ;
}
第21题:
已知类 String 的原型为
class string
{
public:
string(const char *str=null);//普通构造函数
string(const string &other);//拷贝构造函数
---string(void);
string &operate=(const string &other);//赋值函数
private:
char * m-data;//用于保存字符串
};
请编写 string 的上述4 个函数
第22题:
编写类 String 的构造函数、析构函数和赋值函数
已知类 String的原型为:
class String
{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & perate =(const String &other); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
请编写 String的上述 4 个函数。
第23题:
试题三(共15分)
阅读以下说明和C函数,填补C函数中的空缺(1)~(6),将解答写在答题纸的对应栏内。
【说明】
函数numberOfwords (char message[])的功能是计算存储在message字符数组中的一段英文语句中的单词数目,输出每个单词(单词长度超过20时仅输出其前20个字母),并计算每个英文字母出现的次数(即频数),字母计数时不区分大小写。
假设英文语句中的单词合乎规范(此处不考虑单词的正确性),单词不缩写或省略,即不会出现类似don't形式的词,单词之后都为空格或标点符号。
函数中判定单词的规则是:
(1)一个英文字母串是单词;
(2) 一个数字串是单词;
(3)表示名词所有格的撇号(')与对应的单词看作是一个单词。
除上述规则外,其他情况概不考虑。
例如,句子“The 1990's witnessed many changes in people's concepts ofconservation”中有10个单词,输出如下:
The
1990's
witnessed
many
changes
in
people's
concepts
of
conservation
函数numberOfijvords中用到的部分标淮库函数如下所述。
【C函数】
int numberOfwords (char message[])
{
char wordbuffer[21],i=0; /*i用作wordbuffer的下标*/
(1) pstr;
int ps[26]={0); /*ps[0]用于表示字母'A'或'a'的频数*/
/*ps[1]用于表示字母'B'或'b'的频数,依此类推*/
int wordcounter=0;
pstr=message;
while (*pstr){
if((2)(*pstr)){/*调用函数判断是否为一个单词的开头字符*/
i=0;
do{/*将一个单词的字符逐个存入wordbuffer[],并对字母计数*/
wordbuffer[i++]=*pstr;
if(isalpha(*pstr)){
if (3) (*pstr))ps[*pstr-'a']++;
else ps[*pstr-'A']++;
}
(4) ; /*pstr指向下一字符*/
}while (i<20&&(isalnum(*pstr)||*pstr=='\"));
if (i>=20) /*处理超长单词(含名词所有格形式)*/
while (isalnum(*pstr)||*pstr=='\"){pstr++;}
(5) ='\0';/*设置暂存在wordbuffepstrr中的单词结尾*/
wordcounter++; /*单词计数*/
puts(wordbuffer); /*输出单词*/
}
(6); /*pstr指向下一字符*/
}
retum wordcounter;
}