好的,我必须为我的c ++类做简单的任务.两个函数,首先是Fibonacci序列,第二个是随机序列(发现e).它看起来像这样:
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <math.h>
void fib(int number)
{
int a=0, b=1;
printf("%d\n", a);
for (; number>0; number--)
{
printf("%d\n", b);
b+=a;
a = b-a;
}
}
void e_math(unsigned int number)
{
for (double n = 1; number>0; number--, n++)
{
printf("%f\n", pow((1+1/n), n));
}
}
int main(int argc, char** argv)
{
if (std::string(argv[2])=="f") fib(atoi(argv[1])-1);
if (std::string(argv[2])=="c") e_math(atoi(argv[1])-1);
else printf("Bad argument\n");
}
Run Code Online (Sandbox Code Playgroud)
所以最后我做到了g++ main.cpp -o app;./app 10 f.它工作得很好.但是,当我想:嗯,也许让我们检查更大的数字,并添加50它只是搞砸了.我的意思是它对大约40个序列号做了好处(用Python检查),但后来它开始出现printf()否定等等.我认为它可能是关于int …
所以我必须为我的大学课做一个简单的程序,其中包括一个fraction带分子和分母的结构,ints以及一个is_correct()检查两个条件的函数:分母!=0和<分母.如果其中两个是真的,那么我应该返回true,否则false.我有义务使用?:操作员.所以这是我的代码:
struct fraction {int n,d;
bool is_correct(){
d!=0?(return n<d?true:false):return false;
};
};
Run Code Online (Sandbox Code Playgroud)
我的意思是,我想我可以使用一个if条件d!=0,但我必须只使用?:,并g++给我:expected primary-expression before 'return'
所以我确实有一个并发的quicksort实现。看起来像这样:
func Partition(A []int, p int, r int) int {
index := MedianOf3(A, p, r)
swapArray(A, index, r)
x := A[r]
j := p - 1
i := p
for i < r {
if A[i] <= x {
j++
tmp := A[j]
A[j] = A[i]
A[i] = tmp
}
i++
}
swapArray(A, j+1, r)
return j + 1
}
func ConcurrentQuicksort(A []int, p int, r int) {
wg := sync.WaitGroup{}
if p < r {
q := Partition(A, p, …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
src/main.cpp:
#include <include/array.hpp>
int main() {
int temp[] = {1, 2, 3, 4, 5};
sdizo::array<int> a(temp, 5);
print_array(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
include/array.hpp
#pragma once
#include <string.h>
#include <iostream>
namespace sdizo {
template <class T>
class array {
private:
T* data;
int length;
public:
array();
array(T* d, int len);
~array();
T* begin();
T* end();
};
template <typename T>
array<T>::array() {
data = nullptr;
length = 0;
}
template <typename T>
array<T>::array(T* d, int len) {
length = len;
this->data …Run Code Online (Sandbox Code Playgroud) 在标题中,我需要使用字符数组在某个单词的开头添加用户指定的空格数.我需要在一个函数中执行它,该函数将我的数组作为参数并返回它.这是我的代码:
#include <iostream>
using namespace std;
void writeDownCharArray(char t[], int sizee)
{
for (int i=0;i<sizee;i++)
{
cout<<t[i];
}
}
char * addSpaces(char t[], int ammountOfSpaces)
{
int numberOfCharacters=0;
for (int i=0; t[i]!=NULL; i++){numberOfCharacters++;} //checking the amount of characters in my array
char t2[numberOfCharacters+10];
for (int i=0; i<ammountOfSpaces; i++) {t2[i]=' ';} //adding the sapces
for (int i=ilosc;i<numberOfCharacters+ammountOfSpaces;i++) {t2[i]=t[i-ammountOfSpaces];} //filling my new array with characters from the previous one
return t2;
}
int main()
{
int numberOfSpaces;
char t[10];
cout << "Text …Run Code Online (Sandbox Code Playgroud) 好的,所以我开始玩pygame了。但是我有一个问题。我试图以某种方式附加我的代码,使其井井有条,所以我决定在这里使用类。看起来像这样:
import pygame
from pygame.locals import *
import sys
pygame.init()
class MainWindow:
def __init__(self, width, height):
self.width=width
self.height=height
self.display=pygame.display.set_mode((self.width,self.height))
pygame.display.set_caption("Caption")
def background(self)
img = pygame.image.load("image.png")
self.display.blit(img, (0,0))
mainWindow = MainWindow(800,600)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.exit()
sys.exit()
mainWindow.background()
pygame.display.update()
Run Code Online (Sandbox Code Playgroud)
好吧,工作。但是,如果我想例如用白色填充窗户怎么办?然后,我必须定义一个方法fill(),这将只是self.display.fill(),对不对?有没有一种方法可以正常处理它,而无需在我的课程中定义数百个pygame已经存在的方法?
还有另一件事。如果我通过使用我的课程来做某事并且搞砸了,那么我总是会得到以下消息:
File "C:/Python35/game.py", line 23, in <module>
pygame.display.update()
pygame.error
Run Code Online (Sandbox Code Playgroud)
而且我实际上不知道到底是怎么了。如果我正常地这样做,没有上课,那么我就会遇到 pygame object blabla has no method blablabla诸如此类的错误,我只知道发生了什么。有没有办法解决这个问题,并找出正在发生的事情?
在此先感谢您的帮助!
好的.因此,对于我的大学课,我必须使用Windows Forms(使用更高要求的版本.Split())在C#中创建一个简单的计算器.得到了textBox一些buttons.所以我可以将数字3插入到a中textBox然后我想按下一个button会+在我的数字后添加一个符号.是的,它做到了.但是,如果我想回去写东西,我textBox必须再次点击.
现在我找到了使用.Focus()或使用的答案.Select(),但它们有效,但并不像我希望他们那样做,因为它们还标记整体,textBox.text就像你用鼠标选择它一样,蓝色,如果我按另一个数字然后我会删除所有内容textBox.有没有办法在不标记整个文本的情况下执行此操作?
我不知道我的标题是否合适,因为我的问题是:我知道有时候(当我想使用argv []时)编译器必须在堆栈上为命令行参数安排空间.现在我编写了一个简单的程序,只是为了看看C编译器如何处理简单的C数组(实际上它以与std::arrays 相同的方式处理它们).我正在使用Manjaro Linux 64位.C代码如下所示:
#include <stdio.h>
int main(){
int a[5] = {1,2,3,4,5};
//printf("%d", a[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
程序集生成输出(来自gcc main.c -fno-asynchronous-unwind-tables -o XD.s -S):
.file "main.c"
.text
.globl main
.type main, @function
main:
pushq %rbp
movq %rsp, %rbp
movl $1, -32(%rbp)
movl $2, -28(%rbp)
movl $3, -24(%rbp)
movl $4, -20(%rbp)
movl $5, -16(%rbp)
movl $0, %eax
popq %rbp
ret
.size main, .-main
.ident "GCC: (GNU) 6.3.1 20170109"
.section .note.GNU-stack,"",@progbits
Run Code Online (Sandbox Code Playgroud)
现在,当我取消注释printf语句时,代码如下所示:
.file "main.c"
.section .rodata …Run Code Online (Sandbox Code Playgroud) 我在文件中有一个数字矩阵(用于解决TSP的城市之间的距离)。我将其加载到列表中。之后,列表中的每一行如下所示:
' 9999 3 5 48 48 8 8 5 5 3 3 0 3 5 8 8 5\n'
Run Code Online (Sandbox Code Playgroud)
每行上数字之间的空格数可能不同。现在,我只想将其转换为整数/浮点数列表。我有一个循环:
matrix = []
for row in lines[begin:end]:
matrix.append([float(x) for x in row.split()])
Run Code Online (Sandbox Code Playgroud)
它工作正常。我很好奇我是否可以在这里使用生成器(就像我在循环中使用它),但不是在循环中,而是在一行中。我尝试了类似的方法[float(x) for x in [y.split() for y in lines[begin:end]]],但是它说:
float() argument must be a string or a number, not 'list'
Run Code Online (Sandbox Code Playgroud)
那么它可以像一行一样解决吗,还是应该让它循环处理?
所以我必须做一个项目,我需要实现 an array、 a list、binary heap和binary treea red-black tree,然后测量它们的性能,比较它们并得出一些结论。现在,规则是这样的:
std::(仅cout,也许是string),Boost所以我迈出了第一步,实现一个数组。它看起来像这样:
array.h
#pragma once
#include <iostream>
class array
{
int elements;
int *table;
public:
array();
array(int *t, int n);
int size() const;
int *get() const;
};
Run Code Online (Sandbox Code Playgroud)
array.cpp
#include "array.h"
array::array(int *t, int n) : elements(n), table(new int[elements])
{
memcpy(table, t, elements * sizeof(int));
}
int array::size() const
{
return elements;
}
int *array::get() const
{ …Run Code Online (Sandbox Code Playgroud)