两个简单的oracle编程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了两个简单的oracle编程相关的知识,希望对你有一定的参考价值。
用oracle编程写一个循环函数,打印出斐波那契数列 0、1、1、2、3、5、8、13 ……的前100个数字;
用循环体打印出形如
*
* *
* *
* * * *
的图案
#include<stdio.h>
int main()
int i,j;
int x=0,y=0;
int n;
int tot=0;
int p=0;
int a[100][100]=0;
printf("Please enter n\n");
scanf("%d",&n);
a[0][0]=1;
tot=1;
for (i=1;i<=2*n-1;i++)
if (p==0)
while ( (y+1<=n-1) && (a[x][y+1]==0) )
y++;
tot++;
a[x][y]=tot;
else if (p==1)
while ( (x+1<=n-1) && (a[x+1][y]==0) )
x++;
tot++;
a[x][y]=tot;
else if (p==2)
while ( (y-1>=0) && (a[x][y-1]==0) )
y--;
tot++;
a[x][y]=tot;
else if (p==3)
while ( (x-1>=0) && (a[x-1][y]==0) )
x--;
tot++;
a[x][y]=tot;
p++;
p=p%4;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
return 0;
打印星状图,是 1 2 2 4...本回答被提问者采纳
c++类编程的两个简单的例子
关于栈的数据结构:类的头文件定义
// stack.h -- class definition for the stack
/******************************************************************/
#ifndef _STACK_
#define _STACK_
typedef unsigned long Item ;
class Stack
{
private:
enum { MAX = 10 } ;
Item items[MAX] ;
int top ;
public:
Stack();
bool isempty() const ;
bool isfull() const ;
bool push( Item & item );
bool pop( Item & item );
};
#endif
关于具体实现的部分:
// implement for the stack.h //
/**********************************************
never let the implement file has the
cin and cout opeoration , just let
class func opers its own private data !
*********************************************/
#include "stack.h"
Stack::Stack(){
top = 0 ;
}
bool Stack::isempty() const{
return top == 0 ;
}
// the const is needed to be added to the implement part
bool Stack::isfull() const{
return top == 10 ;
}
bool Stack::push( Item & item){
if( top < MAX ){
items[top++] = item ;
return true ;
}
else
return false ;
}
bool Stack::pop(Item & item){
if( top == 0 )
return false ;
else{
item = items[--top];
return true ;
}
}
测试部分:
#include "stack.h"
#include<iostream>
int main(){
int i ;
Item item = 8729 ;
Item get ;
Stack stack;
for ( i = 0 ; i < 15 ; i++ )
stack.push(item);
for( i = 0 ; i < 15 ; i++ )
{
if(stack.pop(get))
std::cout<<get<<std::endl;
}
}
关于列表的实现,可以加入不同的数据类型的列表
声明的头文件:
#ifndef _list_
#define _list_
#include <stdio.h>
typedef struct elem{
char type ;
union
{
int int_ ;
short shrt_;
char char_;
void * ptr ;
double double_;
float float_;
}value;
elem * next ;
elem * fore ;
}Elem;
class List{
private:
enum { INT , SHRT , CHR , PTR , DOU , FLT };
unsigned int count ;
elem * head ;
elem * tail ;
public:
List(){ count = 0 ; head = tail = NULL ;}
bool push(Elem & item );
bool pop (Elem & item );
bool insert( int pos , Elem & item );
bool purge ( int pos , Elem & item );
bool getpos( int pos , Elem & item );
bool isempty();
void reverse();
void show() const;
~List();
};
#endif
具体实现的文件:
#include "list.h"
#include<iostream>
#include<stdio.h>
void innershow(Elem * ptr)
{
printf(" in function \n");
std::cout<<"Type:";
switch (ptr->type){
case(0): printf("int value:%d\n",ptr->value); break;
case(1): printf("short value:%d\n",ptr->value); break;
case(2): printf("char value:%c\n",ptr->value); break;
case(3): printf("pointer value:%p\n",ptr->value); break;
case(4): printf("double value:%f\n",ptr->value); break;
case(5): printf("float value:%f\n",ptr->value); break;
default: printf("UNKNOWN value:%p\n",ptr->value); break;
}
}
bool List::push(Elem & item){
Elem * entity = new Elem ;
if( entity == NULL )
return 0 ;
entity->type = item.type ;
entity->value = item.value ;
count++;
if(head == NULL ){
entity->fore = NULL ;
head = tail = entity ;
}
else{
entity->fore = tail ;
tail->next = entity ;
tail = entity ;
}
entity->next = NULL;
return 1 ;
}
bool List::pop( Elem & item ){
Elem * ptr ;
if ( !count ) return 0;
item.type = tail->type ;
item.value= tail->value;
if( head != tail ){
ptr = tail->fore ;
tail->fore->next = NULL ;
delete tail ;
tail = ptr ;
}
else{
delete tail ;
head = tail = NULL;
}
count--;
return 1;
}
bool List::insert( int pos , Elem & item ){
Elem * ptr = NULL ;
if( pos > count || pos < 0)
return 0 ;
if ( pos == count )
push(item);
else{
ptr = head ;
while(pos--) ptr = ptr->next ;
Elem * entity = new Elem ;
if( entity == NULL )
return 0 ;
entity->type = item.type ;
entity->value = item.value ;
if( ptr == head ){
entity->next = head ;
head->fore = entity ;
entity->fore = NULL ;
head = entity ;
}
else if ( ptr == tail ){
entity->next = NULL ;
entity->fore = tail ;
tail->next = entity ;
tail = entity ;
}
else{
entity->next = ptr ;
entity->fore = ptr->fore;
ptr->fore->next = entity;
ptr->fore = entity ;
}
count++;
}
return 1 ;
}
bool List::purge ( int pos , Elem & item ){
Elem * ptr = NULL ;
if( pos >= count || pos < 0)
return 0 ;
else{
ptr = head ;
while(pos--) ptr = ptr->next ;
if( ptr == head ){
ptr->next->fore = NULL ;
ptr = ptr->next ;
delete head ;
head = ptr ;
}
else if( ptr == tail ){
ptr->fore->next = NULL ;
ptr = ptr->fore ;
delete tail ;
tail = ptr ;
}
else{
ptr->fore->next = ptr->next ;
ptr->next->fore = ptr->fore ;
delete ptr;
}
count--;
}
return 1 ;
}
bool List::getpos( int pos , Elem & item ){
int count ;
Elem * ptr = NULL ;
if( pos >= count || pos < 0)
return 0 ;
else{
ptr = head ;
while(pos--) ptr = ptr->next ;
item.type = ptr->type ;
item.value = ptr->value;
}
return 1 ;
}
bool List::isempty(){
return count==0;
}
void List::reverse(){
Elem * ptr = head ;
Elem * swap ,* next ;
while( ptr ){
next = ptr->next ;
swap = ptr->fore ;
ptr->fore = ptr->next ;
ptr->next = swap ;
ptr = next ;
}
swap = head ;
head = tail ;
tail = swap ;
}
void List::show() const{
Elem * ptr = head ;
Elem * back ;
std::cout<<"List items......\n";
while(ptr){
innershow(ptr);
ptr = ptr->next ;
}
std::cout<<"***************************\n";
}
List::~List(){
count = 0 ;
Elem * ptr = head ;
Elem * back ;
while(ptr){
back = ptr ;
ptr = ptr->next ;
delete back;
}
}
具体的测试文件:
#include<iostream>
#include<stdio.h>
#include"list.h"
int main(){
Elem elem = { 0 , 6526 };
List list;
list.push(elem);
elem.type = 1 ;
list.push(elem);
list.show();
elem.type = 2 ;
list.push(elem);
list.show();
elem.type = 3 ;
list.push(elem);
list.show();
elem.type = 4 ;
list.push(elem);
list.show();
printf("*************\n");
list.show();
printf("*****swap******\n");
list.reverse();
list.show();
return 0 ;
}
相信这两个例子都非常好的解释了类编程的思想
以上是关于两个简单的oracle编程的主要内容,如果未能解决你的问题,请参考以下文章