c语言程序设计曹计昌答案2017,C语言程序设计答案 曹计昌

阅读: 评论:0

c语言程序设计曹计昌答案2017,C语言程序设计答案 曹计昌

c语言程序设计曹计昌答案2017,C语言程序设计答案 曹计昌

1.4

原码:对于一个二进制数X,如果规定其最高位为符号位,其余各位为该数的绝对值,并且规定符号位值为0表示正,为1表示负,采用这种方式的二进制编码称为该二进制数X的原码。

补码:正数的补码等于正数的原码,负数的补码为其原码除符号位不动外,其余各位变反再加1所得。

反码:对于正数而言,反码与原码相同;对于负数而言,反码符号位的定义与原码相同,但需要将对应原码的数值位按位变反。

1.5 和:10101010

差:00010000

1.6 和 01073

差 -0337

1.7 和 0x1AABA

差 -0x5320

1.8

(251)10=(11111011)2=(373)8=(FB)16

1.10

在16位机中,[157]补= 0000000010011101

[-153]补= 1111111101100111

157-153=157+(-153)

= (0000000010011101)

2+(1111111101100111) 2=(0000000000000100)

2=(4) 10

1.14

算法设计:用变量s存储累加和,k表示计数

描述为:

(1)定义变量s,k。

(2)s清零,k赋初值1。

(3)判断k<101?如果是,顺序执行(4);否则转步骤(5);

(4)k加到累加和变量s中,k加1;转步骤(3)。

(5)输出累加和s。

(6)结束。

开始

结束

int s=0,k=1;

k<101?

s=s+k;

k=k+1;

输出s

N

Y

1.16

第二章习题

2.2

(1) x, ++, +, y

(2)-, 0xabL

(3)2.89e+12L

(4)”String+” FOO””

(5)x, *, *, 2

(6)”X??/”

(7)a, ?, b

(8)x, --, +=, y

(9)intx, =, +, 10

(10)”String”, “FOO”

2.3

不是表识符的如下:

4th 首字母为数字 sizeof关键字

x*y *不是字母、数字、下划线

temp-2 -不是字母、数字、下划线

isn’t ’不是字母、数字、下划线

enum 关键字

2.4

合法常数:

.12 0.L

1.E-5 3.F 浮点型常量

2L 33333 0377UL

0x9cfU 整型常量

“a” “” 字符串常量

‘45’ ‘’ ‘a’ 字符常量

非法常数:

‘‘’必须用转义序列

0x1ag 十六进制没有g

E20 没有尾数部分

‘18’ 要用八进制数

‘xa’ 格式错误,可以是’xa’

“3’4”” 需要转义序列

‘”’ 需要转义序列

2.5

(1)int a, b=5;

(2)double h;

(3)int x=2.3; 0.3 会被截取。

(4)const long y=1; 必须赋初值

(5)float a= 2.5*g; g 没有定义。

(6) int a=b=2; 在 turbo C

中编译出错:未定义的符号’b’在main函数中。

2.6

(1)4

(2)0

(3)1

(4)6

(5)8

(6)0

(7)3.00

(8)1

(9)108

(10)0

2.7 答案不确定

(1)a==b==c c未定义

(2)正确

(3)正确

(4)正确

(5)a*++-b 表达式缺值

(6)a||b^i ^运算的操作数必须是整型,而i不是

(7)i*j%a %运算的操作数必须是整型,而a不是

(8)正确

(9)正确

(10)int(a+b) 应该改成(int)(a+b)

2.9

(1)0

(2)-2

(3)65535

(4)5

(5)60

(6)113

(7)-2

(8)-1

(9)65532

(10)3

2.10

unsigned long encrypt(unsigned long x)

{

unsigned long

x0,x1,x2,x3,x4,x5,x6,x7;

x0=(x

& 0x0000000F) <<

8;

x1=(x

& 0x000000F0);

x2=(x

& 0x00000F00) <<

8;

x3=(x

& 0x0000F000);

x4=(x

& 0x000F0000) <<

8;

x5=(x

& 0x00F00000);

x6=(x

& 0x0F000000) >>

24;

x7=(x

& 0xF0000000);

return(x0|x1|x2|x3|x4|x5|x6|x7);

}

2.11

#include

void main()

{

unsigned long in;

unsigned long a,b,c,d;

scanf("%ld",&in);

//in=1563;

a=(in&0xff000000)>>24;

b=(in&0x00ff0000)>>16;

c=(in&0x0000ff00)>>8;

d=in&0x000000ff;

printf("%d.%d.%d.%d",a,b,c,d);

}

2.15

((k

>>8)& 0xFF00) | ((p

& 0x00FF)<<8)

2.16

max=a>b?a>c?a:c:b>c?b:c;

max=a > b ? ((a > c)

? a : c):((b > c) ? b : c);

2.17

X=y>>n

2.18

(c>=’0’

&& c<=’9’)? c – ‘0’

: c

2.19

(a % 3 == 0) && (a

% 10 == 5) ? a : 0;

第三章习题

3.1

函数原型是指对函数的名称、返回值类型、参数的数目和参数类型的说明。其规定了调用该函数的语法格式,即调用形式。

putchar函数的原型为:int putchar(int c);

puts函数的原型为: int puts(const char *s);

printf函数的原型为:int printf(const char *format,…);

getchar函数的原型为:int getchar_r(void);

gets函数的原型为:char * gets_r(char *s);

scanf函数的原型为: int scanf(const char *format,…);

3.2 不同点:① puts为非格式输出函数,printf为格式输出函数;

② puts函数的参数类型和数目一定(一个字符串),printf函数的参数类型和数目不固定;

③ puts函数输出后会自动换行,printf函数没有这一功能。

相同点:①二者都向标准设备输出;

②二者返回值类型都为int。

3.3 x1=-1,177777,ffff,65535

x2=-3,177775,fffd,65533

y1=123.456703, 123.457,123.457,123.457 (注意对齐)

y2=123.449997,1.23450e+02,123.45

x1(M)= -1

3.4

⑴%c;⑵%c;⑶%f;⑷%f;⑸%lu;⑹%d;⑺%d;⑻%d;⑼%f;⑽%Lf

3.5 ⑴错误,运行提示为divide error

⑵正确,结果为b

⑶正确,结果为 *

⑷正确

⑸正确,但无法正常从结果中退出

⑹正确

⑺正确,结果为82,63

⑻编译错误,提示

cannot modify a const object

⑼正确

⑽正确

3.6 -6.70000

-6

177601

123

-2 0

3.8

#include

void main()

{

char c;

c= getchar_r();

if((c>='0'&&c<='9')||(c>='A'&&c<='F')||(c>='a'&&c<='f'))

{

if((c>='0'&&c<='9'))

{

printf("%dn",c-'0');

}

else

if((c>='A'&&c<='F'))

{

printf("%dn",c-'A'+10);

}

else

printf("%dn",c-'a'+10);

}

else

putchar(c);

}

3.9

#include

void main()

{

short num,high,low;

printf("Please input a short number:n");

scanf("%hd",&num);

low = 0x00ff

& num;

high = 0x00ff

& (num >> 8);

printf("The

high byte is:%cn", high);

printf("The

low byte is:%cn", low);

}

3.10

#include "stdafx.h"

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

{

unsigned short int x;

unsigned short int high,low;

printf("input a integer:n");

scanf("%d",&x);

high =

(x>>12)&0x000f;

low =

(x<<12)&0xf000;

x= x&0x0ff0;

x=x|high|low;

printf("%dn",x);

return 0;

}

3.11

#include

void main()

{

unsigned short int x,m,n;

unsigned short int result;

scanf("%hu%hu%hu",&x,&m,&n);

result=(x>>(m-n+1))<

printf("%hun",result);

}

3.12

#include

void main()

{

float f,c;

scanf("%f",&f);

c=(5*(f-32))/9;

printf("%.0f(F)=%.2f(C)n",f,c);

}

或者

#include

void main()

{

int

f;

float c;

scanf("%d",&f);

c=(5*(f-32))/9;

printf("%d(F)=%.2f(C)n",f,c);

}

3.13

#include

#define PI (3.1415926)

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

{

double r, h;

double s, v;

printf("Please input the r and h.");

scanf("%lf,%lf", &r, &h);

s = 2 * PI * r * h + 2 * PI * r * r;

v = PI * r * r * h;

printf("s is %lf, v is %lf", s, v);

return 0;

}

3.14

#include "stdafx.h"

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

{

char a[4] = "编";

printf("机内码:%x%xtn",a[0]&0xff,a[1]&0xff);

printf("区位码:%xtn",a[0]&0xff<<8+a[1]&0xff-0x2020-0x8080);

printf("国际码:%xtn",a[0]&0xff<<8+a[1]&0xff-0x8080);

return 0;

}

第四章习题

4.1

#include

void main(void)

{

float a,b,c;

printf("Please enter the score of A:n");

scanf("%f",&a);

printf("Please enter the score of B:n");

scanf("%f",&b);

printf("Please enter the score of C:n");

scanf("%f",&c);

if((a-b)*(a-c)<0)

printf("A gets the score %.1f",a);

if((b-a)*(b-c)<0)

printf("B gets the score %.1f",b);

if((c-a)*(c-b)<0)

printf("C gets the score %.1f",c);

}

4.3

#include

int mdays(int y,int m)

{

if (m==2) return (y%4==0 && (y0==0

|| y@0==0))?29:28;

else if (m==4 || m==6 || m==9 || m==11) return 30;

else return 31;

}

main()

{

int y,m,d,days;

printf("Enter year:");

scanf("%d",&y);

printf("Enter month:");

scanf("%d",&m);

printf("Enter day:");

scanf("%d",&d);

days=d;

while(m>1){days+=mdays(y,m-1);m--;}

printf("%dn",days);

}

4.4  if方法:

#include "stdafx.h"

#include

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

{

float x = 0;

printf("input the salaryn");

scanf("%f",&x);

if(x<0)

printf("wrongn");

else if(x>0 &&

x<1000)

printf("0n");

else if(x<2000)

printf("%fn",x*0.05);

else if(x<3000)

printf("%fn",x*0.1);

else if(x<4000)

printf("%fn",x*0.15);

else if(x<5000)

printf("%fn",x*0.2);

else

printf("%fn",x*0.25);

return 0;

}

Case方法:

#include "stdafx.h"

#include

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

{

float x ;

printf("input the salaryn");

scanf("%f",&x);

int xCase = 0;

xCase = (int)(x/1000.0);

switch(xCase)

{

case 0:

printf("0n");

break;

case 1:

printf("%fn",x*0.05);

break;

case 2:

printf("%fn",x*0.1);

break;

case 3:

printf("%fn",x*0.15);

break;

case 4:

printf("%fn",x*0.2);

break;

default:

printf("%fn",x*0.25);

}

return 0;

}

4.7

#include "stdafx.h"

#include

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

{

char *sa;

char c;

int i = 0,j = 0,k = 0;

do

{

c= getchar_r();

sa[i++] = c;

}while(c != 'r');

for(i=0;sa[i+1];i++)

{

for(j = i+1;sa[j];j++)

{

if( sa[i]==sa[j] && sa[j] =='

')

{

for(k=j;sa[k];k++)

sa[k] = sa[k+1];

j--;

}

}

}

for(k=0;sa[k];k++)

printf(",",sa[k]);

return 0;

}

4.10

#include

#define EPS 1e-5

void main()

{

int s=1;

float n=1.0,t=1.0,pi=0;

while(1.0/n>=EPS)

{

pi=pi+t;

n=n+2;

s=s*(-1);

t=s/n;

}

pi=pi*4;

printf("pi=.6fn",pi);

}

4.11

#include

int main()

{

int a,b,num1,num2,temp;

printf("Input a & b:");

scanf("%d%d",&num1,&num2);

if(num1>num2)

{

temp=num1; num1=num2; num2=temp;

}

a=num1; b=num2;

while(b!=0)

{

temp=a%b;

a=b;

b=temp;

}

printf("The GCD of %d and %d is: %dn",num1,num2,a);

printf("The LCM of them is: %dn",num1*num2/a);

}

4.13

#include "stdafx.h"

#include

int Primes(int x);//判断素数函数

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

{

int i,j;

int num;

for(num = 4;num<=100;num++)

{

if(num%2 == 0)

{

for(i=1;i

{

for(j=1;j

{

if(num == i+j)

{

if(Primes(i) && Primes(j))

{

printf("%d=%d+%dn",num,i,j);

}

}

}

}

}

}

return 0;

}

int Primes(int x)

{

int i ;

int n = 0;

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

{

if(x%i==0)

n++;

}

if(n==2)

return 1;

else

return 0;

}

4.17

#include

void main(void)

{

int c,i;

for(i=1,c=32;c<=126;++i,++c)

{

printf("=%-5c",c,c);

if(!(i%8))

printf("n");

}

}

4.18

#include "stdafx.h"

#include

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

{

int x;

int i,n,sum;

printf("input 10 numbersn");

for(i = 0,n = 0,sum = 0;i<10;i++)

{

scanf("%d",&x);

if(x >0)

{

sum+=x;

n++;

}

}

if(n)

printf("numbers = %d,average = %fn",n,1.0*sum/n);

return 0;

}

第五章习题

5.5

Extern和static存储类型的区别:

Static型外部变量和extern型外部变量的唯一区别是作用域的限制。静态外部变量只能作用于定义它的文件,其他文件中的函数不能使用。Extern型外部变量的作用域可以扩大到整个程序的所有文件。

Static和auto存储类型的区别:

静态局部变量和自动变量有根本性的区别。由于静态局部变量在程序执行期间不会消失,因此,它的值有连续性。当退出块时,它的值能保存下来,以便再次进入块时使用,而自动变量的值在退出块时都丢失了。如果定义时静态局部变量有显示初始化,只在第一次进入时执行一次赋初值操作,而自动变量每次进入时都要执行赋初值操作。

5.6

不能。

在C语言中,参数的传递方式是“值传递”,即把实参的值拷贝到参数的存储区中。因此,swap()函数交换的只是实参的本地拷贝,代表swap()实参的变量并没有被改变。

5.7 6,12

5.10

#include

double sum_fac(int n)

{

double s=0;

int i;

double fac=1.0;

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

{

fac*=1.0/i;

s+=fac;

}

return s;

}

void main(void)

{

int n;

printf("Please enter the

integer n:");

scanf("%d",&n);

printf("the sum is %lfn",sum_fac(n));

}

5.17

#include

void reverse()

{

char ch= getchar_r();

if(ch!='n')

{

reverse();

putchar(ch);

}

}

int main()

{

reverse();

printf("n");

return

0;

}

6.1

(1)正确

(2)错误,不需要加“;”

(3)错误,“ident”与“(”之间不能有空格

(4)错误,宏名不能是关键字“void”

(5)错误,将x*y改成((x)*(y))

6.4

将会导致变量blue的重复定义

6.5

(1)#define NO 0

(2)#include “common.h”

(3)#line 3000

(4)#undef TRUE

#define TRUE 1

(5)#if TRUE !=0

#define FALSE 0

#else

#define FALSE 1

#endif

(6)#ifndef SIZE

assert(0);

#else

assert(SIZE<10&&SIZE>1);

#endif

(7)#define SQUARE_VOLUME(x) (x)*(x)*(x)

6.10

#include

#define pi 3.1415926

#define BALL_VOLUME(r) ((4/3)*pi*(r)*(r)*(r))

int main()

{

int r;

float v[11];

for(r=1;r<11;r++)

{

v[r]=float(BALL_VOLUME(r));

printf("r=- v=%.2fn",r,v[r]);

}

return 0;

}

7.9

#include

#include

#define g 10

void main()

{

char *buffer;

int gdriver=DETECT,gmode,i,size;

initgraph(&gdriver,&gmode,"c:tcbgi");

setbkcolor(BLUE);

setcolor(RED);

setlinestyle(0,0,1);

setfillstyle(1,5);

circle(200,250,RED);

size=imagesize(200,250,200,300);

buffer=malloc(size);

getimage_r(200,250,200,300,buffer);

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

putimage(200,250+g*i*i/2,buffer,COPY_PUT);

getch_r();

closegraph();

}

7.11

#include

#define RAND_MAX 32767

#define RAND 100

int RandomInteger(int low,int

high)

{

int k;

double

d;

d=(double)rand()/((double)RAND_MAX+1);

k=(int)(d*(high-low+1));

return(low+k);

}

void main()

{

long i;

int n=0;

int szWord[RAND];

char a[]="heads";

char b[]="tails";

srand(time(0));

for(i=0;i

{

szWord[i]=RandomInteger(0,1);

if(szWord[i]==1)

{

printf("n%s",a);

n++;

}

else

{

printf("n%s",b);

n=0;

}

if(n==3)

{

printf("nIt took %ld flips to get heads 3

consecutives times",i+1);

break;

}

}

}

7.16

char *MonthName(int month);

int MonthDays(int month,int year);

int FirstDayOfMonth(int month,int year);

int IsLeapYear(int year);

enum weak{SUNDAY

,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};

#include “caltools.h”

char *MonthName(int month)

{

Switch(month)

{

Case 1: return(“January”);

Case 2: return(“February”);

Case 3: return(“March”);

Case 4: return(“April”);

Case 5: return(“May”);

Case 6: return(“June”);

Case 7: return(“July”);

Case 8: return(“August”);

Case 9: return(“September”);

Case 10: return(“October”);

Case 11: return(“November”);

Case 12: return(“December”);

Default : printf(“input

error!n”);

}

}

int MonthDays(int month,int year)

{

Case 1:

Case 3:

Case 5:

Case 7:

Case 8:

Case 10:

Case 12: return(31);

Case 4:

Case 6:

Case 9:

Case 11: return(30);

Case 1: return (IsLeapYear(year)?29:28);

}

int FirstDayOfMonth(int month,int year)

{

Int i;

Long total=0;

If(year=2000)

{

For(i=1;i

Total+=( MonthDays(i, 2000);

Return((total%7+6)%7);

)

Else if(year>2000)

{

For (i=2000;i

Total+=(IsLeapYear(i)?366:365);

For(i=1;i

Total+=( MonthDays(i, year);

Return((total%7+6)%7);

)

Else if(year=1999)

{

For(i=12;i>month;i--)

Total+=( MonthDays(i, 1999));

Return((13-total%7)%7);

}

Else

{

For (i=1999;i

Total+=(IsLeapYear(i)?366:365);

For(i=12;i>month;i--)

Total+=( MonthDays(i, year);

Return((13-total%7)%7);

}

}

int IsLeapYear(int year)

{

Return (

!((year%4)&&(year@0)) || !(year@0)

);

}

#include

#include “caltools.h”

Void main()

{

Int month,year;

Printf(“please input the month and year:”);

Scanf(“%d%d”

,&month,&year);

Printf(“the name of the month is %sn”, MonthName(

month));

Printf(“there are %d days in this month.n”,

MonthDays( month,int year));

Printf(“the first day of the month in this year is

%d”, FirstDayOfMonth( month,year));

}

8.4

#include "stdafx.h"

#include "malloc.h"

#define N 65535

void DelSpace(char sa[]);

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

{

char sa[N];

char c;

int i =0;

do

{

c = getchar_r();

if(c == 13)

sa[i++] = 'n';

else

sa[i++] = c;

}while(c!='@');

DelSpace(sa);

int j = 0;

while(1)

{

if(sa[j] == '@')

break;

printf("%c",sa[j++]);

}

printf("/n");

return 0;

}

void DelSpace(char sa[])

{

char *t1 = (char*)malloc(sizeof(sa));

char *t2 = (char*)malloc(sizeof(sa));

t1 = t2 = sa;

while(*t1)

{

*t2++ = *t1++;

if(*(t2-1)=='

' && *t1=='

')

t2--;

}

}

还有一个方法:

void DelSpace(char sa[],int n)

{

char* tmpbuf = (char*)malloc(sizeof(sa)+1);

int p1 = 0, p2 = 0;

bool bSpace = false;

while(p1 < n)

{

if(bSpace && sa[p1] == ' ')

{

p1++;

}

else

{

if(sa[p1] == ' ')

bSpace = true;

else

bSpace = false;

tmpbuf[p2++] = sa[p1++];

}

}

strcpy(sa, tmpbuf);

free( tmpbuf);

}

8.7

#include "stdafx.h"

#define NUM 100

struct stuInfo

{

char name[10];

int mark;

}stu[NUM];

void scoreSort(stuInfo stu[],int n);

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

{

int n;

printf("input the number of students:n");

scanf("%d",&n);

printf("intput the name and scoren");

for(int i = 0;i

{

scanf("%s",&stu[i].name);

scanf("%d",&stu[i].mark);

}

scoreSort(stu,n);

int j = 0;;

while(j

{

printf("name:%s score:%dn",stu[j].name,stu[j].mark);

j++;

}

return 0;

}

void scoreSort(stuInfo *stu,int n)//n为学生数

{

for(int i= 0;i

{

for(int j =i;j

{

if(stu[i].mark

{

stuInfo temp;

temp = stu[i];

stu[i] = stu[j];

stu[j] = temp;

}

}

}

}

8.9

// c84.cpp : Defines the entry point for the

console application.

//

#include "stdafx.h"

#define NUM 100

struct stuInfo

{

char name[10];

int mark;

}stu[NUM];

void scoreSort(stuInfo stu[],int n);

int find(stuInfo *stu,int n,int score);

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

{

int n;

printf("input the number of students:n");

scanf("%d",&n);

printf("intput the name and scoren");

for(int i = 0;i

{

scanf("%s",&stu[i].name);

scanf("%d",&stu[i].mark);

}

scoreSort(stu,n);

printf("input the socre you want to findn");

int score;

scanf("%d",&score);

int ifind =find(stu,n,score) ;

if(ifind>=0)

printf("name:%s score:%dn",stu[ifind].name,stu[ifind].mark);

else

printf("Not Find");

return 0;

}

void scoreSort(stuInfo *stu,int n)//n为学生数

{

for(int i= 0;i

{

for(int j =i;j

{

if(stu[i].mark

{

stuInfo temp;

temp = stu[i];

stu[i] = stu[j];

stu[j] = temp;

}

}

}

}

int find(stuInfo *stu,int n,int score)

{

int i = 0,j = n -1,mid;

while(i <= j)

{

mid = (i+j)/2;

if(score

{

j = mid-1;

}

else if(score>stu[mid].mark)

{

i = mid +1;

}

else

return mid;

}

return -1;

}

8.11

#include "stdafx.h"

#include "malloc.h"

int *strnins(char *s,char *t,int n);

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

{

char *s = "abcd";

char *t = "ttt";

int n = 2;

strnins(s,t,n);

return 0;

}

int *strnins(char *s,char *t,int n)

{

char *p = (char*)malloc(sizeof(s)-1) ;

int tlong = 0;//字符串t的长度

while(t[tlong] !='')

{

tlong++;

}

int i = n;

int j = 0;

while(j

{

p[j]=s[j];

j++;

}

j=0;

while(s[i] != ''&&t[j]!='')

{

p[i+tlong] = s[i];

p[i]= t[j];

i++;

j++;

}

if(s[i]=='')

{

p[i] = t[j];

i++;

j++;

}

else if(t[j]=='')

{

p[i+tlong] = s[i];

i++;

}

printf("%s",p);

free(p);

printf("n");

return 0;

}

8.18

#include "stdafx.h";

#define n 200

void del(int *str,int m,int lenth);

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

{

int i=0,j=-1,str[n];

int total = 0;

int m = n;

while(j

{

i++;

j++;

if(i==3)//数到3

{

del(str,j+1,m);

total++;

i=0;

m--;

}

if(total == n-1)//如果退出了N-1个人

break;

if(j == m)//循环到最后一位

j=-1;

}

printf("%d",j);

return 0;

}

void del(int *str,int m,int

lenth)//将编号为m以后的往前移一位

{

int i =m;

while(i

{

str[i] = str[i+1];

i++;

}

}

9.2

#include

void half(int x);

void main()

{

int x;

printf("please input an interer:n");

scanf("%d",&x);

half(x);

}

void half(int x)

{

int k;

char*p;

char up_half,low_half;

p=(char *)&x;

p=p+1;

for(k=1;k>=0;k--)

{

low_half=(*p)&0x0f;

if(low_half<10)

low_half |='0';

else

low_half=(low_half-10)+'A';

up_half=(*p>>4)&0x0f;

if(up_half<10)

up_half |='0';

else

up_half=(up_half-10)+'A';

p--;

printf("%c %cn",up_half,low_half);

}

}

9.3

#include

void chachen(int *x,int *y);

main()

{

int a[3],b[3];

int *x,*y,i;

x=&a[0];

y=&b[0];

printf("please input 3 numbers of vector x:n");

scanf("%d%d%d",x,x+1,x+2);

printf("please input 3 numbers of vector y:n");

scanf("%d%d%d",y,y+1,y+2);

chachen(x,y);

}

void chachen(int *x,int *y)

{

int i,j,a,b,c;

int k='i';

int *pz,z[3][3];

pz=&z[0][0];

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

{

*(pz+3*i+j)=k++;

}

for(i=1,j=0;j<3;j++)

{

*(pz+3*i+j)=*(x+j);

}

for(i=2,j=0;j<3;j++)

{

*(pz+3*i+j)=*(y+j);

}

a=(*(pz+3*1+1))* (*(pz+3*2+2))- (*(pz+3*1+2))*

(*(pz+3*2+1));

b=(*(pz+3*1+0))* (*(pz+3*2+2))- (*(pz+3*1+2))*

(*(pz+3*2+0));

c=(*(pz+3*1+0))* (*(pz+3*2+1))- (*(pz+3*1+1))*

(*(pz+3*2+0));

printf("the juzhen z is:n");

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

printf("

printf("n");

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

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

printf("=", *(pz+3*i+j));

printf("n");

}

printf("the result is: (%d) i + (%d) j + (%d) k",a,b,c);

}

9.5(用指针做)

#include

#define N 5

void reverse(int a[],int n)

{

int i,k,t;

int *p,*q;

p=&a[0];

q=&a[4];

for(i=0;i

{

t=*p,*p=*q,*q=t;

p++;

q--;

}

printf("the five numbers after reverse:");

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

printf("=",a[k]);

printf("n");

}

void main()

{

int i,a[N];

printf("please input five numbers:");

for(i=0;i

scanf("%d",&a[i]);

reverse(a,N);

}

9.5(用数组做)

#include

#define N 5

void reverse(int a[],int n)

{

int i,temp;

for(i=0;i

{

temp=p[i];

p[i]=p[n-1-i];

p[n-1-i]=temp;

}

for(i=0;i

printf("M",p[i]);

}

void main()

{

int i,a[N];

printf("please input five numbers:");

for(i=0;i

scanf("%d",&a[i]);

reverse(a,N);

}

9.9

#include

void main()

{

int x[5][5];

int i,j;

int *p=&x[0][0];

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

printf("please input line %d of the 5*5 matrix:n",i+1);

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

scanf("%d",p+j+5*i);

}

printf("the transpose of the 5*5 matrix is:n");

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

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

printf ("%d",p+ 5*j+i);

printf ("n");

}

}

9.10

void strcat(char *t,char *s)

{

int i=0,j=0;

while(*(t+i)!=’’)

i++;

while(*(s+j)!=’0’)

{

*(t+j)=*(s+j);

i++;

j++;

}

}

10.4

#include

#include

struct point{

int x,y,z;

};

struct line{

struct point start;

struct point end;

};

double distance(struct line);

main()

{

struct line a={{1,1,1},{2,2,2}};

double length;

printf(“please input the position of the starting points”);

scanf(“%d%d%d”, &a.start.x,

&a.start.y, &a.start .z);

printf(“please input the position of the ending points”);

scanf(“%d%d%d”, &d.x,

&d.y, &d.z);

length =distance(a);

printf(“the distance of the two points is %f”, length);

}

double distance(struct line m)

{

return(sqrt((m.start.x-m.

end.x)*(m.d.x)+(m.d.y)*(m.start.y-m.

end.y)+( m. start.z-m. end.z)*( m. start.z-m. end.z)));

}

10.6

#include

#include

struct point{

int x,y,z;

};

struct line{

struct point start;

struct point end;

};

double distance(struct line);

struct line extenddistance(struct line,int);

main()

{

struct line a={{1,1,1},{2,2,2}};

struct line changeline;

double length;

int k;

printf("please input the position of the starting points");

scanf("%d%d%d", &a.start.x,

&a.start.y, &a.start.z);

printf("please input the position of the ending points");

scanf("%d%d%d", &d.x,

&d.y, &d.z);

length =distance(a);

printf("the distance of the two points is %fn", length);

printf("input the value of the kn");

scanf("%d", &k);

changeline= extenddistance(a,k);

printf("the new position of the ending points is

%d,%d,%dn",d.d.d.z);

}

double distance(struct line m)

{

return(sqrt((m.start.x-m.

end.x)*(m.d.x)+(m.d.y)*(m.start.y-m.

end.y)+( m. start.z-m. end.z)*( m. start.z-m. end.z)));

}

struct line extenddistance(struct line m,int n)

{

double extendlength;

struct line y;

extendlength=n*distance(m);

y.start.x=m.start.x;

y.start.y=m.start.y;

y.start.z=m.start.z;

return(y);

}

10.10

#include

#include

struct stu_study{

char num[10];

char name[10];

int math;

int english;

int chinese;

};

scan(struct stu_study student[]);

print(struct stu_study student[]);

modify(struct stu_study *student);

float* aver_score(struct stu_study student[],float

scores[]);

sort(struct stu_study student[],float scores[]);

main()

{

int i,m;float scores[6];

struct stu_study student[6];

scan(student);

print(student);

printf("which student's information do you want to

modify?n");

scanf("%d",&i);

modify(&student[i]);

print(student);

aver_score(student,scores);

for(m=1;m<6;m++)

{

printf("the average score of student %d is

%.2fn",m,scores[m]);

}

sort(student,scores);

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

{

printf("%s %s %d %d %d

%.2fn",student[i].num,student[i].name,student[i].math,student[i].english,student[i].chinese,scores[i]);

}

}

scan(struct stu_study student[])

{

int i;

printf("please input the information of the five

studentsn");

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

{

printf("please input the information of the student %dn",i);

scanf("%s%s%d%d%d", &student[i].num,

&student[i].name, &student[i].math,

&student[i].english,

&student[i].chinese);

}

}

print(struct stu_study student[])

{

int i;

printf("please output the information of the five

studentsn");

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

{

printf("the information of student %d:n",i);

printf("%s %s %d %d %dn", student[i].num, student[i].name,

student[i].math, student[i].english, student[i].chinese);

}

}

modify(struct stu_study *student)

{

int item;

printf("which item dou you want to modify?n" );

scanf("%d",&item);

switch(item)

{

case 1:

printf("please input the new number:n");

scanf("%s",student->num);

printf("modify sucessfully,the new number is

%sn",student->num);

break;

case 2:

printf("please input the new name:n");

scanf("%s",student->name);

printf("modify sucessfully,the new number is

%sn",student->name);

break;

case 3:

printf("please input the new score of

math:n");

scanf("%d",student->math);

printf("modify sucessfully,the new number is

%sn",student->math);

break;

case 4:

printf("please input the new score of

english:n");

scanf("%d",student->english);

printf("modify sucessfully,the new number is

%sn",student->english);

break;

case 5:

printf("please input the new score of

chinese:n");

scanf("%d",student->chinese);

printf("modify sucessfully,the new number is

%sn",student->chinese);

break;

default:

printf("input error! ");

}

}

float *aver_score(struct stu_study student[],float scores[])

{

int i;

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

{

scores[i]=(student[i].math+ student[i].english+

student[i].chinese)/3;

}

return scores;

}

sort(struct stu_study student[],float scores[])

{int i,j,t,k,m;

float s_scores[6];

for(m=1;m<6;m++)

{s_scores[m]=scores[m];}

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

{

for(j=1;j<6-i;j++)

if(s_scores[j]

{t=s_scores[j],s_scores[j]=s_scores[j+1],s_scores[j+1]=t;}

}

for(k=1;k<6;k++)

printf("%.2fn",s_scores[k]);

}

10.11

#include

#include

struct stu_list{

char num[10];

char name[10];

int math;

int english;

int chinese;

float scores;

struct stu_list *next;

};

struct list *head;

void scan();

void print();

void modify();

void aver_score();

void sort();

main()

{

int i=0;

int m,n;

struct stu_list *p,*q;

scan();

print();

modify();

print();

aver_score();

p=head->next;

for(m=1;m<6;m++)

{

printf("the average score of student %d is

%.2fn",m,p->scores);

p=p->next;

}

sort();

q=head->next;

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

{if(q->scores>60.0

&&

q->scores<70.0)

printf("%s %s %d %d %d

%.2fn",q->num,q->name,q->math,q->english,q->chinese,q->scores);

q=q->next;

}

}

void scan()

{

int i;

struct stu_list *p,*q;

p=(struct list *)malloc(sizeof(struct stu_list));

p->next=NULL;

head=p;

q=p;

printf("please input the information of the five

studentsn");

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

{

p=(struct list *)malloc(sizeof(struct stu_list));

printf("please input the information of the student %dn",i);

scanf("%s%s%d%d%d", &p->num,

&p->name,

&p->math,

&p->english,

&p->chinese);

q->next=p;

q=p;

}

}

void print()

{

int i;

struct stu_list *p,*q;

p=head->next;

printf("please output the information of the five

studentsn");

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

{

printf("the information of student %d:n",i);

printf("%s %s %d %d %dn", p->num,

p->name, p->math,

p->english, p->chinese);

p=p->next;

}

}

void modify()

{

int n,item;

int i=0;

struct stu_list *p;

printf("which student's information do you want to

modify?n");

scanf("%d",&n);

printf("which item dou you want to modify?n" );

scanf("%d",&item);

p=head;

while(i

{p=p->next;

i++;

}

switch(item)

{

case 1:

printf("please input the new number:n");

scanf("%s",p->num);

printf("modify sucessfully,the new number is

%sn",p->num);

break;

case 2:

printf("please input the new name:n");

scanf("%s",p->name);

printf("modify sucessfully,the new number is

%sn",p->name);

break;

case 3:

printf("please input the new score of

math:n");

scanf("%d",p->math);

printf("modify sucessfully,the new number is

%sn",p->math);

break;

case 4:

printf("please input the new score of

english:n");

scanf("%d",p->english);

printf("modify sucessfully,the new number is

%sn",p->english);

break;

case 5:

printf("please input the new score of

chinese:n");

scanf("%d",p->chinese);

printf("modify sucessfully,the new number is

%sn",p->chinese);

break;

default:

printf("input error! ");

}

}

void aver_score()

{

int i;

struct stu_list *p;

float m;

p=head->next;

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

{

p->scores=(p->math+p->english+p->chinese)/3;

p=p->next;

}

}

void sort()

{int i,j,m,k,t;

float s_scores[6];

struct stu_list *p;

p=head->next;

for(m=1;m<6;m++)

{s_scores[m]=p->scores;

p=p->next;

}

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

{

for(j=1;j<6-i;j++)

if(s_scores[j]

{t=s_scores[j],s_scores[j]=s_scores[j+1],s_scores[j+1]=t;}

}

printf("scores after sorted:n");

for(k=1;k<6;k++)

printf("%.2fn",s_scores[k]);

}

10.13

#include

struct list{

char data;

struct list *next;

}

main()

{

struct list *head=NULL,*tail,*p;

char c,b;

c= getchar_r();

head=(struct list *)malloc(sizeof(struct list));

head->data=c;

tail=head;

while((c= getchar_r())!=EOF)

{

tail->next=(struct list *)malloc(sizeof(struct

list));

tail=tail->next;

tail->data=c;

}

tail->next=NULL;

p=head;

while(p)

{

printf("%ct",p->data);

p=p->next;

}

printf("n");

}

本文发布于:2024-01-30 03:52:22,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170655794619034.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23