首页 > 试题广场 >

计算器出错:把所有的‘0’都变成了‘*’,而且不能智能的消除

[问答题]

计算器出错:把所有的‘0’都变成了‘*’,而且不能智能的消除小数点数字后多余的0。

比如100.00100写成了1**.**1**,现在要求编程序转换回来,并消除小数点后多余的零。

如输入:1.**1**

输出:1.001

JavaScript代码:
String.prototype.ConvertToNumStr=function(){
    var result=this.replace(/\*/g,"0"),
      index=result.indexOf(".");
    if(index==0){
        result="0"+result.replace(/0*$/g,"");
    }else if(index==1){
        result=result.replace(/0*$/g,"");
    }else if(index>1){
        result=result.replace(/^0*|0*$/g,"");         if(result.indexOf(".")==0)result="0"+result;
    }else{
        result=result.replace(/^0*/g,"");
    }
    if(result.length==0){
        result="0";
    }else if(result.lastIndexOf(".")==result.length-1){         result=result.substring(0,result.length-1);
    }
    return result;
}
测试结果:

发表于 2015-06-16 14:57:15 回复(2)
    System.out.println(Double.parseDouble("11**.1***".replaceAll("\\*", "0")));
发表于 2015-06-16 12:32:21 回复(2)
#include <iostream>
#define CHANGE_ERROR(c) c=='*' ? 0 : c
#define REDUCE_ZERO(c) c=='0' ? '\0' : c
int main()
{
char str[100] = {'\0'};
std::cin>>str;
bool reduceZero = flase;
for(int i = 0; i<100; i++)
{
CHANGE_ERROR(str[i]);
if(str[i] == '.')
{reduceZero = true;}
}
while(reduceZero = true)
{
for(int i = 99; i >= 0; i--)
{
if(str[i] == '0')
{
REDUCE_ZERO(str[i]);
}else{break;}
}
break;
}
std::cout<<str<<std::endl;
return 0;
}
编辑于 2015-06-16 11:14:40 回复(0)
#include<iostream>
#include<string>

using namespace std;

int main(){
    string str;
    int len = 0;//输入数字的长度
    int pointPosition;//小数点的位置
    int flag;//小数点后面最后一位非零的数字的位置
    getline(cin,str);
    len = str.length();//得到输入数字字符的长度
    pointPosition = len;//小数点位置默认值
    flag = len;
    for(int i = 0; i < len; i++){
        if(str[i] == '.'){
            cout << '.';
            pointPosition = i;//获取小数点的位置
            continue;
        }

        if(i < pointPosition){ //处理小数点前面的数字
            if(str[i] == '*')
                str[i] = '0';
            cout << str[i];
        }
        else{                 //处理小数点后面的数字
            if(str[i] == '*') //将*还原为0
                str[i] = '0';
            else if(str[i] != '*')//如果不是*,那么说明该位为数字,记录该位置
                flag = i;
        }
    }
    pointPosition++;
    while(flag < len && pointPosition <= flag){ //打印小数点后面的,位于最后一位有效数字(包括该位)之前的数字
        cout << str[pointPosition]; 
        pointPosition++;
    }
    cout << endl;
    return 0;
}
发表于 2015-06-16 21:04:50 回复(0)
用C实现
#include <stdio.h>
#include <string.h>


int main()
{
    char a[20];
    char *p=a;
    int i=0;
    scanf("%s",p);
    while(*p!='\0'){
        if(*p=='*') *p='0';
        *p++;
        i++;
    }
    while(a[i-1]=='0') {a[i-1]='\0';i--;}

    printf("%s",a);
}
发表于 2015-06-18 11:15:21 回复(0)
测试数据  input
1**.**1**
1.**1**
1**.**1
.****
1****2
1***
 
输出   output
100.001 1.001 100.001  . 100002  1000

#include<iostream>
#include<string>
using namespace std;

