c++

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
こじはる

c++

#1

投稿記事 by こじはる » 13年前

以下の条件を満たしてれば何でもいいのでプログラムを作ってくれませんか?

ファイルの読み込み:書き出しができる。
構造体;共用体:列挙型のどれかが使われていること。
printfの行が全体の20%以下である。
ソースの長さが合計で170行以上。

アバター
h2so5
副管理人
記事: 2212
登録日時: 14年前
住所: 東京
連絡を取る:

Re: c++

#2

投稿記事 by h2so5 » 13年前

ファイルの読み込み:書き出しができます。
構造体を使っています。
printfの行が全体の0%です。
170行あります。

コード:

#include <fstream>
#include <iostream>
#include <string>
 
using namespace std;
 
struct Struct {
        string str;
};
 
int main() {
        Struct s;
 
        // 読み込み
        ifstream ifs( "abc.txt" );
        ifs >> s.str;
 
        // 書き出し
        ofstream ofs( "abc.txt" );
        ofs << "abc";
        return 0;
}

/*

















































































































































*/

アバター
softya(ソフト屋)
副管理人
記事: 11677
登録日時: 14年前
住所: 東海地方
連絡を取る:

Re: c++

#3

投稿記事 by softya(ソフト屋) » 13年前

課題の丸投げは禁止されております。 フォーラムルール → http://dixq.net/board/board.html
的確なアドバイスを受けご自分で作成されるようにお願いします。
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。

アバター
さかまき
記事: 92
登録日時: 14年前

Re: c++

#4

投稿記事 by さかまき » 13年前

http://dixq.net/forum/viewtopic.php?f=3&t=10189
↑の方が同じ内容という事で閉じられてしまいましたが

>空欄以外でソースの長さが合計で170行以上。
という事で、「空欄以外で」が追加されています。

課題だとすれば、ずいぶんアバウトですねぇ。

たかぎ
記事: 328
登録日時: 14年前
住所: 大阪
連絡を取る:

Re: c++

#5

投稿記事 by たかぎ » 13年前

空欄以外で、ということであれば...

コード:

#include <iostream>
int main()
{
	std::cout << std::cin.rdbuf();
}
//   1
//   2
//   3
//   4
//   5
//   6
//   7
//   8
//   9
//  10
//  11
//  12
//  13
//  14
//  15
//  16
//  17
//  18
//  19
//  20
//  21
//  22
//  23
//  24
//  25
//  26
//  27
//  28
//  29
//  30
//  31
//  32
//  33
//  34
//  35
//  36
//  37
//  38
//  39
//  40
//  41
//  42
//  43
//  44
//  45
//  46
//  47
//  48
//  49
//  50
//  51
//  52
//  53
//  54
//  55
//  56
//  57
//  58
//  59
//  60
//  61
//  62
//  63
//  64
//  65
//  66
//  67
//  68
//  69
//  70
//  71
//  72
//  73
//  74
//  75
//  76
//  77
//  78
//  79
//  80
//  81
//  82
//  83
//  84
//  85
//  86
//  87
//  88
//  89
//  90
//  91
//  92
//  93
//  94
//  95
//  96
//  97
//  98
//  99
// 100
// 101
// 102
// 103
// 104
// 105
// 106
// 107
// 108
// 109
// 110
// 111
// 112
// 113
// 114
// 115
// 116
// 117
// 118
// 119
// 120
// 121
// 122
// 123
// 124
// 125
// 126
// 127
// 128
// 129
// 130
// 131
// 132
// 133
// 134
// 135
// 136
// 137
// 138
// 139
// 140
// 141
// 142
// 143
// 144
// 145
// 146
// 147
// 148
// 149
// 150
// 151
// 152
// 153
// 154
// 155
// 156
// 157
// 158
// 159
// 160
// 161
// 162
// 163
// 164
// 165
こんなのでよいのでしょうか?
一見、
> 構造体;共用体:列挙型のどれかが使われていること。
の要件を満たしていないように見えますが、std::coutやstd::cinはstd::char_traits<char>をテンプレート実引数に取っています。
std::char_traitsは構造体ですので、処理系に関わらず、確実に構造体を使っています。

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: c++

