我可是没有课本
学过很长时间了
第一题 #include <stdio.h> #include <conio.h>
double factorial(int n);
void main() { int i; double sum = 0; for(i = 0; i <= 20; i++) { sum += factorial(i); }
printf("The result is %.0f",sum); getch(); }
double factorial(int n) { int i; double result = 1; for(i = 1; i <= n; i++) { result *= i; }
return result; } 第二题 #include <stdio.h> #include <ctype.h> #include <string.h> #include <conio.h>
void main() { int num_count = 0; int alpha_count = 0; int space_count = 0; int other = 0;
char *s; printf("Input a string:"); gets(s);
while(*s != '\0') { if(isdigit(*s)) num_count++; else if(isalpha(*s)) alpha_count++; else if(isspace(*s)) space_count++; else other++; s++; }
printf("The number of number is %2d\n",num_count); printf("The number of alpha is %2d\n",alpha_count); printf("The number of space is %2d\n",space_count); printf("The number of other symbolic is %2d\n",other);
getch(); } 第三题 #include <stdio.h> #include <conio.h>
void main() { int center = 3,i,j,k; int d = 5; char square[7][7];
for(i = 0; i < 7; i++) square[align=center][i] = '*';
j = center + 1; for(i = center-1; i >= 0; i--) { for(k = (7-d)/2; k < (7-d)/2+d; k++) { square[i][k] = '*'; square[j][k] = '*'; } d -= 2; j++; }
for(i = 0; i < 7; i++) { for(j = 0; j < 7; j++) { if(square[i][j] == '*') { printf("%4c",square[i][j]); } else { printf(" "); } } printf("\n"); }
getch(); }