c语言编程思路和两个早期调试器bug
首先:
编程思维 ==》想法 == 》目标(需要规划) ==》面向过程
将大目标变成小目标。
举一个扑克牌游戏的例子:
1. 程序中储存一副牌 2.洗牌3发牌
一.程序中储存一副牌
用什么存 (全局变量)
多大 (54)
怎么表达牌
1、 数字
2、 花色 几种 (4种)
Int Char g_card[54]
Int Char g_colour[54]
设定本地语言切换代码页
用法 setlocal(LC_ALL , ” 语言 ”) 或者setlocal(LC_ALL , ” . 代码页 ”)
二、洗牌
1、洗谁 ? Int Char g_card[54]Int Char g_colour[54]
2、怎么洗
1、算法选择(乱牌)
2、随机一个数(下标1-53)
3、交换数据
//随机种子的初始化 srand((unsigned)time(NULL));
3、发牌
三个玩家
1、怎么表达玩家
2、每个玩家几张牌
Char player_card[3][20]
Char player_colour[3][20]
怎么发牌
1、一张一张的发。发完第一圈后重复17次
排序玩家牌面
一、冒泡排序
1、后面如果有比第一个数字大的就和第一个交换
2、后面如果有比第二个数字大的就和第二个交换
3、···········
[0]3 [1] 2 [2] 1 [3] 4 [4]5 [5]6
[0] 1 [1] 2 [2] 3 [3] 4 [4]5 [5]6
二、选择排序
[0]3 [1] 2 [2] 1 [3] 4 [4]5 [5]6
[2] 3 [1] 2 [0] 1 [3] 4 [4]5 [5]6
(结尾附上以前写的代码)
问题:绘制菱形
6 * 将他看成数学的平面图,求二元一次方程解
5 * * y = - x + 3
4 * * y = - x + 9
3 * * y = x + 3
2 * * y = x + -3
1 * *
0 *
0 1 2 3 4 5 6
for(y = 0;y < 7; y++) { for(i = 0 ;i < 7 ; i++) { if(y = - x + 3||y = - x + 9||y = x + 3||y = x + -3) { printf(“* ”); } else { printf(“ ”); } }
两个早期的bug
go to现阶段仅使用多重循环跳出或者低级语言模拟,其他情况不允许使用。
低版本bug
foo() { goto NEXT; return 0; NEXT ; }
死循环 NEXT: goto NEXT;
float f scanf(“%f”,&f);
系统蹦了
环境浮点初始化麻烦
如果没有浮点运算,就没有初期化浮点环境。Scanf相当于用了浮点运算,所以系统崩溃。
poke代码
#include<stdio.h> #include<stdlib.h> #include<time.h> char g_poker[54]={0}; char g_colour[54]={0}; char player_poker[3][20]={0}; char player_colour[3][20]={0}; char last_poke [3]={0}; int i,j ,g_poker_value=3; void set_poker () { for (i=0;i<52;i+=4) { for (j=0;j<4;j++) { g_poker [i+j]=g_poker_value ; //printf("%-04d",g_poker [i+j] ); } g_poker_value ++; } g_poker [52]=16; g_poker[53]=17; //printf ("\n"); } void poke_shape(int poke_value) { switch( poke_value) { case 1 : case 2 : case 3 :printf("3 "); break; case 4 :printf("4 "); break; case 5 :printf("5 "); break; case 6 :printf("6 "); break; case 7 :printf("7 "); break; case 8 :printf("8 "); break; case 9 :printf("9 "); break; case 10:printf("10 "); break; case 11:printf("j "); break; case 12:printf("Q "); break; case 13:printf("K "); break; case 14:printf("A "); break; case 15:printf("2 "); break; default : break; } } void set_colour() { for (i=0;i<52;i+=4) { for (j=0;j<4;j++) { if (j == 0) g_colour [i+j]=3; else if (j == 1) g_colour [i+j]=4; else if (j == 2) g_colour [i+j]=5 ; else g_colour [i+j]=6 ; //printf("%-04c",g_colour [i+j] ); } } if (i==52) g_colour [52]=' '; if (i==53) g_colour [53]=' '; //printf ("\n"); } void show_poker() { for(i=0;i<54;i++) { if (g_poker [i ] == 16) printf ("xW "); else if (g_poker [i] == 17) printf ("dW "); else poke_shape(g_poker [i]); if(i%17==16) { printf (" \n"); } } } void show_colour() { for(i=0;i<54;i++) { if (g_colour [i ] == 16) printf (" "); else if (g_colour [i] == 17) printf (" "); else { printf ("%-04c",g_colour [i]); } if(i%17==16) { printf (" \n"); } } //printf ("\n"); } void show_poker_colour() { printf ("\n"); for(i=0;i<54;i++) { if (g_poker [i ] == 16) printf (" xw " ); else if (g_poker [i] == 17) printf (" dw "); else { printf ("%-02c",g_colour [i]); poke_shape (g_poker[i]); } if(i%17==16) { printf (" \n"); } } } void rand_poker_colour() { srand ((unsigned)time(NULL)); char temp ,rand_value; for (i=0;i<54;i++) { //洗牌的数字 rand_value =rand ()%54; temp = g_poker [ rand_value ] ; g_poker [ rand_value ] = g_poker[ i ] ; g_poker [ i ] = temp ; //洗牌的颜色 temp = g_colour [rand_value] ; g_colour [ rand_value ] = g_colour[ i ] ; g_colour [ i ] = temp ; } } void rand_poke() { int poker_index; poker_index = 0 ; for(i=0;i<17;i++) { for (j=0;j<3;j ++) { player_poker[j][i] = g_poker [poker_index]; player_colour[j][i] = g_colour [poker_index++]; //printf ("%-02c%-4d",player_colour[j][i],player_poker[j][i]); } } } void show_player_poke() { for(i=0;i<3;i++) { printf ("\n%dplayer:",i+1); for(j=0;j<17;j++) { if (player_poker[i][j] == 16) printf ("xW "); else if (player_poker[i][j] == 17) printf ("dW "); else printf ("%-02c",player_colour[i][j]); poke_shape(player_poker[i][j]); } } } void new_rank() { int temp,k ; for ( j = 0 ; j < 3 ; j ++ ) { for( k = 15;k >= 0 ; k -- ) { for(i = 0;i <= k ; i ++ ) { if( player_poker[j][i]>player_poker[j][i+1]) { //换牌的数字 temp = player_poker[j][i+1]; player_poker[j][i+1] = player_poker[j][i]; player_poker[j][i] = temp; //换牌的大小 temp = player_colour [j][i+1]; player_colour[j][i+1] = player_colour[j][i]; player_colour[j][i] = temp; } } } } } void show_last_card() { printf ("\n\n last three card: \n"); for (i=0;i<3;i++) { last_poke [i]=g_poker [53-i]; if (g_poker [53-i ] == 16) printf ("xW "); else if (g_poker [53-i] == 17) printf ("dW "); else printf ("%-02c",g_colour [53-i]); poke_shape(g_poker [53-i]); } } int main() { //定义牌 set_poker (); //定义花色 set_colour(); //显示牌的数字 //show_poker(); //显示花色 //show_colour(); printf("all card,please look miss:\n"); //显示所有牌和颜色 show_poker_colour() ; //乱牌 rand_poker_colour(); printf ("\n\nrand_card:"); //显示所有牌和颜色 show_poker_colour() ; //发牌 rand_poke(); printf ("\n"); //显示玩家的牌 show_player_poke(); //排序 new_rank(); printf ("\n\n new rand card:"); show_player_poke(); //显示底牌 show_last_card(); return 0; }