您的位置:首页 > 其它

格式字符串的输入输出

2015-04-25 19:27 28 查看
22号去参加华为的武汉实习生上机考试,3道题目,当时就做出来两道,有一道格式字符串的题目没有做出来。回到学校之后还是重新想了想,把当时没做出来的再做一遍。

原题在华为的题库中也没找到,我就凭自己的记忆重新写个大意一样的题目了。

题目差不多是这样的:

有一个格式化的字符串,如下所示:

name=Jorden,job=palyer,age=45


编写代码,可以识别以上的格式,如果输入格式不对,会报错。同时输出格式如下:

[[name,Jorden],[job,player],[age,45]]


其实现在想想这里给的信息也不是很全啊,没有说允不允许空输入的存在。(不知道是不是我忘了点题目的内容 = =)。例如:

name=Jorden,job=,age=45


job为空,输入格式符合不?我是忽略这种情况了。按我的理解就是把

'=' 和 ','


当作关键字符了,如果输多了、少了或者输入的顺序不对就报错了。

以下是我的代码:

#include<stdio.h>
#include<malloc.h>

#define true 1
#define false 0

//获得字符串长度
int getlen(char *s)
{
int i;
for (i=0; s[i]!='\0'; i++) {
;
}
return i;
}
//获得特定字符在字符串内的个数
int num_char(char *s, char flg)
{
int i, count = 0;
for (i=0; s[i]!='\0'; i++) {
if (s[i] == flg) {
count++;
}
}
return count;
}
//获得特定字符在字符串内的位置,返回p
void find_char(char *s, char flg, int *p)
{
int i, index = 0;
for (i=0; s[i]!='\0'; i++) {
if (s[i] == flg) {
p[index++] = i;
}
}
}

int main(void)
{
char s[256], out[256] = "[[";
int count_c, count_e, count_t;
int *p_c, *p_e, *p_t;
int i, j, e=0, c=0, flg, n=2;
scanf("%s", s);

count_c = num_char(s, ',');
count_e = num_char(s, '=');

//判断逗号个数跟等号个数的关系
if ((count_e < 1) || (count_c != count_e - 1)) {
printf("input error");
return 1;
}
//p_c逗号的位置,p_e等号的位置
p_c = (int *)(malloc(sizeof(int) * count_c));
p_e = (int *)(malloc(sizeof(int) * count_e));

find_char(s, ',', p_c);
find_char(s, '=', p_e);
//等号在逗号之前,同时最后一个等号在最后一个逗号之后
//其实这里默认允许空内容的存在了。
for (i=0; i<count_c; i++) {
if (p_c[i] < p_e[i]) {
printf("input error");
return 1;
}
}
if (p_e[count_e-1] < p_c[count_c-1]) {
printf("input error");
return 1;
}

//把所有的位置都记录下来,第一个位置是-1,最后一个为字符串长度
count_t = count_c + count_e + 2;
p_t = (int *)(malloc(sizeof(int) * (count_t)));
p_t[0] = -1;
flg = true;
for(i=1; i<count_t-1; i++) {
if (flg == true) {
p_t[i] = p_e[e++];
} else {
p_t[i] = p_c[c++];
}
flg = !flg;
}
p_t[count_t - 1] = getlen(s);

free(p_c);
free(p_e);

//把位置里的字符串移到out字符串内
flg = true;
for (i=0; i<count_t-1; i++) {
for (j=p_t[i]+1; j<p_t[i+1]; j++) {
out[n++] = s[j];
}
if (flg == true) {
out[n++] = ',';
} else {
out[n++] = ']';
out[n++] = ',';
out[n++] = '[';
}
flg = !flg;
}
//收尾处理
n=n-2;
out[n++] = ']';
out[n++] = '\0';

free(p_t);
printf("%s", out);

system("pause");
return 0;
}


以下是禁止了输入为空的:

#include<stdio.h>
#include<malloc.h>

#define true 1
#define false 0

//获得字符串长度
int getlen(char *s)
{
int i;
for (i=0; s[i]!='\0'; i++) {
;
}
return i;
}
//获得特定字符在字符串内的个数
int num_char(char *s, char flg)
{
int i, count = 0;
for (i=0; s[i]!='\0'; i++) {
if (s[i] == flg) {
count++;
}
}
return count;
}
//获得特定字符在字符串内的位置,返回p
void find_char(char *s, char flg, int *p)
{
int i, index = 0;
for (i=0; s[i]!='\0'; i++) {
if (s[i] == flg) {
p[index++] = i;
}
}
}

int main(void)
{
char s[256], out[256] = "[[";
int count_c, count_e, count_t;
int *p_c, *p_e, *p_t;
int i, j, e=0, c=0, flg, n=2;
scanf("%s", s);

count_c = num_char(s, ',');
count_e = num_char(s, '=');

//判断逗号个数跟等号个数的关系
if ((count_e < 1) || (count_c != count_e - 1)) {
printf("input error");
return 1;
}
//p_c逗号的位置,p_e等号的位置
p_c = (int *)(malloc(sizeof(int) * count_c));
p_e = (int *)(malloc(sizeof(int) * count_e));

find_char(s, ',', p_c);
find_char(s, '=', p_e);
//等号在逗号之前,同时最后一个等号在最后一个逗号之后
//这里的判断变了。
if ((p_e[0] == 0) || (p_e[count_e - 1] == (getlen(s) - 1))) {
printf("input error");
return 1;
}

for (i=0; i<count_c; i++) {
if (p_c[i] <= p_e[i] + 1) {
printf("input error");
return 1;
}
}
if (p_e[count_e-1] <= p_c[count_c-1] + 1) {
printf("input error");
return 1;
}

//把所有的位置都记录下来,第一个位置是-1,最后一个为字符串长度
count_t = count_c + count_e + 2;
p_t = (int *)(malloc(sizeof(int) * (count_t)));
p_t[0] = -1;
flg = true;
for(i=1; i<count_t-1; i++) {
if (flg == true) {
p_t[i] = p_e[e++];
} else {
p_t[i] = p_c[c++];
}
flg = !flg;
}
p_t[count_t - 1] = getlen(s);

free(p_c);
free(p_e);

//把位置里的字符串移到out字符串内
flg = true;
for (i=0; i<count_t-1; i++) {
for (j=p_t[i]+1; j<p_t[i+1]; j++) {
out[n++] = s[j];
}
if (flg == true) {
out[n++] = ',';
} else {
out[n++] = ']';
out[n++] = ',';
out[n++] = '[';
}
flg = !flg;
}
//收尾处理
n=n-2;
out[n++] = ']';
out[n++] = '\0';

free(p_t);
printf("%s", out);

system("pause");
return 0;
}


如果代码有错,多谢指点。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: