精华内容
下载资源
问答
  • 1: #include<stdio.h> int main(void) { long int i,n; printf("This program prints a table of squares.\n"); printf("Enter number of entries in table:"); scanf("%ld",&...i+...
    1#include<stdio.h>
    int main(void)
    {
    	long int i,n;
    	printf("This program prints a table of squares.\n");
    	printf("Enter number of entries in table:");
    	scanf("%ld",&n);
    	for(i=1;i<=n;i++)
    		printf("%10ld%10ld\n",i,i*i);//i最大46340输入到这里,i*i就会到达long int类型的最大值,我的电脑是2147395600。short的情况,i=180,i*i达到最大值//
    
    	return 0;
    }
    
    2#include <stdio.h> 
     
    int main (void)
    {
     	int i, n;
     	
     	printf ("This program prints a table of squares. \n");
     	printf ("Enter number of entries in table: ");
     	scanf ("%d", &n);
      getchar(); //接收scanf留下的回车 这是个缓存区陷阱//
     	
     	for (i = 1; i <= n; i++) {
     		printf ("%10d%10d\n", i, i * i);
     		if (i % 24 == 0) {
     			printf ("Press Enter to continue...");
    			getchar ();
    		 }
    	 }
     
     
    	return 0;
     }
    
    
    3#include <stdio.h>
     
    int main (void) 
    {
    	double n, sum = 0.0;
    	
    	printf ("This program sums a series of integers.\n");
    	printf ("Enter inetgers (0 to terminate): ");
    	
    	scanf ("%lf", &n);                     //这里scanf是用的%lf, printf是用的%f 
    	while (n != 0.0) {
    		sum += n;
    		scanf ("%lf", &n);
    	}
    	
    	printf ("The sum is:%f\n", sum);
    	
    	return 0;
    
    }
    
    4#include <stdio.h>
     
    int main (void)
    {
    	int ch;
    	
    	printf ("Enter phone number: ");
    	
    	while ((ch = getchar ()) != '\n') {
    		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;
    		}
    		
    		printf ("%c", ch);
    		
    	}
    	
    	return 0;
    }
    
    
    
    5:
    #include <stdio.h>
     
    int main (void)
    {
    	int ch;
    	
    	printf ("Enter phone number: ");
    	
    	while ((ch = getchar ()) != '\n') {
    		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;
    		}
    		
    		printf ("%c", ch);
    		
    	}
    	
    	return 0;
    }
    
    
    
    6:
    #include<stdio.h>
    int main(void)
    {
    	printf("int大小=%d\n",(int)sizeof(int));
    	printf("short大小=%d\n",(int)sizeof(short int));
    	printf("long大小=%d\n",(int)sizeof(long int));
    	printf("float大小=%d\n",(int)sizeof(float));
    	printf("double大小=%d\n",( int)sizeof(double));
    	printf("long double大小=%d\n",( int)sizeof(long double));
    
    	return 0;
    }
    
    7:
    
    #include <stdio.h>
     
    int main (void)
    {
    	int num1, denom1, num2, denom2, result_num, result_denom;
    	char ch;
    	
    	printf ("Enter two fractions separated by a sign which wanted: ");
    	scanf ("%d/%d%c%d/%d", &num1, &denom1, &ch, &num2, &denom2);
    	
    	switch (ch) {
    		case '+':
    			result_num = num1 *denom2 + num2 * denom1;
    			result_denom = denom1 * denom2;
    			break;
    		case '-':
    			result_num = num1 *denom2 - num2 * denom1;
    			result_denom = denom1 * denom2;
    			break;
    		case '*':
    			result_num = num1 * num2;
    			result_denom = denom1 * denom2;
    			break;
    		case '/':
    			result_num = num1 *denom2;
    			result_denom = num2 * denom1;
    			break;
    	}
    	
    	
    	//以下为化简最简成最简分式 
    	int n, m, temp;
    	n = result_num;
    	m = result_denom;
    	while (n != 0) {
    		temp = n;
    		n = m % n;
    		m = temp;
    	}
    	
    	result_num = result_num / m;
    	result_denom = result_denom / m;
    	
    	printf ("The result is %d/%d", result_num, result_denom);
    	
    	return 0;
    }
    
    
    8#include <stdio.h>
     
    int main (void)
    {
    	int num1, denom1, num2, denom2, result_num, result_denom;
    	char ch;
    	
    	printf ("Enter two fractions separated by a sign which wanted: ");
    	scanf ("%d/%d%c%d/%d", &num1, &denom1, &ch, &num2, &denom2);
    	
    	switch (ch) {
    		case '+':
    			result_num = num1 *denom2 + num2 * denom1;
    			result_denom = denom1 * denom2;
    			break;
    		case '-':
    			result_num = num1 *denom2 - num2 * denom1;
    			result_denom = denom1 * denom2;
    			break;
    		case '*':
    			result_num = num1 * num2;
    			result_denom = denom1 * denom2;
    			break;
    		case '/':
    			result_num = num1 *denom2;
    			result_denom = num2 * denom1;
    			break;
    	}
    	
    	
    	//以下为化简最简成最简分式 
    	int n, m, temp;
    	n = result_num;
    	m = result_denom;
    	while (n != 0) {
    		temp = n;
    		n = m % n;
    		m = temp;
    	}
    	
    	result_num = result_num / m;
    	result_denom = result_denom / m;
    	
    	printf ("The result is %d/%d", result_num, result_denom);
    	
    	return 0;
    }
    
    
    9#include<stdio.h>
    int main(void)
    {
    	int x,y;
    	char ch;
    	printf("Enter a 12-hour time:");
    	scanf("%d:%d",&x,&y);
    	while((ch=getchar())!='\n'){
    		switch(ch){
    		case 'p':
    		case 'P':x=x+12;
    			break;
    		case 'A':
    		case 'a':x=x+1-1;
    			break;}}
    		printf("Equivalent 24-hour time: %d:%d\n",x,y);
    		return 0;
    	}
    
    10#include<stdio.h>
    int main(void)
    {
    	char ch;
    	int sum=0;
    	printf("Enter a sentence:");
    	while((ch=getchar())!='\n'){
    		
    			if ((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u')||(ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U')){
    				sum++;}
    	}
    	printf("Your sentence contains %d vowels\n",sum);
    	return 0;
    }
    
    11#include<stdio.h>
    int main(void)
    {
    	char x,y,ch;
    	int sum=0;
    	printf("Enter a first and last name:");
    	scanf("  %c",&x);
    	while(1)
    	{
    		y=getchar();
    		if(65<=y&&y<97)
    		{
    			printf("%c",y);
    			while((ch=getchar())!='\n'){
    				if(ch==' ')break;
    				printf("%c",ch);
    				sum++;
    			}
    			
    			}
    		if(sum>=1)break;
    	}	
    		printf(", %1c.",x);
    		return 0;
    
    	}
    
    12#include<stdio.h>
    int main(void)
    {
    	char x,y,ch;
    	int sum=0;
    	printf("Enter a first and last name:");
    	scanf("  %c",&x);
    	while(1)
    	{
    		y=getchar();
    		if(65<=y&&y<97)
    		{
    			printf("%c",y);
    			while((ch=getchar())!='\n'){
    				if(ch==' ')break;
    				printf("%c",ch);
    				sum++;
    			}
    			
    			}
    		if(sum>=1)break;
    	}	
    		printf(", %1c.",x);
    		return 0;
    
    	}
    
    		
    		
    	
    
    
    13#include<stdio.h>
    int main(void)
    {
    	char ch;
    	float sum=0.0,sum1=1.0;
    	printf("Enter a sentence:");
    	while((ch=getchar())!='\n')
    	{    
    		
    		sum++;
    		if(ch==' '){
    		sum1++;
    		 sum--;
    	}
    	}
    	printf("Average word length:%.1f\n",sum/sum1);
    	return 0;
    }
    
    14#include<stdio.h>
    #include<math.h>
    int main(void)
    {
    	double x,y=1;
    	printf("Enter a positive number:");
    	scanf("%lf",&x);
    	while(fabs((((x/y)+y)/2)-y)>=0.00001*y)
    	{
    		y=((x/y)+y)/2;}
    	printf("Square root:%.5f\n",y);
    	return 0;
    }
    
    		
    
    
    
    15#include <stdio.h>
     
    int main (void)
    {
    	float num;           // 具体自己换成不同的基本类型 
    	 float res = 1;
    	
    	printf ("Enter a positive integer: ");
    	scanf ("%f", &num);
    	
    	for (int i = 1; i <= num; i++) {
    		res *= i;
    	}
    	
    	printf ("Factorial of %f:%f", num, res);
    	
    	return 0;
    }
    
    
    展开全文
  • 四题 #include <stdio.h> #include<string> int main(void) { int ch; printf("Enter phone number: "); while ((ch = getchar()) != '\n') { if (ch <= 'Z' && ch >= 'A') ...

    第四题

    #include <stdio.h>
    #include<string>
    
    int main(void)
    {
    	int ch;
    	printf("Enter phone number: ");
    
    	while ((ch = getchar()) != '\n') 
    	{
    		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 <stdio.h>
    #include<string>
    
    int main(void)
    {
    	int ch, sum = 0;
    	printf("Enter a word: ");
    	while ((ch = getchar()) != '\n') 
    	{
    			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\n",sum);
    	system("pause");
    	return 0;
    }
    
    
    

    第六题

    #include <stdio.h>
    #include<string>
    
    int main(void)
    {
    	printf("int:%d\n", sizeof(int));
    	printf("short:%d\n", sizeof(short));
    	printf("long:%d\n", sizeof(long));
    	printf("float:%d\n", sizeof(float));
    	printf("double:%d\n", sizeof(double));
    	printf("long double:%d\n", sizeof(long double));
    	system("pause");
    	return 0;
    }
    
    
    

    第七题

    #include <stdio.h>
    #include<string>
    
    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\n", c1, c2);
    	system("pause");
    	return 0;
    }
    
    
    

    第八题
    第九题

    #include <stdio.h>
    #include<string>
    #include <ctype.h>
    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 <stdio.h>
    #include
    #include <ctype.h>
    int main(void)
    {
    int ch,sum = 0;
    printf(“Enter a sentence:”);
    while ((ch=toupper(getchar())) != ‘\n’)
    {
    if (ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’)
    sum += 1;
    }
    printf(“ssssss %d\n”, sum);
    system(“pause”);
    return 0;
    }

    第十一题

    #include <stdio.h>
    #include<string>
    #include <ctype.h>
    int main(void)
    {
    	int ch,ch1,i=0,sum = 0;
    	printf("Enter a sentence:");
    	ch1 = toupper(getchar());
    	while ((ch=getchar()) != '\n')
    	{
    		if (ch == ' ' || i == 1)
    		{
    			printf("%c", ch);
    			i = 1;
    		}
    	}
    	printf( ",%c\n", ch1);
    	system("pause");
    	return 0;
    }
    
    第十二题
    
    

    #include <stdio.h>
    #include
    #include <ctype.h>
    int main(void)
    {
    float a,b;
    int ch;
    printf(“Enter:”);
    scanf("%f", &a);
    while ((ch = getchar()) != ‘\n’)
    {
    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\n", a);
    system(“pause”);
    return 0;
    }

    第十三题
    
    

    #include <stdio.h>
    #include
    #include <ctype.h>
    int main(void)
    {
    float i=0,j=0;
    float r;
    int ch;
    printf(“Enter:”);
    while ((ch = getchar()) != ‘\n’)
    {
    if (ch == ’ ')
    i++;
    else
    j++;
    }
    r =j / (i + 1);
    printf("%.2f\n", r);
    system(“pause”);
    return 0;
    }

    第十四题
    
    

    #include <stdio.h>
    #include
    #include <ctype.h>
    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;
    

    }

    
    
    展开全文
  • printf("%d位,%d",mid,num); break; } }} 7.10 int main(int argc, char *argv[]) { chara[3][10]={"abc","ABCDEFGHIJ","a b c def"}; int i,j,q=0,w=0,e=0; for(i=0;i;i++) { for(j=0;j;j++) if(a[i][j]>='a'&&a...

    7.1

    int main(int argc, char *argv[]) {
    int i,j;
    for(i=2;i<=100;i++)
             {for(j=2;j<i;j++)
             if(i%j==0)break;
        if(j>=i)
             printf("%d,",i);
             }      

    7.2

     

    int main(int argc, char *argv[]) {

    int a[10]={1,5,9,6,7,3,2,4,8,10};

    int i,j,t;

    for(i=0;i<10;i++)

    for(j=i+1;j<10;j++)

     

             if(a[i]<a[j])

             {t=a[i];a[i]=a[j];a[j]=t;}

    for(i=0;i<10;i++)

    printf("%d ",a[i]);

    }

     7.3

    int main(int argc, char *argv[]) {

    int i,a[3][3]={{1,2,3},{4,5,6},{7,8,9}},t=0;

    for(i=0;i<3;i++)

    t=t+a[i][i];

    printf("%d",t);

    }

    7.4

    int main(int argc, char *argv[]) {

    int i,a[10]={1,2,3,4,5,8,9},t,j,b;

    scanf("%d",&t);

    for(i=0;i<7;i++)

     if(t>=a[i]&& t<a[i+1])

     {

     b=i;

     for(j=7;j>i+1;j--)

    {a[j]=a[j-1];

    }      

     }

    a[b+1]=t;

    for(i=0;i<10;i++)

    printf("%d ",a[i]);

    }

    7.5

    int main(int argc, char *argv[]) {

    int a[5]={8,6,5,4,1};

    int i,t,b;

    t=5/2;

    for(i=0;i<t;i++)

    {

    b=a[i];a[i]=a[4-i];a[4-i]=b;    

    }

    for(i=0;i<5;i++)

    printf("%d",a[i]);

    }

    7.6

    int main(int argc, char *argv[]) {

    int a[10][10]={0};

    int i,j;

    for(i=0;i<10;i++)

    a[i][0]=1;

    for(i=1;i<10;i++)

    for(j=1;j<10;j++)

    {

      a[i][j]=a[i-1][j-1]+a[i-1][j];

    }

    for(i=0;i<10;i++)

    for(j=0;j<=i;j++)

    {

    printf("%d ",a[i][j]);

    if(j==i)printf("\n");

    }

    }

    7.7

    7.8

    int main(int argc, char *argv[]) {

            

    int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};

    int i,j,t,q,m,p;

    p1:for(i=0;i<3;i++)

    {

    q=0;

    for(j=0;j<3;j++)

             if(q<a[i][j])

             {q=a[i][j];

             m=j;

             }

         p=q;

    for(t=0;t<3;t++)

    if(p>a[t][m])p=a[t][j];

    if(q<=p)printf("%d\n",q);

    }

    }

    7.9

    nt main(int argc, char *argv[]) { 

    int a[15]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

    int i=0,mid,j=14,num;

    printf("输入要查找的数\n");

    scanf("%d",&num);

    for(;i!=j;)

    {

    mid=(i+j)/2;

    if(num>a[mid])i=mid;

    else if(num<a[mid])j=mid;

    else {

    printf("第%d位,%d",mid,num);

    break;

    }

    }}

    7.10

    int main(int argc, char *argv[]) {

    chara[3][10]={"abc","ABCDEFGHIJ","a b c def"};

    int i,j,q=0,w=0,e=0;

    for(i=0;i<10;i++)

    {

    for(j=0;j<10;j++)

    if(a[i][j]>='a'&&a[i][j]<='z')

    q++;

    else if(a[i][j]>='A'&&a[i][j]<='Z')

    w++;

    else if(a[i][j]==' ')

    e++;

    }

    printf("%d,%d,%d",q,w,e);

    }

    7.11

    int main(int argc, char *argv[]) {

             chara[5][9]={};

             int i,j;

             for(i=0;i<5;i++)

             for(j=0;j<9;j++)

             {

             if((j<i)||(j>=i+5))

             a[i][j]='';

             else

             a[i][j]='*';

    }

    for(i=0;i<5;i++)

    for(j=0;j<9;j++)

    {printf("%c",a[i][j]);

    if(j%9==0)printf("\n");

    }

    }

    7.12

    int main(int argc, char *argv[]) {

             chara[81];

             int i;

             printf("请输入密码:\n");

             gets(a);

             for(i=0;i<81;i++)

             if(a[i]>=65&&a[i]<=90)

             a[i]=90-(a[i]-65);

             elseif(a[i]>=97&&a[i]<=122)

       a[i]=122-(a[i]-97);

        printf("输出原文:\n");

       printf("%s",a);

    }

    7.13

    int main(int argc, char *argv[]) {

             chara[3]={"abc"},b[8]={"ABC"};

             int i,j;

             for(i=0;i<3;i++)

             b[3+i]=a[i];

             printf("%s",b);

    }

    7.14

    int main(int argc, char *argv[]) {

             chara[3],b[3];

             inti,j=0;

             printf("输入数组a:\n");

             gets(a);

             printf("输入数组b\n");

             gets(b);

             for(i=0;i<3;i++)

             if(a[i]>b[i])

             {printf("a>b大%d",a[i]-b[i]);break;}

             elseif(a[i]<b[i]){printf("a<b %d",a[i]-b[i]);break;}

             elseif((a[i]==b[i])&&i==2)printf("a和b相等:%d",j);

    }

    7.15

    int main(int argc, char *argv[]) {

             chara[15]={"adcv"},s2[3]={"ABC"};

             inti=0,j=0;

    for(;i<3;i++)

                 a[i]=s2[i];

        a[i]='\0';

       printf("%s",a);

    }

    展开全文
  • 自己练习时手写,难免会有些疏忽遗漏等各种各样问题,错误之处还请指出 但这些代码确实已通过编译,实现了书上的输出结果,还希望能给抱有期待之人作为个小参考 7.1 这个直接粘上原答案吧! ...

    自己练习时手写,难免会有些疏忽遗漏等各种各样问题,错误之处还请指出

    但这些代码确实已通过编译,实现了书上的输出结果,还希望能给抱有期待之人作为个小参考


    7.1 这个直接粘上原答案吧!

    7.2

    #include <stdio.h> 
    
    int main (void)
    {
     	int i, n;
     	
     	printf ("This program prints a table of squares. \n");
     	printf ("Enter number of entries in table: ");
     	scanf ("%d", &n);
     	getchar ();     //接收scanf留下的回车 
     	
     	for (i = 1; i <= n; i++) {
     		printf ("%10d%10d\n", i, i * i);
     		if (i % 24 == 0) {
     			printf ("Press Enter to continue...");
    			getchar ();
    		 }
    	 }
    
     
    	return 0;
     } 

    7.3

    #include <stdio.h>
    
    int main (void) 
    {
    	double n, sum = 0.0;
    	
    	printf ("This program sums a series of integers.\n");
    	printf ("Enter inetgers (0 to terminate): ");
    	
    	scanf ("%lf", &n);                     //这里scanf是用的%lf, printf是用的%f 
    	while (n != 0.0) {
    		sum += n;
    		scanf ("%lf", &n);
    	}
    	
    	printf ("The sum is:%f\n", sum);
    	
    	return 0;
    }

    7.4

    #include <stdio.h>
    
    int main (void)
    {
    	int ch;
    	
    	printf ("Enter phone number: ");
    	
    	while ((ch = getchar ()) != '\n') {
    		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;
    		}
    		
    		printf ("%c", ch);
    		
    	}
    	
    	return 0;
    }

    7.5

    #include <stdio.h>
    
    int main (void)
    {
    	int sum = 0; 
    	char ch;
    	
    	printf ("Enter a word: ");
    
    	while ((ch = getchar ()) != '\n') {
    		if ((ch<=65&&ch>=90)&&(ch<=97&&ch>=122))
    			printf ("Illegal input!");
    			
    		switch (ch) {
    			case 'A': case 'a': case 'E': case 'e': case 'I': case 'i':
    			case 'L': case 'l': case 'N': case 'n': case 'O': case 'o': case 'R':
    			case 'r': case 'S': case 's': case 't': case 'T': case 'U': case 'u':
    				sum += 1;
    				break;
    			case 'd': case 'D': case 'G': case 'g':
    				sum += 2;
    				break;
    			case 'B': case 'b': case 'C': case 'c': case 'M': case 'm':
    			case 'P': case 'p':
    				sum += 3;
    				break;
    			case 'F': case 'f': case 'H': case 'h': case 'V': case 'v':
    			case 'W': case 'w': case 'Y': case 'y':
    				sum += 4;
    				break;
    			case 'K': case 'k':
    				sum += 5;
    				break;
    			case 'J': case 'j': case 'X': case 'x':
    				sum += 8;
    				break;
    			case 'Q': case 'q': case 'Z': case 'z':
    				sum += 10;
    				break;
    		}
    	}
    	
    	printf ("Scrabble value: %d", sum);
    	
    	return 0;
    }

    上面是反面教材,不认真审题的后果就是自己瞎写,搞得贼麻烦。调用ctype.h头文件用toupper可以省不少事,这里懒得改了

    7.6

    #include <stdio.h>
    
    int main (void)
    {
    	printf ("%d\n", (int)sizeof(int));
    	printf ("%d\n", (int)sizeof(short));
    	printf ("%d\n", (int)sizeof(long));
    	printf ("%d\n", (int)sizeof(float));
    	printf ("%d\n", (int)sizeof(double));
    	printf ("%d\n", (int)sizeof(long double));
    	
    	return 0;
    }

    7.7

    #include <stdio.h>
    
    int main (void)
    {
    	int num1, denom1, num2, denom2, result_num, result_denom;
    	char ch;
    	
    	printf ("Enter two fractions separated by a sign which wanted: ");
    	scanf ("%d/%d%c%d/%d", &num1, &denom1, &ch, &num2, &denom2);
    	
    	switch (ch) {
    		case '+':
    			result_num = num1 *denom2 + num2 * denom1;
    			result_denom = denom1 * denom2;
    			break;
    		case '-':
    			result_num = num1 *denom2 - num2 * denom1;
    			result_denom = denom1 * denom2;
    			break;
    		case '*':
    			result_num = num1 * num2;
    			result_denom = denom1 * denom2;
    			break;
    		case '/':
    			result_num = num1 *denom2;
    			result_denom = num2 * denom1;
    			break;
    	}
    	
    	
    	//以下为化简最简成最简分式 
    	int n, m, temp;
    	n = result_num;
    	m = result_denom;
    	while (n != 0) {
    		temp = n;
    		n = m % n;
    		m = temp;
    	}
    	
    	result_num = result_num / m;
    	result_denom = result_denom / m;
    	
    	printf ("The result is %d/%d", result_num, result_denom);
    	
    	return 0;
    }

    7.8

    #include <stdio.h>
    #include <ctype.h>
    
    int main (void)
    {
    	int hours, minutes;
    	int time;
    	char ch;
    	
    	printf ("Enter a 12-hour time:");
    	scanf ("%d:%d %c", &hours, &minutes, &ch);
    	
    	switch (toupper(ch)) {
    		case 'P': 
    			time = hours * 60 + minutes + 12 * 60;
    			break;
    		case 'A' :                        
    			time = hours * 60 + minutes;
    			break;
    	}
    	
    	// 480 583 679 767 840 945 1140 1305 这是几个起飞时间换算为分钟的结果 
    	if (time < 480){
    		printf ("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
    	} else if (time < 583) {
    		if ((time-480) < (583-time)) printf ("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
    		else printf ("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
    	} else if (time < 679) {
    		if ((time-583) < (679-time)) printf ("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
    		else printf ("Closest departure time is 11:19 a.m., arriving at 1:31 p.m");
    	} else if (time < 767) {
    		if ((time-679) < (767-time)) printf ("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.");
    		else printf ("Closest departure time is 12:47 a.m., arriving at 3:00 p.m");
    	} else if (time < 840) {
    		if ((time-767) < (840-time)) printf ("Closest departure time is 12:47 a.m., arriving at 3:00 p.m.");
    		else printf ("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
    	} else if (time < 945) {
    		if ((time-840) < (945-time)) printf ("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
    		else printf ("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
    	} else if (time < 1140) {
    		if ((time-945) < (1140-time)) printf ("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
    		else printf ("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
    	} else {
    		if ((time-1140) < (1305-time)) printf ("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
    		else printf ("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
    	}
    	
    	return 0;	
    } 

    7.9

    #include <stdio.h>
    #include <ctype.h>
    
    int main (void)
    {
    	int hours, minutes;
    	char ch;
    	
    	printf ("Enter a 12-hour time: ");
    	scanf ("%d:%d %c", &hours, &minutes, &ch);
    	
    	if (toupper(ch) == 'P') hours = hours + 12;
    	
    	printf ("Equivalent 24-hour time: %d:%d", hours, minutes);
    	
    	return 0;
     } 

    7.10

    #include <stdio.h>
    
    int main (void)
    {
    	char ch;
    	int sum = 0;
    	
    	printf ("Enter a sentence: ");
    	
    	while ((ch = getchar()) != '\n') {
    		if ((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u'))
    			sum++;
    	}
    	
    	printf ("Your sentence contains %d vowels", sum);
    	
    	return 0;
    }

    7.11

    #include <stdio.h>
    
    int main (void)
    {
    	int ch1, ch2;
    	
    	printf ("Enter a first and last name: ");
    	scanf ("%c", &ch1);
    	
    	while ((getchar()) != ' ')
    		;
    	
    	while ((ch2 = getchar()) != '\n') {
    		printf ("%c", ch2);
    	}
    	printf (", %c.", ch1);
    	
    	return 0;
    }

    7.12

    #include <stdio.h>
    
    int main (void)
    {
    	double num1, num2;
    	char sym;
    	
    	printf ("Enter an expression: ");
    	scanf ("%lf", &num1);
    	
    	while (1) {	
    		sym = getchar();
    		if (sym == '\n') break;
    		scanf ("%lf", &num2);
    		switch (sym) {
    			case '+':
    				num1 = num1 + num2;
    				break;
    			case '-':
    				num1 = num1 - num2;
    				break;
    			case '*':
    				num1 = num1 * num2;
    				break;
    			case '/':
    				num1 = num1 / num2;
    				break;
    		}
    		 
    	}
    	
    	printf ("Value of expression: %.1f", num1);
    	
    	return 0;
    }

    7.13

    #include <stdio.h>
    
    int main (void)
    {
    	char ch;
    	int sum = 0, num = 1;
    	
    	printf ("Enter a sentence: ");
    	
    	while ((ch= getchar()) != '\n') {      		//输入字符直到遇到回车为止     	   
    		sum++;													
    		if (ch == ' ') {								
    			num++;								//若遇到空格,则单词个数+1,该单词字母数-1 
    			sum--;								
    		} 
    	}
    	
    	printf ("Average word length: %.1f", (float)sum/num);
    	
    	return 0;
    }

    7.14

    #include <stdio.h>
    #include <math.h>
    
    int main (void)
    {
    	double x, y = 1.0;
    	
    	printf ("Enter a positive number: ");
    	scanf ("%lf", &x);
    	
    	while ((fabs (y - ((y + x / y) / 2))) >= 0.00001 * y) {
    		y = ((y + x / y) /2);
    	}
    	
    	printf ("Square root: %.5f", y);
    	
    	return 0;
    }

    7.15

    #include <stdio.h>
    
    int main (void)
    {
    	int num;           // 具体换成不同的基本类型 
    	int res = 1;
    	
    	printf ("Enter a positive integer: ");
    	scanf ("%d", &num);
    	
    	for (int i = 1; i <= num; i++) {
    		res *= i;
    	}
    	
    	printf ("Factorial of %d: %d", num, res);
    	
    	return 0;
    }

     

     

     

    展开全文
  • 第七章 循环结构 第八章 转移语句 第九章 数组 第十章 函数 第十一章 指针的概念 第十章 多维数组的指针变量 第十三章 结构 第十四章 C语言程序设计基础之联合 第十五章 枚举与位运算 第十六章 预处理 第十七章 ...
  • 教材:C语言程序设计教程(第二版) 王敬华、林萍、张清国编著 数组是一种构造数据类型,表示同一类型的数据项的有序集合 本主要有三个很重要的小节: 7.1 一维数组 7.1.1 一维数组的定义和引用 7.1.2 一维数组的...
  • 第七章 数组排序问题选择法排序冒泡法排序(升序)查找问题分查找法字符串输入与转换十六进制字符串转换成十进制非负整数大小字母转换删除字符串中的空格使用1个数组使用2个数组
  • //在64位机上,测得程序保持正常时,n的最大值为46340,与理论值一致;当n继续增大,输出会越界 CH7_2 # include int main ( void ) { int n ; printf ( "Enter the number: " ) ; scanf...

空空如也

空空如也

1 2 3 4 5 ... 7
收藏数 124
精华内容 49
关键字:

c语言程序设计第二版第七章

c语言 订阅