#6

投稿記事 by みけCAT » 13年前

とりあえず作りました!

コード:

#include <stdio.h>

typedef union {
	int i;
	float f;
} un;

int main(void) {
	FILE* fp;
	un a;
	int b=0;
	fp=fopen("data.txt","r");
	if(fp) {
		fscanf(fp,"%d",&a.i);
		fclose(fp);
	}
	fp=fopen("data2.txt","w");
	if(fp) {
		fprintf(fp,"%f",a.f);
		fclose(fp);
	}
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	b++;
	b--;
	printf("%d\n",b);
	return 0;
}
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

かずま

Re: c++

#7

投稿記事 by かずま » 13年前

次のプログラムは、
http://dixq.net/forum/viewtopic.php?f=3 ... =30#p71536
に書いたものですが、全部で 189行。
printf は 6行で 3.2%
構造体は Message と Data の 2つ。
ファイルの読み込みと書き出しは、load_message() と save_message() で実行。
これでどうでしょうか?

コード:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
typedef struct Message {
    int date;
    char *msg;
    struct Message *prev, *next;
} Message;
 
typedef struct {
    int year, month, day;
    Message root, *message[32];
} Data;
 
void print_help(void)
{
    puts(" q          : 終了 (quit)\n"
         " h          : ヘルプ (help)\n"
         " 1901~2099 : 年の指定\n"
         " m [1~12]  : 月の指定とカレンダーの表示 (month)\n"
         " 1~31      : 日の指定とメモの表示\n"
         " w [1~31]  : 日の指定とメモの書き込み (write)\n"
         " d [1~31]  : 日の指定とメモの削除 (delete)\n");
}
 
int day_of_week(int y, int m, int d)
{
    y -= m < 3;
    return (y + y/4 - y/100 + y/400 + ".bed=pen+mad."[m] + d) % 7;
}
 
#define IS_LEAP(y) ((y) % 4 == 0 && ((y) % 100 || (y) % 400 == 0))
 
int last_day(int y, int m)
{
    return (m == 2) ? IS_LEAP(y) + 28 : (0x15aa>>m & 1) + 30;
}
 
void print_calendar(Data *dp)
{
    int w = day_of_week(dp->year, dp->month, 1);
    int z = last_day(dp->year, dp->month), d;
 
    printf("%13d年 %d月\n" " 日  月  火  水  木  金  土\n" "%*s",
        dp->year, dp->month, w * 4, "");
    for (d = 1; d <= z; d++)
        printf("%*c%d%c", (d<10)+1, dp->message[d]?'*':' ', d, ++w%7?' ':'\n');
    puts(w % 7 ? "\n" : ""); 
}
 
void print_message(Data *dp)
{
    printf("%d/%d/%d: %s", dp->year, dp->month, dp->day,
        dp->message[dp->day] ? dp->message[dp->day]->msg : "\n");
}
 
int confirm(const char *message)
{
    char buf[1024];
    printf("%s (y/n): ", message);
    return fgets(buf, sizeof(buf), stdin) && buf[0] == 'y';
}
 
Message *insert(Message *pos, int date, const char *msg)
{
    Message *mp = static_cast<Message *>(malloc(sizeof *mp));
    mp->date = date;
    mp->msg  = strdup(msg);
    mp->prev = pos->prev;
    mp->next = pos;
    return pos->prev = pos->prev->next = mp;
}
 
