引用 | 编辑
csr
2011-07-07 22:31 |
楼主
▼ |
||
x0
#include<stdio.h>#include<stdlib.h> int main(void) { int y; printf("请输入出生年:"); scanf("%d",&y); if(y%12==1){ printf("生肖:鼠\n"); } else if(y%12==2){ printf("生肖:牛\n"); } else if(y%12==3){ printf("生肖:虎\n"); } else if(y%12==4){ printf("生肖:兔\n"); } else if(y%12==5){ printf("生肖:龙\n"); } else if(y%12==6){ printf("生肖:蛇\n"); } else if(y%12==7){ printf("生肖:马\n"); } else if(y%12==8){ printf("生肖:羊\n"); } else if(y%12==9){ printf("生肖:猴\n"); } else if(y%12==10){ printf("生肖:鸡\n"); } else if(y%12==11){ printf("生肖:狗\n" .. 访客只能看到部份内容,免费 加入会员 x0
|
引用 | 编辑
ebolaman
2011-07-08 00:56 |
1楼
▲ ▼ |
>> 星座表 <<
我真是佩服你一个一个慢慢打,然而 我并没有发现有什么奇怪的错误,不能执行之类的 你所提供的程式码 运行一切 OK。 不能执行的解释是? 输入的 "月" 和 "日" 与 结果不符吗? 那就是你输入的格式打错了,正确的例如: 12 21 还是其他? 请详细说明 另外我忽然也想要练习一下 C++,这是仿造你的程式码打出来的 主要功能一模一样,但是更简化,可以参考 复制程式 #include<stdio.h> #include<stdlib.h> /*----- Prototype -----*/ char* get_str(char*, int, int); int check_range(int*, int); /*_____________________*/ /*----- Variables -----*/ char* tempc; /*_____________________*/ int main() { /*----- Variables -----*/ char* str_year; //Year char* str_cnstl; //Constellation int input[3]; int i; /*_____________________*/ /*--- Set up strings for displaying ---*/ //str_year=(char*)malloc((2+1)*(12)*sizeof(char)); //str_cnstl=(char*)malloc((4+1)*(12)*sizeof(char)); str_year="鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪"; str_cnstl="水瓶,双鱼,牧羊,金牛,双子,巨蟹,狮子,处女,天秤,天蝎,射手,摩羯"; /*_____________________________________*/ /*--- Retrieve data from user ---*/ printf("请输入出生的 年/月/日 :\n"); printf("例 : 2012/12/21\n>"); scanf("%d/%d/%d",&input[0],&input[1],&input[2]); /*_______________________________*/ /*--- Determine the range ---*/ //Year printf("生肖: %s\n", get_str(str_year, 2, ((input[0]-1911)%12-1) )); free(tempc); //Constellation for (i=0; i<12; ++i) if (check_range(input, i)==1) { printf("星座: %s座\n", get_str(str_cnstl, 4, i )); free(tempc); break; } if (i==12) printf("日期好像打错了,无法判断星座\n"); printf("\n"); /*___________________________*/ /*--- Free memories ---*/ //free(str_year); //free(str_cnstl); /*_____________________*/ system("pause"); return 0; } char* get_str(char* str, int symb_len, int ind) { int i=0; tempc=(char*)malloc((symb_len+1)*sizeof(char)); for (i=0; i<symb_len; ++i) tempc[i]=str[ind*(symb_len+1)+i]; tempc[i]='\0'; return tempc; } int check_range(int* input, int ind) { int range [2][12][2]={{1,20, 2,19, 3,21, 4,20, 5,21, 6,22, 7,23, 8,23, 9,23, 10,24, 11,23, 12,22}, {2,18, 3,20, 4,19, 5,20, 6,21, 7,22, 8,22, 9,22, 10,23, 11,22, 12,21, 1,19}}; int daymax; int arr_daymax[12][2]={1,31, 2,28, 3,31, 4,30, 5,31, 6,30, 7,31, 8,31, 9,30, 10,31, 11,30, 12,31}; //Read daymax daymax=arr_daymax[input[1]-1][1]; //Exception if (input[1]==2) if((input[0]%4)==0 && (input[0]%100)!=0 || (input[0]%400) ==0) daymax=29; //Main part if (input[1]>=range[0][ind][0] && input[1]<=range[1][ind][0] ) if (input[1]==range[0][ind][0] && input[2]>=range[0][ind][1] || input[1]==range[1][ind][0] && input[2]<=range[1][ind][1]) if (input[2]>=1 && input[2]<=daymax) return 1; } x4 |
引用 | 编辑
csr
2011-07-08 21:44 |
4楼
▲ ▼ |
下面是引用 ebolaman 于 2011-07-08 00:56 发表的 :大大你好 小弟看了你写的程式 真是佩服 小弟能力还没到这样的功力 只能把大大的程式下载来研究 希望有朝一日也可与大大比较 谢谢 x0 |
引用 | 编辑
星辰雪
2011-07-09 21:57 |
8楼
▲ ▼ |
下面是引用 ebolaman 于 2011-07-08 00:56 发表的 : 请问一下 我有一些疑问 str_year=(char*)malloc((2+1)*(12)*2*sizeof(char)); str_cnstl=(char*)malloc((4+1)*(12)*2*sizeof(char)); -----------以上这两行我不太懂 这两行的意思 可以解释一下吗 x0 |
引用 | 编辑
ebolaman
2011-07-10 09:50 |
9楼
▲ ▼ |
下面是引用 星辰雪 于 2011-07-09 21:57 发表的 : malloc 是动态分配的函数,其实我也不是很懂,当初是在书中看到 主要好像是用在 未知的长度 要分配记忆体使用的 但此处既然 鼠、牛、虎...的长度是确定的,那两行其实删除应该会比较好 感觉就像是 malloc 请求 "额外的" 免费记忆体 然后用指标函数指定 对着这个位置,然后我们就能用 ptr[N] 或 *(ptr+N) 等方式去存取 但是你应该注意到了 函数 get_str 那里面也有用 malloc 现在想起来我没有用 free 函数把他归还,会造成 memory leak 不太好 x1 |
引用 | 编辑
星辰雪
2011-07-10 20:13 |
10楼
▲ ▼ |
再次请问一下
str_year=(char*)malloc((2+1)*(12)*2*sizeof(char)); str_cnstl=(char*)malloc((4+1)*(12)*2*sizeof(char)); ((2+1)*(12)*2*sizeof(char)); 和 ((4+1)*(12)*2*sizeof(char)); 2+1是生肖判断所需要之位数 4+1是星座判断所需要之位数 对吧 后面的 (12)*2 是什么意思呢 x2 |
引用 | 编辑
ebolaman
2011-07-10 21:24 |
12楼
▲ ▼ |
抱歉我又发现我打错了
应该是要 str_year=(char*)malloc((2+1)*(12)*sizeof(char)); str_cnstl=(char*)malloc((4+1)*(12)*sizeof(char)); 至于 str_year=(char*)malloc((2+1)*(12)*sizeof(char)); 2 是 例如 "鸡" 的长度 1 是我额外好看用的分隔字元 "," 12 是有 十二生肖,至于之前 *2 是我之前只打中文时遗留下来的,打错了 既然已经 str_year="鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪"; str_cnstl="水瓶,双鱼,牧羊,金牛,双子,巨蟹,狮子,处女,天秤,天蝎,射手,摩羯"; 我现在才终于了解,那些字串已经被载入到记忆体 因此指定符号只是指定 str_year 指标的位置到那个地方,之前 malloc 这个动作是白费力气了,所以就算删除也不会有问题 但是 get_str 函数内所需要使用的 tempc 就一定要用 malloc 因为内容需要被更动 唉唉,我对字元与字串还有型态还是搞不太清楚噫 我已经将程式码重新修改过了,请参考前面回覆部分 x1 |
引用 | 编辑
星辰雪
2011-07-10 22:13 |
17楼
▲ ▼ |
再次发问一下
if (input[1]>=range[0][ind][0] && input[1]<=range[1][ind][0] ) 请问一下 [ind] 是什么意思? 我有看到宣告成 int x0 |
引用 | 编辑
ebolaman
2011-07-10 22:58 |
18楼
▲ ▼ |
下面是引用 星辰雪 于 2011-07-10 22:13 发表的 : 由于 check_range 是被呼叫的,当初使用他的主部分在这: for (i=0; i<12; ++i) if (check_range(input, i)==1) { printf("星座: %s座\n", get_str(str_cnstl, 4, i )); free(tempc); break; } 到了 check_range 内部就变成了 ind 为了取得十二星座个别的 日期资料所设计的 例如 ind=0 则是取得 水瓶座 的日期资讯 ind=0 此时 range[0][ind][0] 的资料则是 1 (月份),range[1][ind][0] 则是 2 (月份) if (input[1]>=range[0][ind][0] && input[1]<=range[1][ind][0] ) 检查 使用者输入的月份( input[1] ) 是不是在范围内 if (input[1]==range[0][ind][0] && input[2]>=range[0][ind][1] || input[1]==range[1][ind][0] && input[2]<=range[1][ind][1]) 找月份相等的,然后看看日期是不是 高于或低于 (这里是因为星座范围刚好上下都是一个月左右,如果突然变成 6 个星座 这样侦测会出错,还要加入一些 卡在中间的月份检查才行) if (input[2]>=1 && input[2]<=daymax) 检查日期 (input[2] ) 是不是介于 1 和 daymax(一月份最多的天数) 之间 x2 |