/*
悬赏题
*/
class Solution
{
    public :
        string Convert(string str)
        {
            bool flag = false;    //标记是否有小数点
            
            if( str.empty() )
            {
                return str;
            }
            for(int i = 0;i < str.size();i++)    //先变换
            {
                if(str[i] == '.')
                {
                    flag = true;
                    continue;
                }
                if(str[i] == '*')
                {
                    str[i] = '0';
                }
                
            }
            int count = 0;
            if(flag)
            {
                for(int i = str.size() - 1;str[i] == '0';i--)    //记录无效0的个数
                {
                    ++count;
                }
            }
            //cout << str << endl;
            str = str.substr(0,str.size() - count);
            
            return str;
        }
};
int main() {
    // your code goes here
    string str;
    Solution A;
    
    while(cin >> str)
    {
        cout << A.Convert(str) << endl;    
    }
    
    return 0;
}



发表于 2015-06-16 23:39:03 回复(0)
js
var str = "1**.**1**".replace(/\*/g,"0");
str = ~str.indexOf(".") ? str.replace(/\.?0*$/,"") : str;
alert(str);

发表于 2015-06-16 21:01:05 回复(0)
public class Replace {

    public String replace(String number){
        
        if(number==null||number.equals("")){
            return null;
        }
        number=number.replaceAll("\\*", "0");//特殊字符,需要正则表达式
        boolean isContains=containsPoint(number);
        if(isContains){
            char[] charNum=number.toCharArray();
            int len=number.length();
            int count=0;
            for(int i=len-1;i<len;i--){
               if(charNum[i]!='0'){
                   break;
               }
               else{
                   count++;
               }
            }    
            number=number.substring(0,len-count);
        }
        
        return number;
    }
    
    //是否包含小数点
    public boolean containsPoint(String number){
        boolean isContains=true;
        String[] num=number.split("\\.");
        isContains=num.length>1?true:false;
        return isContains;
    }
}


发表于 2015-06-16 20:28:04 回复(0)
class Solution:
    def tran(self, string):
        string = string.replace('*','0')
        flag = '.' in string
        j = len(string)
        for i in range(len(string)-1, -1, -1):
            if string[i] == '*' and flag:
                j = i
            elif string[i] == '.' or string[i] != '*':
                flag = False
        return string[:j]
发表于 2015-06-16 17:30:22 回复(0)
#include <iostream>
#include <string>
#include <string.h>
#include <stack>

using namespace std;
string convertToTrue(string str){
    string result;
    string::reverse_iterator endIndex;//记录字符串后面不为*的位置;
    string::iterator beginIndex;
    int count1=0,count2=0;
    bool potflag;

    string::size_type potIndex=str.find('.');//查询小数点的位置
    if(potIndex!=str.npos) potflag=true;
    else potflag=false;
    
    for(string::reverse_iterator begin=str.rbegin();begin<str.rend();++begin){
        //逆序查找字符串,为去除后面无用的0,记录字符串末尾有效位置
        if(*begin!='*')    break;
        count1++;
    }
    for(string::iterator begin=str.begin();begin<str.end();++begin){
        //正序查找字符串,为去除前面无用的0,记录字符串开始有效位置
        if(*begin!='*') break;
        count2++;
    }

    
    int length=strlen(str.c_str());
    if(potflag==true){
        if(potIndex==1)
            result=str.substr(0,length-count1);
        else{
            length=length-count1-count2;
            if(potIndex==count2){
                count2--;
                length=length+1;
            }
            result=str.substr(count2,length);
            
        }
    }else{
        result=str.substr(count2);
    }
    
    for(string::iterator begin=result.begin();begin<result.end();++begin){
        //将*转换到0
        if((*begin)=='*')
            *begin='0';
    }
    return result;
    
}

double convertToInt(string str){
//将浮点字符串转换到对应的整形
    int i,j;
    double sum=0;//保存最后得到的整形
    string::size_type potIndex;//小数点对应的位置
    stack<int> stk;
    
    potIndex=str.find('.');
    if(potIndex!=str.npos){//字符串中有小数点
        for(i=potIndex-1;i>=0;--i){//计算整形部分的值
            sum=pow(10.0,(potIndex-i-1)*1.0)*(str.at(i)-48)+sum;
        }
        double temp=0.1;
        for(i=potIndex+1;i<strlen(str.c_str());++i){//计算对应小数点部分的值        
            sum=temp*((double)(str.at(i)-48))+sum;
            temp=0.1*temp;
        }
        
    }else{//字符串中没有小数点
        i=0;
        for(string::reverse_iterator begin=str.rbegin();begin<str.rend();++begin,++i){
            sum=pow(10.0,i)*(*begin-48)+sum;
        }
    }
    return sum;
}

