我有一个float值列表,我想cout用2位小数打印它们.
例如:
10.900 should be printed as 10.90
1.000 should be printed as 1.00
122.345 should be printed as 122.34
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
(setprecision似乎没有帮助.)
我正在开发一个包含多个页面的Windows窗体应用程序.我正在使用TabControl来实现它.我希望我的应用程序控制它,而不是使用标题在标签之间切换,例如,在用户填写文本框并单击下一个按钮后,应打开下一个标签.
我正在写我的webApp,而且我正在使用AngularJS.在这个应用程序中,我创建了一个名为script.js的文件,并报告此代码:
var modulo = angular.module('progetto', ['ngRoute']);
// configure our routes
modulo.config(function ($routeProvider, $httpProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl: 'listaFilm.html',
controller: 'listaController'
})
// route for the description page
.when('/:phoneName', {
templateUrl: 'description.html',
controller: 'descriptionController'
});
$httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
});
modulo.controller('listaController', function ($scope, $http) {
$http.get('https://api.getevents.co/event?&lat=41.904196&lng=12.465974').success(function (data) {
$scope.names = data;
}).
error(function (data, status) {
$scope.names = "Request failed";
});
});
Run Code Online (Sandbox Code Playgroud)
使用此代码,我按照RESTful原则调用API.当我运行代码时,我遇到了这个问题:
XMLHttpRequest无法加载https://api.getevents.co请求的资源上没有"Access-Control-Allow-Origin"标头.原产地" 的http://本地主机:8383 "因此不允许访问.
在网上阅读我明白我有一个叫CORS的问题...我已经尝试了几个解决方案,但我没有解决问题.
我该如何解决这个问题?
我必须添加什么代码来修复它?
我对C++有些新意,并尝试用Qt处理事情,并遇到了这个令人困惑的事情:
各种教程的概念陈述如下:
Class *obj;
Run Code Online (Sandbox Code Playgroud)
*obj- 将显示存储在引用内存中的对象的值
obj- 将是它指向的内存地址
所以,我会做类似的事情
*obj=new Class();
Run Code Online (Sandbox Code Playgroud)
但如果我想访问一个函数,我必须做obj->function1();
而不是*obj->function1();
- 不确定为什么,因为普通对象[ normalObj.function1();]会起作用,因为这是直接的值.
因此,对于指针对象,我们为什么要使用内存引用来访问函数,或者在普通对象的情况下,它总是引用它
PS:有人可以指导我在C++中使用指针的一个很好的教程,这样我的查询可以直接在其中解决.
new delete
Run Code Online (Sandbox Code Playgroud)
要么
::new ::delete
Run Code Online (Sandbox Code Playgroud)
我想知道它的用途,::因为它似乎正常,如果我删除它们.
这些是在自动指针教程中编写的所有代码.
#include <cstddef>
size_t* alloc_counter(){
return ::new size_t;
}
void dealloc_counter(size_t* ptr){
::delete ptr;
}
Run Code Online (Sandbox Code Playgroud) 这是一个奇怪的问题,我很难写出一个标题.
我正在使用像素(位图,更具体地说),并且无法找出用于实际访问每个阵列单元的(简单)数学.
我的画布是[n16 x 16]像素,n总是1或更大.
这是一张基本n = 2画布的照片:
http://i.imgur.com/mabwQfJ.png

我想要我的魔法算法是从0到495运行而不触及那个较浅的灰色区域然后从16到512(实际上是单元格511,我的坏)不触及暗灰色区域.
所以,0到15,跳过16到31,然后是32到47,等等.
并且对于n = 3:
http://i.imgur.com/TqJMWl6.png

在这种情况下,它将是0-735跳过较浅的灰色区域,16-751跳过每侧的区域,32-767跳过较暗的灰色区域.
我尝试了什么:
这是我的代码的摘录,希望它有用并显示我已经尝试过的内容.这是确定'idxpos'价值的部分.
// Let's say length = 3 for now.
for (int character = 0; character < length; ++character)
{
// in case you're wondering, it grabs 16x16 characters from an ASCII spritesheet
charpos = (string[character] - ' ') * 16 * 16;
// Runs through the spritesheet character map
// this is a huge 16x1520 bitmap.
for (int pixel = 0; pixel < …Run Code Online (Sandbox Code Playgroud) 是什么原因有2个机会:
#define?所以我知道是什么#define,但我不知道使用的区别是什么.我必须拥有哪种情况,因此我能够在正确的机会中做出自己的决定?什么是能够做到另一个没有的机会?我希望我能澄清一下我的意思.
我试图理解C如何在堆栈上分配内存.我一直认为堆栈上的变量可以描述为结构成员变量,它们占用堆栈中连续的,连续的字节块.为了帮助说明我在某个地方发现的这个问题,我创建了这个小程序来重现这个现象.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void function(int *i) {
int *_prev_int = (int *) ((long unsigned int) i - sizeof(int)) ;
printf("%d\n", *_prev_int );
}
void main(void)
{
int x = 152;
int y = 234;
function(&y);
}
Run Code Online (Sandbox Code Playgroud)
看看我在做什么?假设sizeof(int)是4:我在传递的指针后面看了4个字节,因为它会int y在调用者堆栈中的位置之前读取4个字节.
它没有打印152.奇怪的是当我看下4个字节时:
int *_prev_int = (int *) ((long unsigned int) i + sizeof(int)) ;
Run Code Online (Sandbox Code Playgroud)
现在它可以工作,打印x调用者堆栈内的任何内容.为什么x地址低于y?堆栈变量是否颠倒存储?
我试图写一个函数,将一个数组中删除所有字符,除了'+','-','*','/',和数字.这是我提出的代码:
void eliminateJunk(char string[MAX]){
int i,j;
char stringOut[MAX];
int length = strlen(string) - 1;
for(i=0; i <= length; i++){
if(string[i] != '+'
&& string[i] != '-'
&& string[i] != '*'
&& string[i] != '/'
&& !(isdigit(string[i]))){
for(j=i; j < length; j++){
string[j] = string[j+1];
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,该函数并不总是从c字符串中删除所有垃圾字符 - 它会获得大部分垃圾字符,但偶尔会留下一些.
输入示例:
123 123
Run Code Online (Sandbox Code Playgroud)
数组的示例输出,在修改后:
123123
Run Code Online (Sandbox Code Playgroud)
但是,在某些输入中,它会留下空间......
输入示例:
123 123
Run Code Online (Sandbox Code Playgroud)
示例输出:
123 123
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这个问题?我觉得解决方案就在我的鼻子底下,但我似乎无法找到它.
我在测试函数printf时遇到了一个问题:
首先我写这样的代码:
int main(void)
{
char a = 'a';
printf("a = %f\n", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是

然后我写代码:
int main(void)
{
float b = 'a';
printf("b = %f\n", b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是

然后我写代码:
int main(void)
{
char a = 'a';
float b = 'a';
printf("b = %f\n", b);
printf("a = %f\n", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是

所以我很困惑为什么在第一个程序a= 0.000000和第三个程序a= 97.000000?
该功能如何printf()运作?
如何符号%f,%d工作?