小编Bjö*_*lex的帖子

找到平方根到精度

我刚刚接受了各公司在采访中提出的问题.我发现一个是"找到一个精确数字的平方根.函数定义应该是这样的:double getSquareRoot(int num, int precision)".

我写了一个小函数,它给出了平方根,但不关心精度:

double getSquareRoot(int num){
 int num1=0, num2=0;
 for(int i=1 ;; i++){
   if(i*i == num){
    std::cout<<i <<" is the sq root"<<std::endl;
    break;
   }
  else if(i*i > num){
   num2 = i;
   num1 = --i;
   break;
  }
} 
 // in the above for loop, i get the num1 and num2 where my input should lie 
 // between them
 // in the 2nd loop below.. now i will do the same process but incrementing 
 // by 0.005 …
Run Code Online (Sandbox Code Playgroud)

c++ performance

0
推荐指数
1
解决办法
4500
查看次数

复制构造函数未调用

#include <iostream>


int main(void)
{

class date {
private:
  int day;
  int month;
  int year;
public:
  date( )  { std::cout << "default constructor called" << std::endl; }
  date& operator=(const date& a) { std::cout << "copy constructor called" << std::endl; day=a.day; month=a.month; year=a.year; }
  date(int d ,int m ,int y  ) : day(d),month(m),year(y){ std::cout << "constructor called" << std::endl; }
  void p_date(){ std::cout << "day=" << day << ",month=" << month  << ",year=" << year << std::endl; }
  date& add_day(int d) …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor

0
推荐指数
1
解决办法
282
查看次数

用c ++聚合

在下面的代码中,为什么这两个语句是非法的

const int i[] = { 1, 2, 3, 4 };

// float f[i[3]]; // Illegal

struct S { int i, j; };
const S s[] = { { 1, 2 }, { 3, 4 } };
//double d[s[1].j]; // Illegal
int main() {} 
Run Code Online (Sandbox Code Playgroud)

他们为什么非法?文字定义如下,我不明白.

"在数组定义中,编译器必须能够生成移动堆栈指针以容纳数组的代码.在上面的两个非法定义中,编译器都会抱怨,因为它无法在数组定义中找到常量表达式."

提前致谢.

c++

0
推荐指数
1
解决办法
124
查看次数

如何使用python在命令行中传递多个参数

(我的守则):

import csv
import re
import string
import sys
import fileinput
import os
import random
import glob
import getopt

def getSymbols(filename):
    f = file(filename)
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        print line, 
    f.close()
if len(sys.argv) < 2:
    print 'No action specified.'
    sys.exit()

    elif:
        print "No option"
    sys.exit()

    else:
        for filename in sys.argv[1]:
            readfile(filename)

    with open(filename) as f:
         for line in f:
             if 'symbols' in line:
                print "Total Number of Symbols:\n",line.strip(' has ');
getSymbols(filename)
Run Code Online (Sandbox Code Playgroud)

我有一个要求,我无法找到解决方案:

如何在命令行中将多个文件路径作为参数传递?例如:

test.py …
Run Code Online (Sandbox Code Playgroud)

python

-1
推荐指数
1
解决办法
2万
查看次数

在C++中将一行转换为一个Node

我正在尝试编写一个函数,它将一行作为一个string并将其转换为一个Node.

Node convertLineToNode(string line){
    char lineC[] = line;
    Node *n = new Node();
    n->lastname=strtok(lineC," ");
    n->name=strtok(lineC," ");
    n->ID=strtok(lineC," ");
 }
Run Code Online (Sandbox Code Playgroud)

但它无法正常工作.它期望该string行为char数组.我无法将其转换为char数组.我的问题有什么解决方案吗?

c++

-1
推荐指数
1
解决办法
232
查看次数

我看到了这个节目.这是什么 - 在构造函数复数(double r,double i)之后的re(r),im(i){}?

// operator_overloading.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

struct Complex {
   Complex( double r, double i ) : re(r), im(i) {} // what is this syntax?
   Complex operator+( Complex &other );
   void Display( ) {   cout << re << ", " << im << endl; }
private:
   double re, im;
};

// Operator overloaded using a member function
Complex Complex::operator+( Complex &other ) {
   return Complex( re + other.re, im + other.im );
}

int main() { …
Run Code Online (Sandbox Code Playgroud)

c++ syntax

-4
推荐指数
1
解决办法
377
查看次数

标签 统计

c++ ×5

copy-constructor ×1

performance ×1

python ×1

syntax ×1