j***y 发帖数: 2074 | 1 在193~194页,书里谈到了一个下面的问题:
---
Signedness of char. In C and C++, it is not specified whether the char data type is signed or unsigned. This can lead to trouble when combining chars and ints, such as in code that calls the int-valued routine getchar(). If you say
? char c; /* should be int */
? c = getchar();
the value of c will be between 0 and 255 if char is unsigned, and between -128 and 127 if char is signed, for the almost universal configuration of 8-bit characters on a two's complement machin... 阅读全帖 |
|
s********r 发帖数: 403 | 2 Red Hat 5 Enterprise? 这种企业级 commercial 的产品有这种问题未免太搞笑。
不过有可能有修改过的内存管理模块, lazy deallocation?
建议你这样试试,
char *mem = (char*) malloc(sizeof(char)*(1<<30));
getchar();
free(mem);
getchar();
起同样的进程,第一个进程在 free() 后的getchar()停住,
然后起足够多个同样的进程,在 malloc() 后的getchar() 停住,看会不会把第一个的
进程free() 掉的空间给拿回来。 |
|
y**b 发帖数: 10166 | 3 linux下面我直接用ps或top命令查看的,可以清楚看到各个进程的内存消耗。
swap这个方法我用google搜索到的,似乎是通用的一个trick; effective stl里面也有
提到。
下面是源码,只有swap之后进程的内存消耗才降到近乎0(getchar的时候察看)。
#include
#include
#include
int main () {
std::vector vec;
vec.resize(10000000,-1);
std::cout << "resize finished." << std::endl;
getchar();
vec.clear();
std::cout << "clear finished." << std::endl;
getchar();
std::vector().swap(vec);
std::cout << "swap finished." << std::endl;
getchar();
ret... 阅读全帖 |
|
g**********y 发帖数: 14569 | 4 前天在版上看到打印所有矩阵结合次序(Catalan Numbers)的讨论,最后给出的程序比
较长,我想了一下,既然都是Catalan Numbers, 那么生成Dyck word list的程序应该
可以变形成矩阵结合次序。
CareerCup给出的Dyck word list程序很简洁,我就试着映射了一下:
先推‘A’进栈
对应Dyck word (比如XXXYYY), 遇到X, push nextChar 进栈;
遇到Y, 如果pop()出两个,结合后推进栈
==============================================================
以上是写完后在路上刚悟到的,这个映射应该还算简单。如果不用映射,谁能给个更简洁的解法?
附Java code ==>
public void generate(int count) {
print(count, count, new char[2*count], 0);
}
public void print(int l, int r, char[] s... 阅读全帖 |
|
t********5 发帖数: 522 | 5 我居然还能看得懂c 哈哈哈哈哈
大概就是这个意思了 不知道能不能编译。。。
while (getchar() != '\n')
{
str[i] = getchar();
printf("%c", str[i]);
i++;
}
=>
while (True)
{
char ch = getchar()
if(ch == '\n') {
break;
}
str[i] = ch;
printf("%c", str[i]);
i++;
} |
|
m*********2 发帖数: 701 | 6 wow, good point.
yea, i think what the author saying is:
EOF == -1
0xFF == 255.
that's why you want to use int.
it's large enough to differentiate whether it's -1 or 255.
the short answer is:
getchar() returns int.
and c is int.
so, you are comparing integers, NOT char.
data type is signed or unsigned. This can lead to trouble when combining
chars and ints, such as in code that calls the int-valued routine
getchar(). If you say
between -128 and 127 if char is signed, for the almost universal
co... 阅读全帖 |
|
g*****e 发帖数: 110 | 7 很简单的c代码,求助为什么运行结果不对?
输入 abc回车,然后要输出abc
目前只输出b
#include
int main() {
char str[15];
int i = 0;
printf("input a string:\n");
while (getchar() != '\n')
{
str[i] = getchar();
printf("%c", str[i]);
i++;
}
return 0;
} |
|
A*****s 发帖数: 13748 | 8 以前用c,为了让程序执行到最后能停下来而不是一闪就没了,都加一个getchar()
c++里一旦用了cin这个getchar()就不起作用啦,怎么整?换成啥?
bow |
|
x******a 发帖数: 6336 | 9 I got thousands problems on the following piece of code "dumpfile.h" when I
compile under cygwin. it is ok under visual stduio... can anyone help?
Thanks!
#include
#include
#include //ostream_iterator
#include //cerr
#include //std::copy
template
void dump_to_file(const char* filename, const std::vector& v_d){
std::ofstream ofs(filename);
if(!ofs){
std::cerr<<"Unable to open the file to write!n";
return ;... 阅读全帖 |
|
l********a 发帖数: 1154 | 10 几个解决办法
1.
c++直接在最后加个cin>>就能暂停在那里,等待用户输入,随便输入一个数字就退出
#include
using namespace std;
int main()
{
// 你的代码放这里
int n;
cin >> n;
return 0;
}
2.
沿用c的语法,用getchar()或者system("PAUSE");
#include
int main()
{
// 你的代码放这里
// 或者 char c = getchar();
system("PAUSE");
return 0;
} |
|
k**********i 发帖数: 177 | 11 加入电话号码就是三位
define phone_number_length 3
void pringCom(int num[])
{
char result[3] ={'\0'};
int b[3]= {0,0,0};
int n=0;
while (1)
{
int k =n;
b[2] = k%3;
k = k/3;
b[1] = k%3;
k = k/3;
b[0] = k%3;
int i;
for (i =0; i<3;i++)
{
result[i] = getChar(num[i], b[i]);
printf ("%s", result[i]);
}
printf ("\n");
n++;
if (n > ?) //这里就是总共的个数
break;
}
} |
|
h****b 发帖数: 157 | 12 以下选哪一个
class String {
char *s;
int length;
public:
String(const char *);
String();
/* add code here */
};
int main()
{
String s1 = "abc";
String s2 = "def";
strcmp(s1, s2);
getchar();
return(1);
}
Referring to the sample code above, which one of the following member
functions do you add at the comment in order to allow the strcmp(s1, s2)
statement to compile?
operator const char*() const { return s; }
char* const operator() const { return s; }
operator char* |
|
w*******n 发帖数: 773 | 13 main()
{
char *c1 = "abc";
char c2[] = "abc";
char *c3 = ( char* )malloc(3);
c3 = "abc";
printf("%d %d %s\n",&c1,c1,c1);
printf("%d %d %s\n",&c2,c2,c2);
printf("%d %d %s\n",&c3,c3,c3);
getchar();
}
运行结果
2293628 4199056 abc
2293624 2293624 abc
2293620 4199056 abc
看运行结果
为什么c2所指的地址,
和c2 自己的地址是一样,
我运行了,也是这样的。 |
|
n********y 发帖数: 66 | 14 欢迎拍砖,就用了一个 256 字节的table来检查一个数字是否出现过
#include
#include
#include
#include
char* nextpointer(char* str, int strlen)
{
char *p = str;
char *pend = str + strlen;
char count[256];
memset(count, 0, sizeof(count));
while (p < pend)
{
++count[*p];
if (count[*p] > 1)
{
return p;
}
++p;
}
return p;
}
void longestsingle(char* str, int strlen)
{
char *pstart = str;
char *pend... 阅读全帖 |
|
j***y 发帖数: 2074 | 15
我用你的coding panel调试remove duplicate的程序:
---
#include
//#include
#include |
|
h*****g 发帖数: 312 | 16 class Base{
public:
Base();
virtual ~Base();
};
class Derived: protected Base{
public:
virtual ~Derived();
};
int _tmain(int argc, _TCHAR* argv[])
{
Base *pd = new Derived;
getchar();
return 0;
}
Referring to the sample code above, which one of the following statements is
true?
A.
A constructor needs to be added to Derived class.
B.
The pointer returned by new cannot be type cast from Derived* to Base*.
C.
A pointer to a Base class cannot point to an instance of a Derived class.
D.
Derived class... 阅读全帖 |
|
j***y 发帖数: 2074 | 17
谢谢啊。这两天我也在学习hashtable的方法,刚好看到C++里面有个map可以用。
调试了一下你的程序,稍作调整后的code如下:
---
#include
//#include
#include |
|
b*******8 发帖数: 37364 | 18 以前我写的,测试过:
#include
void main()
{
const int size=7;
int a[size][size];
int x,y,val=0;
for(x=0;x
for(y=x;y
for(y=x;y
for(y=size-x-1;y>x;y--) a[size-x-1][y]=++val;
for(y=size-x-1;y>x;y--) a[y][x]=++val;
}
if (size%2) a[size/2][size/2]=++val;
for(x=0;x
for(y=0;y
printf("\n");
}
getchar();
} |
|
m*********2 发帖数: 701 | 19 getchar() returns int.
char |
|
c****p 发帖数: 6474 | 20 没有。。。。。。
getchar()返回值是int,所以EOF返回-1,0xff返回255。
所以没问题。
char |
|
j***y 发帖数: 2074 | 21 谢谢大家啊,我刚才不自觉地就把getchar()的返回值认为是char了。真是糊涂。
顺便问一下,一个文本文件中可以包含0xFF这样的字节吗(不是文件末尾)? |
|
h****n 发帖数: 1093 | 22 17:12
1.
vector > FindAllPairs(vector input, int givenSum)
{
vector > res;
vector oneRes;
if(input.size()<1) return res;
int i = 0; j = input.size()-1;
while(i
{
if(input[i]+input[j]
else if(input[i]+input[j]>givenSum) j--;
else
{
oneRes.clear();
oneRes.push_back(input[i]);
oneRes.push_back(input[j]);
if(find(res.begin(), res.end(), oneRes)==res.en... 阅读全帖 |
|
e***n 发帖数: 42 | 23 搞定.用了multimap 和两个iterator.
#include |
|
b********s 发帖数: 1676 | 24 没看懂,还是多谢code了。测试了一下,貌似second max不对。。。不管k换成3还是4
,都给第一个72,第二个是69,第三个是70.
int main()
{
int a[10]={39,31,34,53,24,70,42,69,72,44};
int *p;
int size = 0;
p = findPath(a,0,10,size);
cout<<"max="<
cout<<"second max="<
cout<<"p[3]="<
cout<<"p[4]="<
getchar();
return 1;
} |
|
h****n 发帖数: 1093 | 25 我特意跑了一下程序
void foo(int *p1, int val1, int val2)
{
int j;
int *p2, *p3;
int * buf1 = (int*)malloc(sizeof(int)*20);
for(int j =0;j<20;j++)
buf1[j] = 1;
p2 = (int *)buf1;
//val1放到0x12fec8地址也就是当前p2指针指向的位置
*p2 = val1;
//把p1指向val1地址所指向的位置
p1 = (int *)*p2;
for (int i=1; i<10; i++)
{
p2 = (int *)buf1;
*p2 = val1;
p3 = (int *)*p2;
val1+=sizeof(int);
*p3 = val2;
}
printf("buf1:");
for(j =0;j<20;j++)
... 阅读全帖 |
|
w********s 发帖数: 1570 | 26 #include "stdafx.h"
#include |
|
w********s 发帖数: 1570 | 27 #include "stdafx.h"
#include |
|
w********s 发帖数: 1570 | 28 频率统计法
ABCDEF,每次triplet XYZ
记录X的频率,Y的频率,Z的频率
X可能包含了ABCD,A出现的频率是10/20,B:6/20,C:3/20,D:1/20
ABCD如果都相同,那么就是20
ABCD如果其中2个字母相同,就是16,13,11,9,7,4
3个字母相同,结果是19,14,10,17
都不相同就是10,6,3,1
注意10可能是abbb或者abcd,那么用Y的频率来判断哪种情况。
std::string triplet()
{
static std::string password = "google";
std::set digits;
while (digits.size() < 3)
{
int d = rand() % 6;
digits.insert(d);
}
std::string result;
for (std::set::const_iterator it = digits.begin(); it != digits.end
... 阅读全帖 |
|
t**r 发帖数: 3428 | 29 走迷宫的 时间复杂度是多少?谢谢 如这个解法
#include
// Maze size
#define N 4
bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]);
/* A utility function to print solution matrix sol[N][N] */
void printSolution(int sol[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
printf(" %d ", sol[i][j]);
printf("n");
}
}
/* A utility function to check if x,y is valid index for N*N maze */
bool isSafe(int maze[N][N], int x, int y)
{
// if (x,y out... 阅读全帖 |
|
H**********5 发帖数: 2012 | 30 #include
#include
typedef struct node
{
int data;
struct node* next;
};
void printfList(struct node *head)
{
while(head!=NULL)
{
printf("%d-> ",head->data);
head=head->next;
}
printf("\n");
}
void pushAtBegin(struct node **head_ref,int data)
{
struct node *new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=data;
new_node->next=*head_ref;
*head_ref=new_node;
}
void pushAtEnd(struct node **head_ref,i... 阅读全帖 |
|
T******7 发帖数: 1419 | 31 #include
#include
// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
int MatrixChainOrder(int p[], int n)
{
/* For simplicity of the program, one extra row and one extra column are
allocated in m[][]. 0th row and 0th column of m[][] are not used */
int m[n][n];
int i, j, k, L, q;
/* m[i,j] = Minimum number of scalar multiplications needed to compute
the matrix A[i]A[i+1]...A[j] = A[i..j] where dimention of A[i] is
p[i-1] x p[i] */
/... 阅读全帖 |
|
l*****a 发帖数: 14598 | 32 第一个getchar()读了没保留
第二个b保留了
第三个又没保留 |
|
v****s 发帖数: 1112 | 33 在unix下,用c写一个console小程序,每隔1秒print一些信息到屏幕,直到用户按任意键
退出.
试了getchar,可是它是阻塞型输入,一旦执行到这个语句,程序就停下来了。
不用thread或者fork process,如何写“按任意键退出”? |
|
X****r 发帖数: 3557 | 34 其实就按本来面目写也很清楚,不用写成一堆条件,还不用缓冲区。
int ch, branch, state = 0 ;
int table[][2] = { { 1, 0 }, { 2, 3 }, { '0', 3 }, { '1', 0 } };
while( (ch = getchar()) != EOF )
( ch == 'a' && (branch = 0, 1) || ch == 'b' && (branch = 1, 1) ||(state=0))&&
( (state = table[state][branch]) == '0' || state == '1' ) &&
( putchar( state ), state = 0 ) ;
加注:面试的时候可不要写这种code,没拿到offer 不要怪我……,
了解我的意思就行了:) |
|
b****t 发帖数: 82 | 35 来自主题: Programming版 - EOF一问 程序
#include "stdio.h"
main()
{
int c;
c=getchar();
while(c!=EOF)
{
putchar(c);
}
}
程序执行的时候,会提示输入字符(char),但是如何能够退出循环?也就是说如何能够
满足c=EOF?我尝试了-1,好像不行。
请大家帮忙看看
谢谢 |
|
|
t*********n 发帖数: 278 | 37 the sample from programming pearls. But, I got complier error at this line
qsort(a, n, sizeof(char *), pstrcmp). How can I fix this one? Thanx.
#include
#include
#include
int pstrcmp(char **p, char **q)
{ return strcmp(*p, *q); }
#define MAXN 5000000
char c[MAXN], *a[MAXN];
int main()
{ int i, ch, n = 0, maxi, maxlen = -1;
while ((ch = getchar()) != EOF) {
a[n] = &c[n];
c[n++] = ch;
}
c[n] = 0;
qsort(a, n, sizeof(char *), pstrcmp);
} |
|
p****s 发帖数: 32405 | 38 C program. 我想先写个小测试, 如果逻辑对的话再往我的UI上套。
逻辑就是,在输入参数时,如果用户不想改任何设置,直接按回车我就给他一个缺省值;
否则我把it的非空键盘输入读进来.
src是这样:
int _tmain(int argc, _TCHAR* argv[])
{
int i = 2575000;
printf("Enter a number: ");
if (getchar()!= '\n')
scanf_s("%d%*c",&i);
printf("You entered the number %d \n",i);
return 0;
}
运行结果:
C:\Projects\readchatest\debug> readchartest.exe
Enter a number:
You entered the number 2575000
C:\Projects\readchatest\debug>readchartest.exe
Enter a number: 12345
You entered the number 2345
问题很明显,我用getc |
|
|
I**********s 发帖数: 441 | 40 My filter used to change tab to space:
/*
* @usage: ./filter [n]
* The optional n is the number of spaces to replace a tab.
* n default to 4.
*/
void main(int argc, char ** argv) {
char c;
int i, n = (argc > 1) ? atoi(argv[1]) : 4;
while ((c = getchar()) != EOF) {
if (c == '\t') { for (i = 0; i < n; i ++) putchar(' '); }
else { putchar(c); }
}
} |
|
b***y 发帖数: 2799 | 41 ☆─────────────────────────────────────☆
island (不乐) 于 (Sun Sep 25 20:43:43 2005) 提到:
Fortran use PAUSE, don't know what is the statement in C.
☆─────────────────────────────────────☆
campos (campos) 于 (Sun Sep 25 21:56:20 2005) 提到:
getch()
☆─────────────────────────────────────☆
aZhu (a+zhu) 于 (Sun Sep 25 23:17:43 2005) 提到:
or getchar ()
there is a little difference between them.
google.
☆─────────────────────────────────────☆
microbe (纵使相逢应不识) 于 (Mon Sep 26 01:01:42 2005) 提 |
|
a******5 发帖数: 199 | 42 我是菜鸟,问一个很初级的问题. 为什么Visual C++ expression edition里面的
console window运行时就一个黑屏闪过,一定要我加一句getchar()才可以停下来吗(微
软网上那个视频tutorial里那个MM好像就是这么说的)
为什么这个console window不能像eclipse那样就直接将打印结果显示出来呢? 还是我
应该在setting里面设置什么?
谢谢 |
|
v****s 发帖数: 1112 | 43 add this line before your program return(0):
getchar(); |
|
t****t 发帖数: 6806 | 44 1 E
2 A (well D is also right since getchar is not recognized...)
3 B
4 D
5 C
6 B
7 E
8 E
9 D (actually it's not strictly correct: you may call Account::get_name() if
you define it. Yes you can define a pure virtual function. Although in this
case, it's probably not what you want, since in dtor, it will not call
overrided get_name().)
10 A (but I think E is also correct. In fact, A::getBaseNum HIDES Base::
getBaseNum, and A::baseNum HIDES Base::baseNum (which is private anyway).)
11 A
12 E
13 C |
|
y***a 发帖数: 840 | 45 在SLAVE里加点东西让他等在那儿,dead loop or getchar, then gdb attach |
|
v****s 发帖数: 1112 | 46 this program can compile and run.
return value:
-858993460
大侠给解释下c++语法为何会允许这种polymorphism? 这个返回值是怎么决定出来的?
#include
using namespace std;
class clsBase {
public:
char value;
};
class clsA : public clsBase {
public:
void * link;
};
class clsB : public clsBase {
};
int main(){
clsBase *pbase;
clsA a;
int i = 1077;
a.link = (void *)i;
clsB b;
pbase = & b;
cout<< (int)((clsA *)pbase)->link << endl;
getchar();
} |
|
c*******9 发帖数: 6411 | 47 【 以下文字转载自 JobHunting 讨论区 】
发信人: WangYuYan (语笑嫣然), 信区: JobHunting
标 题: c++ 问题
发信站: BBS 未名空间站 (Tue Jan 4 12:36:07 2011, 美东)
main()
{
char *c1 = "abc";
char c2[] = "abc";
char *c3 = ( char* )malloc(3);
c3 = "abc";
printf("%d %d %s\n",&c1,c1,c1);
printf("%d %d %s\n",&c2,c2,c2);
printf("%d %d %s\n",&c3,c3,c3);
getchar();
}
运行结果
2293628 4199056 abc
2293624 2293624 abc
2293620 4199056 abc
看运行结果
为什么c2所指的地址,
和c2 自己的地址是一样,
我运行了,也是这样的。 |
|
h*****g 发帖数: 312 | 48 class Base{
public:
Base();
virtual ~Base();
};
class Derived: protected Base{
public:
virtual ~Derived();
};
int _tmain(int argc, _TCHAR* argv[])
{
Base *pd = new Derived;
getchar();
return 0;
}
Referring to the sample code above, which one of the following statements is
true?
A.
A constructor needs to be added to Derived class.
B.
The pointer returned by new cannot be type cast from Derived* to Base*.
C.
A pointer to a Base class cannot point to an instance of a Derived class.
D.
Derived class... 阅读全帖 |
|
w*****s 发帖数: 98 | 49 【 以下文字转载自 Programming 讨论区 】
【 原文由 windows 所发表 】
anybody knows what is the counter function of "getch()" in unix C?
getchar() works both in dos and in unix, but you have to RETURN after
input a character (which I don't like). There're some other functions,
like kbhit(), bioskey(), but they're all DOS version and can't be used
in Unix.
anybody give me some hints? 3x a lot.
BTW:
I'm using cc under Solaris8 (?). |
|