Saturday, December 4, 2010

Program for water bill Problem

/*program for water bill problem*/

#include<stdio.h>

#define DEMAND_CHG 35.00 /*basic waterdemand charge*/

#define PER_1000_CHG 1.10 /*charge per thousand gallons used */

#define LATE_CHG 2.00 /* surcharge assessed on unpaid balance */


/*Function prototype*/

void instruct_water(void);

double comp_use_charge(int previous, int current);

double comp_late_charge(double unpaid);

void display_bill(double late_charge, double bill, double unpaid);

int
main(void)
{

int previous; /*input - matter reading from previous quarter*/
int current; //input -meter reading from current quarter
double unpaid; //input -unpaid
double bill; //output - water bill
int used;
double use_charge;
double late_charge;

//display user instruction.

instruct_water();

/*Get data: unpaid balance, previous and current meter reading*/

printf("Enter unpaid balance> $");
scanf("%lf", &unpaid);
printf("Enter previous meter reading> ");
scanf("%d",&previous);
printf("Enter current meter reading>");
scanf("%d",&current);

//compute use charge
use_charge = comp_use_charge(previous,current);
//determine applicable late charge
late_charge = comp_late_charge(unpaid);

//figure bill
bill = DEMAND_CHG + use_charge + unpaid + late_charge;

//print bill
display_bill(late_charge, bill,unpaid);

return (0);

}


/*
*Display user instructions
*/

void instruct_water(void)
{
printf("This problem figures a water bill");
printf("based on the demand charge\n");
printf("($%.2f) and a $%.2f per 1000", DEMAND_CHG,PER_1000_CHG);
printf("gallons use charge.\n\n");
printf("A $%.2f surcharge is added to ", LATE_CHG);
printf("accounts with an unpaid balance.\n");
printf("\nEnter unpaid balance, previous.\n");
printf("and current meter readings\n");
printf("on separate lines after the prompts. \n");
printf("Press<return> or <enter>after");
printf("typing each number.\n\n");
}


/*
*
* computes use charge
*/

double
comp_use_charge(int previous,int current)
{
int used; //gallons of water used
double use_charge;
used = current - previous;
use_charge = used*PER_1000_CHG;
return (use_charge);
}

/*
*computes late charge
*/

double
compu_late_charge(double unpaid)
{
double late_charge;
if(unpaid>0)
late_charge = LATE_CHG;
else
late_charge = 0.0;
return (late_charge);
}

/*
*
*Displays late charge if any and bill,
* pre: late charege
*/

void display_bill(double late_charge, double bill, double unpaid)
{
if(late_charge > 0.0) {
printf("\nBill includes $%.2f late charge",late_charge);
printf("on unpaid balance of $%.2f\n",unpaid);
}
printf("\nTotal due = $%.2f\n",bill);
}





4.5 Deciscion Steps in Algorithms

page 170

http://kc.codepad.org/KIVY6Qkf

http://kc.codepad.org/KIVY6Qkf

source code

#include<stdio.h>
#include<math.h>



double scale(double x, int n);

int main(void)
{
double num_1;
int num_2;

//get values for num_1 and num_2

printf("Enter a real number>");
scanf("%lf",&num_1);
printf("Enter an integer>");
scanf("%d",&num_2);
/*call scale and display result*/
printf("Result ofcall to function scale is %f\n",scale(num_1,num_2));
return (0);
//num_1 is called "actual argument"
//num_2 is called " actual arguent"
}

double scale(double x,int n)

{
//dkf
//dfkd
//x is formal parameter
//n is formal parameter
double scale_factor;
//local variable -10 to power n
scale_factor = pow(10,n);
return(x*scale_factor);
}


http://codepad.org/Gj2sasoY

online compiler

so easy and fast

it is better than other IDE or any other thing

컴파일 이 되지 않는 이유는 무었인가?

컴파일 이 되지 않는 이유는 무었인가?


test

test

Sunday, November 28, 2010

lecture

kenchoiwind.objective@blogger.com

·         로그인

·         텍스트큐브

http://rs.textcube.com/service/blog/image/plugins/lightbox/loading.gif

http://rs.textcube.com/service/blog/image/plugins/lightbox/expand.gifhttp://rs.textcube.com/service/blog/image/plugins/lightbox/close.gifhttp://rs.textcube.com/service/blog/image/plugins/lightbox/zzoop.gif

CodeLab


objective c강좌 1. class

language/Objective C | 2009/07/29 16:02 | marocchino

클래스는 인터페이스(선언) 임플리먼트(구현) 구성됨미다.

각각 클래스명.h 클래스명.m 따로 관리하시는게 깔끔하나 그냥 한파일에 넝쿨칡 얽히듯 얽어놔도 실행에는 지장이없습니다.

 

