由买买提看人间百态

topics

全部话题 - 话题: printf
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
k****e
发帖数: 100
1
来自主题: Programming版 - 同一个变量,printf 两个结果
同一个变量,printf 居然给一个非零,一个nan
难道是gcc 4 的bug ?
难以理解啊
程序是前辈改自FORTRAN,到处是全局变量,数组一会儿从0一会儿从1开始,但没想到p
rintf 还给这么个答案。
哪位有经验的?
代码
printf("First: %e\n", g_NAFVariable.TFS[0]);
printf("First: %e\n", g_NAFVariable.TFS[1]);
printf("First: %e\n", g_NAFVariable.TFS[2]);
printf("First: %e\n", g_NAFVariable.TFS[3]);
for (iCpt = 1; iCpt <= g_NAFVariable.NFS; iCpt++)
{
printf("AMPL: ", iCpt);
printf(" %f+%f abs=%f arg(AMPL)=%f FREQ=%f\n",
g_NAFVariable.ZAMP[iCpt].reel
X****r
发帖数: 3557
2
来自主题: Programming版 - printf("%s\n", NULL)的结果
因为对于任何str,printf("%s\n", str);等价于puts(str),所以gcc作了优化,
这里正好str是NULL。printf("%s", str);并不等价于puts(str),所以真正的
printf被调用了,而glibc里的printf是可以处理NULL的。
其实从第一贴开始我就在说这个,要是还不清楚的话我实在不知道该怎么讲了。
l******d
发帖数: 530
3
来自主题: Programming版 - printf("%s\n", NULL)的结果
那为什么
printf("%s", NULL); //not fault
printf("%s\n%s\n", NULL, NULL); //not fault
又可以呢?这个我看着原帖楞是想不明白,直到Xentar解释了:printf("%s\n", str)
被优化成puts(str),而printf("%s", str)不被优化。
X****r
发帖数: 3557
4
来自主题: Programming版 - printf("%s\n", NULL)的结果
唉,敢情我白讲了啊。
在printf里用%s来打印NULL是错误的,会导致未定义的结果,无论是
printf("%s", NULL);还是printf("%s\n", NULL);
注意未定义的结果包括“正确”的结果,但这是没有保证的,回头gcc一升级说不定
printf("%s", NULL);也seg fault了。琢磨这个基本上没有任何用处,你只
需要知道不用%s来打印NULL就行了。
m*********g
发帖数: 273
5
double x;
x=0.0;
printf("%lf\n",x);
is right. however,
printf("%f\n",x); is Ok too.
However,
double x;
scanf(f,"%f",&x);
is wrong.
D*********s
发帖数: 555
6
来自主题: Programming版 - 同一个变量,printf 两个结果
先别考虑是不是printf的问题,先证明不是你自己的问题,比如在开头把你要输出的东西
复制一遍,到local的变量/数组里面,然后在printf输出之前做个比较。
b***y
发帖数: 2799
7
来自主题: Programming版 - [合集] 问个土问题 printf, 别Peng
☆─────────────────────────────────────☆
cogt (苦荆茶) 于 (Fri Apr 29 15:10:07 2005) 提到:
foreach my $i (1..10){
printf "%d\n", $i;
sleep(1);
}
foreach my $i (1..10){
printf "%d\r", $i;
sleep(1);
}
为什么第二个不一个个的print啊
☆─────────────────────────────────────☆
cogt (苦荆茶) 于 (Fri Apr 29 15:16:54 2005) 提到:
Found solution...

$| = 1;
☆─────────────────────────────────────☆
tribology (Order from Chaos) 于 (Fri Apr 29 15:17:57 2005) 提到:
这个是perl么,我测试了下第二个根本看不到输出
原因大概是\r被认为carrage re
l******d
发帖数: 530
8
来自主题: Programming版 - printf("%s\n", NULL)的结果
#include
int main(){
printf("Hello %s\n", NULL);
fflush(stdout);
printf("%s\n", NULL);
fflush(stdout);
return 0;
}
在linux上用gcc编译运行结果为
Hello (null)
Segmentation fault
什么原理?
X****r
发帖数: 3557
9
来自主题: Programming版 - printf("%s\n", NULL)的结果
printf和puts都是标准系统函数,你找个C标准就有详细定义。
不用琢磨用%s输出NULL的事,只要知道(从上面printf的文档里)不能这么用就行了。
软件工程的核心是开发效率,完全没有必要自己挖个坑去跳。

码吗
c**t
发帖数: 2744
10
来自主题: Unix版 - 奇怪的 printf!! ksh programming
有这么一段code:
#!/bin/ksh
func()
{
printf "%10s|%-10s\n" $x $x
return 0;
}
x=123
echo `func`
其output和
printf "%10s|%-10s" 123 123
不同. 区别在于第一个格式.
大家有什么意见, 赫赫
w*********t
发帖数: 928
11
发现
printf
没有%lf
scanf
有%lf
怪不得我这么糊涂。
B********s
发帖数: 3610
12
来自主题: Programming版 - printf的问题
在shell下printf "%b\n" "$*"相当于echo的功能,那么在c程序中如何如何实现这个功
能呢?
O******e
发帖数: 734
13
来自主题: Programming版 - 同一个变量,printf 两个结果
Your output doesn't match your code. The code has four printf("First:")
statements but the output has six "First:". What is the definition of
g_NAFVariable?

到p
O******e
发帖数: 734
14
来自主题: Programming版 - 同一个变量,printf 两个结果
At the beginning of the quoted code, TPS[0...3] is printed.
In the for-loop, at the end of the printf statement TPS[iCpt]
is printed again where iCpt=1...?. However I can't match up the
output with the code, even after seeing the "correction".
kosine you need to post a clearer explanation of what you see
as wrong. Print the address of the variable and the value of
the variable, or run the code in a debugger, make sure you are
looking at the same thing.
s*****w
发帖数: 1527
15
so it's like
myprintf(fmt, ...)
{
printf(fmt, ...);
}
use va_list ?
s*****w
发帖数: 1527
16
LPCWSTR using printf %ls, only comes back with 1st char,
not the whole string, so what's the correct way pls ?
X****r
发帖数: 3557
17
来自主题: Programming版 - printf("%s\n", NULL)的结果
gcc optimizes the second case to call puts() instead, which can't handle
NULL.
%s in printf isn't required to be able to handle NULL anyway, so just don't
do it.
p*********t
发帖数: 2690
18
来自主题: Programming版 - printf("%s\n", NULL)的结果
找函数printf()的源码读读就知道了。
f*******n
发帖数: 12623
19
来自主题: Programming版 - printf("%s\n", NULL)的结果
try
gcc -fno-builtin-printf
l******d
发帖数: 530
20
来自主题: Programming版 - printf("%s\n", NULL)的结果
刚发现格式串里面的\n是关键
printf("%s", NULL)执行没出错,输出(null)
print("%s\n", NULL)执行错了,Seg. fault
搞不懂了

t
l******d
发帖数: 530
21
来自主题: Programming版 - printf("%s\n", NULL)的结果
看了你的帖子,还有一点没懂:
printf("%s",NULL)(%S后没有\n)没有被优化成puts(NULL),所以没有Seg. fault,
是这样子吗?
X****r
发帖数: 3557
22
来自主题: Programming版 - printf("%s\n", NULL)的结果
puts会在字符串最后输出\n,所以printf("%s",NULL)没法优化成puts(NULL)啊。
n*******g
发帖数: 325
23
来自主题: Programming版 - 这个printf结果是什么?
char * s = "1";
printf("%d\n", s);
求大牛指点!谢谢!
g****t
发帖数: 31659
24
来自主题: Programming版 - 这个printf结果是什么?
printf result:
"Clearly the author of this shitty code is an idiot"
n*****k
发帖数: 69
25
来自主题: Programming版 - 这个printf结果是什么?
刚学C,我试了下,
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has
type ‘char *’ [-Wformat=]
printf("%d\n", s);
改成"%s\n"结果就是 1
c**t
发帖数: 2744
26
来自主题: Unix版 - 奇怪的 printf!! ksh programming
如果将 echo 换成 printf 也不行呀
d**********o
发帖数: 1321
27
来自主题: WebRadio版 - 潜水员冒泡兼征版友意见
hw3b c-.y file
上面这一楼贴了载止hw3b deadline时我match的结果(也就是老师可以以这些不match
的ERROR为借口不给后来我补上的成绩),但是因为当时我还是没有写完,后来感恩节
期间就接着又写了一些,而且hw5是based on hw3 & hw3b的基础上(当我hw5是based
on更好的hw3的结果时,我应该可以得更多的分吧)。
hw4因为写得比较顺利,就不曾保留任何交上去作业的output,没有什么一目了然的结
果是我可以贴在这里的。原本我是想要把自己最的一次作业hw5贴出来的,但那已经是
一个完整的compiler,而且以后我还需要用自己的course project来找工作,所以一定
就不贴最终结果了。那就贴一个hw3b的c-.y文件吧,它集中的hw1、hw2、hw3、 hw3b的
结果,是我自己hw3b *.y文件的最完整版本。这些作业里面也有很多机关一一人为增加
的难度,比如那六七个IO相关的function,不仅traverse tree、build syntax tree的
时候会成为一个考点(把它们作为一个node连在syntax... 阅读全帖
d**********o
发帖数: 1321
28
来自主题: WebRadio版 - 潜水员冒泡兼征版友意见
hw3b c-.y file
上面这一楼贴了载止hw3b deadline时我match的结果(也就是老师可以以这些不match
的ERROR为借口不给后来我补上的成绩),但是因为当时我还是没有写完,后来感恩节
期间就接着又写了一些,而且hw5是based on hw3 & hw3b的基础上(当我hw5是based
on更好的hw3的结果时,我应该可以得更多的分吧)。
hw4因为写得比较顺利,就不曾保留任何交上去作业的output,没有什么一目了然的结
果是我可以贴在这里的。原本我是想要把自己最的一次作业hw5贴出来的,但那已经是
一个完整的compiler,而且以后我还需要用自己的course project来找工作,所以一定
就不贴最终结果了。那就贴一个hw3b的c-.y文件吧,它集中的hw1、hw2、hw3、 hw3b的
结果,是我自己hw3b *.y文件的最完整版本。这些作业里面也有很多机关一一人为增加
的难度,比如那六七个IO相关的function,不仅traverse tree、build syntax tree的
时候会成为一个考点(把它们作为一个node连在syntax... 阅读全帖
c***2
发帖数: 838
29
Here's the full file. You may compile and run: any case I missed?
==================================================================
/* wild.c
*/
#include
#include
#include
#define STR_SIZE 256
//===========================================================
int matchndots(const char *text, const char *dstr, int len)
{
while(len&&*text&&*dstr&&(*text==*dstr || *dstr=='.')){
text++;
dstr++;
len--;
}

if(!len)
return 1;
... 阅读全帖
a*****g
发帖数: 19398
30
来自主题: Programming版 - set operation in c
/* http://www.ccodechamp.com/c-program-of-set-operations-in-maths/ */
#include
#include
#define MAX 30
void create(int set[]);
void print(int set[]);
void Union(int set1[],int set2[],int set3[]);
void intersection(int set1[],int set2[],int set4[]);
int member(int set[],int x);
int main()
{ int set1[MAX],set2[MAX],set3[MAX];
int x,op;
set1[0]=set2[0]=set3[0]=0;
printf("-------------------------------------------------------------\n");
printf("----------------made by C co... 阅读全帖
J******8
发帖数: 132
31
来自主题: JobHunting版 - Facebook phone screen
The question is not hard. But I missed two key points.
The details below.
==========================================================
/*
example
char *words[] = {"foo", "bar", "baz", NULL};
setup(words);
1) First step:
isMember("foo") -> true
isMember("garply") -> false
2) Second step:
isMember("f.o") -> true
isMember("..") -> false
*/
#include
#include
#include
char *words[] = {"foo", "bar", "baz", "caz","cat",NULL};
int num=0;
void print_words(void)
{
int i=0... 阅读全帖
b******p
发帖数: 49
32
来自主题: JobHunting版 - leetcode上的Sort List那道题
我来贴个CPP的(注意:以下有乱七八糟的code……)
合并排序确实比较好用,我还在写第一遍leetcode,代码风格也比较乱,带了很多
debug code,还带了测试用例,试了9次才通过…
有一些多边界条件需要判断的。我的方法是加很多debug,或加很多assertion(我做别
的题里面经常用assertion……不知道是不是好习惯)
============================
#include
#include
using namespace std;
/*
Merge sort ?
*/
// Start: 22:40
// End: 23:45 用了一个小时
//Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
#define TOMMYDBG
class Solution {
public:
... 阅读全帖
w***g
发帖数: 5958
33
来自主题: Programming版 - 谁能示范一个小的C程序
前面的回复了的除了SSA以外,别的都是不懂装懂,一看就知道有多少水平。竟然还有
人说这个不是c的强项。你这个问题问得挺好,我给你演示一下。源程序叫hello.c,
贴在最后
$ make hello
$ objdump -t hello | grep XXX
00000000004006ec g F .text 00000000000001b1 XXX_TEXT
0000000000601070 g O .data 0000000000000004 XXX_DATA
0000000000400968 g O .rodata 0000000000000001 XXX_RODATA
0000000000601078 g O .bss 0000000000000004 XXX_BSS
$ ./hello
Global:
&XXX_DATA: 0x601070
&XXX_BSS: 0x601078
... 阅读全帖
n****5
发帖数: 81
34
来自主题: JobHunting版 - fb电面面经
用 C 写了一下,用的递归来处理商和余数。用的unsigned int所以假定输入小于1百万
X1百万
#include
#include
const char* tens[] = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "
Seventy", "Eighty", "Ninety"};
const char* lt20[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "
Sixteen", "Seventeen", "Eighteen", "Nineteen"};
void num2str(unsigned int num)
{
if (num == 0)
return;
el... 阅读全帖
i**p
发帖数: 902
35
来自主题: Programming版 - unsigned long long
哪位大牛能解释一下ullong 1, ullong 2, ullong 4, ullong 5 的输出吗?
此程序在 Android (GB) emulator 上运行。
//刚刚发现,程序中的反斜杠都被mitbbs过滤掉了。
printf("sizeof(unsigned int): %dn", sizeof(unsigned int));
printf("sizeof(unsigned long): %dn", sizeof(unsigned long));
printf("sizeof(unsigned long long): %dn", sizeof(unsigned long long));
unsigned int uint=0x01020304;
printf("uint: 0x%xn", uint);
unsigned long ulong=0x01020304;
printf("ulong 1: 0x%xn", ulong);
printf("ulong 2: 0x%lxn", ulong);
u... 阅读全帖
S**I
发帖数: 15689
36
来自主题: JobHunting版 - [合集] 收到G家拒信,发面经
☆─────────────────────────────────────☆
recursive (递归) 于 (Mon Apr 11 10:56:49 2011, 美东) 提到:
大半夜收到HR的thank you note。不用管什么NDA了
本人ECE fresh PhD,背景是电路/EDA,跟G业务基本没什么关系
同学内部推荐的,很简单的一次电面就给了onsite
题都不难,但是自己没把握好机会,出了一些小bug。
总的感觉,出错就是硬伤,宁可从最简单的算法写起,也不能出错。
电面:
1,Skip list, http://en.wikipedia.org/wiki/Skip_list
写code实现struct skip_list * find(struct skip_list *head, int value)
2,sorted array with repeated elements
for given element, find out its range.
e.g. A A B B B B B C C D D E F G, given B, the out... 阅读全帖
c********s
发帖数: 817
37
This is my implementation for these two functions, and a driver to run them.
cat string_reverse.c
#include
#include
#include
void swap(char* c1, char* c2);
// -----------------
void string_reverse1(char* string) {
if (string == NULL)
return;
char* head = string;
char* tail = head;
// find the tail
while (*tail) ++tail;
--tail;
// while loop to do the swap
while (head < tail)
swap(head++, tail--);
}
// -----------------
// the caller of this function is res... 阅读全帖
h****n
发帖数: 1093
38
来自主题: JobHunting版 - 请教一个指针的面试题
我特意跑了一下程序
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++)
... 阅读全帖
b*******7
发帖数: 907
39
来自主题: JobHunting版 - 几个C语言的题目
1. what's the output? why?
#include
#include
int main()
{
while(1)
{
fprintf(stdout,"hello-std-out");
fprintf(stderr,"hello-std-err");
sleep(1);
}
return 0;
}
2. what's the output?
#include
int main()
{
float a = 12.5;
printf("%d\n", a);
printf("%d\n", (int)a);
printf("%d\n", *(int *)&a);
return 0;
}
3. can it be built? if yes, what's the result?
file1.c:
int arr[80];
file2.c:
extern int *arr;
int main(... 阅读全帖
S**I
发帖数: 15689
40
来自主题: JobHunting版 - [合集] G一面,感觉不咋的
☆─────────────────────────────────────☆
libei (Bei) 于 (Wed Jan 11 15:43:39 2012, 美东) 提到:
面试官是Google+组的,
一上来她说看到我简历上的一篇测试自动化的文章,读了一遍,感觉"very
informative",让后让我介绍一下相关经验。让我小高兴了一下。
第一题是coding,做的还算顺利,后来她评价说所有的cases都覆盖到了。可能算是过
关吧。
第二题我想复杂了,然后在她提示下才解决。自我感觉很不好。其实sort一下就差不多
了,不过我往复杂的树结构想去了。虽然树结构确实能解决这个问题,不过当时我解释
得很不清楚。反正很不爽。
最后瞎聊时间,她说我提到的测试自动化实践和Google内部的基本完全一样blahblah。
。。,不过我觉得这点也算不上加分吧,是个人进google一段时间后都能学会。就怕她
觉得我想问题太复杂,直接negative。
大家有啥建议想法??
☆─────────────────────────────────────☆
peking2 (myfac... 阅读全帖
t****t
发帖数: 6806
41
来自主题: Programming版 - 一个dot net浮点运算的问题
我不懂C#, 不过很显然这是浮点精度不同的结果. 写了个小程序做试验:
#include
double mul(double a, double b); // return a*b, in another compilation unit
int main(int argc, char **argv)
{
printf("12.6 * 0.11985 = %20.18fn", mul(12.6, 0.11985));
printf("float(12.6 * 0.11985) = %20.18fn", (float)mul(12.6, 0.11985));
printf("12.6f * 0.11985 = %20.18fn", mul(12.6f, 0.11985));
printf("float(12.6f * 0.11985) = %20.18fn", (float)mul(12.6f, 0.11985));
printf("12.6 * 0.11985f = %20.18fn", mul(12.6, 0.11985f)... 阅读全帖
m*********a
发帖数: 3299
42
来自主题: Programming版 - C一个问题搞不懂
二位矩阵array,比如multi[5][10]
为啥multi,*multi,&multi的地址都是一样的?
看下面的程序的运行结果
&multi is 0028FE58
multi is 0028FE58
*multi is 0028FE58
**multi is 67
&multi[0] is 0028FE58
multi[0] is 0028FE58
&multi[0][0] is 0028FE58
multi[0][0] is 67
#include
int main(void){
int multi[5][10]; /*define a two dimensional 5x10 array with a name multi*/
multi[0][0]=67; /*assign 67 to the first element of array*/
printf("&multi is %pn",&multi);
printf("multi is %pn",multi);
printf("*multi is %pn",*multi);
printf("**m... 阅读全帖
d****i
发帖数: 4809
43
来自主题: Programming版 - C一个问题搞不懂
This can be a bit confusing. In C, multi-dimensional array is stored in
contiguous memory. So c[i][j]=*(*(c+i)+j). For each line, see my comment
below:
#include
int main(void){
int multi[5][10]; /*define a two dimensional 5x10 array with a name multi*/
multi[0][0]=67; /*assign 67 to the first element of array*/
printf("&multi is %pn",&multi); //address of the array
printf("multi is %pn",multi); //same as above
printf("*multi is %pn",*multi); //address of the first row of t... 阅读全帖
n*******7
发帖数: 181
44
来自主题: Programming版 - 谁帮我测俩use case
osboxes@osboxes:~/proj/pc12306/Release$ ./pc12306
Total 55
start usecase1
Total time = 7.153685
start usecase2
Total time = 8.264017
diff --git a/pc12306.cpp b/pc12306.cpp
index 12ac896..c057c03 100644
--- a/pc12306.cpp
+++ b/pc12306.cpp
@@ -76,7 +76,7 @@ static void generateSearchPatterns() {
nSearches = offsets.size();
printf("Total %d\n", (int)nSearches);
for (Offsets::iterator it = offsets.begin(); it != offsets.end(); ++
it) {
- printf("%d %d %d\n... 阅读全帖
f*********0
发帖数: 861
45
还是到这个版来问问.
快崩溃了, 怎么也run不起来, 不知道哪里有问题.
#include
#include
int main()
{
float grade1;
float grade2;
float grade3;
printf("enter your 3 test grades: n");
scanf(" %fn", &grade1);
scanf(" %fn", &grade2);
scanf(" %fn", &grade3);
float avg =(grade1 + grade2 + grade3)/3;
printf("average: %.2fn", avg);
if(avg >=90){
printf("grade: A");
}else if(avg >=80){
printf("grade: B");
}else if(avg >=70){
printf("grade: C");
}else if(avg >=60){
printf("grade: D");
}else{
prin... 阅读全帖
a***r
发帖数: 93
46
来自主题: JobHunting版 - G onsite面经
My solution is about the same as bitwise, it uses an array:
#include
#include
void add_range(int D[], int l, int a, int b) {
if(b for (int i=a; i<=b; i++) {
D[i%l]+=1;
}
}
bool is_overlap_array(int a,int b,int c,int d) {
int D[7]= {};
int l=sizeof(D)/sizeof(D[0]);
add_range(D,l,a,b);
add_range(D,l,c,d);
for (int i=0; i if (D[i]>1) { return true; }
}
return false;
}
void main(int argc, const char *... 阅读全帖
f*******t
发帖数: 7549
47
来自主题: JobHunting版 - amazon面试题目讨论贴2
#include
#include
#include
#define ENTRYNOTFOUND 0
#define ENTRYFOUND 1
#define NOTCOMPLETE 2
struct Trie;
void FindWords(Trie *root);
struct Trie
{
Trie();
~Trie();
Trie *children[26];
bool isEnd;
};
Trie::Trie()
{
for(int i = 0; i < 26; i++)
children[i] = NULL;
isEnd = false;
}
Trie::~Trie()
{
for(int i = 0; i < 26; i++) {
if(children[i]) {
delete children[i];
children[i] = NULL;
}
}
}
... 阅读全帖
j********e
发帖数: 1192
48
来自主题: JobHunting版 - 插入节点到complete binary tree的末尾
写了个使用O(1)memory, O(logN * logN) (N是tree的size)的程序。
类似于binary search的算法,测试代码也在下面,应该没有大bug。
先获得树的高度h,然后比较h和root->right子树的高度+1,如果相同,
说明树最后一个节点在root->right,否则最后一个节点在root->left的子树。
#include
#include
#include
#include
using namespace std;
class Node {
private:
Node *left;
Node *right;
int value;

public:
Node() {
left = right = NULL;
value = 0;
}

Node(int v) {
left = right = NULL;
value = v;
}

int Height(Node *node) {
int h... 阅读全帖
I**********s
发帖数: 441
49
最喜欢 wwwyhx的解法: recursive descent.
我也写了个, 用的是建造AST, 再evaluate AST. 应该相当完整了: 数字之前可以有+-
号, 中间可以有一个小数点. 数字和运算符号之间可以有空格. 可以使用+,-,*,/,^,以
及括号. 可以检测错误输入并报出错误位置. 就是比较长, 不适合面试用. 供大家参考.
#include
#include // for pow()
using namespace std;
struct Node {
double val;
char op;
int op_prec; // precedence of operator
int type; // 0 - operand, 1 - operator
Node * left;
Node * right;
Node(double _val, char _op, int _type) : val(_val), op(_op),
type(_type), lef... 阅读全帖
I**********s
发帖数: 441
50
最喜欢 wwwyhx的解法: recursive descent.
我也写了个, 用的是建造AST, 再evaluate AST. 应该相当完整了: 数字之前可以有+-
号, 中间可以有一个小数点. 数字和运算符号之间可以有空格. 可以使用+,-,*,/,^,以
及括号. 可以检测错误输入并报出错误位置. 就是比较长, 不适合面试用. 供大家参考.
#include
#include // for pow()
using namespace std;
struct Node {
double val;
char op;
int op_prec; // precedence of operator
int type; // 0 - operand, 1 - operator
Node * left;
Node * right;
Node(double _val, char _op, int _type) : val(_val), op(_op),
type(_type), lef... 阅读全帖
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)