C++与C转换问题,求解
正常C代码如下:#include <stdio.h>
void main()
{
char c;
int letter=0,space=0,digit=0,other=0;
printf("请输入一串字符,我们将统计各种字符的数量:\n");
while ((c=getchar()) != '\n')
{
if ((c>='a' && c<='z' || c>='A' && c<='Z'))
letter++;
else
if(c==' ')
space++;
else if(c>='0' && c<= '9')
digit++;
else
other++;
}
printf("字母=%d,空格=%d,数字=%d,其它=%d,\n",letter,space,digit,other);
}
想换成C++代码,改进如下过渡格式:
#include <iostream.h>
#include <stdio.h>
void main()
{
char c;
int letter=0,space=0,digit=0,other=0,flag=1;
cout << "请输入一串字符,我们将统计各种字符数量:" << endl;
while ((c=getchar()) != '\n')
{
if(c=='\n')
break;
if(c >= 'a' && c<= 'z' || c >= 'A' && c<= 'Z')
letter++;
else if(c== ' ')
space++;
else if(c>= '0' && c<='9')
digit++;
else
other++;
}
cout << "字母= "<< letter << endl;
cout << "空格= "<< space<< endl;
cout << "数字= "<< digit<< endl;
cout << "其它= "<< other<< endl;
}
但并不是真正的C++代码,最终修改如下:
#include <iostream.h>
void main()
{
char c;
int letter=0,space=0,digit=0,other=0,flag=1;
cout << "请输入一串字符,我们将统计各种字符数量:" << endl;
cin >> c;
while ( c != '\n')
{
if(c=='\n')
break;
if(c >= 'a' && c<= 'z' || c >= 'A' && c<= 'Z')
letter++;
else if(c== ' ')
space++;
else if(c>= '0' && c<='9')
digit++;
else
other++;
cin >> c;
}
cout << "字母= "<< letter << endl;
cout << "空格= "<< space<< endl;
cout << "数字= "<< digit<< endl;
cout << "其它= "<< other<< endl;
}
但并不能正常按回车返回,请各位看官求解. 第三个程序,能够正确编译与运行,但就是不能返回。
后我改成循环条件为while(cin>> c),同样不能返回。没招了就 后经LFX指点改成如下,但有弊端,就是可能用户输入足够长的字符串,造成溢出。有没有完美的方法呢,期待中
char szData = {0};
int letter=0,space=0,digit=0,other=0,flag=1;
cout << "请输入一串字符,我们将统计各种字符数量:" << endl;
cin >> szData;
for( int i = 0 ; i < strlen(szData) ; i++ )
{
char c = szData;
if(c=='\n')
break; 送你一段代码
#include <iostream>
#include <string>
using namespace std;
int main(int argc,char** argv)
{
int letter=0,space=0,digit=0,other=0,flag=1;
char c;
cout << "请输入一串字符,我们将统计各种字符数量:" << endl;
while(cin.get(c))
{
if(c=='\n')
break;
else if(isalpha(c))
letter++;
else if(c==' ')
space++;
else if(isdigit(c))
digit++;
else
other++;
}
cout<<"字母="<< letter
<<",空格="<< space
<<",数字="<< digit
<<",其它="<< other<< endl;
return 0;
}
[ 本帖最后由 liveck 于 2008-4-25 06:59 编辑 ] 不懂!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
:lol :lol :lol
以下代码低效但更灵活!
#pragma warning (disable:4786)#include <iostream>
#include <string>
#include <map>
using namespace std;
struct {
const char * lpSet;
const char * lpType;
} g_StringCount[] =
{
{"abcdefghijklmnopqrstuvwxyz", "小写字母"},
{"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "大写字母"},
{"0123456789", "数字"},
{" ", "空格"},
{"AEIOUaeiou", "元音字母"},
};
int main(int argc, char* argv[])
{
typedef map<string, int> MapTypeCount;
MapTypeCount mapTypeCount;
int nOther = 0;
for (char c = 0; cin.get(c) && c != '\n';)
{
bool bFound = false;
for (int i = 0; i < sizeof g_StringCount / sizeof g_StringCount; i ++)
{
string s = g_StringCount.lpSet;
if (string::npos != s.find(c))
{
mapTypeCount.lpType] ++;
bFound = true;
}
}
if (!bFound) nOther ++;
}
for (MapTypeCount::iterator it = mapTypeCount.begin(); it != mapTypeCount.end(); it ++)
{
cout << it->first << ": " << it->second << endl;
}
cout << "其它字符: " << nOther << endl;
return 0;
}
-----
Hello world ! - Powered by SwordLea
大写字母: 4
空格: 6
小写字母: 23
元音字母: 9
其它字符: 2
Press any key to continue 飞刀李代码高深,看起来比较困难,感谢liveck 续上。。。。。
还有飞刀李.
页:
[1]