일단 선언부는.

@interface 클래스이름 : 아빠클래스이름{  인스턴스 변수들...}

@property 변수이름들

클래스(+)혹은 인스턴스(-) 메소드들

@end

요런모양이고.



구현부는

@implementation 클래스이름 : 아빠클래스이름

@synthesize 변수이름들

클래스(+)혹은 인스턴스(-) 메소드들

@end

요런모양이됨미다.



이렇게만든 클래스를 사용하려면 자바에서는

클래스이름 변수이름 = new 클래스이름();

모양으로 선언 해주지만



오브젝티브씨에서는 따로 생성자가없고 메모리할당하고 초기화하는 함수를 불러주는것으로 생성자를 대신합니다.

클래스이름 변수이름 = [[클래스이름 alloc] init];



[클래스이름 alloc]에서 눈치 채신횽들도있겠지만 오브젝티브씨에서는 .대신 []으로 함수를 구분합니다.

처음보면 더럽다라는 생각밖에 안드는데 사용할수록  많은 장점이 있습니다.

예를들어 인수가 9개인 메서드를 선언할떄..

ob.camera(1,1,1,1,1,1,1,1,1)



형식이라면 레퍼런스를 보기전에는

각위치에 뭐가들어가는지 알아보기힘들지만

오브젝티브씨스타일으로는 이게 읽기편하게 정리된다능.

[ob camerax:1 y:2 z:3 tx:1 ty:2 tz:3 uz:1 uy:2 uz:3 ];



물론 그만큼 쓰기길어지기는하지만 가독성이 높아진다는부분이있으니..

권장하지는 않지만 그냥 귀찮다 싶은사람은 다른데서 하던것처럼

[ob camera:1 :2 :3 :1 :2 :3 :1 :2 :3 ]



요런식으로 선언해도 에러는 나지않습니다.

설명만있는거보단 예제를 보면서 이해하는게 간단하고 이해가 잘될듯싶으니 셈플코드를 하나 붙여보겠다능.

접어두기..

#import <Foundation/Foundation.h>

 

@interface Fraction : NSObject

{

               int numerator;

               int denominator;

}

@property int numerator,denominator;

- (void)print;

- (void)setTo:(int)n :(int)d;

- (double)convertToNum;

- (Fraction *)add:(Fraction *)f;

- (Fraction *)subtract:(Fraction *)f;

- (Fraction *)multiply:(Fraction *)f;

- (Fraction *)divide:(Fraction *)f;

- (void)reduce;

@end

 

@implementation Fraction

@synthesize numerator,denominator;

- (void)setTo:(int)n :(int)d

{

               numerator = n;

               denominator = d;

}

- (void)print

{

               NSLog(@"%i/%i", numerator,denominator);

}

 

- (double)convertToNum

{

               if(denominator!=0)

               {

                               return (double) numerator/denominator;

               }else{

                               return 1.0;

               }

}

- (Fraction *)add:(Fraction *)f

{

               Fraction *rFraction = [[Fraction alloc] init];

               [rFraction setTo: ((numerator*f.denominator)+(f.numerator*denominator))

                               :denominator*f.denominator];

               [rFraction reduce];

               return rFraction;

}

- (Fraction *)subtract:(Fraction *)f

{

               Fraction *rFraction = [[Fraction alloc] init];

               [rFraction setTo: ((numerator*f.denominator)-(f.numerator*denominator))

                               :denominator*f.denominator];

               [rFraction reduce];

               return rFraction;

}

 

- (Fraction *)multiply:(Fraction *)f

{

               Fraction *rFraction = [[Fraction alloc] init];

               [rFraction setTo: numerator*f.numerator

                               :denominator*f.denominator];

               [rFraction reduce];

               return rFraction;

}

 

- (Fraction *)divide:(Fraction *)f

{

               Fraction *rFraction = [[Fraction alloc] init];

               [rFraction setTo: numerator*f.denominator

                               :denominator*f.numerator];

               [rFraction reduce];

               return rFraction;

}

 

- (void)reduce

{

               int n = numerator;

               int d = denominator;

               int temp;

               while(d!=0){

                               temp = n % d;

                               n = d;

                               d = temp;

               }

               numerator /= n;

               denominator /= n;

}

@end

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

{

               NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

               Fraction  *aFraction = [[Fraction alloc] init];

               Fraction  *bFraction = [[Fraction alloc] init];

               [aFraction setTo:1 :2];

               [bFraction setTo:1 :4];

               [[aFraction divide: bFraction] print];

               [[aFraction multiply: bFraction] print];

               [[aFraction subtract: bFraction] print];

               [[aFraction add: bFraction] print];

               [aFraction release];

               [bFraction release];

               [pool drain];

               return 0;

}

접어두기..