如何循环输出想要的字符串

Rebornor 2008-04-08 10:00:09
我想得到以下形式字符串的循环输出:
keyword=%CE%C4%D4%B1&jobarea=上海&stype=2&image.x=35&image.y=15
职位名要用%+16进制表示(如上面的=%CE%C4%D4%B1就是教师的意思)
其中变量有两个:分别如下,储存在m,n两个数组中。
--------------------------想以开头要求的格式循环得到所要求的字符串,请问该如何该?或者是请高手支招。


char m[M] = {"上海","台湾","广州"};


char n[N]={"文员","教师","计算机","医生","销售"};
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define M 3
#define N 5

int main(int argc, char* argv[])
{
char m[M] = {"上海","台湾","广州"};


char n[N]={"文员","教师","计算机","医生","销售"};


char str[32];

for (int i = 0; i < 3; ++i)
for (int j = 0; j < 5; ++j)
{
sprintf(str, "jobkeyword=%%%X&jobarea=%s", m[i], n[j]);
cout<<str<<endl;
}}
...全文
61 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Rebornor 2008-04-09
[Quote=引用 3 楼 jennyvenus 的回复:]
C/C++ code// test1.cpp : Defines the entry point for the console application.
// char *n[]={"文员","教师","计算机","医生","销售"};
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

char *gethex( char *src, char *buf )
{
char t[ 10 ] = "";

####################
编译能成功,但是上面有个细节错误:以至于不能显示出正确的16进制码:
sprintf( buf + i * 3, "%%%.2X", ( unsigned char )( *src + i ) );
最后应该改成:
src[i];

回复
Rebornor 2008-04-08
不错,去掉“stdafx.h”头文件了搞定!
如果用CString类实现是不是简单一些呢?
[Quote=引用 3 楼 jennyvenus 的回复:]
C/C++ code// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define M 3
#define N 5

char *m[] = {"上海","台湾","广州"};


char *n[]={"文员","教师","计算机","医生","销售"};
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

char *gethex( char *src, char *buf )
{
char t[ 10 ] = "";

[/Quote]
回复
// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define M 3
#define N 5

char *m[] = {"上海","台湾","广州"};


char *n[]={"文员","教师","计算机","医生","销售"};
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

char *gethex( char *src, char *buf )
{
char t[ 10 ] = "";
int i;
for( i = 0; i < strlen( src ); i++ )
{
sprintf( buf + i * 3, "%%%.2X", ( unsigned char )( *src + i ) );
}
return buf;
}

int main(int argc, char* argv[])
{


char str[100];

for (int i = 0; i < 3; i++)
for (int j = 0; j < 5; j++)
{
//sprintf(str, "jobkeyword=%%%X&jobarea=%s", m[i], n[j]);
*str = 0;
strcat( str, "jobkeyword=" );
gethex( m[ i ], str + strlen( str ) );
strcat( str, "jobarea=" );
gethex( n[ j ], str + strlen( str ) );
cout <<str <<endl;
}
return 0;
}
回复
看错了,等一下。
回复
// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define M 3
#define N 5

char *m[] = {"上海","台湾","广州"};


char *n[]={"文员","教师","计算机","医生","销售"};
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main(int argc, char* argv[])
{


char str[100];

for (int i = 0; i < 3; i++)
for (int j = 0; j < 5; j++)
{
sprintf(str, "jobkeyword=%%%X&jobarea=%s", m[i], n[j]);
cout <<str <<endl;
}
return 0;
}
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2008-04-08 10:00
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……