张凯强-程序设计实践报告

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

程序设计实践报告

姓名张凯强学号 _02_

电气工程学_院__自动化_专业_091_班

2011 _年 12 __月____2___日

指导老师:卢日昌

2-5 将“fly”译成密码“iob”。编码规律:将字母a变成字母d,即变成其后的第3个字母,x变成a,y变成b,z变成c。提示:用赋初值的方法使c1,c2,c3的值分别为f.l.y,按编码规则改变c1,c2,c3后,将结果输出。

#include "iostream.h"

void main()

{

char c1='f',c2='l',c3='y';

c1=(c1>='a'&&c1<='w')?c1+3:c1-23;

c2=(c2>='a'&&c2<='w')?c2+3:c2-23;

c3=(c3>='a'&&c3<='w')?c3+3:c3-23;

cout<

}

3-5编一模拟袖珍计算器的完整程序,运行结果参见图2,.3.1。要求:输入两个操作数和一个操作符,根据操作符决定所做的运算。

#include "iostream.h"

void main()

{

double x,y,z;

char a;

cout<<"请输入操作数1 运算符op 操作数2"<

cin>>x>>a>>y;

switch(a)

{

case '+':z=x+y;break;

case '-':z=x-y;break;

case '*':z=x*y;break;

case '/':z=x/y;break;

}

cout<

}

4-8求Sn=a+aa+aaa+aaaa+…+aa…aaa(n个a),其中a是一个由随机函数产生的1~9(包括1和9)中的一个正整数,n是一个由随机函数产生的5~10(包括5和10)中的一个数。

#include "iostream.h"

#include "stdlib.h"

#include "time.h"

void main()

{

int a,n,i;

long sn=0,temp=0;

srand((unsigned)time(NULL));

a=1+rand()%9;

n=5+rand()%6;

cout<<"a="<

cout<<"n="<

cout<<"sn=";

for(i=1;i

{

temp=temp*10+a;

sn=sn+temp;

cout<

}

temp=temp*10+a;

sn=sn+temp;

cout<

}

5-2随机产生20个学生的计算机课程的成绩(0~100),按照从大到小的顺序排序,分别显示排序前和排序后的结果

#include "iostream.h"

#include "stdlib.h"

#include "time.h"

void main()

{

int a[10],i,j,k,t;

srand(time(0));

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

a[i]=rand()%101;

cout<<"排序前"<

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

cout<

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

{ k=i;

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

if(a[k]>a[j])

{ k=j;

t=a[i],a[i]=a[k];a[k]=t;}

}

cout<<"\n排序后"<

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

cout<

}

6-6编写函数max,其功能是将字符串s中最大字符的地址返回,再编写一个主函

数,调用该函数,将字符串s中从最大字符开始的子串中的小写字母转换成大写字母,然后输出新字符串s。例如,假设s的内容为“qwertyou”,则从最大字符'y'开始的子串为“you”,处理后的s为“qwertYOU”。

函数形式为:char*max(char s[]);

#include "iostream.h"

#include "stdio.h"

#include "string.h"

char *max(char s[])

{

char *m=s,*p=s;

while(*p!='\0')

{

if(*p>*m)

m=p;

p++;

}

return m;

}

void main()

{

char s[1000],*p;

gets(s);

p=max(s);

while(*p!='\0')

{

if(*p>='a'&&*p<='z')

*p=*p-32;

p++;

}

puts(s);

}

7-6 编写程序,建立一个带有头结点的单项链表。链表结点中的数据为从键盘输入的一个字符串,但要求将该字符串按由小到大的顺序组织到链表中的。

#include

#include

#include

#include

struct node

{ char data;

struct node *next;

};

void main()

{

int i,j,k,n;

char s[100],t;

相关文档
最新文档