我试图使用freopen()打印到文本文件和屏幕,但我只是实现打印到文件.
我想知道是否有一个容易将程序输出保存到文件并将其打印到屏幕?因为我以另一种方式工作,但最终我不得不打印两次声明.一个用于文件,另一个用于输出.
注意:我是C++的新手,我正在尝试为下一学期的课程学习它,所以需要直接的答案,因为我已经在线查看,除此之外找不到任何简单的答案.
这是我到目前为止:
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
void menu(){
cout << "\t********************************************************\n"
<< "\t* Welcome to slot machine. *\n"
<< "\t* Would you like to play? (1 to play, 2 not to play) *\n"
<< "\t********************************************************\n\n";
return;
}
void update(int arr[], int &token) {
if (arr[0]==arr[1] && arr[1]==arr[2]) {
token+=4;
cout << "You win\n\n";
} else if (arr[0]==arr[1] || arr[1]==arr[2] || arr[0]==arr[2]) {
token+=1;
cout << "You got two out of three\n\n";
} else {
token-=1; …Run Code Online (Sandbox Code Playgroud) 我知道egrep有一个非常有用的方法,通过使用以下两个表达式:
egrep "pattern1.*pattern2"|egrep "pattern2.*pattern1" filename.txt|wc -l
Run Code Online (Sandbox Code Playgroud)
但是,在搜索三个表达式时,有一种简单的方法可以使用egrep的AND运算符,因为随着添加额外表达式,排列会呈指数增长.
我知道另一种方式,sort|uniq -d 但我正在寻找一个更简单的解决方案.
编辑:
我目前的搜索方式将产生五个总结果:
#!/bin/bash
pid=$$
grep -i "angio" rtrans.txt|sort|uniq|egrep -o "^[0-9]+ [0-9]+ " > /tmp/$pid.1.tmp
grep -i "cardio" rtrans.txt|sort|uniq|egrep -o "^[0-9]+ [0-9]+ " > /tmp/$pid.2.tmp
grep -i "pulmonary" rtrans.txt|sort|uniq|egrep -o "^[0-9]+ [0-9]+ " > /tmp/$pid.3.tmp
cat /tmp/$pid.1.tmp /tmp/$pid.2.tmp|sort|uniq -d > /tmp/$pid.4.tmp
cat /tmp/$pid.4.tmp /tmp/$pid.3.tmp|sort|uniq -d > /tmp/$pid.5.tmp
egrep -o "^[0-9]+ [0-9]+ " /tmp/$pid.5.tmp|getDoc.mps > /tmp/$pid.6.tmp
head -10 /tmp/$pid.6.tmp
mumps@debianMumpsISR:~/Medline2012$ AngioAndCardioAndPulmonary.script
1514 Structural composition of central pulmonary arteries. Growth potential after …Run Code Online (Sandbox Code Playgroud) 我似乎无法在此方法中找到for循环所需的语法.我期待迭代字符串中的单词suit.
编辑:有一点需要注意的是cardArray是一个ArrayList.
public String getSuit(int card){
String suit = cardArray.get(card);
for (String word : suit){
if (word.contains("SPADES")){
suit = "SPADES";
}
}
return suit;
}
Run Code Online (Sandbox Code Playgroud) 我试图以#格式给我一个随机的双重回复#.#但是我得到了这个效果的值:0.9395772067578356
你如何强制一个随机双倍的十进制回报,因为我不能在.nextDouble中添加参数.
myRandomNumGenerator = new Random();
loadedValue = myRandomNumGenerator.nextDouble();
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用我制作的课程,但是当我打电话给课程时,这是不行的,我有点失落.
错误发生在程序底部的类调用和计数器初始化.
package counter;
public class Counter {
private int value;
public Counter(int intialValue){
value = intialValue;
}
private void count() {
value = value + 1;
}
private void reset(){
value = 0;
}
public int getValue(){
return value;
}
Counter tally = new Counter();
tally.count();
}
Run Code Online (Sandbox Code Playgroud)