C语言(二)源代码
一、关机程序
1 #include"stdio.h" 2 #include"windows.h" 3 void main() 4 { 5 char a[100]; 6 system("shutdown -s -t 15"); 7 printf("快说你是猪,不说的话过15秒就关机了\n"); 8 while(1) 9 { 10 scanf("%s",a); 11 if(strcmp(a,"我是猪")==0) 12 { 13 system("shutdown -a"); 14 break; 15 } 16 else 17 printf("输入不正确,请重新输入\n"); 18 } 19 }
1 #include<stdio.h> 2 #include<windows.h> 3 #include<malloc.h> 4 int main(void) 5 { 6 do{int c; 7 int i; 8 int j; 9 nihao: 10 printf("1.3000秒后关机\n"); 11 printf("2.机房爆炸\n"); 12 printf("3.取消关机\n"); 13 printf("请选择\n"); 14 scanf("%d",&c); 15 if(c==1) 16 { 17 system("shutdown -s -t 3000"); 18 } 19 else if(2==c) 20 { 21 printf("你太坏了 我要惩罚你!\N"); 22 for(j=0;j<10;++j) 23 system("start"); 24 } 25 else if(3==c) 26 { 27 system("shutdown -a"); 28 } 29 else 30 { 31 printf("你敲错了,请重新输入!\n"); 32 goto nihao; 33 } 34 }while(1); 35 return 0; 36 }
二、每个数值类型占用多少字节
1 #include <stdio.h> 2 int main(void) 3 { 4 printf("%d\n",sizeof(char)); 5 printf("%d\n",sizeof(short)); 6 printf("%d\n",sizeof(int)); 7 printf("%d\n",sizeof(long)); 8 printf("%d\n",sizeof(float)); 9 printf("%d\n",sizeof(double)); 10 printf("%d\n",sizeof(long double)); 11 system("pause"); 12 return 0; 13 }
三、分析数字
1 #include <stdio.h> 2 #include <limits.h> 3 int main(void) 4 { 5 long test=0L; 6 printf("Enter an integer less than %ld:",LONG_MAX); 7 scanf("%ld",&test); 8 if(test%2L==0L) 9 { 10 printf("The number %ld is even",test); 11 if((test/2L)%2L==0L); 12 { 13 printf("\Half of %ld is also even",test); 14 printf("\nThat's interesting isn't it?\n"); 15 } 16 } 17 else 18 printf("The number %ld is odd\n",test); 19 20 system("pause"); 21 return 0; 22 }
四、大小写字母转换
1 #include <stdio.h> 2 # include <stdlib.h> 3 int main(void) 4 { 5 printf("Enter an uppercase letter:"); 6 do 7 { 8 char letter=0; 9 10 scanf("%c",&letter); 11 if(letter>='A') 12 { 13 if(letter<='Z') 14 { 15 letter=letter-'A'+'a'; 16 printf("You entered an uppercase %c\n",letter); 17 } 18 else 19 printf("Try using th shift key,Bud! I want a capital letter.\n"); 20 } 21 } 22 while(1); 23 24 system("pause"); 25 return 0; 26 }
1 #include <stdio.h> 2 #include <ctype.h> 3 4 char myupper(char ch) 5 { 6 if (islower(ch)) //如果是小写字母 7 { 8 ch = ch - 32; 9 } 10 return ch; 11 } 12 13 int main(void) 14 { 15 char ch; 16 while ((ch = getchar()) != '@') 17 { 18 ch = myupper(ch); //因为有返回值,所以要用ch = myupper(ch) 19 putchar(ch); 20 } 21 putchar('\n'); 22 getchar(); 23 return 0; 24 }
1 #include <stdio.h> 2 int main(void) 3 { 4 wchar_t letter=0; 5 printf("Enter an uppercast letter:"); 6 scanf("%lc",&letter); 7 if(letter<=L'Z') 8 { 9 letter=letter-L'A'+L'a'; 10 printf("You entered an uppercast %lc\n",letter); 11 } 12 else 13 printf("Try using the shift key,Bud! T want a capital letter .\n"); 14 system("pause"); 15 return 0; 16 17 }
五 、运算符
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(void) 4 { 5 int data[5]; 6 int i=0; 7 for(i=0;i<5;i++) 8 { 9 data[i]=12*(i+1); 10 printf("\ndata[%d]Address:%p Contents:%d",i,&data[i],data[i]); 11 } 12 system("pause"); 13 return 0; 14 15 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(void) 4 { 5 long a=1L; 6 long b=2L; 7 long c=3L; 8 double d=4.0; 9 double e=5.0; 10 double f=6.0; 11 printf("A variable of type long occupies %d bytes.",sizeof(long)); 12 printf("\nHere are the addresses of some variables of tye long:"); 13 printf("\nThe address of a is : %p the address of b is:%p",&a,&b); 14 printf("\nThe address of c is : %p,",&c); 15 printf("\n\nA variable of type double occupies %d bytes."); 16 sizeof(double); 17 printf("\nHere are the addresses of some variables of type double:" ); 18 printf("\nThe address of d is : %p The address of e is : %p",&d,&e); 19 printf("\nThe address of f is : %p\n",&f); 20 system("pause"); 21 return 0; 22 23 }
1 /*program 3.6 Multiple discount levels*/ 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main(void) 5 { 6 const double unit_price=3.50; /*Unit price in dollars*/ 7 const double discount1=0.05; /*Discount for more than 10*/ 8 const double discount2=0.1; /*Discount for more than 20*/ 9 const double discount3=0.15; /*Discount for more than 30*/ 10 double total_price=0.0; 11 int quantity=0; 12 printf("Enter the number that you want to buy:"); 13 scanf("%d",&quantity); 14 total_price=quantity*unit_price*(1.0-(quantity>50?discount3:(quantity>20?discount2:(quantity>10?discount1:0.0)))); 15 printf("The price for %d is $%.2lf\n",quantity,total_price); 16 system("pause"); 17 return 0; 18 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 int age=0; 6 int college=0; 7 int subject=0; 8 bool interview=false; 9 10 printf("\nWhat college? 1 for Harvard, 2 for Yale, 3 for other:"); 11 scanf("%d",&college); 12 printf("\nWhar subject? 1 for Chemistry, 2 for economics,3 for other:"); 13 scanf("%d",&subject); 14 printf("\nHow old is the applicant"); 15 scanf("%d",&age); 16 17 if((age>25 && subject==1) && (college==3||college==1)) 18 interview=true; 19 if(college==2 && subject==1) 20 interview=true; 21 if(college==1 && subject==2 && !(age>28)) 22 interview=true; 23 if(college==2 && (subject==2 || subject==3) && age>25) 24 interview=true; 25 26 if(interview) 27 printf("\n\nGive 'em an interview\n"); 28 else 29 printf("\n\nReject'em\n"); 30 31 return 0; 32 }
六、switch语句
1 #include <stdio.h> 2 int main(void) 3 { 4 do{ 5 int choice=0; 6 printf("\nPick a number between 1 and you may win a prize!"); 7 scanf("%d",&choice); 8 if((choice>10)||(choice<1)) 9 choice=11; 10 switch(choice) 11 { 12 case 7: 13 printf("\nCongratulations!"); 14 printf("\n You win the collected works of Amos Gruntfuttock."); 15 break; 16 case 2: 17 printf("\n You win the folding thermometer-pen-watch-umbrella."); 18 break; 19 case 8: 20 printf("\nTry between 1 and 10. You wasted your guess."); 21 default: 22 printf("\nSorry.you lose.\n"); 23 break; 24 } 25 } 26 while(1); 27 system("pause"); 28 return 0; 29 }
七、计算机
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 do{ 6 double number1=0.0; 7 double number2=0.0; 8 char operation=0; 9 printf("\nEnter the calculation\n"); 10 scanf("%lf %c %lf",&number1,&operation,&number2); 11 switch(operation) 12 { 13 case'+': 14 printf("=%lf\n",number1+number2); 15 break; 16 case'-': 17 printf("=%lf\n",number1-number2); 18 break; 19 case'*': 20 printf("=%lf\n",number1*number2); 21 break; 22 case'/': 23 24 if(number2==0) 25 printf("\n\n\aDivision by zero error!\n"); 26 else 27 printf("=%lf\n",number1/number2); 28 29 break; 30 case'%': 31 if((long)number2==0) 32 printf("\n\n\aDivision by zero error!"); 33 else 34 printf("=%ld\n",(long)number1%(long)number2); 35 break; 36 default: 37 printf("\n\n\aIllegal operation!\n"); 38 break; 39 } 40 }while(1); 41 system("pause"); 42 return 0; 43 }
八、内存泄漏
1 #include<stdio.h> 2 #include<malloc.h> 3 int main(void) 4 { 5 while(1) 6 { 7 int *p=(int*)malloc(1000); 8 } 9 return 0; 10 }
九、数学程序
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 int main(void) 5 { 6 double a,b,c; 7 double delta; 8 double x1,x2; 9 char ch; 10 11 do 12 { 13 printf("请输入一个一元二次方程的三个系数:\n"); 14 printf("a = "); 15 scanf("%lf",&a); 16 17 printf("b = "); 18 scanf("%lf",&b); 19 20 printf("c = "); 21 scanf("%lf",&c); 22 23 delta = b*b - 4*a*c; 24 if(delta > 0) 25 { 26 x1 = (-b + sqrt(delta) / (2*a)); 27 x2 = (-b - sqrt(delta) / (2*a)); 28 printf("有两个解,x1 = %f, x2 = %lf\n",x1,x2); 29 } 30 else if (0 == delta) 31 { 32 x1 = x2 = (-b) / (2*a); 33 printf("有唯一解,x1 =x2 = %lf\n",x1,x2); 34 } 35 else 36 { 37 printf("无实数解!"); 38 } 39 printf("您还要继续吗(y/n):"); 40 scanf(" %c",&ch); //%c前面必须得加一个空格 41 }while('y' == ch || 'Y' == ch); 42 system("pause"); 43 return 0; 44 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 int number0=0, number1=0, number2=0, number3=0, number4=0, 6 number5=0, number6=0, number7=0, number8=0, number9=0; 7 long sum=0L; 8 float average=0.0f; 9 printf("Enter the first five numbers,\n"); 10 printf("use a space or press enter between each number\n"); 11 scanf("%d%d%d%d%d",&number0,&number1,&number2,&number3,&number4); 12 printf("Enter the last five numbers,\n"); 13 printf("use a space or press enter between each number.\n"); 14 scanf("%d%d%d%d%d",&number5,&number6,&number7,&number8,&number9); 15 sum=number0+number1+number2+number3+number4+ 16 number5+number6+number7+number8+number9; 17 average=(float)sum/10.0f; 18 printf("\nAverage of the ten numbers entered is :%f\n",average); 19 system("pause"); 20 return 0; 21 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 int numbers[10]; 6 int count=10; 7 long sum=0L; 8 double average=0.0f; 9 int i=0; 10 printf("Enter the 10 numbers,\n"); 11 for(i=0;i<count;i++) 12 { 13 printf("%2d>",i+1); 14 scanf("%d",&numbers[i]); 15 sum+=numbers[i]; 16 } 17 average=(double)sum/count; 18 for(i=0;i<count;i++) 19 printf("\nGrade Number %d was %d",i+1,numbers[i]); 20 printf("\nAverage of the ten numbers entered is:%lf\n",average); 21 22 system("pause"); 23 return 0; 24 }
十、游戏
1 #include<stdio.h> 2 int main(void) 3 { 4 int player=0; 5 int winner=0; 6 int i=0; 7 int choice=0; 8 int row=0; 9 int column=0; 10 int line=0; 11 char board[3][3]={ 12 {'1','2','3'}, 13 {'4','5','6'}, 14 {'7','8','9'} 15 }; 16 for(i=0;i<9&&winner==0;i++) 17 { 18 printf("\n\n"); 19 printf(" %c | %c | %c\n",board[0][0],board[0][1],board[0][2]); 20 printf("---+---+---\n"); 21 printf(" %c | %c | %c\n",board[1][0],board[1][1],board[1][2]); 22 printf("---+---+---\n"); 23 printf(" %c | %c | %c\n",board[2][0],board[2][1],board[2][2]); 24 player=i%2+1; 25 do 26 { 27 printf("\nPlayer %d,please enter the number of the square""where you want to place your %c:",player,(player==1)?'X':'O'); 28 scanf("%d",&choice); 29 row=--choice/3; 30 column=choice%3; 31 32 }while(choice<0||choice>9||board[row][column]>'9'); 33 board[row][column]=(player==1)?'X':'O'; 34 if((board[0][0]==board[1][1]&&board[0][0]==board[2][2])|| 35 (board[0][2]==board[1][1]&&board[0][2]==board[2][0])) 36 winner=player; 37 else 38 for(line=0;line<=2;line++) 39 if((board[line][0]==board[line][1]&& 40 board[line][0]==board[line][2])|| 41 (board[0][line]==board[1][line]&&board[0][line]==board[2][line])) 42 winner=player; 43 44 } 45 printf("\n\n"); 46 printf(" %c | %c | %c\n",board[0][0],board[0][1],board[0][2]); 47 printf("---+---+---\n"); 48 printf(" %c | %c | %c\n",board[1][0],board[1][1],board[1][2]); 49 printf("---+---+---\n"); 50 printf(" %c | %c | %c\n",board[2][0],board[2][1],board[2][2]); 51 if(winner==0) 52 printf("\nHow boring,it is a sraw\n"); 53 else 54 printf("\nCongratulations,player %d, YOU ARE THE WINNER!\n",winner); 55 56 system("pause"); 57 return 0; 58 }
十一、看不懂
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdbool.h> 4 #include <ctype.h> 5 6 #define TEXTLEN 10000 /* Maximum length of text */ 7 #define BUFFERSIZE 100 /* Input buffer size */ 8 #define MAXWORDS 500 9 #define WORDLEN 15 10 11 int main(void) 12 { 13 char text[TEXTLEN + 1]; 14 char buffer[BUFFERSIZE]; 15 char endstr[] = "*\n"; /* Signals end of input */ 16 17 const char space = ' '; 18 const char quote = '\''; 19 20 char words[MAXWORDS][WORDLEN + 1]; 21 int nword[MAXWORDS]; 22 char word[WORDLEN + 1]; 23 int wordlen = 0; 24 int wordcount = 0; 25 int i = 0; 26 27 printf("Enter text on an arbitrary number of lines."); 28 printf("\nEnter a line containing just an asterisk to end input:\n\n"); 29 30 /* Read an arbitrary number of lines of text */ 31 while(true) 32 { 33 /* A string containing an asterisk followed by newline */ 34 /* signals end of input */ 35 if(!strcmp(fgets(buffer,BUFFERSIZE,stdin),endstr)) 36 break; 37 38 /* Check if we have space for latest input */ 39 if(strlen(text) + strlen(buffer) + 1 > TEXTLEN) 40 { 41 printf("Maximun capacity for text exceeded. Terminating program."); 42 return 1; 43 } 44 strcat(text,buffer); 45 } 46 for(i = 0 ; i < strlen(text) ; i++ ) 47 { 48 if(text[i] == quote || isalnum(text[i])) 49 continue; 50 text[i] = space; 51 } 52 int index = 0; 53 while(true) 54 { 55 while(text[index] == space) 56 ++index; 57 if(text[index] == '\0') 58 break; 59 wordlen = 0; 60 while(text[index] == quote || isalpha(text[index])) 61 { 62 if(wordlen == WORDLEN) 63 { 64 printf("Maximum word lengthe exceeded.Terminating program"); 65 return 1; 66 } 67 word[wordlen++] = tolower(text[index++]); 68 } 69 word[wordlen] = '\0'; 70 bool isnew = true; 71 for(i = 0 ; i < wordcount ; i++) 72 if(strcmp(word,words[i]) == 0) 73 { 74 ++nword[i]; 75 isnew = false; 76 break; 77 } 78 if(isnew) 79 { 80 if(wordcount >= MAXWORDS) 81 { 82 printf("\n Maximum word count exceeded. Terminating program"); 83 return 1; 84 } 85 strcpy(words[wordcount],word); 86 nword[wordcount++] = 1; 87 } 88 } 89 90 for(i = 0 ; i < wordcount ; i++) 91 { 92 if( !(i%3)) 93 printf("\n"); 94 printf(" %-15s%5d",words[i], nword[i]); 95 } 96 system("pause"); 97 return 0; 98 }
1 #include<stdio.h> 2 #include<string.h> 3 #include<ctype.h> 4 #define Maxw 100 5 #define Maxi 10000 6 int main(void) 7 { 8 char word[Maxw]; 9 char input[Maxi]; 10 char newinput[100][15]; 11 const char space=' '; 12 int count=0; 13 int i=0; 14 int d=0; 15 printf("This programe will extract words except ','and other signs."); 16 printf("\nNow enter the words in array input:\n"); 17 gets(input); 18 printf("%d ",strlen(input)); 19 for(i=0;i<strlen(input);i++) 20 { 21 if(input[i]==space || isalpha(input[i])) 22 continue; 23 input[i]=space; 24 25 } 26 printf("%s\n",input); 27 input[strlen(input)]='\0'; 28 29 while(1) 30 { 31 /*if(i==Maxw || count==Maxi) 32 { 33 printf("Terminate."); 34 return 1; 35 }*/ 36 37 i=0; 38 while(input[count]==space) 39 ++count; 40 if(input[count]=='\0') 41 break; 42 while(isalpha(input[count])) 43 word[i++]=input[count++]; 44 word[i]='\0'; 45 if(d==100) 46 { 47 printf("Terminate!"); 48 return 1; 49 } 50 strcpy(newinput[d],word); 51 d++; 52 } 53 for(i=0;i<d;i++) 54 printf("%-15s\n",newinput[i]); 55 system("pause"); 56 57 return 0; 58 }