佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1120|回复: 16

课业上的问题(C language)

[复制链接]
发表于 11-6-2006 02:31 PM | 显示全部楼层 |阅读模式
4. Write a program that prompts a user for an integer value in the range 100 to 999 and prints the leftmost digit of the number. [Hint: use integer division.]

我的答安。。。


#include<stdio.h>

int main(void)
{
        int  num;
        int  answer;

        printf("\nEnter an integer value in the range 100 to 999 :");
        scanf("%d", &num);
   
        answer = num /100;
       
    printf("The leftmost digit of the number is %d ", answer);

   return 0;
}  /*main*/


请问有办法将最后的答安改成 "error"而吗?如果num>999,num<100。
或,我可以一开始可以限定在99<num<1000

为何以下的方法行不通呢?


#include<stdio.h>

int main(void)
{
        int  num;
        int  num2;
        int  answer;

        printf("\nEnter an integer value in the range 100 to 999 :");
        scanf("%d", &num);
   
        num2 = 1000;
        answer = num /100;

    printf("The leftmost digit of the number is %d ", answer);
        if (num < num2)
                answer = '0';
       

        return 0;
}  /*main*/
回复

使用道具 举报


ADVERTISEMENT

发表于 11-6-2006 05:35 PM | 显示全部楼层
可以啦..

输入后,检查value是否在你要的范围内..
如不是,就要求重新输入..
回复

使用道具 举报

 楼主| 发表于 11-6-2006 09:19 PM | 显示全部楼层
可以据体的show我看看吗?
这是我的第一个tutorial.

include<stdio.h>