int main(){
    string  str="1**";
    string strNum=convertToTrue(str);
    cout<<convertToInt(strNum);
    
}
发表于 2015-06-16 17:23:10 回复(0)
JAVA:
package ww;

public class demoTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String retString=change("112*4*.**33**");
        System.out.println(retString);
    }
    public static String change(String aa){//aa=10000.11000
        char [] stringArr = aa.toCharArray(); //注意返回值是char数组
        int a=aa.indexOf(".");
        int b=aa.lastIndexOf("*");
        int c=aa.length()-1;
        int d=aa.lastIndexOf(".");
        if(aa.indexOf(".")!=-1){//有小数点
            if(aa.lastIndexOf("*")==(aa.length()-1)){
                for(int j=aa.length()-1;j>aa.lastIndexOf(".");j--){
                    if(stringArr[j]!=stringArr[j-1]){
                         aa=aa.substring(0,j-1);
                         break;
                    }
                }
            }        
        }
        return aa.replace("*","0");
    }

}


发表于 2015-06-16 16:10:03 回复(0)
string fun(string str)
{
    string result="";
    int len = str.length();
    int stop = str.find_last_not_of('*');
    for(int i=0; i<=stop; i++)
    {
        if(str[i]!='*')
           result += str[i];
        else
           result += '0';
    }
    return result;
}
发表于 2015-06-16 14:40:18 回复(0)
首先判断str的数是否是小数,如果不是则将str的‘*’全部替换为‘0’即可,如果是,从str的末尾开始遍历,首先将遇到的‘*’全部剔除,下一步如果遇到'.',则将'.'也剔除,否则将从这一位到str[0]的所有字符为'*'的用‘0’代替,输出str即可。
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    while(cin>>str)
    {
        if(str.find('.')==string::npos)
        {
            for(int i=0;i<str.size();i++)
            {
                if(str[i]=='*')
                    str[i]='0';
            }
        }
        else
        {
            int i=str.size()-1;
            while(str[i]=='*')
            {
                str.resize(str.size()-1);
                i--;
            }
            if(str[i]=='.')
                str.resize(str.size()-1);
            while(i>=0)
            {
                if(str[i]=='*')
                    str[i]='0';
                i--;
            }
        }
        cout<<str<<endl;
    }
    return 0;
}
发表于 2015-06-16 11:56:21 回复(0)
#include <iostream>
using namespace std;
/*
计算器出错:把所有的‘0’都变成了‘*’,而且不能智能的消除小数点数字后多余的0。
比如100.00100写成了1**.* * 1 * *,现在要求编程序转换回来,并消除小数点后多余的零。
如输入:1.* * 1 * *
输出:1.001
*/
int main()
{
    char chd[100] = { 0 };
    cin >> chd;
    //将输入的所有*转为0
    for (int i = 0; i < strlen(chd); i++)
    {
        if (chd[i] == '*')
            chd[i] = '0';
    }
    double dl = atof(chd);
    cout << dl;
    system("pause");
    return 0;
}
发表于 2015-06-16 10:40:15 回复(2)
/***********************1.测试类********************************/
public class Test {
//    计算器出错:把所有的‘0’都变成了‘*’,而且不能智能的消除小数点数字后多余的0。
//    比如100.00100写成了1**.**1**,现在要求编程序转换回来,并消除小数点后多余的零。
//    如输入:1.**1**
//    输出:1.001
    public static void main(String[] args) {
        KillZero k = new KillZero();
        Result r = k.kill_zero("1.**1**");
        String a = r.str;
        for(int i = 0; i < r.len;i++) {
            System.out.print(a.charAt(i));
        }
    }
}

