以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  请问众多高手,这个程序应该怎么编写呀???????  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=53691)


--  作者:yy2954
--  发布时间:10/12/2007 4:09:00 PM

--  请问众多高手,这个程序应该怎么编写呀???????
Lab 1. Coin-changing and the Calculator
COMP Structured Programming
United International College

___________________________________________________________________________


Introduction:

Now you are going to be a programming expert by exploring more in C programming! In this lab, you need to complete two tasks: a coin-changing program and a C calculator.

Task 1: Coin-Changing

You will write a coin-changing program for lab1. First, ask the user for the amount in
Hong Kong dollars, such as 56.30. Then find the minimum number of $10, $5, $2, $1, 50 cent,  20 cent, and 10 cent coins to give in return.

For example, your program first prompts the user for the amount in HKD by displaying the following message.

Please input the amount in Hong Kong dollars:

If the input is 56.30, the output should be

10-dollar coin: 5
5-dollar coin: 1
2-dollar coin: 0
1-dollar coin: 1
50-cent coin: 0
20-cent coin: 1
10-cent coin: 1

Task 2: A Calculator

Your second task is to write an integer arithmetic calculator.

Your calculator program should prompt the following UI (user interface) to the user.
     
      -------------------
| 1  | 2 | 3 | + |
|-----------------|
| 4  | 5 | 6 |  - |
|-----------------|
| 7  | 8 | 9 | * |
|-----------------|
| 0 | % | / | = |
-------------------
Please input the integer arithmetic expression (format: A op B =)

After the user input the expression in the correct format, your program calculates and displays the result. If the user inputs a illegal operator, your program should print the following message and ask the user to re-input it.

Sorry, you’ve input an wrong operator. Please enter the arithmetic expressions again.  

A number can’t be divided by 0, so your program should be able to deal with this properly. When one calculation is completed, your program should ask user to input ‘Y’ or ‘N’ to indicate if anther calculation is needed.

Do you want to calculate again? (Y/N)

If the input is ‘Y’, then prompts the UI again. If the input is ‘N’, then ends the execution.

Hints:
1.    Your program needs to decide what operation is to be used by checking the user input. This can be done by the so- “if… elseif…else” statement in C.

if(condition_1)
          expression_1;
     elseif(condition_2)
         expression_2;
     elseif(condition_3)
         expression_3;
    ....
     else(expression_k)    


2.  Your program will also need to decide whether the user wants to do another calculation or not. After knowing the answer is ‘Y’, the program needs to jump back to prompt the UI again. This can be done by using the ‘goto’ statement.

Display_UI:    printf(“……..”);  // this is the first line of displaying the UI

                    ………………………..

                    goto Display_UI;

Marking Scheme:

    Task 1: 30 marks
    Task 2: 10 marks for displaying the UI
          10 marks for each operator “+, -, *, /, %”
          5 marks for division by 0 error processing
          5 marks for code layout and proper comments

Deadline: You need to demonstrate your program to Dr. Amy Zhang or me by  5:00PM, Monday, Oct. 15.


--  作者:yy2954
--  发布时间:10/12/2007 4:32:00 PM

--  
高手们,加油呀,
--  作者:一分之千
--  发布时间:10/13/2007 9:28:00 AM

--  
第一题  就是一个整除以及取整问题   
   先判断 int(钱数)能否被10整除,可以,得出结果(10$ 的数目),保存余数(对余数继续判断能否被5整除);不可以判断能否被5整除,得出结果(5$的数目),保存余数
以此类推 分别得出2 和1 $的数目。
判断分的时候可以采用这样一种方法  -int(钱数)*100+钱数*100这样得到的是分的数目
然后采用取得dollar数目的方法就可以得到cent的数目了。

第二题  输入采用 数字 (+ - * /)数字的方法
VC的话可以使用对话框来实现   设置如图所示的按钮,每个按钮代表一个数字或者一个运算符,使用一个对话框来显示输入的数字 运算符 以及计算结果 可以采用一下类似的代码
数字键
  void CCalculatorDlg::AdvButton1()
{
 // TODO: Add your control notification handler code here
 m_strBegin=m_strBegin+"1";
 if(!m_IsCheckPoint)
  m_DspEdit.OnDisplay(m_strBegin+".");
 else
  m_DspEdit.OnDisplay(m_strBegin);
}
运算符键
void CCalculatorDlg::AdvButtonAdd()
{
 // TODO: Add your control notification handler code here
 Result();
 m_bit=1;
}

运算符相关函数
void CCalculatorDlg::Result()
{
 m_IsCheckPoint=FALSE;
 m_begin=atof(m_strBegin);

 switch(m_bit) {
 case 1 ://+
  m_end+=m_begin;
  break;
 case 2://-
  m_end-=m_begin;
  break;
 case 3://*
  m_end*=m_begin;
  break;
 case 4:// /
  if(m_begin==0){
   m_DspEdit.OnDisplay("除数不能为零!");
   break;
  }
  m_end/=m_begin;
  break;
 case 5://sqrt
  if(m_end<0){
   m_DspEdit.OnDisplay("函数输入无效!");
   break;
  }
  m_end=sqrt(m_end);
  break;
 case 6: //1/x
  if(m_end==0){
   m_DspEdit.OnDisplay("分母输入无效!");
   break;
  }
  m_end=1/m_end;
  break;
 case 7://=
  break;
 case 8://sin
  m_end=sin(m_end);
  break;
 case 9://cos
  m_end=cos(m_end);
  break;
 case 10://tan
  m_end=tan(m_end);
  break;
 case 11:// square
  m_end=m_end*m_end;
  break;
 case 12://cube
  m_end=m_end*m_end*m_end;
  break;
 case 13://x^y
  m_end=pow(m_end,m_begin);
  break;
 case 14://exp
  m_end=exp(m_end);
  break;
 case 15://ln
  if(m_end<=0){
   m_DspEdit.OnDisplay("函数输入无效!");
   break;
  }
  m_end=log(m_end);
  break;
 case 16://log
  if(m_end<=0){
   m_DspEdit.OnDisplay("函数输入无效!");
   return;
  }
  m_end=log10(m_end);
  break;
 default:
  m_end=m_begin;
  break;
 }
 m_begin=0.0;
 m_bit=0;
 m_strBegin="";

 int i= sprintf(m_buffer,"%10.12f",m_end);
 TrimZero(m_buffer,i);
 m_DspEdit.OnDisplay(m_buffer);
}


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
5,546.875ms