这是我的代码:
\n\nnamespace Cinemaseats\n\n {public partial class MainForm : Form\n {\n private const int numOfSeats = 60;\n private int numOfReservedSeats = 0;\n Seat currentSeat = new Seat();\n public MainForm()\n {\n InitializeComponent();\n InitializeGUI();\n }\n\n private void InitializeGUI()\n {\n radioButton1.Checked = true;\n namn.Text = string.Empty;\n pris.Text = string.Empty;\n }\n private void button1_Click(object sender, EventArgs e)\n {\n **int seatNr = ReadAndValidateSeatNr();**\n\n if (seatNr < 0)\n {\n MessageBox.Show("V\xc3\xa4lj ett f\xc3\xb6rem\xc3\xa5l fr\xc3\xa5n listan");\n return;\n }\n\n if (radioButton2.Checked)\n ReserveSeat(seatNr);\n else\n CancelSeat(seatNr);\n\n UpdateGUI(seatNr);\n }\n\n public int GetnumOfSeats()\n …Run Code Online (Sandbox Code Playgroud) 我有两个代码段,并且都产生不同的结果。我正在使用TDM-GCC 4.9.2编译器,而我的编译器是32位版本
(int的大小为4个字节,float的最小值为-3.4e38)
代码1:
int x;
x=2.999999999999999; // 15 '9s' after decimal point
printf("%d",x);
Run Code Online (Sandbox Code Playgroud)
输出:
2
代码2:
int x;
x=2.9999999999999999; // 16 '9s' after decimal point
printf("%d",x);
Run Code Online (Sandbox Code Playgroud)
输出:
3
为什么在这些情况下隐式转换会有所不同?
是否由于指定的Real常量中的某些溢出而引起的,怎么办?
我和我的朋友们正在玩 C++ 语言。在这样做的过程中,我们遇到了一些我们无法理解的事情。
这是代码:
#include <vector>
#include <iostream>
void print(std::vector<char> const &input)
{
std::cout << input.size();
for (int i = 0; i < input.size(); i++)
{
std::cout << input.at(i) << " - ";
}
}
int main()
{
char cha = 'A';
char chb = 'B';
char * pcha = &cha;
char * pchb = &chb;
try
{
std::vector<char> a = {pcha, pchb};
//std::vector<char> a = {pchb, pcha};
print(a);
}
catch(std::exception e)
{
std::cout << e.what();
}
}
Run Code Online (Sandbox Code Playgroud)
此代码的输出:
一种 …
在下面的代码中,为什么除非我放入term = 1.0/n而不是 when ,否则我不会得到有效的结果term = 1/n。我已将 term 声明为浮动,这还不够吗?
#include <stdio.h>
int main()
{
float sum = 0, term;
int n, i;
printf("enter the value of n:\n");
scanf("%d", &n);
term = 1.0 / n;
for(i = 1; i <= n; i++)
{
sum = term + sum;
}
printf("Sum = %.3f\n", sum);
return 0;
}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <string.h>
int main()
{
const char str[11]= "Hello World";
if(-1 > strlen(str)){
printf(str);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
This if condition should always return false. But here it's not.
But if I put strlen(str) value to another variable and compare with that, then it works as expected.
What I am missing here?
Is it compiler dependent or something?
我有两个重载函数:
void Set(const char * str) { std::cout << "const char * setter: " << str << "\n"; }
void Set(const std::string_view & sv) { std::cout << "string_view setter: " << sv << "\n"; }
Run Code Online (Sandbox Code Playgroud)
我在一个std::string. 它选择std::string_view重载而不是 const char * 重载,即它选择std::stringto std::string_viewover std::stringto的隐式转换const char *。
这是在 中的保证行为std::string,还是他们的选择是任意的?如果这是一个有保证的行为,他们是如何使它更喜欢 string_view 转换而不是另一个?
考虑这个简单的检查是否定义了(全局)函数:
template <typename T>
concept has_f = requires ( const T& t ) { Function( t ); };
// later use in MyClass<T>:
if constexpr ( has_f<T> ) Function( value );
Run Code Online (Sandbox Code Playgroud)
不幸的是,这允许隐式转换。这显然是一个很大的混乱风险。
问题:如何检查 Function( const T& t ) 是否“显式”存在?
就像是
if constexpr ( std::is_same_v<decltype( Function( t ) ), void> )
Run Code Online (Sandbox Code Playgroud)
应该没有隐式转换,但我无法让它工作。
注意:概念方法的重点是摆脱旧的“检测模式”并进行简化。
我正在阅读这本书,它写道我们可以将一种类型的常量引用分配给任何其他类型的对象,原因是,内部编译器将Rvalue分配给与引用相同类型的对象,然后const引用被初始化为相同类型的对象,但是,如果这种类型的隐式转换有助于将常量引用分配给不同类型的对象,那么为什么不能隐式地进行相同的转换,因为对于这种显式转换。
#include<iostream>
using namespace std;
int main()
{
int a = 10;
double temp = (double)a;
double &x = temp;
cout << x << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它的工作方式相同,为什么它没有在编译器中预先配置?
我在这个博客上遇到了这个代码块。我想知道h1("Hello World")从h1(children: Frag*).
object TheirCode{
trait Frag{
def render: String
}
// HTML constructors
def div(children: Frag*): Frag
def p(children: Frag*): Frag
def h1(children: Frag*): Frag
...
implicit def stringFrag(s: String): Frag
}
object MyCode{
import TheirCode._
val frag = div(
h1("Hello World"),
p("I am a paragraph")
)
frag.render // <div><h1>Hello World</h1><p>I am a paragraph</p></div>
}
Run Code Online (Sandbox Code Playgroud)
我猜会implicit def stringFrag自动转换h1(String)为,h1(Frag)但我一直认为方法参数需要隐式前缀才能工作(即implicit h1(children: Frag*))
在我的项目中,我有一个类(可能有多个派生类),其中一个字段是一个字符串。
我们也有实例化这个类的许多对象的代码(出于技术原因,每个实例都有单独的代码)。所以代码文件可能很长。
对于大多数情况,这是可以的。但在极少数情况下,我需要有一个字符串数组作为 TestValue。我知道我可以将其声明为string[]. 但是因为通常我们只分配单个字符串,所以当只需要一个字符串时,有可能不必总是在代码中显式创建一个数组,就像这样:
public class Datapoint
{
public uint Id;
public string[] TestValue;
}
public void CreateEntries()
{
Table.Add(new Datapoint { Id = 1, TestValue = "123" });
Table.Add(new Datapoint { Id = 2, TestValue = "12.9" });
Table.Add(new Datapoint { Id = 3, TestValue = "Enabled" });
Table.Add(new Datapoint { Id = 4, TestValue = "Temperature" });
Table.Add(new Datapoint { Id = 5, TestValue = { "12.3", "9.8", "7.3" } });
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢!