我在质疑我对Accelerated C++的最后一个练习的解决方案:
写一个自我复制的程序.这样的程序是没有输入的程序,并且在运行时,在标准输出流上写入其自己的源文本的副本.
我的解决方案
using std::string;
using std::cout;
using std::endl;
using std::ifstream;
using std::getline;
void selfReproduce16_1()
{
ifstream thisFile("C:\\Users\\Kevin\\Documents\\NetBeansProjects\\Accelerated_C++_Exercises\\Chapter_16.cpp", ifstream::in);
string curLine;
bool foundHeader = false;
while(getline(thisFile, curLine))
{
if(!curLine.compare("void selfReproduce16_1()") || foundHeader)
{
foundHeader = true;
cout << curLine << endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这仅打印出解决方案的源文本(此功能).这是他们想到的解决方案吗?
我想要一个动态解决方案,不需要硬编码源文件的位置.但是,我不知道在运行时自动获取源文件位置的方法.
与此相关的另一点是包含"包含"文件,并且(当遇到函数调用时),自动获取存储函数的源文件的位置.对我来说,这将是一个真正的"自我复制" "节目.
这在C++中是否可行?如果是这样,怎么样?
我需要在我的WP7应用程序中动态编译C#代码.
通常我会使用Microsoft.CSharp和System.CodeDom在运行时编译代码,但它们在wp7上不可用:(
有任何想法吗 ?
我为我的iPhone应用程序实现了"添加到收藏夹"功能.它工作正常,除了在运行时将单元格添加到我的收藏表视图中.例如,我给出了以下方法.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
tableView.hidden = YES;
warningLabel.hidden = YES;
// Load any change in favourites
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:kFavouriteItemsKey];
self.favourites = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if([self.favourites count] > 0)
tableView.hidden = NO;
else
warningLabel.hidden = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.favourites count]; // Favorites is a dictionary contains required data
}
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell"; …Run Code Online (Sandbox Code Playgroud) 我已经读过Java和C++/C#之间的主要区别之一是Java在运行时做了什么,C#在编译时做了些什么.这是真的?如果是这样,你能解释一下吗?
在C#中,我创建了一个函数,该函数接收两个输入并RandomNumber(int x, int y)使用随机对象返回一个随机数(称为).然后我在另一个函数中调用了这个函数两次,期望获得两个不同的值(两个不同的随机数).但是,我不断得到相同的值,这让我感到困惑,因为我可以用Java做同样的事情并期望得到不同的数字.
然后我决定删除我的函数 - RandomNumber(int x,int y)并在我的其他函数中调用Random,如下所示.
Random random = new Random();
int randomNum;
int la;
randomNum = random.Next(1, 10);
numOne.Text = randomNum.ToString();
la = random.Next(1, 10);
Run Code Online (Sandbox Code Playgroud)
这会生成两个不同的随机数.为什么会这样?
下面是我正在运行的代码片段.我是否使用数组pf字符串(cmd)或它是单个字符串我得到一个例外(见下文),同时有一个无密码登录到目标Linux系统.
private static int bringHostFile() {
try {
String[] cmd ={"ssh" , "root@im6-64s" , "/root/bring_hosts"};
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
String s = null;
// read the output from the command
if ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
例外: …
我一直在努力获取系统信息 Runtime.getRuntime().exec(cmd);
我收到以下错误:
java.lang.NullPointerException
at java.lang.Runtime.exec(Runtime.java:422)
at java.lang.Runtime.exec(Runtime.java:326)
at mytest.SystemInfoToTextArea.doInBackground(SystemInfoToTextArea.java:31)
at mytest.SystemInfoToTextArea.doInBackground(SystemInfoToTextArea.java:16)
Run Code Online (Sandbox Code Playgroud)
欢迎任何建议
课程代码
public class SystemInfoToTextArea extends SwingWorker<String, Void> {
private JTextArea textArea;
private String cmd;
public SystemInfoToTextArea(JTextArea textArea, String cmd) {
textArea = textArea;
cmd = cmd;
}
@Override
protected String doInBackground() throws Exception {
String outputMem = null;
try {
String lineMem;
outputMem = "";
Process procMem = Runtime.getRuntime().exec(cmd);
BufferedReader input =
new BufferedReader
(new InputStreamReader(procMem.getInputStream()));
while ((lineMem = input.readLine()) != null) {
outputMem = (lineMem + …Run Code Online (Sandbox Code Playgroud) 考虑一下这段片段:
char *str = "hellow Ghost";
str[0] = 'z';
printf("%s", str);
Run Code Online (Sandbox Code Playgroud)
这是一个分段错误.它是否也在运行时内存错误?
我通过分段故障理解的是:当您访问不属于您的内存时,分段错误.它基本上是为您创建的实用程序,可以在不让您破坏内存的情况下轻松完成工作.
分段错误涵盖了多少和什么样的内存错误以及调用它来检查指针或引用处理内存的错误.
由于逻辑不正确,也会发生运行时错误.除此之外,运行时错误和关于内存的分段错误之间是否存在任何差异.
我已经使用此代码制作了一个ImageButton&之后我想在运行时更改它的大小.这两个代码都写在下面,
<ImageButton
android:id="@+id/btn_new"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/untitled3"
/>
Run Code Online (Sandbox Code Playgroud)
public void scaler1() {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
btn_new.setMinimumWidth(width/4);
btn_new.setMinimumHeight(height/6);
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用请给出任何解决方案.
这是我正在使用的实际代码
public void scaler1() {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
edit_screen.setWidth(width);
edit_screen.setHeight(height / 6);
lpsame = (LayoutParams) btn_0.getLayoutParams();
lpsame.width=width/4;
lpsame.height=height/6;
btn_0.setLayoutParams(lpsame);
LayoutParams lpedit = (LayoutParams) edit_screen.getLayoutParams();
lpedit.width=width;
lpedit.height=height/6;
LayoutParams lpequal = (LayoutParams) btn_0.getLayoutParams();
lpequal.width=width/2;
lpequal.height=height/6;
edit_screen.setLayoutParams(lpedit);
btn_2.setLayoutParams(lpsame);
btn_3.setLayoutParams(lpsame);
btn_4.setLayoutParams(lpsame);
btn_5.setLayoutParams(lpsame);
btn_6.setLayoutParams(lpsame);
btn_7.setLayoutParams(lpsame);
btn_8.setLayoutParams(lpsame);
btn_9.setLayoutParams(lpsame);
btn_add.setLayoutParams(lpsame);
btn_subtrac.setLayoutParams(lpsame); …Run Code Online (Sandbox Code Playgroud) 增加了三个新的私有类是iOS 10
当容器init与单个对象一起使用时,容器的集群将使用这三个新类.
在iOS 9中,还添加了'__NSArray0'和'__NSDictionary0'.
我不知道添加这些特殊私人课程的原因.有人可以告诉我为什么吗?
runtime ×10
java ×3
c++ ×2
ios ×2
objective-c ×2
algorithm ×1
android ×1
c ×1
c# ×1
compilation ×1
dynamic ×1
height ×1
imagebutton ×1
ios10 ×1
iphone ×1
memory ×1
quine ×1
runtime.exec ×1
swing ×1
swingworker ×1
uitableview ×1
width ×1