1、编写程序,统计输入的一行字符串中字母,数字,空格,和其他的字符的数目。
#include
#include
// 编写程序,统计输入的一行字符串中字母,数字,空格,和其他的字符的数目。
int isAlpha(char c) {
if ( (c>='a' && c<='z') || (c>='A' && c<='Z') )
return 1;
else return 0;
}
int isDigit(char c) {
if (c>='0' && c<='9')
return 1;
else return 0;
}
int isSpace(char c) {
if (c == ' ')
return 1;
else return 0;
}
int main(int argc, char const *argv[])
{
int alpha = 0, digit = 0, space = 0, other = 0;
char c;
while(1)
{
printf("\n请输入一行字符串:");
alpha = 0, digit = 0, space = 0, other = 0;
while ((c = getchar()) != '\n')
{
if (isAlpha(c)) {
alpha++;
} else if (isDigit(c)) {
digit++;
} else if (isSpace(c)) {
space++;
} else {
other++;
}
}
printf("字母:%d\t数字:%d\t空格:%d\t其他:%d\n", alpha,digit,space,other);
}
return 0;
}
2、一个数可以有一个数的平方求得,则这个数被称为完全平方数,现在一个数加上100后是一个完全平方数,加上168后也是一个完全平方数,请写代码求出这个数。
#include
#include
#include
int main(int argc, char const *argv[])
{
int num;
int x, y;
for (num = 0; num < 100000; ++num)
{
x = sqrt(num+100);
y = sqrt(num+168);
if ( (x*x == num+100) && (y*y == num+168) )
printf("%d\n", num);
}
return 0;
}
3、有100个球场,然后又不同单位来定,可以定多个场地,但是所定场号需要连续,被定的场地不能重复使用,当单位退场需回收场地,请设计一个完整系统进行管理。(考虑球场利用率)
typedef struct TennisGround
{
int num;
char *agentName;
}TG;
void mallocTG(TG *total);
void freeTG(TG *total);
#include "tennis.h"
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
int i, sw;
TG *total = (TG*)malloc(sizeof(TG)*100);
for (i=0; i<100; i++) {
(total+i)->num = i;
(total+i)->agentName = " ";
}
while (1)
{
printf("---------------Tennis Ground Mallocation----------------\n");
for (i=0; i<100; i++)
{
printf("%d(%s) ", (total+i)->num, (total+i)->agentName);
if (i % 5 == 0)
printf("\n");
}
printf("\n-------------------------------------------------------\n");
printf("Please input your choosen:(1-malloc, 2-free)");
scanf("%d", &sw);
if (sw == 1) {
mallocTG(total);
} else
freeTG(total);
}
return 0;
}
void mallocTG(TG *total)
{
int size, start, count = 0;
char *agentName = (char *)malloc(sizeof(char)*10);
printf("Please input your agentName:");
scanf("%s", agentName);
printf("Please input the size of the TennisGround:");
scanf("%d", &size);
printf("Please input the TennisGround num you want to start:");
scanf("%d", &start);
if ( (total+start)->agentName != " " ) {
printf("malloc failed:");
exit(-1);
} else {
while ( count < size ) {
(total+start+count)->agentName = agentName;
count++;
}
}
}
void freeTG(TG *total)
{
char *an = malloc(sizeof(char)*10);
printf("Please input the agentName you want to free:");
scanf("%s", an);
int count = 0;
while (count < 100)
{
if ( strcmp( (total+count)->agentName, an) == 0 )
(total+count)->agentName = " ";
count++;
}
}