/*******************2.算法*********************/
public class KillZero {
    public Result kill_zero(String str) {
        String temp_str = str;
        String a = temp_str.replace('*', '0');//将1**.**1**换成了,1.00100
        System.out.println(a);
        int endIndex = a.length();
        int len_a = a.length();
        for(int i = endIndex-1;i >= 0;i--) {
            if(a.charAt(i) == '0') {  
                len_a--;                //查找最后了0,查到了,把字符串长度标记-1
            } else  {
                return new Result(a,len_a);
            }
        }
        return null;    
    }
}

/*******************3..结果类*********************/

public class Result {
    //已经把*换成0的字符串,同时存好去到末尾0后字符串应该有的长度
    public int len;
    public String str;
    public Result(String str,int len){
        this.str = str;
        this.len = len;
    }
}



发表于 2015-06-16 10:17:58 回复(0)
import java.util.Scanner;
    
public class RToZ{
    public String Chrysan(String str){
        str = str.replaceAll("[*]", "0");
        if(str.indexOf(".") > 0){  
            str = str.replaceAll("0+?$", "");
        }
            return str ; 
    }
        
    public static void main(String[] args) {
        System.out.print("请输入:");
        Scanner s = new Scanner(System.in);
        String str = s.next();
        str =new RToZ().Chrysan(str);
        System.out.print("调整为:");
        System.out.println(str);
    }
}

发表于 2015-06-16 10:14:47 回复(2)
string recover(string *ch)
{
    int len=(*ch).length();
    if(len==0) return "\0";
    int i=0;
    while(i<len&&(*ch)[i]!='.')   //首先处理小数点‘.’前面的数字
    {
        if((*ch)[i]=='*')
            (*ch)[i]='0';
        i++;
    }
    if(i<=len-1)               //如果i的值没有到最后,说明有小数点,下面就对小数点后面的数字处理
    {
        i=len-1;
        while((*ch)[i]!='.')  //从最后一位往前处理数字,如果出现零就删除元素,如果出现非零则终止循环
        {
            if((*ch)[i]=='*')
                (*ch).pop_back();

            else break;
            i--;
        }
        while ((*ch)[i]!='.') //因为已经找到小数点位置,则此后的数字就直接把*转换为0就ok了
        {
            if((*ch)[i]=='*')
                (*ch)[i]='0';
            i--;
        }
    } 
    return *ch;
}

发表于 2015-06-16 10:09:04 回复(0)
'use strict';
const convert = function (numberStr) {
    return parseFloat(numberStr.replace(/\*/g, '0'));
};

发表于 2015-06-16 10:00:36 回复(0)
package year2015.months06;

import java.util.Scanner;

public class Day16 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner s =new Scanner(System.in);
        Day16 d = new Day16();
        String num = s.next();
        d.manageErrorCalculator(num);
    }
    public void getBreedGerm(int num){
        
    }
    /**
     * 处理错误计算器
     * 计算器出错:把所有的‘0’都变成了‘*’,而且不能智能的消除小数点数字后多余的0。
     * 比如100.00100写成了1**.**1**,现在要求编程序转换回来,并消除小数点后多余的零。
     * 如输入:1.**1**
     * 输出:1.001
     * @param num
     */
    public void manageErrorCalculator(String num){
        num = num.replaceAll("\\*", "0");
        int location = num.indexOf(".");
        char[] nums = num.toCharArray();
        int endZero = 0;
        //有小数点
        if(location!=-1){
            //查找小数点后最后一个不是0的数字在什么位置
            for(int i=nums.length-1;i>location;i--){
                if(nums[i]>0){
                    endZero=i;
                    break;
                }
            }
            StringBuffer r = new StringBuffer();
            for(int i=0;i<endZero-1;i++){
                r.append(nums[i]);
            }
            System.out.println(r.toString());
        }else{
            System.out.println(num);
        }
        
        
    }
}


发表于 2015-06-16 09:17:27 回复(0)
Code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    string str;
    int i, index;

    cin >> str;
    for(i = 0; i != str.size(); i++)
    {
        if(str[i] == '*')
        {
            str[i] = '0';
        }
    }

    index = str.find_last_not_of('0');
    str = str.substr(0, index + 1);        // index要加1,[1, index+1)区间

    cout << str << endl;
    return 0;
}

发表于 2014-12-29 23:54:46 回复(0)