void load_message(Data *dp)
{
    char buf[1024];
    int y, m, d, n;
    FILE *fp = fopen("Calendar.txt", "r");
    dp->root.prev = dp->root.next = &dp->root;
    if (!fp) return;
    while (fgets(buf, sizeof(buf), fp))
        if (sscanf(buf, "%d/%d/%d %n", &y, &m, &d, &n) == 3)
            insert(&dp->root, y << 9 | m << 5 | d, buf + n);
    fclose(fp);
}
 
void save_message(Data *dp)
{
    FILE *fp = fopen("Calendar.txt", "w");
    if (fp) {
        Message *mp = dp->root.next;
        while (mp != &dp->root) {
            Message *np = mp->next;
            fprintf(fp, "%d/%02d/%02d %s", mp->date >> 9,
                mp->date >> 5 & 0x0f, mp->date & 0x1f, mp->msg);
            free(mp->msg);
            free(mp);
            mp = np;
        }
        fclose(fp);
    }
}
 
void set_message(Data *dp)
{
    int date1 = dp->year << 9 | dp->month << 5 | 1;
    int date31 = dp->year << 9 | dp->month << 5 | 31;
    Message *mp = dp->root.next;
    int d;
    for (d = 0; d < 32; d++) dp->message[d] = 0;
    for (; mp != &dp->root && mp->date <= date31; mp = mp->next)
        if (mp->date >= date1) dp->message[mp->date & 0x1f] = mp;
}
 
void delete_message(Data *dp)
{
    Message *mp = dp->message[dp->day];
    if (mp) {
        mp->prev->next = mp->next;
        mp->next->prev = mp->prev;
        free(mp->msg);
        free(mp);
        dp->message[dp->day] = 0;
        print_calendar(dp);
    }
}
 
void write_message(Data *dp, const char *buf)
{
    Message *mp = dp->message[dp->day];
    if (mp) {
        free(mp->msg);
        mp->msg = strdup(buf);
    } else {
        int date = dp->year << 9 | dp->month << 5 | dp->day;
        for (mp = dp->root.next; mp != &dp->root && mp->date < date; mp = mp->next) ;
        dp->message[dp->day] = insert(mp, date, buf);
    }
    print_calendar(dp);
}
 
int main(void)
{
    Data data;
    char buf[1024];
    time_t t = time(0);
    struct tm *tp = localtime(&t);
    data.year = tp->tm_year + 1900;
    data.month = tp->tm_mon + 1;
    data.day = tp->tm_mday;
 
    load_message(&data);
    set_message(&data);
    print_help();
    print_calendar(&data);
    print_message(&data);
 
    while (printf("> "), fgets(buf, sizeof(buf), stdin)) {
        int n = atoi(buf);
        if (n > 1900 && n < 2100 && n != data.year)  // change year
            data.year = n, print_calendar(&data);
        else if (n >= 1 && n <= last_day(data.year, data.month)) // the day
            data.day = n, print_message(&data);
        else if (buf[0] == 'm') {  // change month and print calendar
            n = atoi(buf + 1);
            if (n >= 1 && n <= 12 && n != data.month)
                data.month = n, set_message(&data);
            print_calendar(&data);
        } else if (buf[0] == 'w') {  // write
            n = atoi(buf + 1);
            if (n >= 1 && n <= last_day(data.year, data.month)) data.day = n;
            printf("%d/%d/%d: ", data.year, data.month, data.day);
            if (fgets(buf, sizeof(buf), stdin) && buf[0] != '\n')
                if (!data.message[data.day] || confirm("上書きしますか?"))
                    write_message(&data, buf);
        } else if (buf[0] == 'd') {  // delete
            n = atoi(buf + 1);
            if (n >= 1 && n <= last_day(data.year, data.month)) data.day = n;
            if (data.message[data.day] && confirm("削除しますか?"))
               delete_message(&data);
        } else if (buf[0] == 'q') {  // quit
            if (confirm("終了しますか?")) break;
        } else if (buf[0] == 'h') print_help();
    }
    save_message(&data);
    return 0;
}

閉鎖

“C言語何でも質問掲示板” へ戻る