微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我必须在我的程序中写什么,以便它只在适当的位置接受逗号即不能输入 10,00.0 、 100,0.2334 等?

如何解决我必须在我的程序中写什么,以便它只在适当的位置接受逗号即不能输入 10,00.0 、 100,0.2334 等?

我正在参加计算机科学暑期课程,每周分配两个项目,所以如果我弄错了一些术语,请耐心等待。

本周,我完成了第一个,但没有完成第二个。在第二个作业中,我们需要重写 ReadDouble 函数,使其更加用户友好;用户友好,允许用户输入逗号和数字。此外,

  1. 我们被要求允许第一个字符是数字、加号或减号或小数点。

  2. 所有其他字符可以是数字、逗号或小数点(如果已经没有)。

  3. 如前所述,逗号必须正确写入(我们不能允许用户输入 10,00.244、343,2.334 或类似的无意义数字)。

  4. 小数点后不能有逗号。

  5. 数字中只能有一个小数点。

到目前为止,我能够满足 1)、2) 和 5)。 3) 和 4)?没那么多。

潜在的问题是我不知道我应该使用哪些类、对象和其他东西来让程序读取字符串输入并确定逗号是否正确插入。我有一个想法,我需要使用类似于“input.length()”代码的东西,将其设置为可以在 if 语句中进行比较的变量,以确保可以使用直到下一个逗号的位数满足。

我还尝试编写一个 for 循环,它会检查小数点后是否有逗号或任何其他无效字符,但我不知道在初始化时写什么。在知道存在一位小数后,如何让 for 循环从小数开始查找?

我遇到的另一个主要问题是,当我输入 1.2 之类的内容时,它显示为 12,这意味着“atof(convert.cstr())”已从返回值中去除了小数。但是,当我只输入 0.2 时,结果显示为 0.2。

我将提供我目前所写的代码以及朋友向我建议的代码

我的代码

#include <iostream> 
#include <cstdlib>
#include <string>
#include <climits>

using namespace std;

// Main Menu Error Prototype
double ReadDouble(string prompt);

double ReadDouble(string prompt)
{
    string input;
    string convert;
    bool isValid = true;

    do {
        // Reset the flag to valid input
        isValid = true;

        // Read input from user
        cout << prompt;
        cin >> input;

        // Validate user input
        // Check the first character is a number,+ or -
        int decimal = 0;
        if (input[0] != '+' && input[0] != '-' && input[0] != '.' && isdigit(input[0]) == 0) {
            cout << "Error! Input was not an integer.\n";
            isValid = false;
        }
        else {
            if (input[0] == '.') {
                decimaL++;
                //cout << "." << endl;
            }
            convert = input.substr(0,1);
        }

        // check that the remaining characters are numeric
        long len = input.length();
        for (long index = 1; index < len && isValid == true && decimal <= 1; index++) {
            if (input[index] == ',') {
                ;  // do nothing if character is a ','
            }
            else if (input[index] == '.') {
                decimaL++; // do nothing if character is a '.'
                if (decimal > 1) {
                    cout << "Error! You can have only one decimal point.\n";
                    isValid = false;
                }
            }
            else if (isdigit(input[index]) == 0) {
                cout << "Error! Input was not an integer.\n";
                isValid = false;
            }
            
            else {
                convert += input.substr(index,1);
            }
            
        }


        // Start looking where the decimal starts 

        /*
        long decimal=input.find('.');
        for (decimal; decimal < len && isValid==true; decimaL++) {
            if (input[decimal] =='.') {
                ; // do nothing if character is a '.'
            }

        }
        */
        //cout << "\nDecimal value is " << decimal << endl; -- Test Code
    } while (isValid == false);

    
    double returnvalue = atof(convert.c_str());
    
    return returnvalue;
}

int main()
{
    double x = ReadDouble("Enter a value: ");
    cout << "Value entered was " << x << endl;
    return 0;
}

我朋友的不完整代码

ReadDouble(){
     isValid = true

     do{
            get user input and set it to a variable called input
            set output variable to a variable called output
            bool hasDecimal = false;
            int digitsUntilNextComma = 3

            for(i = 0; i < input length; i++){
                    if(input[i] == ','){
                             if((i < 3 && i > 0) || digitsUntilNextComma == 0){
                                     digitsUntilNextComma = 3;
                                     output += input[i];
                             }else{ //if it gets to here the comma was in a bad place like,123 or 12,12,123
                                        isValid = false; 
                                        i = input length  //breaks out of for loop
                              }
                    } else if(input[i] == '.'){
                             if(i < 3 || digitsUntilNextComma == 0){
                                     if(hasDecimal){ //if this is true then the input had 2 decimals 
                                               isValid = false; 
                                               i = input length  //breaks out of for loop
                                      }else{
                                              hasDecimal = true;
                                              digitsUntilNextComma = 3;
                                              output += input[i];
                                       }
                             }else{ //if it gets to here,a prevIoUs comma was in a bad place like 12,34.56
                                      isValid = false; 
                                      i = input length  //breaks out of for loop
                              }
                    }else{
                            output += input[i];
                            digitsUntilNextComma--;
                    }
            }
          
     }while(isValid == false)
}

我希望我提供的内容不会太模糊或混乱。再说一次,我过去很少接触编程,所以如果我把一些术语搞砸了,请原谅我。

解决方法

这是 FSM 的工作。

Start on + to IN0
Start on - to IN0
Start on Digit to IN1
Start on Decimal to D_prime
IN0 on Decimal to D_prime
IN0 on Digit to IN1
IN1 on Digit to IN2
IN1 on Decimal to D
IN1 on Comma to N0
IN2 on Digit to N3
IN2 on Comma to N0
IN2 on Decimal to D

IN# 是“初始数字,看到的#”。

D_prime on Digit to D
D on Digit to D

N0 on Digit to N1
N1 on Digit to N2
N2 on Digit to N3
N3 on Comma to N0
N3 on Decimal to D

有效的结束状态为 N3、IN1、IN2、IN3 和 D。

任何意外字符都是输入错误。

对于 State,保留一个累加器、一个符号和一个十进制计数,初始状态为 0,正数,0。

在开始(-)时将符号设置为负。

每当您看到一个数字时,将累加器乘以 10 并相加。

在 D 或 D_prime 状态下,也将十进制计数增加 1。D_prime 的存在是为了消除 . 作为有效数字。

结果是(符号是否为负?-1:1)*累加器/10的小数次方。

为了测试这一点,编写一个程序来生成有效数字,另一个生成乱码,另一个从有效数字中随机添加、减去、打乱和拼接字符。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。