朋友c/c++的三道面试题。。附上他的代码
1、给出五门课的成绩,求成绩的平均值,注意输入的成绩均在0~100之间,如果输入的成绩不足五个,按照五个成绩算(补0);如果输入超过五个,取前五个计算:
例,输入:
(终端输入) (过滤) (平均)
10,200,30,-11,101,2 -> 10,30,2,0,0 -> 8.4
10,20,30,40,50,60 -> 10,20,30,40,50 -> 30
-1,20,-4,50,60,70 -> 20,50,60,70,0 -> 40
,20,30,40,-2,99,50 -> 0,20,30,40,99 -> 37.8
20,30 -> 20,30,0,0,0 -> 10
40,50,101 -> 40,50,0,0,0 -> 18
参考代码:
class Person
{
public:
Person(){init();}
void init();
void getGraden();
double getAverage();
private:
char graden[5][4];
};
int main(void)
{
Person *son = new Person;
son->getGraden();
cout << endl << "averageis :" << son->getAverage();
delete son;
son = nullptr;
return 0;
}
unsigned int _atoi(char*tmp)
{
char *p=tmp;
bool _b=false;
unsigned int number=0;
unsigned int cnt=0;
if(*p=='-')
{
_b=true;
++p;
}
while(*p)
{
if(cnt==0)
{
number=number+(*p-48);
}
else
{
number=number*(cnt*10)+(*p-48);
}
++cnt;
++p;
}
if(_b)
{
number*=-1;
}
return number;
}
void Person::init()
{
for(int i=0;i<5;++i)
{
memset(graden[i],0,4);
strcpy(graden[i],"0\0");
}
}
void Person::getGraden()
{
cout<<"inputgraden:";
char *buf = new char[100];
memset(buf,0,100);
cin>>buf;
buf[strlen(buf)]=',';
buf[strlen(buf)]='\0';
unsigned int cnt,i;
cnt=i=0;
char*p=buf;
while(*p)
{
if(*p==','||*p=='')
{
cnt=0;
unsigned int tmp=_atoi(graden[i]);
if(tmp<0 || tmp>100)
{
memset(graden[i],0,4);
strcpy(graden[i],"0\0");
++p;
continue;
}
else
{
++i;
}
static unsigned intcount=0;
if(++count==5)
{
break;
}
}
else
{
graden[i][cnt++]=*p;
}
++p;
}
}
double Person::getAverage()
{
unsigned int sum=0;
for(char *tmp : graden)
{
sum+=_atoi(tmp);
}
return sum/5.0;
}
2、给一个字符串,将字符串中的字母按照ASCII排序,其余非字母的字符位置不变。
参考代码:
using namespace std;
class Exm
{
public:
Exm(char *tmpStr)
{
str=new char[strlen(tmpStr)+1];
memset(str,0,strlen(tmpStr)+1);
memcpy(str,tmpStr,strlen(tmpStr));
}
~Exm()
{
delete str;
str=nullptr;
}
void endShow();
void makeFun();
private:
char *str;
};
int main(void)
{
//Exm exm("hello,world\0"); //result:dehll,loorw
//Exm exm("hello,python\0"); //result:ehhll,noopty
//result: E:QT5.7.0(aceee)gggiiiiklmmm530_3mnnnnoo32-stww.wxx
Exm exm("E:\Qt5.7.0(mingw)\cx\Tools\mingw530_32\bin\mingw32-make.exe\0");
exm.endShow();
return 0;
}
void Exm::endShow()
{
cout<<"source:"<<str;
makeFun();
cout<<"\n\nresult:"<<str;
}
void Exm::makeFun()
{
if(strlen(str)<=0)
{
return;
}
for(unsigned int i=0;i<strlen(str)-1;++i)
{
for(unsigned int j=i+1;j<strlen(str);++j)
{
if(!((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')))
{
break;
}
if(!((str[j]>='A' && str[j]<='Z') || (str[j]>='a' && str[j]<='z')))
{
continue;
}
if((str[i]-48)>(str[j]-48))
{
char tmp=str[i];
str[i]=str[j];
str[j]=tmp;
}
}
}
}
3、将输入的数字表示为大写形式,例如输入10010,那么转为壹万壹拾。
参考代码:
enum number
{
zero=0,one,two,three,four,five,six,seven,eight,nine
};
enum unit
{
yuan=1,ten,hundred,thousand,TenThousand
};
class Money
{
public:
Money(char*_m)
{
_money=new char[strlen(_m)+1];
memset(_money,0,strlen(_m)+1);
strcpy(_money,_m);
_money[strlen(_money)]='\0';
}
~Money()
{
if(_money)
{
delete_money;
_money=nullptr;
}
if(_result)
{
delete_result;
_result=nullptr;
}
}
void setMake();
char *getMake();
private:
char *_money;
char *_result;
};
intmain(void)
{
Money *money=new Money("10101");
money->setMake();
cout<<money->getMake();
return0;
}
char trNumber(const char ch)
{
//Qt编译器写纯C++,不支持中文:于是用QWERTYUIOP代表(零壹貳叁肆伍陆柒捌镹)
char _ch='\0';
switch((enum number)(ch-48))
{
case zero:_ch='Q';break;
case one:_ch='W';break;
case two:_ch='E';break;
case three:_ch='R';break;
case four:_ch='T';break;
case five:_ch='Y';break;
case six:_ch='U';break;
case seven:_ch='I';break;
case eight:_ch='O';break;
case nine:_ch='P';break;
}
return _ch;
}
char trUnit(const unsigned int cnt)
{
//Qt编译器写纯C++,不支持中文:于是ASDFG用代表(圆拾佰仟万)
char _ch='\0';
switch((enum number)cnt)
{
case yuan:_ch='A';break;
case ten:_ch='S';break;
case hundred:_ch='D';break;
case thousand:_ch='F';break;
case TenThousand:_ch='G';break;
}
return _ch;
}
void Money::setMake()
{
if(strlen(_money)<=0 || !_money)
{
return;
}
bool flag=true;
unsigned int cnt=strlen(_money);
char *tmp=new char[strlen(_money)*2+1];
memset(tmp,0,strlen(_money)*2+1);
char *q=tmp;
char *p=_money;
while(*p)
{
if(flag && *p=='0')
{
--cnt;
++p;
continue;
}
flag=false;
if(*p=='0' && *(++p)=='0')
{
cnt-=2;
++p;
continue;
}
*q++=trNumber(*p);
*q=trUnit(cnt);
--cnt;
++p;
++q;
}
_result =new char[strlen(tmp)+1];
memset(_result,0,strlen(tmp)+1);
memcpy(_result,tmp,strlen(tmp));
_result[strlen(_result)]='\0';
delete tmp;
p=q=tmp=nullptr;
}
char *Money::getMake()
{
return _result;
}
关注
笔者 - jxd
微信公众号搜索 “码农总动员” 或 微信扫描下方二维码,了解更多你不知道的XX,O(∩_∩)O