佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1132|回复: 9

C Language: data types [已解决]

[复制链接]
发表于 15-10-2008 09:26 PM | 显示全部楼层 |阅读模式
  1. Write a printf and scanf statement for each of the following:
  2. a. Print 200 with and without a sign.
  3. b. Print 1.234 in a 9-digit field with preceding zeros.
  4. c. Print 123.456789 in exponential notation with a sign(+ or -) and 3 digits of precision.
  5. d. Read characters into array s until the letter p is encountered
  6. e. Read a float value into variable num.
  7. */

  8. #include
  9. void main()
  10. {
  11. int a=200;
  12. unsigned int b=200;
  13. const double c=1.234;
  14. const double d=123.456789;
  15. char s[50];
  16. int count=0;
  17. float f;
  18. int i;

  19. printf("rint 200 with and without a sign:\n";
  20. printf("With a sign = %d\n",a);
  21. printf("Without a sign = %d\n",b);
  22. printf("\n";

  23. printf("rint 1.234 in a 9-digit field with preceding zeros: ";
  24. printf("%.9f\n",c);
  25. printf("\n";

  26. printf("rint 123.456789 in exponential notation with:\n";
  27. printf("a sign(+ or -) and 3 digits of precision:\n";
  28. printf("%.3Ef\n",d);
  29. printf("\n";

  30. printf("Read characters into array s until the letter p is encountered:\n";
  31. do
  32. {
  33. scanf("%c",&s[count]);
  34. count++;
  35. }while(s[count]!='p');
  36. printf("\n";

  37. printf("Read a float value into variable num:\n";
  38. scanf("%f",&f);
  39. i = (int)(f + 0.5f);
  40. printf("%d",i);
  41. }
复制代码
没有error
可是output卡在bold字那个部分了。。。
打了p也不会停
看不到问题。。。

[ 本帖最后由 蜡笔小烦 于 18-10-2008 03:33 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 15-10-2008 10:26 PM | 显示全部楼层
printf("\n";

少了 )

第一眼看到這個
回复

使用道具 举报

发表于 15-10-2008 11:01 PM | 显示全部楼层
  1. do
  2. {
  3. scanf("%c",&s[count]);
  4. count++;
  5. }while(s[count-1] != 'p');
复制代码
index應該是count-1
s[count]還沒有被assign value
回复

使用道具 举报

发表于 15-10-2008 11:23 PM | 显示全部楼层
scanf("%c",&s[count]);

s 在這條line會被改寫
回复

使用道具 举报

 楼主| 发表于 16-10-2008 10:45 AM | 显示全部楼层
原帖由 cheng1986 于 15-10-2008 11:01 PM 发表
do
{
scanf("%c",&s[count]);
count++;
}while(s[count-1] != 'p');index應該是count-1
s[count]還沒有被assign value


可以了
谢谢~

其实我不是很明白题目要求:
Write a printf and scanf statement for each of the following:
a. Print 200 with and without a sign.
b. Print 1.234 in a 9-digit field with preceding zeros.
c. Print 123.456789 in exponential notation with a sign(+ or -) and 3 digits of precision.
d. Read characters into array s until the letter p is encountered
e. Read a float value into variable num.


意思是说每一项都要有scanf吗?
如果是的话我做得是不是不符合要求了呢?
回复

使用道具 举报

 楼主| 发表于 16-10-2008 10:45 AM | 显示全部楼层
原帖由 零翼 于 15-10-2008 11:23 PM 发表
scanf("%c",&s[count]);

s 在這條line會被改寫


啊???
不明
不好意思。。
回复

使用道具 举报

Follow Us
发表于 16-10-2008 06:38 PM | 显示全部楼层

回复 5# 蜡笔小烦 的帖子

現在這樣就對了
應該是問題有問題...
回复

使用道具 举报

 楼主| 发表于 16-10-2008 06:48 PM | 显示全部楼层
原帖由 cheng1986 于 16-10-2008 06:38 PM 发表
現在這樣就對了
應該是問題有問題...



是这样子


这一个部分
我不是很确定
是不是这样子做。。。
printf("rint 200 with and without a sign:\n";
printf("With a sign = %d\n",a);
printf("Without a sign = %d\n",b);
printf("\n";


[ 本帖最后由 蜡笔小烦 于 16-10-2008 06:57 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 17-10-2008 09:12 AM | 显示全部楼层
print without sign
printf("%d", 200);

with sign
printf("%+d",200);

c. Print 123.456789 in exponential notation with a sign(+ or -) and 3 digits of precision.
printf("%+.3e", 123.456789);




[ 本帖最后由 onlylonly 于 17-10-2008 09:19 AM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 18-10-2008 03:10 PM | 显示全部楼层
原帖由 onlylonly 于 17-10-2008 09:12 AM 发表
print without sign
printf("%d", 200);

with sign
printf("%+d",200);

c. Print 123.456789 in exponential notation with a sign(+ or -) and 3 digits of precision.
printf("%+.3e", 123.456789);

...


原来是这样
谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-12-2025 11:18 AM , Processed in 0.159371 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表