English 中文(简体)
在C中添加插图特征
原标题:Adding Characters of a string in C
  • 时间:2012-05-12 14:10:41
  •  标签:
  • c

我有方案拟定任务,要求我们制定一项方案,使用户能够以这种格式(d/mm/year)进入这一日期。 然后,我们必须找到一种办法,从结构中抽取每批数字,并在日、月和年领域投入。

我想把用户的全部投入看作是一种扼杀,而只是选择了我所希望的扼杀性价值。 这里是我的法典。

结构声明:

 struct datestructure{
    char day;
   char month;
   char year;
  };

struct prj{
   int ID;
   struct namestructure name;
   struct datestructure date;
   float price;    

 };



 struct prj project[MAX];

这里是我撰写的法典。

     char dateinput[11];
 scanf("%s", dateinput);


 printf("%s", dateinput); //making sure that the input is read correctly


 project[n].date.day = dateinput[0]+dateinput[1];


 printf("%s", project[n].date.day );

然而,这并不奏效,我没有想法。 请允许我就如何解决这一问题提供一些指导。

感谢。

最佳回答

以下是一些建议:

  1. Consider changing the types of day, month & year to int.
  2. Parse the day, month & year out of your complete date string
  3. Convert each of the day, month & year into integer either using one of the system functions (atoi or strtol etc) or your own logic code.

修饰的一些基本样本(你可以理解的一个实例)。 还有其他更好的方法:

char day[3] = {0};
char month[3] = {0};
char year[5] = {0};

memcpy(day, dateinput,2);
memcpy(month,dateinput+3, 2);
memcpy(year,dateinput+5, 4);

<><>><>>说明:<>>>> 以上代码没有考虑到错误的投入,并认为,有单一数字的日月和月将与前的0一同ene。 投入:07/03/2012。 前注7和3。 首先,你可以尝试这样做,然后临时处理所有错误情景。

问题回答

If you want to save them as numbers, use int:

struct datestructure{
    int day;
    int month;
    int year;
  };

至于转换,果园到暗中......

project[n].date.day = (dateinput[0] -  0 ) * 10 + dateinput[1] -  0 ;

理解这项工作如何,并在同一月和一年中进行。

You can take 12/05/2012[today] as input sting. Now write
i = 0;//consider the date is in str
printf("%d", str[i]);//getting the equivalent ascii char

现在,你知道2号果园。 将其从2号 in子中分离出来,储存在一个变数中,并分散在整个阵列中。

You re already using scanf, which can do more than you realise:

#include <stdio.h>

struct datestructure {
    int day;
    int month;
    int year;
};

int main() {
    struct datestructure date;
    int count = scanf("%d/%d/%d", &date.day, &date.month, &date.year);
    if (count != 3) {
        printf("Invalid input
");
    } else {
        printf("You said %d-%d-%d
", date.year, date.month, date.day);
    }
}

另有<代码>strftime,其中略微努力使用,但将根据对日历的了解验证投入。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签