如何去掉字符串中的小数点?急急急!!!
时间:2008-06-12 15:10:02
来源:论坛整理 作者: 编辑:chinaitzhe
字符串 1328.00 转换为 132800
字符串 1356.10 转换为 135610
网友回复:char *strtok(char *str1, char *str2);
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #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; }
网友回复: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);
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #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; }
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #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; }
网友回复:#include <string>
#include <iostream>
using namespace std;
void main()
{
string s = "1.2356";
int i = s.find('.');
s.erase(i,i);
cout < <s < <endl;
}
网友回复:#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这个函数。很简单,所以就不多写了
网友回复:乘以100,截取一下
网友回复:#include"string.h"
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ void qd(char *p) { int i=0; while(p[i]!='.' && p[i]!='\0')i ; if(p[i]!='\0')strcpy(p i,p i 1);//假如字符串中没有小数点就不用去除 }
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <string.h> #include <iostream> using namespace std; int main() { char* str = "1328.00"; int temp = atoi(str); cout<<temp*100<<endl; }
雁南飞的.....
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ /* 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"); }
网友回复:编译成功之后生成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
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ 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
网友回复:lz可以给分了
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ //用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就是你想要的东西咯 }
关键字:去掉,字符串,小数点,急急,
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面