C言語プログラム設計現代方法第二版、第七章授業後のプログラミング練習問題
4928 ワード
第四題
第五題は大文字だけを書いた.
第六題
第七題
第8題第9題
#include #include #include int main(void) { int ch,sum = 0; printf(“Enter a sentence:”); while ((ch=toupper(getchar())) != ‘’) { if (ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’) sum += 1; } printf(“ssssss %d”, sum); system(“pause”); return 0; }
第十一題
#include #include #include int main(void) { float a,b; int ch; printf(“Enter:”); scanf("%f", &a); while ((ch = getchar()) != ‘’) { scanf("%f", &b); switch (ch) { case ‘+’: a = a + b;break; case ‘-’: a = a - b;break; case ‘*’: a = a * b;break; case ‘/’: a = a/b; break; } } printf("%f", a); system(“pause”); return 0; }
#include #include #include int main(void) { float i=0,j=0; float r; int ch; printf(“Enter:”); while ((ch = getchar()) != ‘’) { if (ch == ’ ') i++; else j++; } r =j/(i + 1); printf("%.2f", r); system(“pause”); return 0; }
#include #include #include int main(void) { double y=1, y1=0,y2=1; float x; printf(“Enter x:”); scanf("%f", &x); printf(“Enter y:”); scanf("%f", &y); while (fabs(y2 - y1) >= (0.00001*y1)) { y2 = y; y = (y + x/y)/2;
}
#include
#include
int main(void)
{
int ch;
printf("Enter phone number: ");
while ((ch = getchar()) != '
')
{
if (ch <= 'Z' && ch >= 'A')
{
switch (ch)
{
case 65: case 66: case 67:
printf("2");break;
case 68: case 69: case 70:
printf("3");break;
case 71: case 72: case 73:
printf("4");break;
case 74: case 75: case 76:
printf("5");break;
case 77: case 78: case 79:
printf("6");break;
case 81: case 82: case 83: case 80:
printf("7");break;
case 84: case 85: case 86: case 87:
printf("8");break;
case 88: case 89: case 90:
printf("9");break;
}
continue;
}
putchar(ch);
}
system("pause");
return 0;
}
第五題は大文字だけを書いた.
#include
#include
int main(void)
{
int ch, sum = 0;
printf("Enter a word: ");
while ((ch = getchar()) != '
')
{
switch (ch)
{
case 65: case 69: case 73:case 76: case 78: case 79: case 82: case 83: case 84: case 85:
sum += 1; break;
case 68: case 71:
sum += 2;break;
case 66: case 67: case 77: case 80:
sum += 3;break;
case 70: case 72: case 86:case 87: case 89:
sum += 4; break;
case 75:
sum += 5;break;
case 74: case88:
sum += 8;break;
case 81: case 90:
sum += 10; break;
}
//continue;
}
printf("Scarabble value: %d
",sum);
system("pause");
return 0;
}
第六題
#include
#include
int main(void)
{
printf("int:%d
", sizeof(int));
printf("short:%d
", sizeof(short));
printf("long:%d
", sizeof(long));
printf("float:%d
", sizeof(float));
printf("double:%d
", sizeof(double));
printf("long double:%d
", sizeof(long double));
system("pause");
return 0;
}
第七題
#include
#include
int main(void)
{
int a1, a2, b1, b2, c1, c2;
char ch;
printf("Enter two fractions separated by a plus sign:");
scanf_s("%d/%d%c%d/%d", &a1, &a2,&ch, &b1, &b2);
if (ch == '+')
{
c1 = a1*b2 + b1*a2;
c2 = a2*b2;
}
else if (ch == '-')
{
c1 = a1*b2 - b1*a2;
c2 = a2*b2;
}
else if (ch == '*')
{
c1 = a1*b1;
c2 = a2*b2;
}
else if (ch == '/')
{
c1 = a1*b2;
c2 = a2*b1;
}
else
printf("ERRO");
printf("The result is: %d/%d
", c1, c2);
system("pause");
return 0;
}
第8題第9題
#include
#include
#include
int main(void)
{
int h, m;
char ch,ch1;
printf("Enter a 12-hour time:");
scanf_s("%d:%d %c%c",&h,&m,&ch,1,&ch1,1);
if (toupper(ch) == 'P')
h += 12;
printf("Equivalalent 24-hour time:%d:%d ", h, m);
system("pause");
#include #include #include int main(void) { int ch,sum = 0; printf(“Enter a sentence:”); while ((ch=toupper(getchar())) != ‘’) { if (ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’) sum += 1; } printf(“ssssss %d”, sum); system(“pause”); return 0; }
第十一題
#include
#include
#include
int main(void)
{
int ch,ch1,i=0,sum = 0;
printf("Enter a sentence:");
ch1 = toupper(getchar());
while ((ch=getchar()) != '
')
{
if (ch == ' ' || i == 1)
{
printf("%c", ch);
i = 1;
}
}
printf( ",%c
", ch1);
system("pause");
return 0;
}
#include #include #include int main(void) { float a,b; int ch; printf(“Enter:”); scanf("%f", &a); while ((ch = getchar()) != ‘’) { scanf("%f", &b); switch (ch) { case ‘+’: a = a + b;break; case ‘-’: a = a - b;break; case ‘*’: a = a * b;break; case ‘/’: a = a/b; break; } } printf("%f", a); system(“pause”); return 0; }
#include #include #include int main(void) { float i=0,j=0; float r; int ch; printf(“Enter:”); while ((ch = getchar()) != ‘’) { if (ch == ’ ') i++; else j++; } r =j/(i + 1); printf("%.2f", r); system(“pause”); return 0; }
#include #include #include int main(void) { double y=1, y1=0,y2=1; float x; printf(“Enter x:”); scanf("%f", &x); printf(“Enter y:”); scanf("%f", &y); while (fabs(y2 - y1) >= (0.00001*y1)) { y2 = y; y = (y + x/y)/2;
}
printf("%f",y1);
system("pause");
return 0;
}