创意安天

 找回密码
 注册创意安天

C++与C转换问题,求解

[复制链接]
发表于 2008-4-9 16:13 | 显示全部楼层 |阅读模式
正常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;
}
但并不能正常按回车返回,请各位看官求解.
 楼主| 发表于 2008-4-9 16:16 | 显示全部楼层
第三个程序,能够正确编译与运行,但就是不能返回。
后我改成循环条件为  while(cin>> c),同样不能返回。没招了就
 楼主| 发表于 2008-4-9 17:05 | 显示全部楼层
后经LFX指点改成如下,但有弊端,就是可能用户输入足够长的字符串,造成溢出。有没有完美的方法呢,期待中
   char szData[1024] = {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;
发表于 2008-4-25 06:20 | 显示全部楼层
送你一段代码

#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 编辑 ]
发表于 2008-5-3 10:39 | 显示全部楼层
不懂!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
发表于 2008-5-3 13:11 | 显示全部楼层

以下代码低效但更灵活!

#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[0]; i ++)
                {
                        string s = g_StringCount[i].lpSet;
                        if (string::npos != s.find(c))
                        {
                                mapTypeCount[g_StringCount[i].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
 楼主| 发表于 2008-5-5 16:15 | 显示全部楼层
飞刀李代码高深,看起来比较困难,感谢liveck
 楼主| 发表于 2008-5-5 16:15 | 显示全部楼层
续上。。。。。
还有飞刀李.
您需要登录后才可以回帖 登录 | 注册创意安天

本版积分规则

小黑屋|手机版|Archiver|创意安天 ( 京ICP备09068574,ICP证100468号。 )

GMT+8, 2024-5-19 11:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表