1.鞍点071
#include<iostream.h>
#include<stdio.h>
#define N 10
#define M 10
main()
{
int i,j,k,m,n,flag1,flag2,a[N][M],max,maxj;
printf("\ninput hang n:");
scanf("%d",&n);
printf("\ninput lie m:");
scanf("%d",&m);
for(i=0;i<n;i++)
{
printf("di %dhang?\n",i);
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%5d",a[i][j]);
printf("\n");
}
flag2=0;
for(i=0;i<n;i++)
{
max=a[i][0];
for(j=0;j<m;j++)
if(a[i][j]>max)
{
max=a[i][j];
maxj=j;
}
for(k=0,flag1=1;k<n&&flag1;k++)
if(max>a[k][maxj])
flag1=0;
if(flag1)
{
printf("\ndi%dhang,di%lie de %d shi andian\n",i,maxj,max); flag2=1;
}
}
if(!flag2)
printf("\njuzhen zhong wu andian!\n");
}
- 1 -
.2。超市072
#include<iostream>
using namespace std;
struct Goods
{
double Id;
char *Name;
float Price;
int Number;
Goods *next;
};
class Supermarket
{
public:
Goods *L;
Goods *Query(double i)
{
Goods *r;
r=L->next;
while(r!=NULL){
if(r->Id==i){
cout<<"Goods ID: "<<r->Id<<endl
<<"Name: "<<r->Name<<endl
<<"Price: "<<r->Price<<endl
<<"Number: "<<r->Number<<endl<<endl;
return r;
}
r=r->next;
}
cout<<"No goods found!"<<endl;
return NULL;
}
};
Supermarket::Supermarket() 在类声明的外部定义函数,必须指定类名
{
L=new Goods; 开辟存放结构体的空间,返回一个指向结构体的指针。 L->next=NULL;
}
void Supermarket::Append(double id,char *name,float price,int number)
{
Goods *r=L;
Goods *p;
while(r->next!=NULL)
{
- 2 -
r=r->next;
}
p=new Goods;
p->Id=id;
p->Name=name;
p->Price=price;
p->Number=number;
p->next=NULL;
r->next=p;
}
void Supermarket::Delete(double i) {
Goods *p,*r;
p=L;
r=L->next;
while(r!=NULL)
{
if(r->Id==i)break;
p=r;r=r->next;
}
if(r==NULL)
{
cout<<"goods do not exist!"<<endl; return;
}
p->next=r->next;
delete r;
}
void main()
{
Supermarket s;
printf("%p\n",s.L);
s.Append(1,"apple",5.7,10);
s.Append(2,"meat",9.8,5);
s.Append(3,"milk",2.7,8);
s.Query(1);
s.Query(2);
s.Query(3);
s.Delete(1);
s.Query(1);
}
.3。居民062
#include<iostream.h>
class Resident
{
public:
- 3 -
void adddata(char *Id,char *Name,char Sex,char *Birth)
{
id=Id;
name=Name;
sex=Sex;
birth=Birth;
}
void deldata()
{
id=" ";
name=" ";
sex=' ';
birth="";
}
void outdata()
{
cout<<"id:"<<id<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"birth:"<<birth<<endl;
}
private:
char *id;
char *name;
char sex;
char *birth;
};
class Adult:public Resident
{
public:
void adddata(char *Id,char *Name,char Sex,char *Birth,char *Edu,char *Occ) {
Resident::adddata(Id,Name,Sex,Birth);
edu=Edu;
occ=Occ;
}
void deldata()
{
Resident::deldata();
edu=" ";
occ=" ";
}
void outdata()
{
Resident::outdata();
cout<<"education:"<<edu<<endl;
cout<<"occupation:"<<occ<<endl;
- 4 -
}
private:
char *edu;
char *occ;
};
class Pm:public Adult
{
public:
void adddata(char *Id,char *Name,char Sex,char *Birth,char *Edu,char *Occ,char *P) {
Adult::adddata(Id,Name,Sex,Birth,Edu,Occ);
p=P;
}
void deldata()
{
Adult::deldata();
p=" ";
}
void outdata()
{
Adult::outdata();
cout<<"party:"<<p<<endl;
}
private:
char *p;
};
void main()
{
Resident r1;
Adult a1;
Pm p1;
r1.adddata("090411123","liming",'m',"19870101");
r1.outdata();
r1.deldata();
r1.outdata();
cout<<endl<<endl;
a1.adddata("090411123","liming",'m',"19870101","benke","teacher");
a1.outdata();
a1.deldata();
a1.outdata();
cout<<endl<<endl;
p1.adddata("090411123","liming",'m',"19870101","benke","teacher","dangyuan"); p1.outdata();
p1.deldata();
p1.outdata();
}
- 5 -
.5数组合并01
#include <stdio.h>
#define M 3
#define N 4
void combine(int*,int*,int *,int,int); void main(){
int a[M],b[N],c[M+N];
int i,j;
for(i=0;i<M;i++){
//scanf("%d",&a[i]);
a[i]=2*(i+6);
printf("%d ",a[i]);
}
putchar('\n');
for(j=0;j<N;j++){
//scanf("%d",&b[j]);
b[j]=5*(j+1);
printf("%d ",b[j]);
}
putchar('\n');
combine(a,b,c,M,N);
for(i=0;i<M+N;i++){
printf("%d ",c[i]);
}
putchar('\n');
}
void combine(int a[],int b[],int c[],int m,int n){ int i=0,j=0,k=0;
while(i<m&&j<n){
if(a[i]<b[j]){
c[k]=a[i];
k++;
i++;
}
else{
c[k]=b[j];
k++;
j++;
}
}
while(i!=m){
if(j==n){
c[k]=a[i];
k++;
- 6 -
i++;
}
}
while(j!=n){
if(j==m){
c[k]=b[j];
k++;
j++;
}
}
}
6栈052
#include <iostream>
using namespace std;
typedef int type;//type表示元素数据类型,此时为int型
struct StackNode{ //结构体定义双向链表结点
type element;
StackNode* previous;
StackNode* next;
};
class Stack{ //Stack类
public:
enum {MaxStack=5}; //堆栈最大元素个数
Stack(){ //构造函数创建一个空栈
head=new StackNode;
head->element=0;
head->previous=NULL;
head->next=NULL;
top=head;
}
void push(type n){ //往栈里压入一个数据
if(isFull()){ errMsg("*** The stack is full! ***"); return;}
StackNode* p;
p=top;
top=new StackNode;
p->next=top;
top->element=n;
top->previous=p;
top->next=NULL;
}
type pop(){ //弹出一个数据
if(isEmpty()){ errMsg("*** The stack is empty! ***\n"); return dummy_val;} type n;
- 7 -
StackNode* p;
p=top;
n=p->element;
top=p->previous;
delete p;
return n;
}
bool isEmpty(){ return top==head;}
bool isFull(){ return head->element>MaxStack;}
void prtypeStack(){ //显示栈里的所以数据
if(isEmpty()){ errMsg("*** The stack is empty! ***\n"); return;} StackNode* p;
p=top;
cout<<"Stack contents, top to bottom.";
while(p!=head){
cout<<'\n'<<p->element<<'\t';
p=p->previous;
}
}
private:
void errMsg(char* msg){cout<<msg;}
StackNode* head;
StackNode* top;
type dummy_val;
};
void main(){
Stack s;
type a,b;
cout<<"Please input two int numbers.\n";
cin>>a>>b;
s.push(a);
s.push(b);
s.prtypeStack();
cout<<"\npop:"<<s.pop()<<'\n';
s.prtypeStack();
system("pause");
}
7字符串类02
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
class sstring
{
- 8 -
public:
sstring();
bool isempty();
int length();
void del_k(int i,int k);
void upper();
void lower();
char *s;
};
sstring::sstring()//初始化
{
s=NULL;
}
bool sstring::isempty()//判断是否为空
{
if((*s)!='\0')
{
cout<<"this sstring is not empty."<<endl; return true;
}
}
int sstring::length()//求长度
{
int n=0;
char *tmp=s;
while((*tmp)!='\0'){
n++;
tmp++;
}
return(n);
cout<<"the length of this string is "<<n<<endl; }
void sstring::del_k(int i,int k)//删除i后的k个 {
int j;
int leg=length();
for(j=i+k;j<=leg;j++)
s[j-k]=s[j];
cout<<"the new string is "<<s<<endl; }
void sstring::lower()//大写转小写
{
char *tmp=s;
int leg=length();
for(int i=0;i<leg;i++){
if(*tmp>='A' && *tmp<='Z'){ - 9 -
(*tmp)+=32;
}
tmp++;
}
cout<<"the new string is "<<s<<endl; }
void sstring::upper()//小写转大写 {
char *tmp=s;
int leg=length();
for(int i=0;i<leg;i++)
{
if(*tmp>='a' && *tmp<='z'){ (*tmp)-=32;
}
tmp++;
}
cout<<"the new string is "<<s<<endl; }
int main()
{
sstring r;
char str[]="asdffSNJM";
printf("%s\n",str);
r.s=str;
r.isempty();
r.length();
r.del_k(2,2);
r.lower();
r.upper();
}
- 10 -