我必须在我的程序中写什么,以便它只在适当的位置接受逗号即不能输入 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)
}

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)