我刚刚接受了各公司在采访中提出的问题.我发现一个是"找到一个精确数字的平方根.函数定义应该是这样的: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) #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) 在下面的代码中,为什么这两个语句是非法的
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)
他们为什么非法?文字定义如下,我不明白.
"在数组定义中,编译器必须能够生成移动堆栈指针以容纳数组的代码.在上面的两个非法定义中,编译器都会抱怨,因为它无法在数组定义中找到常量表达式."
提前致谢.
(我的守则):
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) 我正在尝试编写一个函数,它将一行作为一个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数组.我的问题有什么解决方案吗?
// 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)