如何打印正数的符号。 [英] How do i print the sign of a positive number.

查看:129
本文介绍了如何打印正数的符号。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   iostream  >  
使用 命名空间标准;

int main()
{
int a,b,c,d,e;
int f = -d;
int g = -e;
cout<< 分解二次方程式:;
cout<< Ax ^ 2 + Bx + C<< endl ;
cout<< 输入A,B和C的值:< < ENDL;
cout<< A:;
cin>> a;
cout<< B:;
cin>> b;
cout<< C:;
cin>> c;
d = -b +(((b * b) - 4 * a * c)^(1/2))/ 2 * a;
e = + b +(((b * b) - 4 * a * c)^(1/2))/ 2 * a;

cout<< 两个零是:<< ; d<< << e<< endl;
cout<< 因此因素是:<< X<< d<< << X<< e控制;< ENDL;


system( PAUSE
return 0 ;

}





在代码的最后一行,如果数字是正数,它将打印为X5,而不是X + 5。我不能做X +,因为数字可以是负数(取决于输入的数字。)



我尝试过:



我试过谷歌搜索我的问题,但我找不到解决方案,所以我来到这里:)

解决方案

< blockquote>您可以使用 showout 格式标志为 cout 流添加 + 在正数之前签名:

showpos - C ++参考 [ ^ ]

 cout<< showpos<<  因此因素是:<<   X<< d<<  <<   X<< e<< endl; 



如果您还想要 + 出现在第一个输出行中,然后将标志添加到您使用cout流的第一个代码行。


#include <iostream>
using namespace std;

int main()
{
	int a, b, c, d, e;
	int f=-d;
	int g=-e;
	cout<<"Factoring a Second Degree Equation: ";
	cout<<"Ax^2+Bx+C"<<endl;
	cout<<"Enter values for A, B, and C:"<<endl;
	cout<<"A: ";
	cin>>a;
	cout<<"B: ";
	cin>>b;
	cout<<"C: ";
	cin>>c;
	d=-b+(((b*b)-4*a*c)^(1/2))/2*a;
	e=+b+(((b*b)-4*a*c)^(1/2))/2*a;
	
	cout<<"The two zeros are: "<<d<<" and "<<e<<endl;
	cout<<"So factors are: "<<"X"<<d<<" and "<<"X"<<e<<endl;
	

system("PAUSE")
return 0;
	
}



In the last line of the code, if a number was positive it would print out as X5, not X+5. And I cant do "X+" because the number can be negative(depends on the numbers inputted.)

What I have tried:

I have tried googling my problem but I couldn't find a solution so I came here :)

解决方案

You can use the std::showpos format flag for the cout stream to add a + sign before the positive numbers:
showpos - C++ Reference[^]

cout<<showpos<<"So factors are: "<<"X"<<d<<" and "<<"X"<<e<<endl;


If you also want the + to appear in the first output line, then add the flag to the first code line where you use the cout stream.


这篇关于如何打印正数的符号。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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