hw机试题,总结就是尽量少用库函数,其次每道题都会涉及到输入字符串分割与string转化成int型数据
代码如下:
#include <iostream>
#include <string>
#include<stdlib.h>
using namespace std;
int pos1=0;
int count=0;
void iscount(int a){
if(!(a&(a-1))){
count++;
}
}
/**重点函数(字符分割)*/
string spiltStream(string str,int pos,char symbol){
string result;
int j=pos;
while(str[j]!=symbol&&str[j]!='\0')
{
result+=str[j++];
}
pos1=j+1;
return result;
}
/**重点函数(atoi)*/
int myatoi(string s){
int i,j,k,len,value = 0;
int flag = 1;
len=s.size();
j=0;
for(i=0; i<len; i++)
{
if(s[i]!=' ' && s[i]!='\t')
{
s[j]=s[i];
j=j+1;
}
}
s[j]='\0';
if(j==0) return 0;
k=0;
if (s[0] == '-')
{
flag =-1;
k=1;
}
else if (s[0]=='+')
{
flag = 1;
k=1;
}
for(i=k; i<j; i++)
{
value = value * 10 + s[i]-'0';
}
value =flag*value;
return value;
}
int main(){
string strNum,sub_str;
cout<<"please input the number:";
cin>>strNum;
int number;
int str_size=strNum.size();
while(pos1<=str_size){
string temp=spiltStream(strNum,pos1,',');
number=myatoi(temp);
iscount(number);
}
cout<<count<<endl;
return 0;
}