int main(void)
{
        int  num;
        int  answer;
在这里写东西吗?



还是
#include<stdio.h>

int main(void)
{
        int  num;
        int  num2;
        int  answer;

        printf("\nEnter an integer value in the range 100 to 999 :");
        scanf("%d", &num);
   
        num2 = 1000;
        answer = num /100;

    printf("The leftmost digit of the number is %d ", answer);
在这里写以检查value是否在我要的范围内
回复

使用道具 举报

发表于 11-6-2006 10:30 PM | 显示全部楼层
在 scanf("%d", &num); 之后..
while loop 这个scanf ,如果不是所要的值..

你的书上不是有教吗..如果没有,那你的书可以拿去烧了..
回复

使用道具 举报

 楼主| 发表于 11-6-2006 11:05 PM | 显示全部楼层
已决绝.
谢谢你
原来在很后的chapter才提到 ... repetition.
回复

使用道具 举报

 楼主| 发表于 12-6-2006 10:30 PM | 显示全部楼层
out of range 的话它会重复问题,
但为何在range里面它又不会出答案呢.
哪里出错了?可以教教我吗?


#include<stdio.h>

int main(void)
{
        int  num;
    int  answer;

        printf("\nEnter an integer value in the range 100 to 999 :");
       
        while (scanf("%d", &num))
        {
        if (num>999)
                printf("\nEnter an integer value in the range 100 to 999 :");
        if (num<100)
                printf("\nEnter an integer value in the range 100 to 999 :");
        }
       
                answer = num /100;
            printf("The leftmost digit of the number is %d\n ", answer);

        return num;
}  /*main*/
回复

使用道具 举报

Follow Us
发表于 12-6-2006 10:48 PM | 显示全部楼层
while (scanf("%d", &num))

这根本是错的..你没搞懂while的用法..


While (条件成立){
// 在这做东西



* 我看你的书真的可以烧了..
回复

使用道具 举报

 楼主| 发表于 13-6-2006 09:59 PM | 显示全部楼层
oh
我知到了.问题解决了.
非常非常的谢谢.
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 25-6-2006 09:36 PM | 显示全部楼层
有新问题
1.        Write a program to take a depth (in kilometers) inside the earth as input data; compute and display the temperature at this depth in degrees Celsius and degrees Fahrenheit.

                Celsius = 10(depth) + 20
                Fahrenheit = 1.8(Celsius) + 32

        Include the following functions in your program:

&#8226;        print_Info()
Print instruction to user.

&#8226;        get_Depth():
Read the depth from user.

&#8226;        get_Celsius(depth):
Compute and return the Celsius temperature at the depth.

&#8226;        get_Fahrenheit(celsius)
Convert a Celsius temperature to Fahrenheit and return to result to calling function.

The prototypes of the functions are given as follow:

        void print_Info(void);
        float get_Depth(void);
        float get_Celsius(float depth);
        float get_Fahrenheit(float Celsius);

请问问题出在哪里,为何只在 print_info 那里打转?

/*This program take a depth (in kilometers) inside the earth
  and display the temperature at this depth in degrees Celsius
  and degrees Fahrenheit.
*/
#include <stdio.h>

//Function Declarations
void  print_Info(void);
float get_Depth(void);
float get_Celsius(float depth);
float get_Fahrenheit(float Celsius);

int main (void)
{   //local definations
        float depth;
    //statements
        print_Info( );
        scanf("%f", &depth);
        return 0;
}
/* This function is use to print instruction to user */
void print_Info()
{
        printf("Please enter the depth:");
   
}//print_Info

/*This function is use to read the depth from user*/
float get_Depth(void)
{
        //local definations
        float depth;
        //statements
        scanf("%f", &depth);
        return depth;
}//main

/*This function is use to compute and return the
  Celsius temperature at the depth*/
float get_Celsius(float depth)
{
        float celsius;
        //statements
        celsius = 10 * depth + 20;
        return (celsius);
}//main

/* This function is to convert a Celsius temperature to
   Fahrenheit and return to result to calling function
*/
float get_Fahrenheit(float celsius)
{
        //local definations
        float Fahrenheit;
        //statements
        Fahrenheit = 1.8 * celsius + 32;
        return Fahrenheit;
}
回复

使用道具 举报

发表于 25-6-2006 10:04 PM | 显示全部楼层
int main (void)
{   //local definations
        float depth;
    //statements
        print_Info( );
        scanf("%f", &depth);
        return 0;
}

主程序只用了一个print_Info( );..
而且是输入了,就结束..

你说呢?
回复

使用道具 举报

 楼主| 发表于 26-6-2006 07:51 PM | 显示全部楼层
我想问下到底是functiondeclarations 后才写
int main (void)

还是 一直写到
float get_Fahrenheit(float celsius)
{
        //local definations
        float Fahrenheit;
        //statements
        Fahrenheit = 1.8 * celsius + 32;
        return Fahrenheit;
}


过后 才写
int main (void)
{   //local definations
        float depth;



两者之间有分别吗?
回复

使用道具 举报

发表于 26-6-2006 08:03 PM | 显示全部楼层
基本上是先写main程序,至于细节如function就写在下面..
至于可不可以倒转..还没试过..
而且也不合理..

如果是你,你会先看大纲,还是先看细节呢?
回复

使用道具 举报

 楼主| 发表于 27-6-2006 07:57 PM | 显示全部楼层
在 C language 里,乘是*,除是/,
哪 square root 要怎样写呢?
回复

使用道具 举报

发表于 27-6-2006 08:47 PM | 显示全部楼层
原帖由 boyboyscout 于 26-6-2006 07:51 PM 发表
我想问下到底是functiondeclarations 后才写
int main (void)

还是 一直写到
float get_Fahrenheit(float celsius)
{
        //local definations
        float Fahrenheit;
        //statements
   ...

如果放main 在前面function 在后面,就要写function prototype.
在function declaration 的后面放 ;就是 function prototype 了。
function prototype 是写在 include# 和 main() 之间。
function prototype 的 功能其实是提醒compiler 在 main() 之后还有一个function 的意思。
因为 c compiler 是 一行一行 compile 的,如果没有放function prototype,compiler 就不懂在main() 里面call 的function.

如果先放function 在前面, main()在后面,就不用写 function prototype.

大多数课本上都是有用 function prototype的,要不要用也是看个人,哪种都没错。

原帖由 boyboyscout 于 27-6-2006 07:57 PM 发表
在 C language 里,乘是*,除是/,
哪 square root 要怎样写呢?

用 sqrt() 这个function, 之前要先 #include<math.h> 因为 sqrt()这个function 是在 math.h 这个library底下的一个function来的。就好像你要用 printf() 和 scanf() 一样 要 #include<stdio.h>.
在 sqrt()的 ()里面输入你要的数字,就是那个数字的square root 了。
如:sqrt(9);//答案就是 3 咯。

p/s:记得在main 里call function 就是一个 statement了,所以 call sqrt() 这个function 之后,
    后面也要加;咯。

[ 本帖最后由 顽固石 于 27-6-2006 09:05 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 29-6-2006 12:10 AM | 显示全部楼层
Write a program that calculates the speed of sound (a) in the air of a given temperature T (&#61616;F). The formula is given below:

        a=1086 * sqrt ((5T+267)/247)         

Use 3 functions in your program for 3 different purposes: display instruction, perform the computation and display the final result.

问题知到出现在哪里,可是不知到怎样改。

/*This program is use to calculates the speed of sound (a)
  in the air of a given temperature T (F).
*/
#include <stdio.h>
#include <math.h>

// Function Declaration
void  print_Info(void);
float computation(float sound, float temperature);
void Display_Final_Result(float sound, float temperature);

int main (void)
{
        //Local Declaration
        float sound, temperature;
        //Statements
        print_Info();
        sound = computation(sound, temperature);
        Display_Final_Result(sound, temperature);
       
        return 0;
}//main

/* This function is use to print instruction to user */
void  print_Info()
{
        printf("Please enter a temperature T (F)in order to ");
        printf("calculates the speed of sound (a) in the air ");
       
}//print_Info

//Compute
float computation(float sound, float temperature)
{
        //local definations
        float sound, temperature;
        //statements
        scanf("%f", &temperature);
        sound = 1086 * (sqrt ((5 * temperature + 297) / 247));
       
        return sound;
}//computation


/* This function is use to print out the final result to user */
void Display_Final_Result(float sound, float temperature)
{   
        printf("\nWhen the Temperature is %.2f F,\n", temperature);
        printf("the Speed of Sound in the air is %.2f\n", sound);
       
}//Display_Final_Result
回复

使用道具 举报

发表于 29-6-2006 02:50 AM | 显示全部楼层
int main (void)
{
        //Local Declaration
        float sound, temperature;
        
        你的初始值呢?

        //Statements
        print_Info();

        scanf("%f", &temperature); 应该放在主程序中..

        sound = computation(sound, temperature); 只需要一个parameter..
        
        Display_Final_Result(sound, temperature);
        
        return 0;
}//main
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 7-7-2006 10:52 PM | 显示全部楼层
1.        Write a program for a self-service restaurant.

Description:
i)       
The program will first display a main menu, customer needs to choose either order drinks or order food, for example:

1. Food
2. Drinks
3. Print Order

Choice:

The main menu should be able to accept a special code to exit the program, for example, if the user enters -9999 as choice, the program will exit.

ii)       
A new menu will be displayed according to the selection in (i).
oThe drinks menu should display the following choices:
&#61607;        Hot drinks
&#61607;        Cold drinks
&#61607;        Tea
&#61607;        Coffee
&#61607;        Milo
&#61607;        Back to previous menu

oThe food menu should display the following choices:
&#61607;        Fried rice
&#61607;        Fried Noodle
&#61607;        Burger
&#61607;        Back to previous menu

After the user entered their selection, prompt user to enter the quantity and back to main menu again.

iii)       
If user selects 3 in the main menu, a summary of order should be printed out in the following format:

Item                        QTY        Unit Price    Amount
----                        ---        ----------        ------
Milo                        2         1.00             2.00
Coke                        3         1.50             4.50
Burger                        5         3.00            15.00

Total: RM 21.50
Thank you. Please pay at the counter.

iv)       
Below are the prototypes of functions that you should have in your program.

&#61510;        void main_menu(void);
&#61510;        void drinks_menu(int*, int*, int*, int*, int*);
&#61510;        void food(int*, int*, int*);




我的答安。
我想问下
1.到底要怎样exit program.

2.竟然已是void function 哪麽要怎样return所有scanf的value
  放进去printf那离.
  (没有input的value不可在printf那里出现,例如我没select fried rice,在printf 那里就不会show出来)

3.我老师说价钱自己define,但,当我加了#define后,C language就有error出现。



#include <stdio.h>

//Function Declarations
void main_menu(void);
void drinks_menu(int COKE, int PEPSI, int TEA, int COF, int MILO);
void food(int FR, int FN, int BUR);

int main (void)
{
        //Local Definations
        main_menu();

  return 0;
}//main

void main_menu(void)
{
        int choice;
        int COKE, PEPSI, Tea , COF, MILO;
        int FR, FN, BUR;
        int drink;

        printf("1. Food\n");
        printf("2. Drinks\n");
        printf("3. Print Order\n\n");
        printf("Choice:");
        scanf("%d",&choice);

        if (choice ==1)
        {
                food(FR, FN, BUR);
        }
        if (choice == 2)
        {
                drinks_menu(COKE, PEPSI, Tea , COF, MILO);
        }

        if (choice == 3)
        {
                printf("Item\t\tQTY\tUnit Price\t\tAmount\n");
                printf("----\t\t---\t---- -----\t\t------\n");
               
                printf("Coke\n");
                printf("Pepsi\n");
                printf("Tea\n");
                printf("Coffe\n");
                printf("Milo\n");
                printf("Fried rice\n");
                printf("Fried Noodle\n");
                printf("Burger\n");
        }


       
       
}//main_menu

void drinks_menu(int COKE, int PEPSI, int Tea , int COF, int MILO)
{
        int drink, QTY;

        printf("1.Coke\n");
        printf("2.Pepsi\n");
        printf("3.Tea\n");
        printf("4.Coffe\n");
        printf("5.Milo\n");
        printf("6.Back to previous menu\n\n");
        printf("Select Drink:");
        scanf("%d",&drink);

        while (drink>6)
        {
                printf("Out of order\n");
                printf("Please Select The Drink:");
                scanf("%d",&drink);
        }
       

        if (drink == 6)
        {
                main_menu();
        }
        else
        printf("\nPlease enter the quantity:");
        scanf("%d",&QTY);

        main_menu();

}

void food(int FR, int FN, int BUR)
{
        int food, QTY;

        printf("1.Fried rice\n");
        printf("2.Fried Noodle\n");
        printf("3.Burger\n");
        printf("4.Back to previous menu\n\n");
        printf("Select Food:");
        scanf("%d",&food);

        while (food>4)
        {
                printf("Out of order\n");
                printf("Please Select The Food:");
                scanf("%d",&food);
        }

        if (food == 4)
        {
                main_menu();
        }
        else
        printf("\nPlease enter the quantity:");
        scanf("%d",&QTY);

        main_menu();

}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 8-6-2024 10:21 PM , Processed in 0.072893 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表