Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 7349|回复: 60
打印 上一主题 下一主题

【入门C作业】【strcmp / for / while】 #25楼 有Debug Coding提供。

[复制链接]

14

主题

1

好友

1491

积分

黄金长老

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

跳转到指定楼层
1#
发表于 2009-1-10 11:23 PM |只看该作者 |倒序浏览
这PROGRAM是一篇帮顾客计算所需清还的税务。
Program会要求一些个人资料,如IC、名字、生日等等。最后要求Total income, total relief, 和total rebate。

软件功用:
可以不断输入很多户口,然后储存起来。
之后,利用IC搜索资料和显示税务。

税务的算法是正确的。

Coding样本在#25-27楼!由衷感谢Super-Tomato帮忙。


各位高手,我还有一些想请教你们。见#27楼。


[ 本帖最后由 无我不在 于 2009-1-23 11:05 PM 编辑 ]




收藏收藏0

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

2#
发表于 2009-1-11 12:00 AM |只看该作者
原帖由 无我不在 于 2009-1-10 11:23 PM 发表
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
    int code=37, i=-1;
    char choice=0, icno_search[8];
    char name[10][50]={""}, icno[10][8]={""}, DOB[10][10]={""}, sex[10]={""};
    float total_income[10]={0}, total_relief[10]={0}, total_rebate[10]={0}, pre_payment[10]={0}, taxable_amount[10]={0};
    float amount_payment[10]={0};
   
    while(choice!='D'&& choice!='d')
    {
        printf("Tax Advisor Program\n");
        printf("(A)\tDisplay taxation rate\n");
        printf("(B)\tInput tax payer's particulars\n");
        printf("(C)\tCompute and display tax payable\n");
        printf("(D)\tQuit\n");
        printf("Enter Choice:  ");
        flushall();
        scanf("%c", &choice);

        switch(choice)
        {
            case 'a'    :
            case 'A'    : printf("\n\nTax Rate Information Table\n");
                          printf("==========================");
                          printf("\n\t\tIncome\t\tRate\n");
                          printf("\t\t$\t\t(%c)\n", code);
                          printf("On the frist\t20,000\t\t0\n");
                          printf("On the next\t10,000\t\t3.5\n");
                          printf("On the next\t10,000\t\t5.5\n");
                          printf("On the next\t40,000\t\t8.5\n");
                          printf("On the next\t80,000\t\t14\n");
                          printf("On the next\t160,000\t\t17\n");
                          printf("Above\t\t320,000\t\t20\n");
                          printf("\n\n");
                          break;

            case 'b'    :
            case 'B'    : i = i + 1;
                          printf("\n\n");
                          printf("Fill Your Descriptions in the following\n");
                          printf("=======================================");
                          printf("\nEnter NRIC no.: ");
                          flushall();
                          scanf("%s", &icno);
                          printf("Enter Name: ");
                          flushall();
                          gets(name);
                          printf("Enter Date of Birth(DD-MM-YYYY): ");
                          flushall();
                          gets(DOB);
                          printf("Enter Sex(M/F): ");
                          flushall();
                          scanf("%c", &sex);
                          printf("Enter Total Income: $");
                          flushall();
                          scanf("%f", &total_income);
                          printf("Enter Total Relief: $");
                          flushall();
                          scanf("%f", &total_relief);
                          printf("Enter Total Rebate: $");
                          flushall();
                          scanf("%f", &total_rebate);
                          printf("\n");

                          printf("**To compute and display your amount payment, please press C to continue.\n\n\n");
                                             
                          pre_payment = total_income - total_relief;

                          if(pre_payment>=0 && pre_payment<20000)
                          {
                              taxable_amount = pre_payment * 0;
                          }
                          else
                          {
                              if(pre_payment > 20000 && pre_payment <= 30000)
                              {
                                  taxable_amount = 0 + (pre_payment-20000) * 0.035;
                              }
                              else
                              {
                                  if(pre_payment > 30000 && pre_payment <= 40000)
                                  {
                                      taxable_amount = 350 + (pre_payment-30000) * 0.055;
                                  }
                                  else
                                  {
                                      if(pre_payment > 40000 && pre_payment <= 80000)
                                      {
                                          taxable_amount = 900 + (pre_payment-40000) * 0.085;
                                      }
                                      else
                                      {
                                          if(pre_payment > 80000 && pre_payment <= 160000)
                                          {
                                              taxable_amount = 4300 + (pre_payment-80000) * 0.14;
                                          }
                                          else
                                          {
                                              if(pre_payment > 160000 && pre_payment <= 320000)
                                              {
                                                  taxable_amount = 15500 + (pre_payment-160000) * 0.17;
                                              }
                                              else
                                              {
                                                  taxable_amount = 42700 + (pre_payment-320000) * 0.2;
                                              }
                                          }
                                      }
                                  }
                              }
                          }
                          amount_payment = taxable_amount - total_rebate;
                          break;

            case 'c'    :
            case 'C'    : i = i + 1;
                          printf("Enter Your NRIC: ");
                          flushall();
                          gets(icno_search);                                                      
                           
                            if(strcmp(icno, icno_search) == 0)
                            {
                                printf("\n\nYour Total Amount Payment");
                                printf("\n=========================");
                                printf("\nNRIC\t\t\t   : %s", icno);
                                printf("\nName\t\t\t   : %s", name);
                                printf("\nDate of Brith\t\t   : %s\n", DOB);
                                printf("Sex\t\t\t   : %c\n\n", sex);
                                printf("Total income\t\t   :  $%9.2f\n", total_income);
                                printf("\t\tLess relief: -$%9.2f\n", total_relief);
                                printf("Total taxable amount\t   :  $%9.2f\n", taxable_amount);
                                printf("\t\tLess rebate: -$%9.2f\n", total_rebate);
                                printf("\n");
                                printf("Amount payment\t\t   :  $%9.2f\n", amount_payment);
                              }
                          printf("\n");

                          printf("**If you don't use this Tax Advisor Program, please press D to QUIT.\n\n\n");
                          break;

            case 'd'    :
            case 'D'    : printf("\nThank You... Have a Nice Day!");
                          printf("\n\n");
                          break;

            default        : printf("\nInvalid Choice! Please try again...");
                          printf("\n\n");
        }
    }
    printf("\n\n");
}



因為你所訂的 name 是 2 dimension, 而你所 gets 的卻是 1 dimension, 所以不能被儲存


回复

使用道具 举报

14

主题

1

好友

1491

积分

黄金长老

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

3#
发表于 2009-1-11 07:07 AM |只看该作者

还是不能运作

我在每个variable都加了作用于是为了让它记录在哪个位子(空间)。可是还是没办法运行.

[quote]
原帖由 无我不在 于 2009-1-10 11:23 PM 发表
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
    int code=37, i=-1;
    char choice=0, icno_search[8];
    char name[10][50]={""}, icno[10][8]={""}, DOB[10][10]={""}, sex[10]={""};
    float total_income[10]={0}, total_relief[10]={0}, total_rebate[10]={0}, pre_payment[10]={0}, taxable_amount[10]={0};
    float amount_payment[10]={0};
   
    while(choice!='D'&& choice!='d')
    {
        printf("Tax Advisor Program\n");
        printf("(A)\tDisplay taxation rate\n");
        printf("(B)\tInput tax payer's particulars\n");
        printf("(C)\tCompute and display tax payable\n");
        printf("(D)\tQuit\n");
        printf("Enter Choice:  ");
        flushall();
        scanf("%c", &choice);

        switch(choice)
        {
            case 'a'    :
            case 'A'    : printf("\n\nTax Rate Information Table\n");
                          printf("==========================");
                          printf("\n\t\tIncome\t\tRate\n");
                          printf("\t\t$\t\t(%c)\n", code);
                          printf("On the frist\t20,000\t\t0\n");
                          printf("On the next\t10,000\t\t3.5\n");
                          printf("On the next\t10,000\t\t5.5\n");
                          printf("On the next\t40,000\t\t8.5\n");
                          printf("On the next\t80,000\t\t14\n");
                          printf("On the next\t160,000\t\t17\n");
                          printf("Above\t\t320,000\t\t20\n");
                          printf("\n\n");
                          break;

            case 'b'    :
            case 'B'    : i = i + 1;
                          printf("\n\n");
                          printf("Fill Your Descriptions in the following\n");
                          printf("=======================================");
                          printf("\nEnter NRIC no.: ");
                          flushall();
                          scanf("%s", &icno{i});
                          printf("Enter Name: ");
                          flushall();
                          gets(name
{i});
                          printf("Enter Date of Birth(DD-MM-YYYY): ");
                          flushall();
                          gets(DOB
{i});
                          printf("Enter Sex(M/F): ");
                          flushall();
                          scanf("%c", &sex
{i});
                          printf("Enter Total Income: $");
                          flushall();
                          scanf("%f", &total_income
{i});
                          printf("Enter Total Relief: $");
                          flushall();
                          scanf("%f", &total_relief
{i});
                          printf("Enter Total Rebate: $");
                          flushall();
                          scanf("%f", &total_rebate
{i});
                          printf("\n");

                          printf("**To compute and display your amount payment, please press C to continue.\n\n\n");
                                             
                          pre_payment
{i} = total_income{i} - total_relief{i};

                          if(pre_payment
{i}>=0 && pre_payment{i}<20000)
                          {
                              taxable_amount
{i} = pre_payment{i} * 0;
                          }
                          else
                          {
                              if(pre_payment
{i} > 20000 && pre_payment{i} <= 30000)
                              {
                                  taxable_amount
{i} = 0 + (pre_payment{i}-20000) * 0.035;
                              }
  ......
                                 ......



[ 本帖最后由 无我不在 于 2009-1-11 07:13 AM 编辑 ]


回复

使用道具 举报

14

主题

1

好友

1491

积分

黄金长老

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

4#
发表于 2009-1-11 07:17 AM |只看该作者
我在google搜索过了,ARRAY的示范都已经分别指定的位子(空间),不然就是用for loop (exp. for(i=0; i<10, i=i+1) ),所以 i 能自动增加,所以每当一个input就有新的位子(空间)记入起来。

可是,我没办法每当我输入新的input,也不能记入新的位子(空间)。也没有 i=i+1 去自动增加位子(空间)的数量。

char[x][y] = {""}         x ==> number of store ; y ==> size of character
**difference with int and float**
int[x][y] = {"")
float[x][y] = {""}         x ==> number of row in store ; y ==> number of column in store

这样可以说明,我这篇program是用 2D char 和 1D int/float 了。

我现在不知道什么方法储存input。


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

5#
发表于 2009-1-11 02:20 PM |只看该作者
原帖由 无我不在 于 2009-1-11 07:17 AM 发表
我在google搜索过了,ARRAY的示范都已经分别指定的位子(空间),不然就是用for loop (exp. for(i=0; i number of store ; y ==> size of character
**difference with int and float**
int[x][y] = {"")
fl ...



方法用錯了, 這個例子你去參考變通

#include <stdio.h>

int main()
{
    char name[3][50] = {};
    int age[3];

    for(int a=0; a<3; a++)
    {
        printf("\nPlease enter name %d : ", a+1);
        scanf("%s", name[a]);
        printf(" Please enter %s's age : ", name[a]);
        scanf("%d", &age[a]);
    }

    printf("\nSecond record is : %s and %d years old", name[1], age[1]);
    return 0;
}


例子都有了, 希望改好之後把完整的coding放上來讓其他人學習
p/s: 儘量不要用 i, b, u 做為變數貼在這裡, 不然會被默認為斜體, 粗體和底線

[ 本帖最后由 Super-Tomato 于 2009-1-11 02:41 PM 编辑 ]


回复

使用道具 举报

14

主题

0

好友

476

积分

翡翠长老

Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6

6#
发表于 2009-1-11 03:56 PM |只看该作者
为什么总觉得C++的格式很复杂
(头脑过于简单了)


回复

使用道具 举报

14

主题

1

好友

1491

积分

黄金长老

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

7#
发表于 2009-1-11 04:08 PM |只看该作者

回复 #5 Super-Tomato 的帖子

这个列子会要求重复input。到了第四次input才会结束for loop的。

我的作业要求输入一次资料后回到menu,然后再做选择。然而,第二次输入资料,可以自动地记入另一个空间。最后,客户以IC搜索(strcmp),找回资料。


还是不行啊~~~都不能 a=a+1,自动记入新的储存空间。

我还是认为自己不是学programming的料。一个languague就让我瓶颈了,而且只是入门知识罢了。苦恼

[ 本帖最后由 无我不在 于 2009-1-11 04:32 PM 编辑 ]


回复

使用道具 举报

14

主题

1

好友

1491

积分

黄金长老

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

8#
发表于 2009-1-11 04:25 PM |只看该作者

回复 #6 MercyGodlikE 的帖子

你学习进展如何了啊?


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

9#
发表于 2009-1-11 05:06 PM |只看该作者
原帖由 无我不在 于 2009-1-11 04:08 PM 发表
这个列子回要求重复input。到了第四次input才会结束for loop的。

我的作业要求输入一次资料后回到menu,然后再做选择。然而,第二次输入资料,可以自动地记入另一个空间。最后,客户以IC搜索(strcmp),找回 ...



你怎麼沒變通啊, 循環和自己遞增不都是由一個 variable 控制的嗎??
難道最基本的 + 你都忘了嗎?? 之前的那些 coding 你是怎麼寫出來的??


#include <stdio.h>

int main()
{
    char name[3][50] = {}, choice;
    int age[3], i=0;

    while(choice!='D'&& choice!='d')
    {
        choice = NULL;
        printf("Tax Advisor Program\n");
        printf("(A)\tDisplay taxation rate\n");
        printf("(B)\tInput tax payer's particulars\n");
        printf("(C)\tCompute and display tax payable\n");
        printf("(D)\tQuit\n\n");
        printf("Enter Choice:  ");
        scanf("%s", &choice);

        switch(choice)
        {
            case 'A':
            case 'a':
                printf("Please enter name %i : ", i+1);
                scanf("%s", name[ i ]);
                printf("Please enter %s's age : ", name);
                scanf("%i", &age[ i ]);
                i++;
                break;
        }

        printf("\n\n");
    }

    printf("\nSecond record is : %s and %i years old", name[1], age[1]);
    return 0;
}


回复

使用道具 举报

14

主题

1

好友

1491

积分

黄金长老

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

10#
发表于 2009-1-11 05:26 PM |只看该作者
原帖由 Super-Tomato 于 2009-1-11 05:06 PM 发表
switch(choice)
        {
            case 'A':
            case 'a':
                printf("Please enter name %i : ", i+1);
                scanf("%s", name[ i ]);
                printf("Please enter %s's age : ", name);
                scanf("%i", &age[ i ]);
                i++;
                break;
        }


%i 什么作用,我运行出现乱码。是不是%d?

**我越写越自己乱了。抱歉

[ 本帖最后由 无我不在 于 2009-1-11 05:30 PM 编辑 ]


回复

使用道具 举报

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

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-10-25 03:32 AM , Processed in 0.115070 second(s), 26 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部