如何去掉字符串中的小数点?急急急!!!

sxebcnk 2008-06-02 07:23:53
比如: 字符串 12789.56 转换为 1278956
字符串 1328.00 转换为 132800
字符串 1356.10 转换为 135610
...全文
1815 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
des2006 2008-06-04
  • 打赏
  • 举报
回复

//用stl的string,问题很简单的
#include <string>
#include <iostream>
using namespace std;
void main()
{
std::string str1 = "12789.56";
std::string res = str1.substr(0,str1.find(".",0,1)-1)+str1.substr(str1.find(".",0,1)+1,str1.size());
cout<<res<<endl;//res就是你想要的东西咯
}
yyyapple 2008-06-04
  • 打赏
  • 举报
回复
lz可以给分了
yyyapple 2008-06-03
  • 打赏
  • 举报
回复

void chardel(char *source, const char sub)
{
char *pc1, *pc2;
int isource;
isource= strlen(source);

while(*source != 0)
{
pc1 = source;

if(sub == *pc1)
{
memmove(pc1, pc1+1, strlen(source));
} else {
source++;
}
}
}

int main()
{
char str[32] = "1328.00";
chardel(str, '.');

}

chardel
  • 打赏
  • 举报
回复
编译成功之后生成d.exe
从命令行输入:
1)
输入:
d
输出:
Input Error!
Usage:programmename data
2)
输入:
d agkja1324
输出:
Input Error!
(The inputed data must be the numbers 0-9 or + - . and + - must be the first.)
3)
输入:
d 123.12
输出:
12312
4)
输入:
d 123.12.34
输出:
1231234
  • 打赏
  • 举报
回复

/* d.c
* 如何去掉字符串中的小数点?
* 比如: 字符串 12789.56 转换为 1278956
* 字符串 1328.00 转换为 132800
* 字符串 1356.10 转换为 135610
*/

#include <stdio.h>
#include <stdlib.h>

void judge_argc(int argc);
void judge_inputdata(char *pInputData);
void del_dot(char *pInputData);
void display(char *argv);

int main(int argc,char *argv[])
{
judge_argc(argc);
judge_inputdata(argv[1]);
del_dot(argv[1]);
display(argv[1]);

return 0;
}

/**
* @brief
* This function named judge_argc is used for judging the number of
*data inputed from command line.
*If the data is wrong,it will show an information prompts error.
*@param argc the number of the command line param.
*/
void judge_argc(int argc)
{
if(argc < 2)
{
printf("Input Error!\nUsage:programmename data\n");
exit(0);
}
}

/**
* @brief
* This function named judge_inputdata is used for judging the validity of
*data inputed from command line.
*If the data is wrong,it will show an information prompts error.
*@param pInputData the pointer of the command line param array.
*/
void judge_inputdata(char *pInputData)
{
int i;

/* ASCII code:+:043 -:045 .:046 0-9:048-057 */
for(i = 0;i < strlen(pInputData);i++)
{
if(((pInputData[i] >= 48) && (pInputData[i] <= 57)) || (pInputData[i] == 46))
{
continue;
}
else
{
if(((pInputData[i] == 43) || (pInputData[i] == 45)) && (i == 0))
{
continue;
}
else
{
printf("Input Error!\n(The inputed data must be the numbers 0-9 or + - . and + - must be the first.)\n");
exit(0);
}
}
}
}

/**
* @brief
* This function named del_dot is used for delete the dot in the data.
*@param pInputData the pointer of the command line param array.
*/
void del_dot(char *pInputData)
{
int i,j,k;
int iFlag = 0; /* the number of the dot */
int iLen = strlen(pInputData);

for(i = 0;i < iLen;i++)
{
if(pInputData[i] == '.')
{
iFlag = iFlag + 1;
for(j = (i + 1),k = i;j < iLen;j++,k++)
{
pInputData[k] = pInputData[j];
}
}
}

if(0 != iFlag)
{
for(i = 0;i < iFlag;i++)
{
pInputData[iLen - i - 1] = '\0';
}
}
}

/**
* @brief
* This function named display is used for display the processed data.
*@param argv the pointer of the processed data.
*/
void display(char *argv)
{
int i;

printf("The processed data is:\n");
for(i = 0;i < strlen(argv);i++)
{
printf("%c",argv[i]);
}
printf("\n");
}
xhd3767 2008-06-02
  • 打赏
  • 举报
回复


#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char* str = "1328.00";
int temp = atoi(str);
cout<<temp*100<<endl;

}

雁南飞的.....
leeldy 2008-06-02
  • 打赏
  • 举报
回复
#include"string.h"

void qd(char *p)
{
int i=0;
while(p[i]!='.' && p[i]!='\0')i++;
if(p[i]!='\0')strcpy(p+i,p+i+1);//如果字符串中没有小数点就不用去除
}
飞哥 2008-06-02
  • 打赏
  • 举报
回复
乘以100,截取一下
noliper 2008-06-02
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stlib.h>

#include <string.h>

char *p = "12345.78";
char *tmp;
char buf[100];

memset(buf,'\0',100);
tmp = strrchr(p,".");
memcpy(buf,p,tmp -p -1);
atoi(buf);

程序写错了,不过可以考虑strrchr这个函数。很简单,所以就不多写了
bitxinhai 2008-06-02
  • 打赏
  • 举报
回复
#include<string>
#include<iostream>
using namespace std;
void main()
{
string s = "1.2356";

int i = s.find('.');
s.erase(i,i);
cout<<s<<endl;

}
K行天下 2008-06-02
  • 打赏
  • 举报
回复

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

char* getoutpoint(char* s)
{
int i, j,len = strlen(s);
char* out =malloc(len);
for(i=0,j=0; i<len;i++)
{
if(*(s+i)!='.')
{
out[j]= *(s+i);
/* printf("%c\t%d\n",out[j],j); */
j++;

}
}
out[j] = '\0';
return out;
}
int main()
{
char* p1 = getoutpoint("12789.56");
printf("%s\n",p1);
getch();
free(p1);
return 0;
}
chenzhp 2008-06-02
  • 打赏
  • 举报
回复
#include <string.h>
#include <stdio.h>

int main(void)
{
char input[16] = "abc,d";
char *p;

/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
if (p) printf("%s
", p);

/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%s
", p);
return 0;
}
野男孩 2008-06-02
  • 打赏
  • 举报
回复
char str[] = "12789.56";
char dst[1024] = {0};

char* p = strtok(str, ".");
while(p)
{
strcat(dst, p);
p = strtok(NULL, ".");
}

printf("dst = %s\n", dst);
baihacker 2008-06-02
  • 打赏
  • 举报
回复
#include  <stdio.h>
char* Modify(char* str)
{
char* curr = str, * ret = str;
while (*curr=*str++)
if (*curr != '.') curr++;
return ret;
}
int main(int argc, char* argv[])
{
char data[][32] = {"12789.56", "1328.00", "1356.10"};
int i;
for (i = 0; i < sizeof(data)/sizeof(data[0]); ++i)
printf("%s\n", Modify(data[i]));
return 0;
}
chlaws 2008-06-02
  • 打赏
  • 举报
回复
char *strtok(char *str1, char *str2);

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