我可以在"切换"语句中添加集而不是数字吗?喜欢:
switch(number)
{
case 1<50: //if number is between 1 and 50
{
blah;
break;
}
case 50<100: //if number is between 50 and 100
{
blah;
break;
}
Run Code Online (Sandbox Code Playgroud)
等等.
我要求将非成员函数设为常量,即我想强制它不允许对全局变量进行任何修改.
我知道非成员函数是不可能的,但是想知道是否有解决方法.
我想的一种方法是为此声明一个带有常量成员函数的单独类,并访问const全局变量.但不幸的是,它允许在常量成员函数中访问和修改非常量全局变量(为什么??).
我有两个简单的测试线:
cout<<(cout<<"ok"<<endl, 8)<<endl;
cout<<(int i(8), 8)<<endl;
Run Code Online (Sandbox Code Playgroud)
第一行工作,但第二行编译失败
error: expected primary-expression before 'int'
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我确实需要在逗号运算符中声明.更具体地说,我想声明一些变量,获取它们的值,并从我的类构造函数的初始化列表中将它们分配给我的常量类成员.以下显示了我的意图.如果使用逗号运算符无法实现,还有其他建议吗?
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
void readFile(const string & fileName, int & a, int & b)
{
fstream fin(fileName.c_str());
if (!fin.good()) {cerr<<"file not found!"<<endl; exit(1);}
string line;
getline(fin, line);
stringstream ss(line);
try {ss>>a>>b;}
catch (...) {cerr<<"the first two entries in file "<<fileName<<" have to be numbers!"<<endl; exit(1);}
fin.close();
}
class A
{
private:
const int _a;
const int _b; …Run Code Online (Sandbox Code Playgroud) 所以,我正在阅读C编程.我预订了这个练习:
Write a program which asks the user to enter a dollars-and-cents amount, then displays the amount with 5% added?
方案:
#include <stdio.h>
int main(void) {
float original_amount;
printf("Enter an amount: ");
scanf("%f", &original_amount);
printf("With tax added: $%.2f\n", original_amount * 1.05f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道是什么.3f意思(在...之后应该有3位数字),但是什么1.05f意思?
当我尝试在 PHP 中发出 jQuery AJAX 请求时,遇到了几个问题。我这个简单的例子有什么问题吗?
index.php- 加载 JS、PHP 并定义按钮和段落。
<html>
<head>
<script src='jquery-3.0.0.js'></script>
<script src='main.js'></script>
</head>
<body>
<button id="action" onClick="Send()">Send data</button>
<p id="result"></p>
<?php require('main.php'); ?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
main.js- 它包含与“onClick”事件关联的函数。
function Send(){
$.ajax({
url: 'main.php',
type: 'POST',
data: {
input: "test",
message: "Sending..."
},
contentType: 'application/json',
success: function(data) {
alert(data);
document.getElementById("result").innerHTML = "DONE!";
}
});
}
Run Code Online (Sandbox Code Playgroud)
main.php - 它监听 POST 请求,并发回 JSON。
<?php
if ($_POST){
// Make a array with the values
$vals = [
'input' => …Run Code Online (Sandbox Code Playgroud)