12月到bin功能 [英] Dec to bin function

查看:90
本文介绍了12月到bin功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我需要有关dec_to_bin函数的帮助...我在第79行收到错误消息:[错误]左值作为左操作数分配所需



这是问题描述:


您的类项目需要一个程序,用户在该程序中输入0到255之间的整数。该条目必须使用名为input_val的函数作为整数完全验证1( )。如果条目无效,则input_val()必须提示用户输入有效条目(这将重复,直到用户输入有效条目)。一旦input_val()返回了一个有效的整数,就必须在main()中调用一个名为

dec_to_bin()的函数。该函数的参数是有效整数和一个名为A []的数组。该函数必须取整数并将其转换为存储为1和0的二进制数到数组A []为:



(A [7] A [6] A [5] A [4] A [3] A [2] A [1] A [0])2

其中A [7]是最高有效位,A [0]是最重要的一点。



我尝试了什么:



< pre lang =c ++> #include < < span class =code-leadattribute> iostream > //预处理程序命令
#include < string < span class =code-keyword>>
#include < span class =code-keyword>< cstdlib >
使用 namespace std; // 图书馆的位置

int input_val(); // 函数原型

int input_val()
{
string text;
int q = 99 ,i;

while (q!= 0
{
尝试
{
// 从用户获取整数作为字符串
cout<< 输入一个整数: ;
getline(cin,text);


if (text == q throw 5 ;

// 字符串不能为空
if (text.length()== 0 throw 1 ;

// 字符串中可能不存在字符或空格,而不是上面提到的字符或空格。
if (!isdigit(text.at( 0 ))& &!(text.at( 0 )== ' +')) throw 2 ;

// +符号可能是也可能不是第一个字符(不是 - 符号)
如果((text.at( 0 ) == ' +')&& text.length()< 2) throw 2 ;

// 如果出现+符号,则必须至少有一位数字标志。
for (i = 1 ; i< text.length() ;>

// 有效整数必须介于0到255之间。
if (atoi(text.c_str())> 255 throw 4 ;

// 如果所有测试都正常:退出while循环
q = 0 ;
}
catch int e)
{
if (e == 1 )cout<< \ n空字符串!<< endl<< 请重新输入一个值...< /跨度>;
if (e == 2 )cout<< \ n第一个数字错误!<< endl<< 请重新输入一个值......;
if (e == 3 )cout<< \ n必须是所有数字!<< endl<< 请重新输入一个值......;
if (e == 4 )cout<< \ n整数必须为0-255<< endl<< 请重新输入一个值......;
if (e == 5 )退出( 0 );

// 设置while循环标记重复
q = 99 ;
}
}
// atoi()将C字符串转换为整数
return atoi(text.c_str());
}

int main() // 驱动程序函数标题
{
char ch;
int A [ 100 ],i = 0 ,J;

// 标题
cout<< ------------ Rasberry Pi Project ------------ << ENDL<< ENDL;

cout<< << input_val()< ;< 是一个有效的整数。<< endl<< endl;

while (input_val()> 0)
{

A [i] = input_val ()%2;
i ++;
input_val()= input_val()/ 2;

}

cout<< 二进制数是: ;
for (j = i- 1 ; j> = 0 ,j--)
{
cout<< A [j];
}

cout<< 输入'e'退出.. ;
cin>> ch;

return 0 ;
}< / cstdlib>< / string>< / iostream>

解决方案

引用:

我在第79行收到错误消息:[错误]左值作为赋值的左操作数

你看过第79行吗?

Quote:

 input_val()= input_val()/ 2; 

没有什么伤害你的眼睛?


Hi. I need help with the dec_to_bin function...I got an error message on line 79: [Error] lvalue required as left operand of assignment

Here is the problem description:

Your class project requires a program in which the user enters an integer between 0 and 255. The entry must be fully validated1 as an integer using a function called input_val( ). If the entry is not valid, input_val( ) must prompt the user for a valid entry (this will repeat until the user enters a valid entry). Once a valid integer has been returned by input_val( ), a function called
dec_to_bin( ) must be called in main( ). The arguments of this function are the valid integer and an array called A[ ]. The function must take the integer and convert it to a binary number stored as 1’s and 0’s to array A[ ] as:

(A[7] A[6] A[5] A[4] A[3] A[2] A[1] A[0])2
where A[7] is the most significant bit and A[0] is the least significant bit.

What I have tried:

#include<iostream>                                         //Preprocessor commands
#include<string>
#include<cstdlib>
using namespace std;                                       //Location of library 

int input_val();                                           //Function prototype   

int input_val()
{
    string text;
    int q = 99, i;
    
    while (q != 0)
    {
        try
        {  
            //Get integer as string from user            
            cout<<"Enter an integer:  ";
            getline(cin,text);
            
            
            if (text == "q") throw 5;   
		            
            //The string must not be empty
            if (text.length() == 0) throw 1;
            
            //No characters or blanks may exist in the string other than be ones mentioned above.
            if ( !isdigit(text.at(0)) && !(text.at(0) == '+') ) throw 2;
            
            //A "+" sign may or may not be the first character (not a "-" sign)
            if ( ( text.at(0) == '+')&& text.length()<2 ) throw 2;
                                  
            //If a "+" sign is present, at least one digit must follow that sign.                
            for (i = 1; i <text.length();>
            
            //The valid integer must be between 0 and 255.
            if (atoi(text.c_str()) > 255)throw 4;
             
            //if all tests are OK: exit the "while" loop                    
            q = 0;
        }
        catch (int e)
        {
            if (e == 1) cout<<"\n          empty string!"<<endl<<"          	please reenter a value... ";
            if (e == 2) cout<<"\n          first digit error!"<<endl<<"     	please reenter a value...";
            if (e == 3) cout<<"\n          must be all digits!"<<endl<<"    	please reenter a value... ";
            if (e == 4) cout<<"\n          integer must be 0-255"<<endl<<" 		please reenter a value...";
            if (e == 5) exit(0); 
		                 		    
            //Set "while" loop sentinel to repeat
            q = 99;
        }      
    } 
    //atoi() converts a C-string to an integer
    return atoi(text.c_str());
}  

int main()                                                 //Driver function header
{
    char ch;  
    int A[100],i=0,j;
     
    //Title 
    cout<<"         ------------Rasberry Pi Project------------"<<endl<<endl;
    
    cout<<"                               "<<input_val()<<" is a valid integer."<<endl<<endl;
    
   while (input_val()>0)
   {
   	
   		A[i]=input_val()%2;
   		i++;
   		input_val()=input_val()/2;
   		
   }
   
   cout<<" Binary Number is: ";
   for (j=i-1;j>=0,j--)
   {
   		cout<<A[j];
   }
    
    cout<<"Enter 'e' to exit.. ";
    cin>>ch;
    
    return 0;
}</cstdlib></string></iostream>

解决方案

Quote:

I got an error message on line 79: [Error] lvalue required as left operand of assignment

Did you look at line 79 ?

Quote:

input_val()=input_val()/2;

Nothing hurt your eyes ?


这篇关于12月到bin功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