目录
3.用户输入一个英文字符串,只将其中的偶数下标位置字符排序。
1.文字信息统计。
键盘输入一段中英文文章,分别计算以下字符的个数。
① 字符总个数(包括空格);
② 大小写字母个数;
③ 数字个数;
④ 中文字符个数;
⑤ 其他字符个数。
#include<iostream>
using namespace std;
int main()
{
const int N=1000;
char pass[N];
int all=0,vocal=0,digital=0;
int chinese=0,others=0;
cin.getline(pass,N);
int i=0;
while(pass[i]!='\0')
{
all++;
if((pass[i]>='a'&&pass[i]<='z')||(pass[i]>='A'&&pass[i]<='Z'))
{
vocal++;
}
else if(pass[i]>='0'&&pass[i]<='9')
{
digital++;
}
else if(pass[i]<0)
{
i++;
chinese++;
}
else {
others++;
}
i++;
}
cout<<"总字符数"<<all<<endl;
cout<<"数字字符数"<<digital<<end;
cout<<"字母个数"<<vocal<<endl;
cout<<"中文字符"<<chinese<<endl;
cout<<"其他字符"<<others<<endl;
return 0;
}
2.整数的英文显示。
键盘输入一个整数,将其翻译为英文。
例如:
输入1234
输出one thousand two hundred and thirty four
我重新改了改我的代码,还有最后的循环部分应该是3*i而不是3*i-1
#include<iostream>
#include<string>
using namespace std;
//单个列出
void f1(char x)//1-9
{
switch(x-'0')
{
case 1:cout<<"one"<<' ';break;
case 2:cout<<"two"<<' ';break;
case 3:cout<<"three"<<' ';break;
case 4:cout<<"four"<<' ';break;
case 5:cout<<"five"<<' ';break;
case 6:cout<<"six"<<' ';break;
case 7:cout<<"seven"<<' ';break;
case 8:cout<<"eight"<<' ';break;
case 9:cout<<"nine"<<' ';break;
}
}
void f2(char x)//10-19
{
switch(x-'0'){
case 1:cout<<"ten"<<' ';break;
case 2:cout<<"twelve"<<' ';break;
case 3:cout<<"thirteen"<<' ';break;
case 4:cout<<"fourteen"<<' ';break;
case 5:cout<<"fifteen"<<' ';break;
case 6:cout<<"sixteen"<<' ';break;
case 7:cout<<"seventeen"<<' ';break;
case 8:cout<<"eighteen"<<' ';break;
case 9:cout<<"nineteen"<<' ';break; }
}
void f3(char x)//20-90
{
switch (x-'0'){
//case 1:cout<<"one"<<' ';break;
case 2:cout<<"twenty"<<' ';break;
case 3:cout<<"thirty"<<' ';break;
case 4:cout<<"forty"<<' ';break;
case 5:cout<<"fifty"<<' ';break;
case 6:cout<<"sixty"<<' ';break;
case 7:cout<<"seventy"<<' ';break;
case 8:cout<<"eighty"<<' ';break;
case 9:cout<<"ninty"<<' ';break; }
}
//C++中string作为参数的传递
void print_weishu(int x)
{
if(x==1)
{
cout<<"thousand"<<' ';
}
else if(x==2)
{
cout<<"million"<<' ';
}
else if(x==3)
{
cout<<"billion"<<' ';
}
}
void print_hun(const string &str) //这里的const是因为下面传入的是一个常量string
{
int len=str.size();
if(len==3)
{
f1(str[0]);
if(str[0]>'1')
{
cout<<"hunreds"<<' ';
}
else if(str[0]=='1'){
cout<<"hunred"<<' ';
}//解决百位数
if(str[1]=='1')
{
cout<<"and"<<' ';
f2(str[2]);
}
else if(str[1]>'1')
{
cout<<"and"<<' ';
f3(str[1]);
f1(str[2]);
}
}
else if(len==2)
{
if(str[0]=='1')
{
f2(str[1]);
}
else if(str[0]>'1')
{
f3(str[0]);
f1(str[1]);
}
}
else if(len==1)
{
f1(str[0]);
}
}
int main()
{
//输入
string n;
getline(cin,n);
// cout<<n.size()<<endl;
// cout<<n;
int len=n.size();
int weishu=len/3;//循环次数 每一次都有百位,多余的直接加进制单词
int top=len%3;
if(top==0)
{
weishu--;
}
//hunred thousand million billion 1、000、000
//字符串分割,[].substr()--指定位置处的指定长度
//[].substring()起点位置,终点位置
for(int i=0;i<=weishu;i++)
{
if(i==0&&top!=0)
{
print_hun(n.substr(0,top));
print_weishu(weishu-i) ;
}
else if(i==0&&top==0)
{
print_hun(n.substr(0,3));
print_weishu(weishu-i);
}
else
{
print_hun(n.substr(3*i,3));
print_weishu(weishu-i);
}
}
return 0;
}
整数机器人
#include<iostream>
#include<cstring>
using namespace std;
class robot
{
protected:
char name[20];
char type[20];
// int num;//待翻译的整数
// char *ps//指向英文字符的字符串
public:
robot()
{
strcpy(name,"XXX");
strcpy(type,"XXX");
//num=0;
}
void set(char n[],char t[]);//设置修改数据
void out(int a);//英文中每三位数读法相同,所以定义Out函数翻译小于1000的整数
void tran_int(int n);//将1至1999999999的整数翻译成英文句子
~robot(){}
};
//定义了两个全局字符指针数组,存取所需的单词
//num1中为1到19,空出了0,所以可以直接用num1[n]调用,得到n对应的单词
static char* num1[]=
{
"","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen"
};
//num10中为20-90空出了0和1,所以可以直接用num10[n/10]调用,得到n对应单词
static char* num10[]=
{
"","","twenty","thirty","forty","fifty","sixty","seventy",
"eighty","ninety"
};
void robot::out(int a)
{
int b=a%100;
//若百位不等于0,输出百位数加hundred,若此时十位个位均为0,不加and
if(a/100!=0)
{
cout<<num1[a/100]<<' '<<"hunred"<<' ';
if(b!=0)
{
cout<<"and"<<' ';
}
}
if(b<20)
{
cout<<num1[b]<<' ';
}
else
{
//先调用num10,输出十位数
cout<<num10[b/10]<<' ';
//个位不为0时,应输出个位数
if(b%10!=0)
{
cout<<num1[b%10]<<' ';
}
}
}
void robot::tran_int(int n)
{
if(n>1999999999)
{
cout<<"dev C++平台无法处理大于1999999999位的数!"<<endl;
}
else
{
//三位三位取出,存入abcd中
int a=n/1000000000;
int b=(n%1000000000)/1000000;
int c=(n%1000000)/1000;
int d=n%1000;
if(a!=0)
{
out(a);
cout<<"billion"<<' ';
}
if(b!=0)
{
out(b);
cout<<"million"<<' ';
}
if(c!=0)
{
out(c);
cout<<"thousand"<<' ';
}
if(d!=0)
{
//根据英语语法规则,最后前两位前一定有and
if(d<100&&(a!=0||b!=0||c!=0))
{
cout<<"and"<<' ';
}
out(d);
}
}
cout<<endl;
}
int main()
{
int n;
cout<<"输入n:";
cin>>n;
robot brown;
brown.tran_int(n);
return 0;
}
3.用户输入一个英文字符串,只将其中的偶数下标位置字符排序。
注意:下标从0算起。
例如,输入"student",输出"etsdtnu"。
#include<iostream>
#include<cstring>//cstring与string区别?
using namespace std;
int main()
{
//输入 char 类型数组
//排序
const int N=100;
char x[N];
cin.getline(x,N);
int len=strlen(x);
//cout<<len;
for(int i=0;i<len/2+1;i++)//选择法排序
{
int k=i;//选出最小的
for(int j=i;j<len/2+1;j++)
{
if(x[2*k]>x[2*j])
{
char temp=x[2*k];
x[2*k]=x[2*j];
x[2*j]=temp;
}
}
}
for(int j=0;j<len;j++)
{
cout<<x[j];
}
}
4.去掉一个字符串末尾的空格符。
例如:输入"abc ",输出"abc"。
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
string str;
getline(cin,str);
int len=str.size();
int k=str.find_last_not_of(' ');
//在字符串中查找最后一个与str中字符都不匹配的字符。返回它的位置
//如果没有找到就返回string::nops
if(k!=string::npos)
{
str.erase(k+1,str.size()-k-1);//除去的子字符串长度是str.size()-k
}
cout<<str;
//cout<<"mis";//测试用的hhh
}